-
Notifications
You must be signed in to change notification settings - Fork 9
/
action.ts
255 lines (219 loc) · 6.61 KB
/
action.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import { fabric } from "fabric";
import React from "react";
import { PartialRecord } from "@mehra/ts";
import AssertType from "../types/assert";
import { FabricTeXImage } from "../types/fabric";
import { Tool, Tools } from "./tools";
import Pages from "./pages";
import FileHandler from "./files";
import ClipboardHandler from "./clipboard";
import HistoryHandler from "./history";
import { Dash, Fill, Stroke, Style } from "./styles";
import Page from "./page";
import TeXToDataURL, { LaTeXError } from "./latex";
export enum Action {
PreviousPage = "previousPage",
NextPage = "nextPage",
AddPageStart = "addPageStart",
AddPageEnd = "addPageEnd",
Undo = "undo",
Redo = "redo",
Open = "open",
Save = "save",
Export = "export",
Cut = "cut",
Copy = "copy",
Paste = "paste",
Deselect = "deselect",
SelectAll = "selectAll",
Duplicate = "duplicate",
Move = "move",
Pen = "pen",
Eraser = "eraser",
Laser = "laser",
Line = "line",
Ellipse = "ellipse",
Rectangle = "rectangle",
LaTeX = "latex",
Dotted = "dotted",
Dashed = "dashed",
Solid = "solid",
Black = "black",
Blue = "blue",
Green = "green",
Orange = "orange",
Yellow = "yellow",
Transparent = "transparent",
HalfFilled = "halfFilled",
Filled = "filled",
Help = "help",
ResetStyles = "resetStyles",
FullScreen = "fullScreen",
EnterFullScreen = "enterFullScreen",
ExitFullScreen = "exitFullScreen",
}
const nameMap: PartialRecord<Action, string> = {
previousPage: "–Page",
nextPage: "+Page",
addPageStart: "-Page",
addPageEnd: "+Page",
selectAll: "Select All",
duplicate: "Clone",
eraser: "Cut / Eraser",
rectangle: "Rect.",
latex: "LaTeX",
transparent: "Unfilled",
halfFilled: "Half Fill",
resetStyles: "Reset Styles",
fullScreen: "Full Screen",
enterFullScreen: "Enter Full Screen",
exitFullScreen: "Exit Full Screen",
};
export const actionName = (action: Action): string => {
const name = nameMap[action] ?? action;
return name[0].toUpperCase() + name.slice(1);
};
export default class ActionHandler {
canvas: Page;
readonly actionMap: Record<Action, () => void>;
constructor(
public switchTool: (tool: Tool) => void,
tools: Tools,
public currentStyle: Style,
public pages: Pages,
public files: FileHandler,
public history: HistoryHandler,
public clipboard: ClipboardHandler,
public setStyle: (
dash: Dash | null,
stroke: Stroke | null,
fill: Fill | null
) => void,
/**
* Intentionally mutable global state object
*/
private globalState: {
/**
* A ref to the global input element used for file input
*
* Readonly because we ourselves don't mutate this
*/
readonly fileInputRef?: React.RefObject<HTMLInputElement>;
readonly toggleHelpModal?: () => void;
}
) {
this.canvas = this.pages.canvas;
this.actionMap = {
previousPage: pages.previousOrNewPage,
nextPage: pages.nextOrNewPage,
addPageStart: pages.previousOrNewPage,
addPageEnd: pages.nextOrNewPage,
undo: history.undo,
redo: history.redo,
open: () => globalState.fileInputRef?.current?.click(),
save: pages.saveFile,
export: pages.export,
cut: clipboard.cut,
copy: clipboard.copy,
paste: clipboard.paste,
deselect: () => {
this.canvas.discardActiveObject();
this.canvas.requestRenderAll();
},
selectAll: () => {
this.canvas.discardActiveObject();
this.canvas.setActiveObject(
new fabric.ActiveSelection(this.canvas.getObjects(), {
canvas: this.canvas,
})
);
this.canvas.requestRenderAll();
},
duplicate: () => {
this.clipboard.copy();
this.clipboard.paste();
},
move: () => this.switchTool(tools.Move),
pen: () => this.switchTool(tools.Pen),
eraser: () => this.switchTool(tools.Eraser),
laser: () => this.switchTool(tools.Laser),
line: () => this.switchTool(tools.Line),
ellipse: () => this.switchTool(tools.Ellipse),
rectangle: () => this.switchTool(tools.Rectangle),
latex: this.requestTeX,
dotted: () => this.setDash(Dash.Dotted),
dashed: () => this.setDash(Dash.Dashed),
solid: () => this.setDash(Dash.Solid),
blue: () => this.setStroke(Stroke.Blue),
green: () => this.setStroke(Stroke.Green),
yellow: () => this.setStroke(Stroke.Yellow),
orange: () => this.setStroke(Stroke.Orange),
black: () => this.setStroke(Stroke.Black),
transparent: () => this.setFill(Fill.Transparent),
halfFilled: () => this.setFill(Fill.HalfSolid),
filled: () => this.setFill(Fill.Solid),
resetStyles: () =>
this.setStyle(Dash.Solid, Stroke.Black, Fill.Transparent),
help: () => globalState.toggleHelpModal?.(),
fullScreen: () =>
this.doAction(
!document.fullscreenElement
? Action.EnterFullScreen
: Action.ExitFullScreen
),
enterFullScreen: () => document.documentElement.requestFullscreen(),
exitFullScreen: () => document.exitFullscreen(),
};
}
doAction = (action: Action): void => this.actionMap[action]();
setDash = (dash: Dash): void => {
if (dash === this.currentStyle.dash) {
this.setStyle(Dash.Solid, null, null);
} else {
this.setStyle(dash, null, null);
}
};
setStroke = (stroke: Stroke): void => {
if (stroke === this.currentStyle.stroke) {
this.setStyle(null, Stroke.Black, null);
} else {
this.setStyle(null, stroke, null);
}
};
setFill = (fill: Fill): void => {
if (fill === this.currentStyle.fill) {
this.setStyle(null, null, Fill.Transparent);
} else {
this.setStyle(null, null, fill);
}
};
requestTeX = async (): Promise<
"success" | "no latex entered" | "invalid latex"
> => {
const text = window.prompt("Enter LaTeX source");
if (text === null) return "no latex entered";
let dataURL: `data:image/svg+xml,${string}`;
try {
dataURL = TeXToDataURL(text);
} catch (e: unknown) {
AssertType<LaTeXError>(e);
// eslint-disable-next-line no-console
console.error(e, e.node);
window.alert(
`Error in LaTeX: ${e.errorText}
More details printed to console.`
);
return "invalid latex";
}
const img: FabricTeXImage = await this.canvas.addImage(dataURL, undefined, {
scaleX: 3,
scaleY: 3,
data: { texSource: text },
});
this.history.add([img]);
// apparently this does something?
await this.history.undo();
await this.history.redo();
return "success";
};
}