js-draw 1.21.1 → 1.21.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (187) hide show
  1. package/dist/Editor.css +4 -2
  2. package/dist/bundle.js +2 -2
  3. package/dist/bundledStyles.js +1 -1
  4. package/dist/cjs/toolbar/widgets/InsertImageWidget/ImageWrapper.d.ts +1 -1
  5. package/dist/cjs/tools/BaseTool.d.ts +61 -0
  6. package/dist/cjs/tools/BaseTool.js +179 -0
  7. package/dist/cjs/tools/Eraser.d.ts +60 -0
  8. package/dist/cjs/tools/Eraser.js +299 -0
  9. package/dist/cjs/tools/Eraser.test.d.ts +1 -0
  10. package/dist/cjs/tools/FindTool.d.ts +21 -0
  11. package/dist/cjs/tools/FindTool.js +137 -0
  12. package/dist/cjs/tools/FindTool.test.d.ts +1 -0
  13. package/dist/cjs/tools/InputFilter/ContextMenuRecognizer.d.ts +17 -0
  14. package/dist/cjs/tools/InputFilter/ContextMenuRecognizer.js +105 -0
  15. package/dist/cjs/tools/InputFilter/ContextMenuRecognizer.test.d.ts +1 -0
  16. package/dist/cjs/tools/InputFilter/FunctionMapper.d.ts +12 -0
  17. package/dist/cjs/tools/InputFilter/FunctionMapper.js +21 -0
  18. package/dist/cjs/tools/InputFilter/InputMapper.d.ts +23 -0
  19. package/dist/cjs/tools/InputFilter/InputMapper.js +38 -0
  20. package/dist/cjs/tools/InputFilter/InputPipeline.d.ts +15 -0
  21. package/dist/cjs/tools/InputFilter/InputPipeline.js +54 -0
  22. package/dist/cjs/tools/InputFilter/InputPipeline.test.d.ts +1 -0
  23. package/dist/cjs/tools/InputFilter/InputStabilizer.d.ts +29 -0
  24. package/dist/cjs/tools/InputFilter/InputStabilizer.js +181 -0
  25. package/dist/cjs/tools/InputFilter/StrokeKeyboardControl.d.ts +21 -0
  26. package/dist/cjs/tools/InputFilter/StrokeKeyboardControl.js +84 -0
  27. package/dist/cjs/tools/PanZoom.d.ts +125 -0
  28. package/dist/cjs/tools/PanZoom.js +517 -0
  29. package/dist/cjs/tools/PanZoom.test.d.ts +1 -0
  30. package/dist/cjs/tools/PasteHandler.d.ts +23 -0
  31. package/dist/cjs/tools/PasteHandler.js +109 -0
  32. package/dist/cjs/tools/Pen.d.ts +54 -0
  33. package/dist/cjs/tools/Pen.js +335 -0
  34. package/dist/cjs/tools/Pen.test.d.ts +1 -0
  35. package/dist/cjs/tools/PipetteTool.d.ts +28 -0
  36. package/dist/cjs/tools/PipetteTool.js +69 -0
  37. package/dist/cjs/tools/ScrollbarTool.d.ts +18 -0
  38. package/dist/cjs/tools/ScrollbarTool.js +85 -0
  39. package/dist/cjs/tools/SelectionTool/SelectAllShortcutHandler.d.ts +9 -0
  40. package/dist/cjs/tools/SelectionTool/SelectAllShortcutHandler.js +32 -0
  41. package/dist/cjs/tools/SelectionTool/Selection.d.ts +72 -0
  42. package/dist/cjs/tools/SelectionTool/Selection.js +634 -0
  43. package/dist/cjs/tools/SelectionTool/SelectionHandle.d.ts +62 -0
  44. package/dist/cjs/tools/SelectionTool/SelectionHandle.js +141 -0
  45. package/dist/cjs/tools/SelectionTool/SelectionMenuShortcut.d.ts +32 -0
  46. package/dist/cjs/tools/SelectionTool/SelectionMenuShortcut.js +86 -0
  47. package/dist/cjs/tools/SelectionTool/SelectionTool.d.ts +42 -0
  48. package/dist/cjs/tools/SelectionTool/SelectionTool.js +500 -0
  49. package/dist/cjs/tools/SelectionTool/SelectionTool.selecting.test.d.ts +1 -0
  50. package/dist/cjs/tools/SelectionTool/SelectionTool.test.d.ts +1 -0
  51. package/dist/cjs/tools/SelectionTool/ToPointerAutoscroller.d.ts +23 -0
  52. package/dist/cjs/tools/SelectionTool/ToPointerAutoscroller.js +83 -0
  53. package/dist/cjs/tools/SelectionTool/TransformMode.d.ts +42 -0
  54. package/dist/cjs/tools/SelectionTool/TransformMode.js +155 -0
  55. package/dist/cjs/tools/SelectionTool/keybindings.d.ts +15 -0
  56. package/dist/cjs/tools/SelectionTool/keybindings.js +38 -0
  57. package/dist/cjs/tools/SelectionTool/types.d.ts +35 -0
  58. package/dist/cjs/tools/SelectionTool/types.js +14 -0
  59. package/dist/cjs/tools/SelectionTool/util/makeClipboardErrorHandlers.d.ts +6 -0
  60. package/dist/cjs/tools/SelectionTool/util/makeClipboardErrorHandlers.js +50 -0
  61. package/dist/cjs/tools/SelectionTool/util/showSelectionContextMenu.d.ts +5 -0
  62. package/dist/cjs/tools/SelectionTool/util/showSelectionContextMenu.js +43 -0
  63. package/dist/cjs/tools/SoundUITool.d.ts +26 -0
  64. package/dist/cjs/tools/SoundUITool.js +171 -0
  65. package/dist/cjs/tools/TextTool.d.ts +36 -0
  66. package/dist/cjs/tools/TextTool.js +285 -0
  67. package/dist/cjs/tools/TextTool.test.d.ts +1 -0
  68. package/dist/cjs/tools/ToolController.d.ts +73 -0
  69. package/dist/cjs/tools/ToolController.js +304 -0
  70. package/dist/cjs/tools/ToolController.test.d.ts +1 -0
  71. package/dist/cjs/tools/ToolEnabledGroup.d.ts +6 -0
  72. package/dist/cjs/tools/ToolEnabledGroup.js +13 -0
  73. package/dist/cjs/tools/ToolSwitcherShortcut.d.ts +16 -0
  74. package/dist/cjs/tools/ToolSwitcherShortcut.js +40 -0
  75. package/dist/cjs/tools/ToolbarShortcutHandler.d.ts +12 -0
  76. package/dist/cjs/tools/ToolbarShortcutHandler.js +34 -0
  77. package/dist/cjs/tools/UndoRedoShortcut.d.ts +8 -0
  78. package/dist/cjs/tools/UndoRedoShortcut.js +27 -0
  79. package/dist/cjs/tools/UndoRedoShortcut.test.d.ts +1 -0
  80. package/dist/cjs/tools/keybindings.d.ts +16 -0
  81. package/dist/cjs/tools/keybindings.js +58 -0
  82. package/dist/cjs/tools/lib.d.ts +14 -0
  83. package/dist/cjs/tools/lib.js +36 -0
  84. package/dist/cjs/tools/localization.d.ts +43 -0
  85. package/dist/cjs/tools/localization.js +45 -0
  86. package/dist/cjs/tools/util/StationaryPenDetector.d.ts +25 -0
  87. package/dist/cjs/tools/util/StationaryPenDetector.js +107 -0
  88. package/dist/cjs/tools/util/createMenuOverlay.d.ts +10 -0
  89. package/dist/cjs/tools/util/createMenuOverlay.js +126 -0
  90. package/dist/cjs/tools/util/createMenuOverlay.test.d.ts +1 -0
  91. package/dist/cjs/version.js +1 -1
  92. package/dist/mjs/toolbar/widgets/InsertImageWidget/ImageWrapper.d.ts +1 -1
  93. package/dist/mjs/tools/BaseTool.d.ts +61 -0
  94. package/dist/mjs/tools/BaseTool.mjs +177 -0
  95. package/dist/mjs/tools/Eraser.d.ts +60 -0
  96. package/dist/mjs/tools/Eraser.mjs +292 -0
  97. package/dist/mjs/tools/Eraser.test.d.ts +1 -0
  98. package/dist/mjs/tools/FindTool.d.ts +21 -0
  99. package/dist/mjs/tools/FindTool.mjs +131 -0
  100. package/dist/mjs/tools/FindTool.test.d.ts +1 -0
  101. package/dist/mjs/tools/InputFilter/ContextMenuRecognizer.d.ts +17 -0
  102. package/dist/mjs/tools/InputFilter/ContextMenuRecognizer.mjs +76 -0
  103. package/dist/mjs/tools/InputFilter/ContextMenuRecognizer.test.d.ts +1 -0
  104. package/dist/mjs/tools/InputFilter/FunctionMapper.d.ts +12 -0
  105. package/dist/mjs/tools/InputFilter/FunctionMapper.mjs +15 -0
  106. package/dist/mjs/tools/InputFilter/InputMapper.d.ts +23 -0
  107. package/dist/mjs/tools/InputFilter/InputMapper.mjs +36 -0
  108. package/dist/mjs/tools/InputFilter/InputPipeline.d.ts +15 -0
  109. package/dist/mjs/tools/InputFilter/InputPipeline.mjs +49 -0
  110. package/dist/mjs/tools/InputFilter/InputPipeline.test.d.ts +1 -0
  111. package/dist/mjs/tools/InputFilter/InputStabilizer.d.ts +29 -0
  112. package/dist/mjs/tools/InputFilter/InputStabilizer.mjs +175 -0
  113. package/dist/mjs/tools/InputFilter/StrokeKeyboardControl.d.ts +21 -0
  114. package/dist/mjs/tools/InputFilter/StrokeKeyboardControl.mjs +78 -0
  115. package/dist/mjs/tools/PanZoom.d.ts +125 -0
  116. package/dist/mjs/tools/PanZoom.mjs +510 -0
  117. package/dist/mjs/tools/PanZoom.test.d.ts +1 -0
  118. package/dist/mjs/tools/PasteHandler.d.ts +23 -0
  119. package/dist/mjs/tools/PasteHandler.mjs +103 -0
  120. package/dist/mjs/tools/Pen.d.ts +54 -0
  121. package/dist/mjs/tools/Pen.mjs +306 -0
  122. package/dist/mjs/tools/Pen.test.d.ts +1 -0
  123. package/dist/mjs/tools/PipetteTool.d.ts +28 -0
  124. package/dist/mjs/tools/PipetteTool.mjs +63 -0
  125. package/dist/mjs/tools/ScrollbarTool.d.ts +18 -0
  126. package/dist/mjs/tools/ScrollbarTool.mjs +79 -0
  127. package/dist/mjs/tools/SelectionTool/SelectAllShortcutHandler.d.ts +9 -0
  128. package/dist/mjs/tools/SelectionTool/SelectAllShortcutHandler.mjs +26 -0
  129. package/dist/mjs/tools/SelectionTool/Selection.d.ts +72 -0
  130. package/dist/mjs/tools/SelectionTool/Selection.mjs +606 -0
  131. package/dist/mjs/tools/SelectionTool/SelectionHandle.d.ts +62 -0
  132. package/dist/mjs/tools/SelectionTool/SelectionHandle.mjs +137 -0
  133. package/dist/mjs/tools/SelectionTool/SelectionMenuShortcut.d.ts +32 -0
  134. package/dist/mjs/tools/SelectionTool/SelectionMenuShortcut.mjs +83 -0
  135. package/dist/mjs/tools/SelectionTool/SelectionTool.d.ts +42 -0
  136. package/dist/mjs/tools/SelectionTool/SelectionTool.mjs +493 -0
  137. package/dist/mjs/tools/SelectionTool/SelectionTool.selecting.test.d.ts +1 -0
  138. package/dist/mjs/tools/SelectionTool/SelectionTool.test.d.ts +1 -0
  139. package/dist/mjs/tools/SelectionTool/ToPointerAutoscroller.d.ts +23 -0
  140. package/dist/mjs/tools/SelectionTool/ToPointerAutoscroller.mjs +77 -0
  141. package/dist/mjs/tools/SelectionTool/TransformMode.d.ts +42 -0
  142. package/dist/mjs/tools/SelectionTool/TransformMode.mjs +146 -0
  143. package/dist/mjs/tools/SelectionTool/keybindings.d.ts +15 -0
  144. package/dist/mjs/tools/SelectionTool/keybindings.mjs +32 -0
  145. package/dist/mjs/tools/SelectionTool/types.d.ts +35 -0
  146. package/dist/mjs/tools/SelectionTool/types.mjs +11 -0
  147. package/dist/mjs/tools/SelectionTool/util/makeClipboardErrorHandlers.d.ts +6 -0
  148. package/dist/mjs/tools/SelectionTool/util/makeClipboardErrorHandlers.mjs +45 -0
  149. package/dist/mjs/tools/SelectionTool/util/showSelectionContextMenu.d.ts +5 -0
  150. package/dist/mjs/tools/SelectionTool/util/showSelectionContextMenu.mjs +38 -0
  151. package/dist/mjs/tools/SoundUITool.d.ts +26 -0
  152. package/dist/mjs/tools/SoundUITool.mjs +165 -0
  153. package/dist/mjs/tools/TextTool.d.ts +36 -0
  154. package/dist/mjs/tools/TextTool.mjs +279 -0
  155. package/dist/mjs/tools/TextTool.test.d.ts +1 -0
  156. package/dist/mjs/tools/ToolController.d.ts +73 -0
  157. package/dist/mjs/tools/ToolController.mjs +275 -0
  158. package/dist/mjs/tools/ToolController.test.d.ts +1 -0
  159. package/dist/mjs/tools/ToolEnabledGroup.d.ts +6 -0
  160. package/dist/mjs/tools/ToolEnabledGroup.mjs +10 -0
  161. package/dist/mjs/tools/ToolSwitcherShortcut.d.ts +16 -0
  162. package/dist/mjs/tools/ToolSwitcherShortcut.mjs +34 -0
  163. package/dist/mjs/tools/ToolbarShortcutHandler.d.ts +12 -0
  164. package/dist/mjs/tools/ToolbarShortcutHandler.mjs +28 -0
  165. package/dist/mjs/tools/UndoRedoShortcut.d.ts +8 -0
  166. package/dist/mjs/tools/UndoRedoShortcut.mjs +21 -0
  167. package/dist/mjs/tools/UndoRedoShortcut.test.d.ts +1 -0
  168. package/dist/mjs/tools/keybindings.d.ts +16 -0
  169. package/dist/mjs/tools/keybindings.mjs +38 -0
  170. package/dist/mjs/tools/lib.d.ts +14 -0
  171. package/dist/mjs/tools/lib.mjs +14 -0
  172. package/dist/mjs/tools/localization.d.ts +43 -0
  173. package/dist/mjs/tools/localization.mjs +42 -0
  174. package/dist/mjs/tools/util/StationaryPenDetector.d.ts +25 -0
  175. package/dist/mjs/tools/util/StationaryPenDetector.mjs +103 -0
  176. package/dist/mjs/tools/util/createMenuOverlay.d.ts +10 -0
  177. package/dist/mjs/tools/util/createMenuOverlay.mjs +121 -0
  178. package/dist/mjs/tools/util/createMenuOverlay.test.d.ts +1 -0
  179. package/dist/mjs/version.mjs +1 -1
  180. package/package.json +4 -4
  181. package/src/tools/FindTool.css +7 -0
  182. package/src/tools/ScrollbarTool.scss +57 -0
  183. package/src/tools/SelectionTool/SelectionTool.scss +165 -0
  184. package/src/tools/SelectionTool/util/makeClipboardErrorHandlers.scss +15 -0
  185. package/src/tools/SoundUITool.scss +22 -0
  186. package/src/tools/tools.scss +6 -0
  187. package/src/tools/util/createMenuOverlay.scss +67 -0
@@ -0,0 +1,299 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.EraserMode = void 0;
7
+ const types_1 = require("../types");
8
+ const BaseTool_1 = __importDefault(require("./BaseTool"));
9
+ const math_1 = require("@js-draw/math");
10
+ const Erase_1 = __importDefault(require("../commands/Erase"));
11
+ const Pointer_1 = require("../Pointer");
12
+ const keybindings_1 = require("./keybindings");
13
+ const ReactiveValue_1 = require("../util/ReactiveValue");
14
+ const EditorImage_1 = __importDefault(require("../image/EditorImage"));
15
+ const uniteCommands_1 = __importDefault(require("../commands/uniteCommands"));
16
+ const RenderablePathSpec_1 = require("../rendering/RenderablePathSpec");
17
+ var EraserMode;
18
+ (function (EraserMode) {
19
+ EraserMode["PartialStroke"] = "partial-stroke";
20
+ EraserMode["FullStroke"] = "full-stroke";
21
+ })(EraserMode || (exports.EraserMode = EraserMode = {}));
22
+ /** Handles switching from other primary tools to the eraser and back */
23
+ class EraserSwitcher extends BaseTool_1.default {
24
+ constructor(editor, eraser) {
25
+ super(editor.notifier, editor.localization.changeTool);
26
+ this.editor = editor;
27
+ this.eraser = eraser;
28
+ }
29
+ onPointerDown(event) {
30
+ if (event.allPointers.length === 1 && event.current.device === Pointer_1.PointerDevice.Eraser) {
31
+ const toolController = this.editor.toolController;
32
+ const enabledPrimaryTools = toolController.getPrimaryTools().filter(tool => tool.isEnabled());
33
+ if (enabledPrimaryTools.length) {
34
+ this.previousEnabledTool = enabledPrimaryTools[0];
35
+ }
36
+ else {
37
+ this.previousEnabledTool = null;
38
+ }
39
+ this.previousEraserEnabledState = this.eraser.isEnabled();
40
+ this.eraser.setEnabled(true);
41
+ if (this.eraser.onPointerDown(event)) {
42
+ return true;
43
+ }
44
+ else {
45
+ this.restoreOriginalTool();
46
+ }
47
+ }
48
+ return false;
49
+ }
50
+ onPointerMove(event) {
51
+ this.eraser.onPointerMove(event);
52
+ }
53
+ restoreOriginalTool() {
54
+ this.eraser.setEnabled(this.previousEraserEnabledState);
55
+ if (this.previousEnabledTool) {
56
+ this.previousEnabledTool.setEnabled(true);
57
+ }
58
+ }
59
+ onPointerUp(event) {
60
+ this.eraser.onPointerUp(event);
61
+ this.restoreOriginalTool();
62
+ }
63
+ onGestureCancel(event) {
64
+ this.eraser.onGestureCancel(event);
65
+ this.restoreOriginalTool();
66
+ }
67
+ }
68
+ /**
69
+ * A tool that allows a user to erase parts of an image.
70
+ */
71
+ class Eraser extends BaseTool_1.default {
72
+ constructor(editor, description, options) {
73
+ super(editor.notifier, description);
74
+ this.editor = editor;
75
+ this.lastPoint = null;
76
+ this.isFirstEraseEvt = true;
77
+ this.toAdd = new Set();
78
+ // Commands that each remove one element
79
+ this.eraseCommands = [];
80
+ this.addCommands = [];
81
+ this.thickness = options?.thickness ?? 10;
82
+ this.thicknessValue = ReactiveValue_1.ReactiveValue.fromInitialValue(this.thickness);
83
+ this.thicknessValue.onUpdate(value => {
84
+ this.thickness = value;
85
+ this.editor.notifier.dispatch(types_1.EditorEventType.ToolUpdated, {
86
+ kind: types_1.EditorEventType.ToolUpdated,
87
+ tool: this,
88
+ });
89
+ });
90
+ this.modeValue = ReactiveValue_1.ReactiveValue.fromInitialValue(options?.mode ?? EraserMode.FullStroke);
91
+ this.modeValue.onUpdate(_value => {
92
+ this.editor.notifier.dispatch(types_1.EditorEventType.ToolUpdated, {
93
+ kind: types_1.EditorEventType.ToolUpdated,
94
+ tool: this,
95
+ });
96
+ });
97
+ }
98
+ /**
99
+ * @returns a tool that briefly enables the eraser when a physical eraser is used.
100
+ * This tool should be added to the tool list after the primary tools.
101
+ */
102
+ makeEraserSwitcherTool() {
103
+ return new EraserSwitcher(this.editor, this);
104
+ }
105
+ clearPreview() {
106
+ this.editor.clearWetInk();
107
+ }
108
+ getSizeOnCanvas() {
109
+ return this.thickness / this.editor.viewport.getScaleFactor();
110
+ }
111
+ drawPreviewAt(point) {
112
+ this.clearPreview();
113
+ const size = this.getSizeOnCanvas();
114
+ const renderer = this.editor.display.getWetInkRenderer();
115
+ const rect = this.getEraserRect(point);
116
+ const rect2 = this.getEraserRect(this.lastPoint ?? point);
117
+ const fill = {
118
+ fill: math_1.Color4.transparent,
119
+ stroke: { width: size / 10, color: math_1.Color4.gray },
120
+ };
121
+ renderer.drawPath((0, RenderablePathSpec_1.pathToRenderable)(math_1.Path.fromConvexHullOf([...rect.corners, ...rect2.corners]), fill));
122
+ }
123
+ /**
124
+ * @returns the eraser rectangle in canvas coordinates.
125
+ *
126
+ * For now, all erasers are rectangles or points.
127
+ */
128
+ getEraserRect(centerPoint) {
129
+ const size = this.getSizeOnCanvas();
130
+ const halfSize = math_1.Vec2.of(size / 2, size / 2);
131
+ return math_1.Rect2.fromCorners(centerPoint.minus(halfSize), centerPoint.plus(halfSize));
132
+ }
133
+ /** Erases in a line from the last point to the current. */
134
+ eraseTo(currentPoint) {
135
+ if (!this.isFirstEraseEvt && currentPoint.distanceTo(this.lastPoint) === 0) {
136
+ return;
137
+ }
138
+ this.isFirstEraseEvt = false;
139
+ // Currently only objects within eraserRect or that intersect a straight line
140
+ // from the center of the current rect to the previous are erased. TODO: Erase
141
+ // all objects as if there were pointerMove events between the two points.
142
+ const eraserRect = this.getEraserRect(currentPoint);
143
+ const line = new math_1.LineSegment2(this.lastPoint, currentPoint);
144
+ const region = math_1.Rect2.union(line.bbox, eraserRect);
145
+ const intersectingElems = this.editor.image.getElementsIntersectingRegion(region).filter(component => {
146
+ return component.intersects(line) || component.intersectsRect(eraserRect);
147
+ });
148
+ // Only erase components that could be selected (and thus interacted with)
149
+ // by the user.
150
+ const eraseableElems = intersectingElems.filter(elem => elem.isSelectable());
151
+ if (this.modeValue.get() === EraserMode.FullStroke) {
152
+ // Remove any intersecting elements.
153
+ this.toRemove.push(...eraseableElems);
154
+ // Create new Erase commands for the now-to-be-erased elements and apply them.
155
+ const newPartialCommands = eraseableElems.map(elem => new Erase_1.default([elem]));
156
+ newPartialCommands.forEach(cmd => cmd.apply(this.editor));
157
+ this.eraseCommands.push(...newPartialCommands);
158
+ }
159
+ else {
160
+ const toErase = [];
161
+ const toAdd = [];
162
+ for (const targetElem of eraseableElems) {
163
+ toErase.push(targetElem);
164
+ // Completely delete items that can't be divided.
165
+ if (!targetElem.withRegionErased) {
166
+ continue;
167
+ }
168
+ // Completely delete items that are completely or almost completely
169
+ // contained within the eraser.
170
+ const grownRect = eraserRect.grownBy(eraserRect.maxDimension / 3);
171
+ if (grownRect.containsRect(targetElem.getExactBBox())) {
172
+ continue;
173
+ }
174
+ // Join the current and previous rectangles so that points between events are also
175
+ // erased.
176
+ const erasePath = math_1.Path.fromConvexHullOf([
177
+ ...eraserRect.corners, ...this.getEraserRect(this.lastPoint ?? currentPoint).corners
178
+ ].map(p => this.editor.viewport.roundPoint(p)));
179
+ toAdd.push(...targetElem.withRegionErased(erasePath, this.editor.viewport));
180
+ }
181
+ const eraseCommand = new Erase_1.default(toErase);
182
+ const newAddCommands = toAdd.map(elem => EditorImage_1.default.addElement(elem));
183
+ eraseCommand.apply(this.editor);
184
+ newAddCommands.forEach(command => command.apply(this.editor));
185
+ const finalToErase = [];
186
+ for (const item of toErase) {
187
+ if (this.toAdd.has(item)) {
188
+ this.toAdd.delete(item);
189
+ }
190
+ else {
191
+ finalToErase.push(item);
192
+ }
193
+ }
194
+ this.toRemove.push(...finalToErase);
195
+ for (const item of toAdd) {
196
+ this.toAdd.add(item);
197
+ }
198
+ this.eraseCommands.push(new Erase_1.default(finalToErase));
199
+ this.addCommands.push(...newAddCommands);
200
+ }
201
+ this.drawPreviewAt(currentPoint);
202
+ this.lastPoint = currentPoint;
203
+ }
204
+ onPointerDown(event) {
205
+ if (event.allPointers.length === 1 || event.current.device === Pointer_1.PointerDevice.Eraser) {
206
+ this.lastPoint = event.current.canvasPos;
207
+ this.toRemove = [];
208
+ this.toAdd.clear();
209
+ this.isFirstEraseEvt = true;
210
+ this.drawPreviewAt(event.current.canvasPos);
211
+ return true;
212
+ }
213
+ return false;
214
+ }
215
+ onPointerMove(event) {
216
+ const currentPoint = event.current.canvasPos;
217
+ this.eraseTo(currentPoint);
218
+ }
219
+ onPointerUp(event) {
220
+ this.eraseTo(event.current.canvasPos);
221
+ const commands = [];
222
+ if (this.addCommands.length > 0) {
223
+ this.addCommands.forEach(cmd => cmd.unapply(this.editor));
224
+ // Remove items from toAdd that are also present in toRemove -- adding, then
225
+ // removing these does nothing, and can break undo/redo.
226
+ for (const item of this.toAdd) {
227
+ if (this.toRemove.includes(item)) {
228
+ this.toAdd.delete(item);
229
+ this.toRemove = this.toRemove.filter(other => other !== item);
230
+ }
231
+ }
232
+ for (const item of this.toRemove) {
233
+ if (this.toAdd.has(item)) {
234
+ this.toAdd.delete(item);
235
+ this.toRemove = this.toRemove.filter(other => other !== item);
236
+ }
237
+ }
238
+ commands.push(...[...this.toAdd].map(a => EditorImage_1.default.addElement(a)));
239
+ this.addCommands = [];
240
+ }
241
+ if (this.eraseCommands.length > 0) {
242
+ // Undo commands for each individual component and unite into a single command.
243
+ this.eraseCommands.forEach(cmd => cmd.unapply(this.editor));
244
+ this.eraseCommands = [];
245
+ const command = new Erase_1.default(this.toRemove);
246
+ commands.push(command);
247
+ }
248
+ if (commands.length === 1) {
249
+ this.editor.dispatch(commands[0]); // dispatch: Makes undo-able.
250
+ }
251
+ else {
252
+ this.editor.dispatch((0, uniteCommands_1.default)(commands));
253
+ }
254
+ this.clearPreview();
255
+ }
256
+ onGestureCancel(_event) {
257
+ this.addCommands.forEach(cmd => cmd.unapply(this.editor));
258
+ this.eraseCommands.forEach(cmd => cmd.unapply(this.editor));
259
+ this.eraseCommands = [];
260
+ this.addCommands = [];
261
+ this.clearPreview();
262
+ }
263
+ onKeyPress(event) {
264
+ const shortcuts = this.editor.shortcuts;
265
+ let newThickness;
266
+ if (shortcuts.matchesShortcut(keybindings_1.decreaseSizeKeyboardShortcutId, event)) {
267
+ newThickness = this.getThickness() * 2 / 3;
268
+ }
269
+ else if (shortcuts.matchesShortcut(keybindings_1.increaseSizeKeyboardShortcutId, event)) {
270
+ newThickness = this.getThickness() * 3 / 2;
271
+ }
272
+ if (newThickness !== undefined) {
273
+ newThickness = Math.min(Math.max(1, newThickness), 200);
274
+ this.setThickness(newThickness);
275
+ return true;
276
+ }
277
+ return false;
278
+ }
279
+ /** Returns the side-length of the tip of this eraser. */
280
+ getThickness() {
281
+ return this.thickness;
282
+ }
283
+ /** Sets the side-length of this' tip. */
284
+ setThickness(thickness) {
285
+ this.thicknessValue.set(thickness);
286
+ }
287
+ /**
288
+ * Returns a {@link MutableReactiveValue} that can be used to watch
289
+ * this tool's thickness.
290
+ */
291
+ getThicknessValue() {
292
+ return this.thicknessValue;
293
+ }
294
+ /** @returns An object that allows switching between a full stroke and a partial stroke eraser. */
295
+ getModeValue() {
296
+ return this.modeValue;
297
+ }
298
+ }
299
+ exports.default = Eraser;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,21 @@
1
+ import Editor from '../Editor';
2
+ import { KeyPressEvent } from '../inputEvents';
3
+ import BaseTool from './BaseTool';
4
+ export default class FindTool extends BaseTool {
5
+ private editor;
6
+ private overlay;
7
+ private searchInput;
8
+ private currentMatchIdx;
9
+ constructor(editor: Editor);
10
+ canReceiveInputInReadOnlyEditor(): boolean;
11
+ private getMatches;
12
+ private focusCurrentMatch;
13
+ private toNextMatch;
14
+ private toPrevMatch;
15
+ private fillOverlay;
16
+ private isVisible;
17
+ private setVisible;
18
+ private toggleVisible;
19
+ onKeyPress(event: KeyPressEvent): boolean;
20
+ setEnabled(enabled: boolean): void;
21
+ }
@@ -0,0 +1,137 @@
1
+ "use strict";
2
+ // Displays a find dialog that allows the user to search for and focus text.
3
+ //
4
+ // @packageDocumentation
5
+ var __importDefault = (this && this.__importDefault) || function (mod) {
6
+ return (mod && mod.__esModule) ? mod : { "default": mod };
7
+ };
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ const TextComponent_1 = __importDefault(require("../components/TextComponent"));
10
+ const ImageComponent_1 = __importDefault(require("../components/ImageComponent"));
11
+ const BaseTool_1 = __importDefault(require("./BaseTool"));
12
+ const keybindings_1 = require("./keybindings");
13
+ const cssPrefix = 'find-tool';
14
+ class FindTool extends BaseTool_1.default {
15
+ constructor(editor) {
16
+ super(editor.notifier, editor.localization.findLabel);
17
+ this.editor = editor;
18
+ this.currentMatchIdx = 0;
19
+ this.overlay = document.createElement('div');
20
+ this.fillOverlay();
21
+ editor.createHTMLOverlay(this.overlay);
22
+ this.overlay.style.display = 'none';
23
+ this.overlay.classList.add(`${cssPrefix}-overlay`);
24
+ }
25
+ canReceiveInputInReadOnlyEditor() {
26
+ return true;
27
+ }
28
+ getMatches(searchFor) {
29
+ const lowerSearchFor = searchFor.toLocaleLowerCase();
30
+ const matchingComponents = this.editor.image.getAllElements().filter(component => {
31
+ let text = '';
32
+ if (component instanceof TextComponent_1.default) {
33
+ text = component.getText();
34
+ }
35
+ else if (component instanceof ImageComponent_1.default) {
36
+ text = component.getAltText() ?? '';
37
+ }
38
+ else {
39
+ return false;
40
+ }
41
+ const hasLowercaseMatch = text.toLocaleLowerCase().indexOf(lowerSearchFor) !== -1;
42
+ const hasSameCaseMatch = text.indexOf(searchFor) !== -1;
43
+ return hasLowercaseMatch || hasSameCaseMatch;
44
+ });
45
+ return matchingComponents.map(match => match.getBBox());
46
+ }
47
+ focusCurrentMatch() {
48
+ const matches = this.getMatches(this.searchInput.value);
49
+ let matchIdx = this.currentMatchIdx % matches.length;
50
+ if (matchIdx < 0) {
51
+ matchIdx = matches.length + matchIdx;
52
+ }
53
+ if (matchIdx < matches.length) {
54
+ const undoable = false;
55
+ void this.editor.dispatch(this.editor.viewport.zoomTo(matches[matchIdx], true, true), undoable);
56
+ this.editor.announceForAccessibility(this.editor.localization.focusedFoundText(matchIdx + 1, matches.length));
57
+ }
58
+ }
59
+ toNextMatch() {
60
+ this.currentMatchIdx++;
61
+ this.focusCurrentMatch();
62
+ }
63
+ toPrevMatch() {
64
+ this.currentMatchIdx--;
65
+ this.focusCurrentMatch();
66
+ }
67
+ fillOverlay() {
68
+ const label = document.createElement('label');
69
+ this.searchInput = document.createElement('input');
70
+ const nextBtn = document.createElement('button');
71
+ const closeBtn = document.createElement('button');
72
+ // Math.random() ensures that the ID is unique (to allow us to refer to it
73
+ // with an htmlFor).
74
+ this.searchInput.setAttribute('id', `${cssPrefix}-searchInput-${Math.random()}`);
75
+ label.htmlFor = this.searchInput.getAttribute('id');
76
+ label.innerText = this.editor.localization.findLabel;
77
+ nextBtn.innerText = this.editor.localization.toNextMatch;
78
+ closeBtn.innerText = this.editor.localization.closeDialog;
79
+ this.searchInput.onkeydown = (ev) => {
80
+ if (ev.key === 'Enter') {
81
+ if (ev.shiftKey) {
82
+ this.toPrevMatch();
83
+ }
84
+ else {
85
+ this.toNextMatch();
86
+ }
87
+ }
88
+ else if (ev.key === 'Escape') {
89
+ this.setVisible(false);
90
+ }
91
+ else if (this.editor.shortcuts.matchesShortcut(keybindings_1.toggleFindVisibleShortcutId, ev)) {
92
+ ev.preventDefault();
93
+ this.toggleVisible();
94
+ }
95
+ };
96
+ nextBtn.onclick = () => {
97
+ this.toNextMatch();
98
+ };
99
+ closeBtn.onclick = () => {
100
+ this.setVisible(false);
101
+ };
102
+ this.overlay.replaceChildren(label, this.searchInput, nextBtn, closeBtn);
103
+ }
104
+ isVisible() {
105
+ return this.overlay.style.display !== 'none';
106
+ }
107
+ setVisible(visible) {
108
+ if (visible !== this.isVisible()) {
109
+ this.overlay.style.display = visible ? 'block' : 'none';
110
+ if (visible) {
111
+ this.searchInput.focus();
112
+ this.editor.announceForAccessibility(this.editor.localization.findDialogShown);
113
+ }
114
+ else {
115
+ this.editor.focus();
116
+ this.editor.announceForAccessibility(this.editor.localization.findDialogHidden);
117
+ }
118
+ }
119
+ }
120
+ toggleVisible() {
121
+ this.setVisible(!this.isVisible());
122
+ }
123
+ onKeyPress(event) {
124
+ if (this.editor.shortcuts.matchesShortcut(keybindings_1.toggleFindVisibleShortcutId, event)) {
125
+ this.toggleVisible();
126
+ return true;
127
+ }
128
+ return false;
129
+ }
130
+ setEnabled(enabled) {
131
+ super.setEnabled(enabled);
132
+ if (this.isEnabled()) {
133
+ this.setVisible(false);
134
+ }
135
+ }
136
+ }
137
+ exports.default = FindTool;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,17 @@
1
+ import { InputEvt } from '../../inputEvents';
2
+ import InputMapper from './InputMapper';
3
+ export default class ContextMenuRecognizer extends InputMapper {
4
+ private canShowContextMenu;
5
+ private contextMenuTriggerPointer;
6
+ private contextMenuStartPoint;
7
+ private stationaryDetector;
8
+ private clickTolerance;
9
+ constructor();
10
+ /**
11
+ * In general, only certain events (i.e. touchscreens) are expected to be able to
12
+ * create long-press menus. This method checks whether `event` was generated by
13
+ * one such device.
14
+ */
15
+ private canMakeLongPressMenuEvent;
16
+ onEvent(event: InputEvt): boolean;
17
+ }
@@ -0,0 +1,105 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ const inputEvents_1 = require("../../inputEvents");
30
+ const InputMapper_1 = __importDefault(require("./InputMapper"));
31
+ const StationaryPenDetector_1 = __importStar(require("../util/StationaryPenDetector"));
32
+ const Pointer_1 = require("../../Pointer");
33
+ class ContextMenuRecognizer extends InputMapper_1.default {
34
+ constructor() {
35
+ super();
36
+ this.canShowContextMenu = false;
37
+ this.stationaryDetector = null;
38
+ this.clickTolerance = 12;
39
+ }
40
+ /**
41
+ * In general, only certain events (i.e. touchscreens) are expected to be able to
42
+ * create long-press menus. This method checks whether `event` was generated by
43
+ * one such device.
44
+ */
45
+ canMakeLongPressMenuEvent(event) {
46
+ const allowedDevices = [
47
+ Pointer_1.PointerDevice.Touch,
48
+ ];
49
+ return event.allPointers.length === 1 && allowedDevices.includes(event.current.device);
50
+ }
51
+ onEvent(event) {
52
+ const sendContextMenuEvent = () => {
53
+ if (!(0, inputEvents_1.isPointerEvt)(event))
54
+ return false;
55
+ if (this.canShowContextMenu) {
56
+ const eventHandled = this.emit({
57
+ kind: inputEvents_1.InputEvtType.ContextMenu,
58
+ screenPos: event.current.screenPos,
59
+ canvasPos: event.current.canvasPos,
60
+ });
61
+ if (eventHandled) {
62
+ this.emit({
63
+ kind: inputEvents_1.InputEvtType.GestureCancelEvt,
64
+ });
65
+ return true;
66
+ }
67
+ }
68
+ return false;
69
+ };
70
+ if (event.kind === inputEvents_1.InputEvtType.PointerDownEvt) {
71
+ if (event.allPointers.length === 1) {
72
+ this.canShowContextMenu = true;
73
+ this.contextMenuTriggerPointer = event.current;
74
+ this.contextMenuStartPoint = event.current.screenPos;
75
+ if (this.canMakeLongPressMenuEvent(event)) {
76
+ this.stationaryDetector = new StationaryPenDetector_1.default(event.current, StationaryPenDetector_1.defaultStationaryDetectionConfig, sendContextMenuEvent);
77
+ }
78
+ }
79
+ else {
80
+ this.canShowContextMenu = false;
81
+ }
82
+ }
83
+ else if (event.kind === inputEvents_1.InputEvtType.PointerMoveEvt) {
84
+ if (this.canShowContextMenu) {
85
+ this.stationaryDetector?.onPointerMove(event.current);
86
+ // Only clicks/stationary long presses can create context menu events.
87
+ const deltaPosition = event.current.screenPos.minus(this.contextMenuStartPoint);
88
+ const threshold = this.clickTolerance;
89
+ if (deltaPosition.length() > threshold) {
90
+ this.canShowContextMenu = false;
91
+ }
92
+ }
93
+ }
94
+ else if (event.kind === inputEvents_1.InputEvtType.PointerUpEvt) {
95
+ this.stationaryDetector?.destroy();
96
+ if (this.contextMenuTriggerPointer?.id === event.current.id &&
97
+ this.contextMenuTriggerPointer.device === Pointer_1.PointerDevice.RightButtonMouse &&
98
+ sendContextMenuEvent()) {
99
+ return true;
100
+ }
101
+ }
102
+ return this.emit(event);
103
+ }
104
+ }
105
+ exports.default = ContextMenuRecognizer;
@@ -0,0 +1,12 @@
1
+ import { InputEvt } from '../../inputEvents';
2
+ import InputMapper from './InputMapper';
3
+ /**
4
+ * An `InputMapper` that applies a function to all events it receives.
5
+ *
6
+ * Useful for automated testing.
7
+ */
8
+ export default class FunctionMapper extends InputMapper {
9
+ private fn;
10
+ constructor(fn: (event: InputEvt) => InputEvt);
11
+ onEvent(event: InputEvt): boolean;
12
+ }
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const InputMapper_1 = __importDefault(require("./InputMapper"));
7
+ /**
8
+ * An `InputMapper` that applies a function to all events it receives.
9
+ *
10
+ * Useful for automated testing.
11
+ */
12
+ class FunctionMapper extends InputMapper_1.default {
13
+ constructor(fn) {
14
+ super();
15
+ this.fn = fn;
16
+ }
17
+ onEvent(event) {
18
+ return this.emit(this.fn(event));
19
+ }
20
+ }
21
+ exports.default = FunctionMapper;
@@ -0,0 +1,23 @@
1
+ import { InputEvt } from '../../inputEvents';
2
+ type OnEventCallback = (event: InputEvt) => boolean;
3
+ export interface InputEventListener {
4
+ onEvent: OnEventCallback;
5
+ }
6
+ /**
7
+ * Accepts input events and emits input events.
8
+ */
9
+ export default abstract class InputMapper implements InputEventListener {
10
+ #private;
11
+ constructor();
12
+ setEmitListener(listener: InputEventListener | OnEventCallback | null): void;
13
+ protected emit(event: InputEvt): boolean;
14
+ /**
15
+ * @returns true if the given `event` should be considered "handled" by the app and thus not
16
+ * forwarded to other targets. For example, returning "true" for a touchpad pinch event prevents
17
+ * the pinch event from zooming the webpage.
18
+ *
19
+ * Generally, this should return the result of calling `this.emit` with some event.
20
+ */
21
+ abstract onEvent(event: InputEvt): boolean;
22
+ }
23
+ export {};