js-draw 1.21.2 → 1.21.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (183) hide show
  1. package/dist/bundle.js +1 -1
  2. package/dist/cjs/tools/BaseTool.d.ts +61 -0
  3. package/dist/cjs/tools/BaseTool.js +179 -0
  4. package/dist/cjs/tools/Eraser.d.ts +60 -0
  5. package/dist/cjs/tools/Eraser.js +299 -0
  6. package/dist/cjs/tools/Eraser.test.d.ts +1 -0
  7. package/dist/cjs/tools/FindTool.d.ts +21 -0
  8. package/dist/cjs/tools/FindTool.js +137 -0
  9. package/dist/cjs/tools/FindTool.test.d.ts +1 -0
  10. package/dist/cjs/tools/InputFilter/ContextMenuRecognizer.d.ts +17 -0
  11. package/dist/cjs/tools/InputFilter/ContextMenuRecognizer.js +105 -0
  12. package/dist/cjs/tools/InputFilter/ContextMenuRecognizer.test.d.ts +1 -0
  13. package/dist/cjs/tools/InputFilter/FunctionMapper.d.ts +12 -0
  14. package/dist/cjs/tools/InputFilter/FunctionMapper.js +21 -0
  15. package/dist/cjs/tools/InputFilter/InputMapper.d.ts +23 -0
  16. package/dist/cjs/tools/InputFilter/InputMapper.js +38 -0
  17. package/dist/cjs/tools/InputFilter/InputPipeline.d.ts +15 -0
  18. package/dist/cjs/tools/InputFilter/InputPipeline.js +54 -0
  19. package/dist/cjs/tools/InputFilter/InputPipeline.test.d.ts +1 -0
  20. package/dist/cjs/tools/InputFilter/InputStabilizer.d.ts +29 -0
  21. package/dist/cjs/tools/InputFilter/InputStabilizer.js +181 -0
  22. package/dist/cjs/tools/InputFilter/StrokeKeyboardControl.d.ts +21 -0
  23. package/dist/cjs/tools/InputFilter/StrokeKeyboardControl.js +84 -0
  24. package/dist/cjs/tools/PanZoom.d.ts +125 -0
  25. package/dist/cjs/tools/PanZoom.js +517 -0
  26. package/dist/cjs/tools/PanZoom.test.d.ts +1 -0
  27. package/dist/cjs/tools/PasteHandler.d.ts +23 -0
  28. package/dist/cjs/tools/PasteHandler.js +109 -0
  29. package/dist/cjs/tools/Pen.d.ts +54 -0
  30. package/dist/cjs/tools/Pen.js +335 -0
  31. package/dist/cjs/tools/Pen.test.d.ts +1 -0
  32. package/dist/cjs/tools/PipetteTool.d.ts +28 -0
  33. package/dist/cjs/tools/PipetteTool.js +69 -0
  34. package/dist/cjs/tools/ScrollbarTool.d.ts +18 -0
  35. package/dist/cjs/tools/ScrollbarTool.js +85 -0
  36. package/dist/cjs/tools/SelectionTool/SelectAllShortcutHandler.d.ts +9 -0
  37. package/dist/cjs/tools/SelectionTool/SelectAllShortcutHandler.js +32 -0
  38. package/dist/cjs/tools/SelectionTool/Selection.d.ts +72 -0
  39. package/dist/cjs/tools/SelectionTool/Selection.js +634 -0
  40. package/dist/cjs/tools/SelectionTool/SelectionHandle.d.ts +62 -0
  41. package/dist/cjs/tools/SelectionTool/SelectionHandle.js +141 -0
  42. package/dist/cjs/tools/SelectionTool/SelectionMenuShortcut.d.ts +32 -0
  43. package/dist/cjs/tools/SelectionTool/SelectionMenuShortcut.js +86 -0
  44. package/dist/cjs/tools/SelectionTool/SelectionTool.d.ts +42 -0
  45. package/dist/cjs/tools/SelectionTool/SelectionTool.js +500 -0
  46. package/dist/cjs/tools/SelectionTool/SelectionTool.selecting.test.d.ts +1 -0
  47. package/dist/cjs/tools/SelectionTool/SelectionTool.test.d.ts +1 -0
  48. package/dist/cjs/tools/SelectionTool/ToPointerAutoscroller.d.ts +23 -0
  49. package/dist/cjs/tools/SelectionTool/ToPointerAutoscroller.js +83 -0
  50. package/dist/cjs/tools/SelectionTool/TransformMode.d.ts +42 -0
  51. package/dist/cjs/tools/SelectionTool/TransformMode.js +155 -0
  52. package/dist/cjs/tools/SelectionTool/keybindings.d.ts +15 -0
  53. package/dist/cjs/tools/SelectionTool/keybindings.js +38 -0
  54. package/dist/cjs/tools/SelectionTool/types.d.ts +35 -0
  55. package/dist/cjs/tools/SelectionTool/types.js +14 -0
  56. package/dist/cjs/tools/SelectionTool/util/makeClipboardErrorHandlers.d.ts +6 -0
  57. package/dist/cjs/tools/SelectionTool/util/makeClipboardErrorHandlers.js +50 -0
  58. package/dist/cjs/tools/SelectionTool/util/showSelectionContextMenu.d.ts +5 -0
  59. package/dist/cjs/tools/SelectionTool/util/showSelectionContextMenu.js +43 -0
  60. package/dist/cjs/tools/SoundUITool.d.ts +26 -0
  61. package/dist/cjs/tools/SoundUITool.js +171 -0
  62. package/dist/cjs/tools/TextTool.d.ts +36 -0
  63. package/dist/cjs/tools/TextTool.js +285 -0
  64. package/dist/cjs/tools/TextTool.test.d.ts +1 -0
  65. package/dist/cjs/tools/ToolController.d.ts +73 -0
  66. package/dist/cjs/tools/ToolController.js +304 -0
  67. package/dist/cjs/tools/ToolController.test.d.ts +1 -0
  68. package/dist/cjs/tools/ToolEnabledGroup.d.ts +6 -0
  69. package/dist/cjs/tools/ToolEnabledGroup.js +13 -0
  70. package/dist/cjs/tools/ToolSwitcherShortcut.d.ts +16 -0
  71. package/dist/cjs/tools/ToolSwitcherShortcut.js +40 -0
  72. package/dist/cjs/tools/ToolbarShortcutHandler.d.ts +12 -0
  73. package/dist/cjs/tools/ToolbarShortcutHandler.js +34 -0
  74. package/dist/cjs/tools/UndoRedoShortcut.d.ts +8 -0
  75. package/dist/cjs/tools/UndoRedoShortcut.js +27 -0
  76. package/dist/cjs/tools/UndoRedoShortcut.test.d.ts +1 -0
  77. package/dist/cjs/tools/keybindings.d.ts +16 -0
  78. package/dist/cjs/tools/keybindings.js +58 -0
  79. package/dist/cjs/tools/lib.d.ts +14 -0
  80. package/dist/cjs/tools/lib.js +36 -0
  81. package/dist/cjs/tools/localization.d.ts +43 -0
  82. package/dist/cjs/tools/localization.js +45 -0
  83. package/dist/cjs/tools/util/StationaryPenDetector.d.ts +25 -0
  84. package/dist/cjs/tools/util/StationaryPenDetector.js +107 -0
  85. package/dist/cjs/tools/util/createMenuOverlay.d.ts +10 -0
  86. package/dist/cjs/tools/util/createMenuOverlay.js +126 -0
  87. package/dist/cjs/tools/util/createMenuOverlay.test.d.ts +1 -0
  88. package/dist/cjs/version.js +1 -1
  89. package/dist/mjs/tools/BaseTool.d.ts +61 -0
  90. package/dist/mjs/tools/BaseTool.mjs +177 -0
  91. package/dist/mjs/tools/Eraser.d.ts +60 -0
  92. package/dist/mjs/tools/Eraser.mjs +292 -0
  93. package/dist/mjs/tools/Eraser.test.d.ts +1 -0
  94. package/dist/mjs/tools/FindTool.d.ts +21 -0
  95. package/dist/mjs/tools/FindTool.mjs +131 -0
  96. package/dist/mjs/tools/FindTool.test.d.ts +1 -0
  97. package/dist/mjs/tools/InputFilter/ContextMenuRecognizer.d.ts +17 -0
  98. package/dist/mjs/tools/InputFilter/ContextMenuRecognizer.mjs +76 -0
  99. package/dist/mjs/tools/InputFilter/ContextMenuRecognizer.test.d.ts +1 -0
  100. package/dist/mjs/tools/InputFilter/FunctionMapper.d.ts +12 -0
  101. package/dist/mjs/tools/InputFilter/FunctionMapper.mjs +15 -0
  102. package/dist/mjs/tools/InputFilter/InputMapper.d.ts +23 -0
  103. package/dist/mjs/tools/InputFilter/InputMapper.mjs +36 -0
  104. package/dist/mjs/tools/InputFilter/InputPipeline.d.ts +15 -0
  105. package/dist/mjs/tools/InputFilter/InputPipeline.mjs +49 -0
  106. package/dist/mjs/tools/InputFilter/InputPipeline.test.d.ts +1 -0
  107. package/dist/mjs/tools/InputFilter/InputStabilizer.d.ts +29 -0
  108. package/dist/mjs/tools/InputFilter/InputStabilizer.mjs +175 -0
  109. package/dist/mjs/tools/InputFilter/StrokeKeyboardControl.d.ts +21 -0
  110. package/dist/mjs/tools/InputFilter/StrokeKeyboardControl.mjs +78 -0
  111. package/dist/mjs/tools/PanZoom.d.ts +125 -0
  112. package/dist/mjs/tools/PanZoom.mjs +510 -0
  113. package/dist/mjs/tools/PanZoom.test.d.ts +1 -0
  114. package/dist/mjs/tools/PasteHandler.d.ts +23 -0
  115. package/dist/mjs/tools/PasteHandler.mjs +103 -0
  116. package/dist/mjs/tools/Pen.d.ts +54 -0
  117. package/dist/mjs/tools/Pen.mjs +306 -0
  118. package/dist/mjs/tools/Pen.test.d.ts +1 -0
  119. package/dist/mjs/tools/PipetteTool.d.ts +28 -0
  120. package/dist/mjs/tools/PipetteTool.mjs +63 -0
  121. package/dist/mjs/tools/ScrollbarTool.d.ts +18 -0
  122. package/dist/mjs/tools/ScrollbarTool.mjs +79 -0
  123. package/dist/mjs/tools/SelectionTool/SelectAllShortcutHandler.d.ts +9 -0
  124. package/dist/mjs/tools/SelectionTool/SelectAllShortcutHandler.mjs +26 -0
  125. package/dist/mjs/tools/SelectionTool/Selection.d.ts +72 -0
  126. package/dist/mjs/tools/SelectionTool/Selection.mjs +606 -0
  127. package/dist/mjs/tools/SelectionTool/SelectionHandle.d.ts +62 -0
  128. package/dist/mjs/tools/SelectionTool/SelectionHandle.mjs +137 -0
  129. package/dist/mjs/tools/SelectionTool/SelectionMenuShortcut.d.ts +32 -0
  130. package/dist/mjs/tools/SelectionTool/SelectionMenuShortcut.mjs +83 -0
  131. package/dist/mjs/tools/SelectionTool/SelectionTool.d.ts +42 -0
  132. package/dist/mjs/tools/SelectionTool/SelectionTool.mjs +493 -0
  133. package/dist/mjs/tools/SelectionTool/SelectionTool.selecting.test.d.ts +1 -0
  134. package/dist/mjs/tools/SelectionTool/SelectionTool.test.d.ts +1 -0
  135. package/dist/mjs/tools/SelectionTool/ToPointerAutoscroller.d.ts +23 -0
  136. package/dist/mjs/tools/SelectionTool/ToPointerAutoscroller.mjs +77 -0
  137. package/dist/mjs/tools/SelectionTool/TransformMode.d.ts +42 -0
  138. package/dist/mjs/tools/SelectionTool/TransformMode.mjs +146 -0
  139. package/dist/mjs/tools/SelectionTool/keybindings.d.ts +15 -0
  140. package/dist/mjs/tools/SelectionTool/keybindings.mjs +32 -0
  141. package/dist/mjs/tools/SelectionTool/types.d.ts +35 -0
  142. package/dist/mjs/tools/SelectionTool/types.mjs +11 -0
  143. package/dist/mjs/tools/SelectionTool/util/makeClipboardErrorHandlers.d.ts +6 -0
  144. package/dist/mjs/tools/SelectionTool/util/makeClipboardErrorHandlers.mjs +45 -0
  145. package/dist/mjs/tools/SelectionTool/util/showSelectionContextMenu.d.ts +5 -0
  146. package/dist/mjs/tools/SelectionTool/util/showSelectionContextMenu.mjs +38 -0
  147. package/dist/mjs/tools/SoundUITool.d.ts +26 -0
  148. package/dist/mjs/tools/SoundUITool.mjs +165 -0
  149. package/dist/mjs/tools/TextTool.d.ts +36 -0
  150. package/dist/mjs/tools/TextTool.mjs +279 -0
  151. package/dist/mjs/tools/TextTool.test.d.ts +1 -0
  152. package/dist/mjs/tools/ToolController.d.ts +73 -0
  153. package/dist/mjs/tools/ToolController.mjs +275 -0
  154. package/dist/mjs/tools/ToolController.test.d.ts +1 -0
  155. package/dist/mjs/tools/ToolEnabledGroup.d.ts +6 -0
  156. package/dist/mjs/tools/ToolEnabledGroup.mjs +10 -0
  157. package/dist/mjs/tools/ToolSwitcherShortcut.d.ts +16 -0
  158. package/dist/mjs/tools/ToolSwitcherShortcut.mjs +34 -0
  159. package/dist/mjs/tools/ToolbarShortcutHandler.d.ts +12 -0
  160. package/dist/mjs/tools/ToolbarShortcutHandler.mjs +28 -0
  161. package/dist/mjs/tools/UndoRedoShortcut.d.ts +8 -0
  162. package/dist/mjs/tools/UndoRedoShortcut.mjs +21 -0
  163. package/dist/mjs/tools/UndoRedoShortcut.test.d.ts +1 -0
  164. package/dist/mjs/tools/keybindings.d.ts +16 -0
  165. package/dist/mjs/tools/keybindings.mjs +38 -0
  166. package/dist/mjs/tools/lib.d.ts +14 -0
  167. package/dist/mjs/tools/lib.mjs +14 -0
  168. package/dist/mjs/tools/localization.d.ts +43 -0
  169. package/dist/mjs/tools/localization.mjs +42 -0
  170. package/dist/mjs/tools/util/StationaryPenDetector.d.ts +25 -0
  171. package/dist/mjs/tools/util/StationaryPenDetector.mjs +103 -0
  172. package/dist/mjs/tools/util/createMenuOverlay.d.ts +10 -0
  173. package/dist/mjs/tools/util/createMenuOverlay.mjs +121 -0
  174. package/dist/mjs/tools/util/createMenuOverlay.test.d.ts +1 -0
  175. package/dist/mjs/version.mjs +1 -1
  176. package/package.json +4 -4
  177. package/src/tools/FindTool.css +7 -0
  178. package/src/tools/ScrollbarTool.scss +57 -0
  179. package/src/tools/SelectionTool/SelectionTool.scss +165 -0
  180. package/src/tools/SelectionTool/util/makeClipboardErrorHandlers.scss +15 -0
  181. package/src/tools/SoundUITool.scss +22 -0
  182. package/src/tools/tools.scss +6 -0
  183. package/src/tools/util/createMenuOverlay.scss +67 -0
@@ -0,0 +1,285 @@
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 TextComponent_1 = __importDefault(require("../components/TextComponent"));
7
+ const EditorImage_1 = __importDefault(require("../image/EditorImage"));
8
+ const math_1 = require("@js-draw/math");
9
+ const Pointer_1 = require("../Pointer");
10
+ const types_1 = require("../types");
11
+ const BaseTool_1 = __importDefault(require("./BaseTool"));
12
+ const Erase_1 = __importDefault(require("../commands/Erase"));
13
+ const uniteCommands_1 = __importDefault(require("../commands/uniteCommands"));
14
+ const ReactiveValue_1 = require("../util/ReactiveValue");
15
+ const overlayCSSClass = 'textEditorOverlay';
16
+ class TextTool extends BaseTool_1.default {
17
+ constructor(editor, description, localizationTable) {
18
+ super(editor.notifier, description);
19
+ this.editor = editor;
20
+ this.localizationTable = localizationTable;
21
+ this.textInputElem = null;
22
+ this.textTargetPosition = null;
23
+ this.textMeasuringCtx = null;
24
+ this.textScale = math_1.Vec2.of(1, 1);
25
+ this.removeExistingCommand = null;
26
+ const editorFonts = editor.getCurrentSettings().text?.fonts ?? [];
27
+ this.textStyleValue = ReactiveValue_1.ReactiveValue.fromInitialValue({
28
+ size: 32,
29
+ fontFamily: editorFonts.length > 0 ? editorFonts[0] : 'sans-serif',
30
+ renderingStyle: {
31
+ fill: math_1.Color4.purple,
32
+ },
33
+ });
34
+ this.textStyleValue.onUpdateAndNow(() => {
35
+ this.textStyle = this.textStyleValue.get();
36
+ this.updateTextInput();
37
+ this.editor.notifier.dispatch(types_1.EditorEventType.ToolUpdated, {
38
+ kind: types_1.EditorEventType.ToolUpdated,
39
+ tool: this,
40
+ });
41
+ });
42
+ this.textEditOverlay = document.createElement('div');
43
+ this.textEditOverlay.classList.add(overlayCSSClass);
44
+ this.editor.addStyleSheet(`
45
+ .${overlayCSSClass} {
46
+ height: 0;
47
+ overflow: visible;
48
+
49
+ /* Allows absolutely-positioned textareas to scroll with
50
+ the containing overlay. */
51
+ position: relative;
52
+ }
53
+
54
+ .${overlayCSSClass} textarea {
55
+ background-color: rgba(0, 0, 0, 0);
56
+
57
+ white-space: pre;
58
+ overflow: hidden;
59
+
60
+ padding: 0;
61
+ margin: 0;
62
+ border: none;
63
+ padding: 0;
64
+
65
+ min-width: 100px;
66
+ min-height: 1.1em;
67
+ }
68
+ `);
69
+ this.editor.createHTMLOverlay(this.textEditOverlay);
70
+ this.editor.notifier.on(types_1.EditorEventType.ViewportChanged, () => this.updateTextInput());
71
+ }
72
+ initTextMeasuringCanvas() {
73
+ this.textMeasuringCtx ??= document.createElement('canvas').getContext('2d');
74
+ }
75
+ getTextAscent(text, style) {
76
+ this.initTextMeasuringCanvas();
77
+ if (this.textMeasuringCtx) {
78
+ this.textMeasuringCtx.textBaseline = 'alphabetic';
79
+ TextComponent_1.default.applyTextStyles(this.textMeasuringCtx, style);
80
+ const measurement = this.textMeasuringCtx.measureText(text);
81
+ return measurement.fontBoundingBoxAscent ?? measurement.actualBoundingBoxAscent;
82
+ }
83
+ // Estimate
84
+ return style.size * 2 / 3;
85
+ }
86
+ // Take input from this' textInputElem and add it to the EditorImage.
87
+ // If [removeInput], the HTML input element is removed. Otherwise, its value
88
+ // is cleared.
89
+ flushInput(removeInput = true) {
90
+ if (this.textInputElem && this.textTargetPosition) {
91
+ const content = this.textInputElem.value.trimEnd();
92
+ this.textInputElem.value = '';
93
+ if (removeInput) {
94
+ // In some browsers, .remove() triggers a .blur event (synchronously).
95
+ // Clear this.textInputElem before removal
96
+ const input = this.textInputElem;
97
+ this.textInputElem = null;
98
+ input.remove();
99
+ }
100
+ if (content === '') {
101
+ return;
102
+ }
103
+ const textTransform = math_1.Mat33.translation(this.textTargetPosition).rightMul(this.getTextScaleMatrix()).rightMul(math_1.Mat33.scaling2D(this.editor.viewport.getSizeOfPixelOnCanvas())).rightMul(math_1.Mat33.zRotation(this.textRotation));
104
+ const textComponent = TextComponent_1.default.fromLines(content.split('\n'), textTransform, this.textStyle);
105
+ const action = EditorImage_1.default.addElement(textComponent);
106
+ if (this.removeExistingCommand) {
107
+ // Unapply so that `removeExistingCommand` can be added to the undo stack.
108
+ this.removeExistingCommand.unapply(this.editor);
109
+ this.editor.dispatch((0, uniteCommands_1.default)([this.removeExistingCommand, action]));
110
+ this.removeExistingCommand = null;
111
+ }
112
+ else {
113
+ this.editor.dispatch(action);
114
+ }
115
+ }
116
+ }
117
+ getTextScaleMatrix() {
118
+ return math_1.Mat33.scaling2D(this.textScale.times(1 / this.editor.viewport.getSizeOfPixelOnCanvas()));
119
+ }
120
+ updateTextInput() {
121
+ if (!this.textInputElem || !this.textTargetPosition) {
122
+ this.textInputElem?.remove();
123
+ return;
124
+ }
125
+ const viewport = this.editor.viewport;
126
+ const textScreenPos = viewport.canvasToScreen(this.textTargetPosition);
127
+ this.textInputElem.placeholder = this.localizationTable.enterTextToInsert;
128
+ this.textInputElem.style.fontFamily = this.textStyle.fontFamily;
129
+ this.textInputElem.style.fontStyle = this.textStyle.fontStyle ?? '';
130
+ this.textInputElem.style.fontVariant = this.textStyle.fontVariant ?? '';
131
+ this.textInputElem.style.fontWeight = this.textStyle.fontWeight ?? '';
132
+ this.textInputElem.style.fontSize = `${this.textStyle.size}px`;
133
+ this.textInputElem.style.color = this.textStyle.renderingStyle.fill.toHexString();
134
+ this.textInputElem.style.position = 'absolute';
135
+ this.textInputElem.style.left = `${textScreenPos.x}px`;
136
+ this.textInputElem.style.top = `${textScreenPos.y}px`;
137
+ this.textInputElem.style.margin = '0';
138
+ this.textInputElem.style.width = `${this.textInputElem.scrollWidth}px`;
139
+ this.textInputElem.style.height = `${this.textInputElem.scrollHeight}px`;
140
+ // Get the ascent based on the font, using a string of characters
141
+ // that is tall in most fonts.
142
+ const tallText = 'Testing!';
143
+ const ascent = this.getTextAscent(tallText, this.textStyle);
144
+ const vertAdjust = ascent;
145
+ const rotation = this.textRotation + viewport.getRotationAngle();
146
+ const scale = this.getTextScaleMatrix();
147
+ this.textInputElem.style.transform =
148
+ `${scale.toCSSMatrix()} rotate(${rotation * 180 / Math.PI}deg) translate(0, ${-vertAdjust}px)`;
149
+ this.textInputElem.style.transformOrigin = 'top left';
150
+ // Match the line height of default rendered text.
151
+ const lineHeight = Math.floor(this.textStyle.size);
152
+ this.textInputElem.style.lineHeight = `${lineHeight}px`;
153
+ }
154
+ startTextInput(textCanvasPos, initialText) {
155
+ this.flushInput();
156
+ this.textInputElem = document.createElement('textarea');
157
+ this.textInputElem.value = initialText;
158
+ this.textInputElem.style.display = 'inline-block';
159
+ this.textTargetPosition = this.editor.viewport.roundPoint(textCanvasPos);
160
+ this.textRotation = -this.editor.viewport.getRotationAngle();
161
+ this.textScale = math_1.Vec2.of(1, 1).times(this.editor.viewport.getSizeOfPixelOnCanvas());
162
+ this.updateTextInput();
163
+ // Update the input size/position/etc. after the placeHolder has had time to appear.
164
+ setTimeout(() => this.updateTextInput(), 0);
165
+ this.textInputElem.oninput = () => {
166
+ if (this.textInputElem) {
167
+ this.textInputElem.style.width = `${this.textInputElem.scrollWidth}px`;
168
+ this.textInputElem.style.height = `${this.textInputElem.scrollHeight}px`;
169
+ }
170
+ };
171
+ this.textInputElem.onblur = () => {
172
+ // Delay removing the input -- flushInput may be called within a blur()
173
+ // event handler
174
+ const removeInput = false;
175
+ const input = this.textInputElem;
176
+ this.flushInput(removeInput);
177
+ this.textInputElem = null;
178
+ setTimeout(() => {
179
+ input?.remove();
180
+ }, 0);
181
+ };
182
+ this.textInputElem.onkeyup = (evt) => {
183
+ if (evt.key === 'Enter' && !evt.shiftKey) {
184
+ this.flushInput();
185
+ this.editor.focus();
186
+ }
187
+ else if (evt.key === 'Escape') {
188
+ // Cancel input.
189
+ this.textInputElem?.remove();
190
+ this.textInputElem = null;
191
+ this.editor.focus();
192
+ this.removeExistingCommand?.unapply(this.editor);
193
+ this.removeExistingCommand = null;
194
+ }
195
+ };
196
+ this.textEditOverlay.replaceChildren(this.textInputElem);
197
+ setTimeout(() => this.textInputElem?.focus(), 0);
198
+ }
199
+ setEnabled(enabled) {
200
+ super.setEnabled(enabled);
201
+ if (!this.isEnabled()) {
202
+ this.flushInput();
203
+ }
204
+ this.textEditOverlay.style.display = enabled ? 'block' : 'none';
205
+ }
206
+ onPointerDown({ current, allPointers }) {
207
+ if (current.device === Pointer_1.PointerDevice.Eraser) {
208
+ return false;
209
+ }
210
+ if (allPointers.length === 1) {
211
+ // Are we clicking on a text node?
212
+ const canvasPos = current.canvasPos;
213
+ const halfTestRegionSize = math_1.Vec2.of(2.5, 2.5).times(this.editor.viewport.getSizeOfPixelOnCanvas());
214
+ const testRegion = math_1.Rect2.fromCorners(canvasPos.minus(halfTestRegionSize), canvasPos.plus(halfTestRegionSize));
215
+ const targetNodes = this.editor.image.getElementsIntersectingRegion(testRegion);
216
+ let targetTextNodes = targetNodes.filter(node => node instanceof TextComponent_1.default);
217
+ // Don't try to edit text nodes that contain the viewport (this allows us
218
+ // to zoom in on text nodes and add text on top of them.)
219
+ const visibleRect = this.editor.viewport.visibleRect;
220
+ targetTextNodes = targetTextNodes.filter(node => !node.getBBox().containsRect(visibleRect));
221
+ // End any TextNodes we're currently editing.
222
+ this.flushInput();
223
+ if (targetTextNodes.length > 0) {
224
+ const targetNode = targetTextNodes[targetTextNodes.length - 1];
225
+ this.setTextStyle(targetNode.getTextStyle());
226
+ // Create and temporarily apply removeExistingCommand.
227
+ this.removeExistingCommand = new Erase_1.default([targetNode]);
228
+ this.removeExistingCommand.apply(this.editor);
229
+ this.startTextInput(targetNode.getBaselinePos(), targetNode.getText());
230
+ const transform = targetNode.getTransform();
231
+ this.textRotation = transform.transformVec3(math_1.Vec2.unitX).angle();
232
+ const scaleFactor = transform.transformVec3(math_1.Vec2.unitX).magnitude();
233
+ this.textScale = math_1.Vec2.of(1, 1).times(scaleFactor);
234
+ this.updateTextInput();
235
+ }
236
+ else {
237
+ this.removeExistingCommand = null;
238
+ this.startTextInput(current.canvasPos, '');
239
+ }
240
+ return true;
241
+ }
242
+ return false;
243
+ }
244
+ onGestureCancel() {
245
+ this.flushInput();
246
+ this.editor.focus();
247
+ }
248
+ setFontFamily(fontFamily) {
249
+ if (fontFamily !== this.textStyle.fontFamily) {
250
+ this.textStyleValue.set({
251
+ ...this.textStyle,
252
+ fontFamily: fontFamily,
253
+ });
254
+ }
255
+ }
256
+ setColor(color) {
257
+ if (!color.eq(this.textStyle.renderingStyle.fill)) {
258
+ this.textStyleValue.set({
259
+ ...this.textStyle,
260
+ renderingStyle: {
261
+ ...this.textStyle.renderingStyle,
262
+ fill: color,
263
+ },
264
+ });
265
+ }
266
+ }
267
+ setFontSize(size) {
268
+ if (size !== this.textStyle.size) {
269
+ this.textStyleValue.set({
270
+ ...this.textStyle,
271
+ size,
272
+ });
273
+ }
274
+ }
275
+ getTextStyle() {
276
+ return this.textStyle;
277
+ }
278
+ getStyleValue() {
279
+ return this.textStyleValue;
280
+ }
281
+ setTextStyle(style) {
282
+ this.textStyleValue.set(style);
283
+ }
284
+ }
285
+ exports.default = TextTool;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,73 @@
1
+ import Editor from '../Editor';
2
+ import BaseTool from './BaseTool';
3
+ import ToolEnabledGroup from './ToolEnabledGroup';
4
+ import { ToolLocalization } from './localization';
5
+ import InputMapper, { InputEventListener } from './InputFilter/InputMapper';
6
+ import { InputEvt } from '../inputEvents';
7
+ export default class ToolController implements InputEventListener {
8
+ private tools;
9
+ private activeTool;
10
+ private primaryToolGroup;
11
+ private inputPipeline;
12
+ private isEditorReadOnly;
13
+ /** @internal */
14
+ constructor(editor: Editor, localization: ToolLocalization);
15
+ /**
16
+ * Replaces the current set of tools with `tools`. This should only be done before
17
+ * the creation of the app's toolbar (if using `AbstractToolbar`).
18
+ *
19
+ * If no `primaryToolGroup` is given, an empty one will be created.
20
+ */
21
+ setTools(tools: BaseTool[], primaryToolGroup?: ToolEnabledGroup): void;
22
+ /**
23
+ * Add a tool that acts like one of the primary tools (only one primary tool can be enabled at a time).
24
+ *
25
+ * If the tool is already added to this, the tool is converted to a primary tool.
26
+ *
27
+ * This should be called before creating the app's toolbar.
28
+ */
29
+ addPrimaryTool(tool: BaseTool): void;
30
+ getPrimaryTools(): BaseTool[];
31
+ /**
32
+ * Add a tool to the end of this' tool list (the added tool receives events after tools already added to this).
33
+ * This should be called before creating the app's toolbar.
34
+ *
35
+ * If `options.addToFront`, the tool is added to the beginning of this' tool list.
36
+ *
37
+ * Does nothing if the tool is already present.
38
+ */
39
+ addTool(tool: BaseTool, options?: {
40
+ addToFront: boolean;
41
+ }): void;
42
+ /**
43
+ * Removes **and destroys** all tools in `tools` from this.
44
+ */
45
+ removeAndDestroyTools(tools: BaseTool[]): void;
46
+ private insertTools;
47
+ /**
48
+ * Removes a tool from this' tool list and replaces it with `replaceWith`.
49
+ *
50
+ * If any of `toolsToInsert` have already been added to this, the tools are
51
+ * moved.
52
+ *
53
+ * This should be called before creating the editor's toolbar.
54
+ */
55
+ insertToolsAfter(insertAfter: BaseTool, toolsToInsert: BaseTool[]): void;
56
+ /** @see {@link insertToolsAfter} */
57
+ insertToolsBefore(insertBefore: BaseTool, toolsToInsert: BaseTool[]): void;
58
+ private onEventInternal;
59
+ /** Alias for {@link dispatchInputEvent}. */
60
+ onEvent(event: InputEvt): boolean;
61
+ dispatchInputEvent(event: InputEvt): boolean;
62
+ /**
63
+ * Adds a new `InputMapper` to this' input pipeline.
64
+ *
65
+ * A `mapper` is really a relation that maps each event to no, one,
66
+ * or many other events.
67
+ *
68
+ * @see {@link InputMapper}.
69
+ */
70
+ addInputMapper(mapper: InputMapper): void;
71
+ getMatchingTools<Type extends BaseTool>(type: new (...args: any[]) => Type): Type[];
72
+ onEditorDestroyed(): void;
73
+ }
@@ -0,0 +1,304 @@
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 types_1 = require("../types");
30
+ const math_1 = require("@js-draw/math");
31
+ const PanZoom_1 = __importStar(require("./PanZoom"));
32
+ const Pen_1 = __importDefault(require("./Pen"));
33
+ const ToolEnabledGroup_1 = __importDefault(require("./ToolEnabledGroup"));
34
+ const Eraser_1 = __importDefault(require("./Eraser"));
35
+ const SelectionTool_1 = __importDefault(require("./SelectionTool/SelectionTool"));
36
+ const UndoRedoShortcut_1 = __importDefault(require("./UndoRedoShortcut"));
37
+ const TextTool_1 = __importDefault(require("./TextTool"));
38
+ const PipetteTool_1 = __importDefault(require("./PipetteTool"));
39
+ const ToolSwitcherShortcut_1 = __importDefault(require("./ToolSwitcherShortcut"));
40
+ const PasteHandler_1 = __importDefault(require("./PasteHandler"));
41
+ const ToolbarShortcutHandler_1 = __importDefault(require("./ToolbarShortcutHandler"));
42
+ const PressureSensitiveFreehandLineBuilder_1 = require("../components/builders/PressureSensitiveFreehandLineBuilder");
43
+ const FindTool_1 = __importDefault(require("./FindTool"));
44
+ const SelectAllShortcutHandler_1 = __importDefault(require("./SelectionTool/SelectAllShortcutHandler"));
45
+ const SoundUITool_1 = __importDefault(require("./SoundUITool"));
46
+ const inputEvents_1 = require("../inputEvents");
47
+ const InputPipeline_1 = __importDefault(require("./InputFilter/InputPipeline"));
48
+ const InputStabilizer_1 = __importDefault(require("./InputFilter/InputStabilizer"));
49
+ const ScrollbarTool_1 = __importDefault(require("./ScrollbarTool"));
50
+ class ToolController {
51
+ /** @internal */
52
+ constructor(editor, localization) {
53
+ this.activeTool = null;
54
+ this.isEditorReadOnly = editor.isReadOnlyReactiveValue();
55
+ this.inputPipeline = new InputPipeline_1.default();
56
+ this.inputPipeline.setEmitListener(event => this.onEventInternal(event));
57
+ const primaryToolGroup = new ToolEnabledGroup_1.default();
58
+ this.primaryToolGroup = primaryToolGroup;
59
+ const panZoomTool = new PanZoom_1.default(editor, PanZoom_1.PanZoomMode.TwoFingerTouchGestures | PanZoom_1.PanZoomMode.RightClickDrags, localization.touchPanTool);
60
+ const keyboardPanZoomTool = new PanZoom_1.default(editor, PanZoom_1.PanZoomMode.Keyboard, localization.keyboardPanZoom);
61
+ const primaryPenTool = new Pen_1.default(editor, localization.penTool(1), { color: math_1.Color4.purple, thickness: 8 });
62
+ const secondaryPenTool = new Pen_1.default(editor, localization.penTool(2), { color: math_1.Color4.clay, thickness: 4 });
63
+ // Stabilize the secondary pen tool.
64
+ secondaryPenTool.setInputMapper(new InputStabilizer_1.default(editor.viewport));
65
+ const eraser = new Eraser_1.default(editor, localization.eraserTool);
66
+ const primaryTools = [
67
+ // Three pens
68
+ primaryPenTool,
69
+ secondaryPenTool,
70
+ // Highlighter-like pen with width=40
71
+ new Pen_1.default(editor, localization.penTool(3), {
72
+ color: math_1.Color4.ofRGBA(1, 1, 0, 0.5),
73
+ thickness: 40,
74
+ factory: PressureSensitiveFreehandLineBuilder_1.makePressureSensitiveFreehandLineBuilder
75
+ }),
76
+ eraser,
77
+ new SelectionTool_1.default(editor, localization.selectionTool),
78
+ new TextTool_1.default(editor, localization.textTool, localization),
79
+ new PanZoom_1.default(editor, PanZoom_1.PanZoomMode.SinglePointerGestures, localization.anyDevicePanning),
80
+ ];
81
+ // Accessibility tools
82
+ const soundExplorer = new SoundUITool_1.default(editor, localization.soundExplorer);
83
+ soundExplorer.setEnabled(false);
84
+ this.tools = [
85
+ new ScrollbarTool_1.default(editor),
86
+ new PipetteTool_1.default(editor, localization.pipetteTool),
87
+ soundExplorer,
88
+ panZoomTool,
89
+ ...primaryTools,
90
+ keyboardPanZoomTool,
91
+ new UndoRedoShortcut_1.default(editor),
92
+ new ToolbarShortcutHandler_1.default(editor),
93
+ new ToolSwitcherShortcut_1.default(editor),
94
+ eraser.makeEraserSwitcherTool(),
95
+ new FindTool_1.default(editor),
96
+ new PasteHandler_1.default(editor),
97
+ new SelectAllShortcutHandler_1.default(editor),
98
+ ];
99
+ primaryTools.forEach(tool => tool.setToolGroup(primaryToolGroup));
100
+ panZoomTool.setEnabled(true);
101
+ primaryPenTool.setEnabled(true);
102
+ editor.notifier.on(types_1.EditorEventType.ToolEnabled, event => {
103
+ if (event.kind === types_1.EditorEventType.ToolEnabled) {
104
+ editor.announceForAccessibility(localization.toolEnabledAnnouncement(event.tool.description));
105
+ }
106
+ });
107
+ editor.notifier.on(types_1.EditorEventType.ToolDisabled, event => {
108
+ if (event.kind === types_1.EditorEventType.ToolDisabled) {
109
+ editor.announceForAccessibility(localization.toolDisabledAnnouncement(event.tool.description));
110
+ }
111
+ });
112
+ this.activeTool = null;
113
+ }
114
+ /**
115
+ * Replaces the current set of tools with `tools`. This should only be done before
116
+ * the creation of the app's toolbar (if using `AbstractToolbar`).
117
+ *
118
+ * If no `primaryToolGroup` is given, an empty one will be created.
119
+ */
120
+ setTools(tools, primaryToolGroup) {
121
+ this.tools = tools;
122
+ this.primaryToolGroup = primaryToolGroup ?? new ToolEnabledGroup_1.default();
123
+ }
124
+ /**
125
+ * Add a tool that acts like one of the primary tools (only one primary tool can be enabled at a time).
126
+ *
127
+ * If the tool is already added to this, the tool is converted to a primary tool.
128
+ *
129
+ * This should be called before creating the app's toolbar.
130
+ */
131
+ addPrimaryTool(tool) {
132
+ tool.setToolGroup(this.primaryToolGroup);
133
+ if (tool.isEnabled()) {
134
+ this.primaryToolGroup.notifyEnabled(tool);
135
+ }
136
+ if (!this.tools.includes(tool)) {
137
+ this.addTool(tool);
138
+ }
139
+ }
140
+ getPrimaryTools() {
141
+ return this.tools.filter(tool => {
142
+ return tool.getToolGroup() === this.primaryToolGroup;
143
+ });
144
+ }
145
+ /**
146
+ * Add a tool to the end of this' tool list (the added tool receives events after tools already added to this).
147
+ * This should be called before creating the app's toolbar.
148
+ *
149
+ * If `options.addToFront`, the tool is added to the beginning of this' tool list.
150
+ *
151
+ * Does nothing if the tool is already present.
152
+ */
153
+ addTool(tool, options) {
154
+ // Only add if not already present.
155
+ if (!this.tools.includes(tool)) {
156
+ if (options?.addToFront) {
157
+ this.tools.splice(0, 0, tool);
158
+ }
159
+ else {
160
+ this.tools.push(tool);
161
+ }
162
+ }
163
+ }
164
+ /**
165
+ * Removes **and destroys** all tools in `tools` from this.
166
+ */
167
+ removeAndDestroyTools(tools) {
168
+ const newTools = [];
169
+ for (const tool of this.tools) {
170
+ if (tools.includes(tool)) {
171
+ if (this.activeTool === tool) {
172
+ this.activeTool = null;
173
+ }
174
+ tool.onDestroy();
175
+ }
176
+ else {
177
+ newTools.push(tool);
178
+ }
179
+ }
180
+ this.tools = newTools;
181
+ }
182
+ insertTools(insertNear, toolsToInsert, mode) {
183
+ this.tools = this.tools.filter(tool => !toolsToInsert.includes(tool));
184
+ const newTools = [];
185
+ for (const tool of this.tools) {
186
+ if (mode === 'after') {
187
+ newTools.push(tool);
188
+ }
189
+ if (tool === insertNear) {
190
+ newTools.push(...toolsToInsert);
191
+ }
192
+ if (mode === 'before') {
193
+ newTools.push(tool);
194
+ }
195
+ }
196
+ this.tools = newTools;
197
+ }
198
+ /**
199
+ * Removes a tool from this' tool list and replaces it with `replaceWith`.
200
+ *
201
+ * If any of `toolsToInsert` have already been added to this, the tools are
202
+ * moved.
203
+ *
204
+ * This should be called before creating the editor's toolbar.
205
+ */
206
+ insertToolsAfter(insertAfter, toolsToInsert) {
207
+ this.insertTools(insertAfter, toolsToInsert, 'after');
208
+ }
209
+ /** @see {@link insertToolsAfter} */
210
+ insertToolsBefore(insertBefore, toolsToInsert) {
211
+ this.insertTools(insertBefore, toolsToInsert, 'before');
212
+ }
213
+ // @internal use `dispatchEvent` rather than calling `onEvent` directly.
214
+ onEventInternal(event) {
215
+ const isEditorReadOnly = this.isEditorReadOnly.get();
216
+ const canToolReceiveInput = (tool) => {
217
+ return tool.isEnabled() && (!isEditorReadOnly || tool.canReceiveInputInReadOnlyEditor());
218
+ };
219
+ let handled = false;
220
+ if (event.kind === inputEvents_1.InputEvtType.PointerDownEvt) {
221
+ let canOnlySendToActiveTool = false;
222
+ if (this.activeTool && !this.activeTool.eventCanBeDeliveredToNonActiveTool(event)) {
223
+ canOnlySendToActiveTool = true;
224
+ }
225
+ for (const tool of this.tools) {
226
+ if (canOnlySendToActiveTool && tool !== this.activeTool) {
227
+ continue;
228
+ }
229
+ if (canToolReceiveInput(tool) && tool.onEvent(event)) {
230
+ if (this.activeTool !== tool) {
231
+ this.activeTool?.onEvent({ kind: inputEvents_1.InputEvtType.GestureCancelEvt });
232
+ }
233
+ this.activeTool = tool;
234
+ handled = true;
235
+ break;
236
+ }
237
+ }
238
+ }
239
+ else if (event.kind === inputEvents_1.InputEvtType.PointerUpEvt) {
240
+ const upResult = this.activeTool?.onEvent(event);
241
+ const continueHandlingEvents = upResult && event.allPointers.length > 1;
242
+ // Should the active tool continue handling events (without an additional pointer down?)
243
+ if (!continueHandlingEvents) {
244
+ // No -- Remove the current tool
245
+ this.activeTool = null;
246
+ }
247
+ handled = true;
248
+ }
249
+ else if (event.kind === inputEvents_1.InputEvtType.PointerMoveEvt) {
250
+ if (this.activeTool !== null) {
251
+ this.activeTool.onEvent(event);
252
+ handled = true;
253
+ }
254
+ }
255
+ else if (event.kind === inputEvents_1.InputEvtType.GestureCancelEvt) {
256
+ if (this.activeTool !== null) {
257
+ this.activeTool.onEvent(event);
258
+ this.activeTool = null;
259
+ }
260
+ }
261
+ else {
262
+ for (const tool of this.tools) {
263
+ if (!canToolReceiveInput(tool)) {
264
+ continue;
265
+ }
266
+ handled = tool.onEvent(event);
267
+ if (handled) {
268
+ break;
269
+ }
270
+ }
271
+ }
272
+ return handled;
273
+ }
274
+ /** Alias for {@link dispatchInputEvent}. */
275
+ onEvent(event) {
276
+ return this.dispatchInputEvent(event);
277
+ }
278
+ // Returns true if the event was handled.
279
+ dispatchInputEvent(event) {
280
+ // Feed the event through the input pipeline
281
+ return this.inputPipeline.onEvent(event);
282
+ }
283
+ /**
284
+ * Adds a new `InputMapper` to this' input pipeline.
285
+ *
286
+ * A `mapper` is really a relation that maps each event to no, one,
287
+ * or many other events.
288
+ *
289
+ * @see {@link InputMapper}.
290
+ */
291
+ addInputMapper(mapper) {
292
+ this.inputPipeline.addToTail(mapper);
293
+ }
294
+ getMatchingTools(type) {
295
+ return this.tools.filter(tool => tool instanceof type);
296
+ }
297
+ // @internal
298
+ onEditorDestroyed() {
299
+ for (const tool of this.tools) {
300
+ tool.onDestroy();
301
+ }
302
+ }
303
+ }
304
+ exports.default = ToolController;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,6 @@
1
+ import BaseTool from './BaseTool';
2
+ export default class ToolEnabledGroup {
3
+ private activeTool;
4
+ constructor();
5
+ notifyEnabled(tool: BaseTool): void;
6
+ }
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ // Connects a group of tools -- at most one tool in the group can be enabled.
4
+ class ToolEnabledGroup {
5
+ constructor() { }
6
+ notifyEnabled(tool) {
7
+ if (tool !== this.activeTool) {
8
+ this.activeTool?.setEnabled(false);
9
+ this.activeTool = tool;
10
+ }
11
+ }
12
+ }
13
+ exports.default = ToolEnabledGroup;