js-draw 1.21.2 → 1.22.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (421) hide show
  1. package/README.md +103 -75
  2. package/build-config.json +2 -2
  3. package/dist/Editor.css +29 -16
  4. package/dist/bundle.js +2 -2
  5. package/dist/bundledStyles.js +1 -1
  6. package/dist/cjs/Editor.js +36 -22
  7. package/dist/cjs/EventDispatcher.js +1 -1
  8. package/dist/cjs/Pointer.js +3 -3
  9. package/dist/cjs/SVGLoader/SVGLoader.js +13 -6
  10. package/dist/cjs/UndoRedoHistory.js +1 -1
  11. package/dist/cjs/Viewport.js +4 -2
  12. package/dist/cjs/commands/Command.js +7 -5
  13. package/dist/cjs/commands/Duplicate.js +2 -2
  14. package/dist/cjs/commands/Erase.js +3 -4
  15. package/dist/cjs/commands/invertCommand.js +4 -4
  16. package/dist/cjs/commands/lib.d.ts +1 -1
  17. package/dist/cjs/commands/uniteCommands.js +4 -4
  18. package/dist/cjs/components/AbstractComponent.d.ts +1 -1
  19. package/dist/cjs/components/AbstractComponent.js +3 -3
  20. package/dist/cjs/components/BackgroundComponent.js +8 -6
  21. package/dist/cjs/components/ImageComponent.js +12 -5
  22. package/dist/cjs/components/RestylableComponent.js +1 -1
  23. package/dist/cjs/components/SVGGlobalAttributesObject.js +1 -2
  24. package/dist/cjs/components/Stroke.js +37 -24
  25. package/dist/cjs/components/TextComponent.js +13 -10
  26. package/dist/cjs/components/UnknownSVGObject.js +2 -3
  27. package/dist/cjs/components/builders/ArrowBuilder.d.ts +6 -0
  28. package/dist/cjs/components/builders/ArrowBuilder.js +9 -3
  29. package/dist/cjs/components/builders/CircleBuilder.d.ts +6 -0
  30. package/dist/cjs/components/builders/CircleBuilder.js +11 -4
  31. package/dist/cjs/components/builders/FreehandLineBuilder.d.ts +6 -0
  32. package/dist/cjs/components/builders/FreehandLineBuilder.js +10 -4
  33. package/dist/cjs/components/builders/LineBuilder.d.ts +6 -0
  34. package/dist/cjs/components/builders/LineBuilder.js +8 -4
  35. package/dist/cjs/components/builders/PolylineBuilder.d.ts +4 -1
  36. package/dist/cjs/components/builders/PolylineBuilder.js +9 -5
  37. package/dist/cjs/components/builders/PressureSensitiveFreehandLineBuilder.js +16 -10
  38. package/dist/cjs/components/builders/RectangleBuilder.d.ts +12 -0
  39. package/dist/cjs/components/builders/RectangleBuilder.js +17 -3
  40. package/dist/cjs/components/builders/autocorrect/makeShapeFitAutocorrect.js +5 -8
  41. package/dist/cjs/components/builders/autocorrect/makeSnapToGridAutocorrect.js +1 -1
  42. package/dist/cjs/components/builders/lib.d.ts +7 -0
  43. package/dist/cjs/components/builders/lib.js +18 -0
  44. package/dist/cjs/components/lib.d.ts +1 -4
  45. package/dist/cjs/components/lib.js +2 -9
  46. package/dist/cjs/components/util/StrokeSmoother.js +5 -6
  47. package/dist/cjs/dialogs/makeAboutDialog.js +1 -1
  48. package/dist/cjs/dialogs/makeMessageDialog.js +2 -2
  49. package/dist/cjs/image/EditorImage.js +13 -15
  50. package/dist/cjs/image/export/editorImageToSVG.js +1 -1
  51. package/dist/cjs/inputEvents.js +3 -3
  52. package/dist/cjs/lib.d.ts +2 -2
  53. package/dist/cjs/localizations/de.js +2 -2
  54. package/dist/cjs/localizations/es.js +7 -3
  55. package/dist/cjs/rendering/Display.js +7 -3
  56. package/dist/cjs/rendering/RenderablePathSpec.js +26 -11
  57. package/dist/cjs/rendering/RenderingStyle.js +22 -15
  58. package/dist/cjs/rendering/TextRenderingStyle.js +1 -1
  59. package/dist/cjs/rendering/caching/CacheRecord.js +1 -1
  60. package/dist/cjs/rendering/caching/CacheRecordManager.js +1 -1
  61. package/dist/cjs/rendering/caching/RenderingCache.js +1 -1
  62. package/dist/cjs/rendering/caching/RenderingCacheNode.js +26 -15
  63. package/dist/cjs/rendering/caching/testUtils.js +2 -2
  64. package/dist/cjs/rendering/renderers/AbstractRenderer.js +3 -1
  65. package/dist/cjs/rendering/renderers/CanvasRenderer.js +4 -3
  66. package/dist/cjs/rendering/renderers/DummyRenderer.js +1 -1
  67. package/dist/cjs/rendering/renderers/SVGRenderer.js +37 -19
  68. package/dist/cjs/rendering/renderers/TextOnlyRenderer.js +13 -15
  69. package/dist/cjs/shortcuts/KeyBinding.js +6 -12
  70. package/dist/cjs/shortcuts/KeyboardShortcutManager.js +2 -2
  71. package/dist/cjs/testing/createEditor.js +6 -1
  72. package/dist/cjs/testing/findNodeWithText.d.ts +4 -1
  73. package/dist/cjs/testing/findNodeWithText.js +12 -3
  74. package/dist/cjs/testing/getUniquePointerId.js +1 -1
  75. package/dist/cjs/testing/sendHtmlSwipe.js +7 -3
  76. package/dist/cjs/testing/sendPenEvent.js +1 -3
  77. package/dist/cjs/testing/sendTouchEvent.js +1 -4
  78. package/dist/cjs/testing/startPinchGesture.js +3 -1
  79. package/dist/cjs/toolbar/AbstractToolbar.js +7 -11
  80. package/dist/cjs/toolbar/EdgeToolbar.js +11 -15
  81. package/dist/cjs/toolbar/IconProvider.js +5 -3
  82. package/dist/cjs/toolbar/localization.js +3 -3
  83. package/dist/cjs/toolbar/utils/HelpDisplay.js +8 -6
  84. package/dist/cjs/toolbar/utils/makeDraggable.js +4 -7
  85. package/dist/cjs/toolbar/widgets/BaseToolWidget.js +3 -2
  86. package/dist/cjs/toolbar/widgets/BaseWidget.js +7 -7
  87. package/dist/cjs/toolbar/widgets/DocumentPropertiesWidget.js +2 -2
  88. package/dist/cjs/toolbar/widgets/EraserToolWidget.js +5 -3
  89. package/dist/cjs/toolbar/widgets/HandToolWidget.js +8 -6
  90. package/dist/cjs/toolbar/widgets/InsertImageWidget/InsertImageWidget.js +9 -10
  91. package/dist/cjs/toolbar/widgets/PenToolWidget.js +22 -13
  92. package/dist/cjs/toolbar/widgets/SelectionToolWidget.js +2 -2
  93. package/dist/cjs/toolbar/widgets/TextToolWidget.js +5 -5
  94. package/dist/cjs/toolbar/widgets/components/makeFileInput.js +7 -7
  95. package/dist/cjs/toolbar/widgets/components/makeGridSelector.js +5 -5
  96. package/dist/cjs/toolbar/widgets/components/makeSnappedList.js +9 -5
  97. package/dist/cjs/toolbar/widgets/keybindings.js +2 -2
  98. package/dist/cjs/toolbar/widgets/layout/DropdownLayoutManager.js +6 -6
  99. package/dist/cjs/tools/BaseTool.d.ts +61 -0
  100. package/dist/cjs/tools/BaseTool.js +181 -0
  101. package/dist/cjs/tools/Eraser.d.ts +60 -0
  102. package/dist/cjs/tools/Eraser.js +304 -0
  103. package/dist/cjs/tools/Eraser.test.d.ts +1 -0
  104. package/dist/cjs/tools/FindTool.d.ts +21 -0
  105. package/dist/cjs/tools/FindTool.js +137 -0
  106. package/dist/cjs/tools/FindTool.test.d.ts +1 -0
  107. package/dist/cjs/tools/InputFilter/ContextMenuRecognizer.d.ts +17 -0
  108. package/dist/cjs/tools/InputFilter/ContextMenuRecognizer.js +103 -0
  109. package/dist/cjs/tools/InputFilter/ContextMenuRecognizer.test.d.ts +1 -0
  110. package/dist/cjs/tools/InputFilter/FunctionMapper.d.ts +12 -0
  111. package/dist/cjs/tools/InputFilter/FunctionMapper.js +21 -0
  112. package/dist/cjs/tools/InputFilter/InputMapper.d.ts +23 -0
  113. package/dist/cjs/tools/InputFilter/InputMapper.js +38 -0
  114. package/dist/cjs/tools/InputFilter/InputPipeline.d.ts +15 -0
  115. package/dist/cjs/tools/InputFilter/InputPipeline.js +54 -0
  116. package/dist/cjs/tools/InputFilter/InputPipeline.test.d.ts +1 -0
  117. package/dist/cjs/tools/InputFilter/InputStabilizer.d.ts +29 -0
  118. package/dist/cjs/tools/InputFilter/InputStabilizer.js +188 -0
  119. package/dist/cjs/tools/InputFilter/StrokeKeyboardControl.d.ts +21 -0
  120. package/dist/cjs/tools/InputFilter/StrokeKeyboardControl.js +87 -0
  121. package/dist/cjs/tools/PanZoom.d.ts +125 -0
  122. package/dist/cjs/tools/PanZoom.js +522 -0
  123. package/dist/cjs/tools/PanZoom.test.d.ts +1 -0
  124. package/dist/cjs/tools/PasteHandler.d.ts +23 -0
  125. package/dist/cjs/tools/PasteHandler.js +115 -0
  126. package/dist/cjs/tools/Pen.d.ts +54 -0
  127. package/dist/cjs/tools/Pen.js +343 -0
  128. package/dist/cjs/tools/Pen.test.d.ts +1 -0
  129. package/dist/cjs/tools/PipetteTool.d.ts +28 -0
  130. package/dist/cjs/tools/PipetteTool.js +69 -0
  131. package/dist/cjs/tools/ScrollbarTool.d.ts +18 -0
  132. package/dist/cjs/tools/ScrollbarTool.js +86 -0
  133. package/dist/cjs/tools/SelectionTool/SelectAllShortcutHandler.d.ts +9 -0
  134. package/dist/cjs/tools/SelectionTool/SelectAllShortcutHandler.js +32 -0
  135. package/dist/cjs/tools/SelectionTool/Selection.d.ts +72 -0
  136. package/dist/cjs/tools/SelectionTool/Selection.js +638 -0
  137. package/dist/cjs/tools/SelectionTool/SelectionHandle.d.ts +62 -0
  138. package/dist/cjs/tools/SelectionTool/SelectionHandle.js +144 -0
  139. package/dist/cjs/tools/SelectionTool/SelectionMenuShortcut.d.ts +32 -0
  140. package/dist/cjs/tools/SelectionTool/SelectionMenuShortcut.js +88 -0
  141. package/dist/cjs/tools/SelectionTool/SelectionTool.d.ts +42 -0
  142. package/dist/cjs/tools/SelectionTool/SelectionTool.js +509 -0
  143. package/dist/cjs/tools/SelectionTool/SelectionTool.selecting.test.d.ts +1 -0
  144. package/dist/cjs/tools/SelectionTool/SelectionTool.test.d.ts +1 -0
  145. package/dist/cjs/tools/SelectionTool/ToPointerAutoscroller.d.ts +23 -0
  146. package/dist/cjs/tools/SelectionTool/ToPointerAutoscroller.js +83 -0
  147. package/dist/cjs/tools/SelectionTool/TransformMode.d.ts +42 -0
  148. package/dist/cjs/tools/SelectionTool/TransformMode.js +154 -0
  149. package/dist/cjs/tools/SelectionTool/keybindings.d.ts +15 -0
  150. package/dist/cjs/tools/SelectionTool/keybindings.js +38 -0
  151. package/dist/cjs/tools/SelectionTool/types.d.ts +35 -0
  152. package/dist/cjs/tools/SelectionTool/types.js +14 -0
  153. package/dist/cjs/tools/SelectionTool/util/makeClipboardErrorHandlers.d.ts +6 -0
  154. package/dist/cjs/tools/SelectionTool/util/makeClipboardErrorHandlers.js +71 -0
  155. package/dist/cjs/tools/SelectionTool/util/showSelectionContextMenu.d.ts +5 -0
  156. package/dist/cjs/tools/SelectionTool/util/showSelectionContextMenu.js +52 -0
  157. package/dist/cjs/tools/SoundUITool.d.ts +26 -0
  158. package/dist/cjs/tools/SoundUITool.js +173 -0
  159. package/dist/cjs/tools/TextTool.d.ts +36 -0
  160. package/dist/cjs/tools/TextTool.js +287 -0
  161. package/dist/cjs/tools/TextTool.test.d.ts +1 -0
  162. package/dist/cjs/tools/ToolController.d.ts +73 -0
  163. package/dist/cjs/tools/ToolController.js +310 -0
  164. package/dist/cjs/tools/ToolController.test.d.ts +1 -0
  165. package/dist/cjs/tools/ToolEnabledGroup.d.ts +6 -0
  166. package/dist/cjs/tools/ToolEnabledGroup.js +13 -0
  167. package/dist/cjs/tools/ToolSwitcherShortcut.d.ts +16 -0
  168. package/dist/cjs/tools/ToolSwitcherShortcut.js +40 -0
  169. package/dist/cjs/tools/ToolbarShortcutHandler.d.ts +12 -0
  170. package/dist/cjs/tools/ToolbarShortcutHandler.js +34 -0
  171. package/dist/cjs/tools/UndoRedoShortcut.d.ts +8 -0
  172. package/dist/cjs/tools/UndoRedoShortcut.js +27 -0
  173. package/dist/cjs/tools/UndoRedoShortcut.test.d.ts +1 -0
  174. package/dist/cjs/tools/keybindings.d.ts +16 -0
  175. package/dist/cjs/tools/keybindings.js +58 -0
  176. package/dist/cjs/tools/lib.d.ts +14 -0
  177. package/dist/cjs/tools/lib.js +36 -0
  178. package/dist/cjs/tools/localization.d.ts +45 -0
  179. package/dist/cjs/tools/localization.js +47 -0
  180. package/dist/cjs/tools/util/StationaryPenDetector.d.ts +25 -0
  181. package/dist/cjs/tools/util/StationaryPenDetector.js +107 -0
  182. package/dist/cjs/tools/util/createMenuOverlay.d.ts +10 -0
  183. package/dist/cjs/tools/util/createMenuOverlay.js +126 -0
  184. package/dist/cjs/tools/util/createMenuOverlay.test.d.ts +1 -0
  185. package/dist/cjs/util/ClipboardHandler.d.ts +1 -1
  186. package/dist/cjs/util/ClipboardHandler.js +19 -18
  187. package/dist/cjs/util/ReactiveValue.js +16 -12
  188. package/dist/cjs/util/adjustEditorThemeForContrast.js +6 -2
  189. package/dist/cjs/util/guessKeyCodeFromKey.js +1 -1
  190. package/dist/cjs/util/listenForKeyboardEventsFrom.js +8 -6
  191. package/dist/cjs/util/waitForAll.js +3 -3
  192. package/dist/cjs/util/waitForImageLoaded.js +3 -3
  193. package/dist/cjs/util/waitForTimeout.js +1 -1
  194. package/dist/cjs/version.js +1 -1
  195. package/dist/mjs/Editor.mjs +37 -23
  196. package/dist/mjs/EventDispatcher.mjs +1 -1
  197. package/dist/mjs/Pointer.mjs +3 -3
  198. package/dist/mjs/SVGLoader/SVGLoader.mjs +14 -7
  199. package/dist/mjs/UndoRedoHistory.mjs +1 -1
  200. package/dist/mjs/Viewport.mjs +4 -2
  201. package/dist/mjs/commands/Command.mjs +7 -5
  202. package/dist/mjs/commands/Duplicate.mjs +2 -2
  203. package/dist/mjs/commands/Erase.mjs +3 -4
  204. package/dist/mjs/commands/invertCommand.mjs +4 -4
  205. package/dist/mjs/commands/lib.d.ts +1 -1
  206. package/dist/mjs/commands/lib.mjs +1 -1
  207. package/dist/mjs/commands/uniteCommands.mjs +4 -4
  208. package/dist/mjs/components/AbstractComponent.d.ts +1 -1
  209. package/dist/mjs/components/AbstractComponent.mjs +3 -3
  210. package/dist/mjs/components/BackgroundComponent.mjs +10 -8
  211. package/dist/mjs/components/ImageComponent.mjs +12 -5
  212. package/dist/mjs/components/RestylableComponent.mjs +2 -2
  213. package/dist/mjs/components/SVGGlobalAttributesObject.mjs +1 -2
  214. package/dist/mjs/components/Stroke.mjs +40 -27
  215. package/dist/mjs/components/TextComponent.mjs +15 -12
  216. package/dist/mjs/components/UnknownSVGObject.mjs +2 -3
  217. package/dist/mjs/components/builders/ArrowBuilder.d.ts +6 -0
  218. package/dist/mjs/components/builders/ArrowBuilder.mjs +9 -3
  219. package/dist/mjs/components/builders/CircleBuilder.d.ts +6 -0
  220. package/dist/mjs/components/builders/CircleBuilder.mjs +11 -4
  221. package/dist/mjs/components/builders/FreehandLineBuilder.d.ts +6 -0
  222. package/dist/mjs/components/builders/FreehandLineBuilder.mjs +10 -4
  223. package/dist/mjs/components/builders/LineBuilder.d.ts +6 -0
  224. package/dist/mjs/components/builders/LineBuilder.mjs +8 -4
  225. package/dist/mjs/components/builders/PolylineBuilder.d.ts +4 -1
  226. package/dist/mjs/components/builders/PolylineBuilder.mjs +10 -6
  227. package/dist/mjs/components/builders/PressureSensitiveFreehandLineBuilder.mjs +17 -11
  228. package/dist/mjs/components/builders/RectangleBuilder.d.ts +12 -0
  229. package/dist/mjs/components/builders/RectangleBuilder.mjs +17 -3
  230. package/dist/mjs/components/builders/autocorrect/makeShapeFitAutocorrect.mjs +5 -8
  231. package/dist/mjs/components/builders/autocorrect/makeSnapToGridAutocorrect.mjs +1 -1
  232. package/dist/mjs/components/builders/lib.d.ts +7 -0
  233. package/dist/mjs/components/builders/lib.mjs +7 -0
  234. package/dist/mjs/components/lib.d.ts +1 -4
  235. package/dist/mjs/components/lib.mjs +2 -5
  236. package/dist/mjs/components/util/StrokeSmoother.mjs +5 -6
  237. package/dist/mjs/dialogs/makeAboutDialog.mjs +1 -1
  238. package/dist/mjs/dialogs/makeMessageDialog.mjs +2 -2
  239. package/dist/mjs/image/EditorImage.mjs +13 -15
  240. package/dist/mjs/image/export/editorImageToSVG.mjs +1 -1
  241. package/dist/mjs/inputEvents.mjs +3 -3
  242. package/dist/mjs/lib.d.ts +2 -2
  243. package/dist/mjs/lib.mjs +2 -2
  244. package/dist/mjs/localization.mjs +2 -2
  245. package/dist/mjs/localizations/de.mjs +2 -2
  246. package/dist/mjs/localizations/es.mjs +7 -3
  247. package/dist/mjs/rendering/Display.mjs +7 -3
  248. package/dist/mjs/rendering/RenderablePathSpec.mjs +26 -11
  249. package/dist/mjs/rendering/RenderingStyle.mjs +22 -15
  250. package/dist/mjs/rendering/TextRenderingStyle.mjs +1 -1
  251. package/dist/mjs/rendering/caching/CacheRecord.mjs +1 -1
  252. package/dist/mjs/rendering/caching/CacheRecordManager.mjs +1 -1
  253. package/dist/mjs/rendering/caching/RenderingCache.mjs +1 -1
  254. package/dist/mjs/rendering/caching/RenderingCacheNode.mjs +26 -15
  255. package/dist/mjs/rendering/caching/testUtils.mjs +2 -2
  256. package/dist/mjs/rendering/renderers/AbstractRenderer.mjs +3 -1
  257. package/dist/mjs/rendering/renderers/CanvasRenderer.mjs +4 -3
  258. package/dist/mjs/rendering/renderers/DummyRenderer.mjs +1 -1
  259. package/dist/mjs/rendering/renderers/SVGRenderer.mjs +38 -20
  260. package/dist/mjs/rendering/renderers/TextOnlyRenderer.mjs +13 -15
  261. package/dist/mjs/shortcuts/KeyBinding.mjs +6 -12
  262. package/dist/mjs/shortcuts/KeyboardShortcutManager.mjs +2 -2
  263. package/dist/mjs/testing/createEditor.mjs +6 -1
  264. package/dist/mjs/testing/findNodeWithText.d.ts +4 -1
  265. package/dist/mjs/testing/findNodeWithText.mjs +12 -3
  266. package/dist/mjs/testing/getUniquePointerId.mjs +1 -1
  267. package/dist/mjs/testing/sendHtmlSwipe.mjs +7 -3
  268. package/dist/mjs/testing/sendPenEvent.mjs +1 -3
  269. package/dist/mjs/testing/sendTouchEvent.mjs +1 -4
  270. package/dist/mjs/testing/startPinchGesture.mjs +3 -1
  271. package/dist/mjs/toolbar/AbstractToolbar.mjs +7 -11
  272. package/dist/mjs/toolbar/EdgeToolbar.mjs +11 -15
  273. package/dist/mjs/toolbar/IconProvider.mjs +5 -3
  274. package/dist/mjs/toolbar/localization.mjs +3 -3
  275. package/dist/mjs/toolbar/utils/HelpDisplay.mjs +8 -6
  276. package/dist/mjs/toolbar/utils/makeDraggable.mjs +4 -7
  277. package/dist/mjs/toolbar/widgets/BaseToolWidget.mjs +3 -2
  278. package/dist/mjs/toolbar/widgets/BaseWidget.mjs +8 -8
  279. package/dist/mjs/toolbar/widgets/DocumentPropertiesWidget.mjs +2 -2
  280. package/dist/mjs/toolbar/widgets/EraserToolWidget.mjs +5 -3
  281. package/dist/mjs/toolbar/widgets/HandToolWidget.mjs +8 -6
  282. package/dist/mjs/toolbar/widgets/InsertImageWidget/InsertImageWidget.mjs +9 -10
  283. package/dist/mjs/toolbar/widgets/PenToolWidget.mjs +23 -14
  284. package/dist/mjs/toolbar/widgets/SelectionToolWidget.mjs +2 -2
  285. package/dist/mjs/toolbar/widgets/TextToolWidget.mjs +5 -5
  286. package/dist/mjs/toolbar/widgets/components/makeFileInput.mjs +7 -7
  287. package/dist/mjs/toolbar/widgets/components/makeGridSelector.mjs +5 -5
  288. package/dist/mjs/toolbar/widgets/components/makeSnappedList.mjs +9 -5
  289. package/dist/mjs/toolbar/widgets/keybindings.mjs +2 -2
  290. package/dist/mjs/toolbar/widgets/layout/DropdownLayoutManager.mjs +6 -6
  291. package/dist/mjs/tools/BaseTool.d.ts +61 -0
  292. package/dist/mjs/tools/BaseTool.mjs +179 -0
  293. package/dist/mjs/tools/Eraser.d.ts +60 -0
  294. package/dist/mjs/tools/Eraser.mjs +297 -0
  295. package/dist/mjs/tools/Eraser.test.d.ts +1 -0
  296. package/dist/mjs/tools/FindTool.d.ts +21 -0
  297. package/dist/mjs/tools/FindTool.mjs +131 -0
  298. package/dist/mjs/tools/FindTool.test.d.ts +1 -0
  299. package/dist/mjs/tools/InputFilter/ContextMenuRecognizer.d.ts +17 -0
  300. package/dist/mjs/tools/InputFilter/ContextMenuRecognizer.mjs +74 -0
  301. package/dist/mjs/tools/InputFilter/ContextMenuRecognizer.test.d.ts +1 -0
  302. package/dist/mjs/tools/InputFilter/FunctionMapper.d.ts +12 -0
  303. package/dist/mjs/tools/InputFilter/FunctionMapper.mjs +15 -0
  304. package/dist/mjs/tools/InputFilter/InputMapper.d.ts +23 -0
  305. package/dist/mjs/tools/InputFilter/InputMapper.mjs +36 -0
  306. package/dist/mjs/tools/InputFilter/InputPipeline.d.ts +15 -0
  307. package/dist/mjs/tools/InputFilter/InputPipeline.mjs +49 -0
  308. package/dist/mjs/tools/InputFilter/InputPipeline.test.d.ts +1 -0
  309. package/dist/mjs/tools/InputFilter/InputStabilizer.d.ts +29 -0
  310. package/dist/mjs/tools/InputFilter/InputStabilizer.mjs +182 -0
  311. package/dist/mjs/tools/InputFilter/StrokeKeyboardControl.d.ts +21 -0
  312. package/dist/mjs/tools/InputFilter/StrokeKeyboardControl.mjs +81 -0
  313. package/dist/mjs/tools/PanZoom.d.ts +125 -0
  314. package/dist/mjs/tools/PanZoom.mjs +515 -0
  315. package/dist/mjs/tools/PanZoom.test.d.ts +1 -0
  316. package/dist/mjs/tools/PasteHandler.d.ts +23 -0
  317. package/dist/mjs/tools/PasteHandler.mjs +109 -0
  318. package/dist/mjs/tools/Pen.d.ts +54 -0
  319. package/dist/mjs/tools/Pen.mjs +314 -0
  320. package/dist/mjs/tools/Pen.test.d.ts +1 -0
  321. package/dist/mjs/tools/PipetteTool.d.ts +28 -0
  322. package/dist/mjs/tools/PipetteTool.mjs +63 -0
  323. package/dist/mjs/tools/ScrollbarTool.d.ts +18 -0
  324. package/dist/mjs/tools/ScrollbarTool.mjs +80 -0
  325. package/dist/mjs/tools/SelectionTool/SelectAllShortcutHandler.d.ts +9 -0
  326. package/dist/mjs/tools/SelectionTool/SelectAllShortcutHandler.mjs +26 -0
  327. package/dist/mjs/tools/SelectionTool/Selection.d.ts +72 -0
  328. package/dist/mjs/tools/SelectionTool/Selection.mjs +610 -0
  329. package/dist/mjs/tools/SelectionTool/SelectionHandle.d.ts +62 -0
  330. package/dist/mjs/tools/SelectionTool/SelectionHandle.mjs +140 -0
  331. package/dist/mjs/tools/SelectionTool/SelectionMenuShortcut.d.ts +32 -0
  332. package/dist/mjs/tools/SelectionTool/SelectionMenuShortcut.mjs +85 -0
  333. package/dist/mjs/tools/SelectionTool/SelectionTool.d.ts +42 -0
  334. package/dist/mjs/tools/SelectionTool/SelectionTool.mjs +502 -0
  335. package/dist/mjs/tools/SelectionTool/SelectionTool.selecting.test.d.ts +1 -0
  336. package/dist/mjs/tools/SelectionTool/SelectionTool.test.d.ts +1 -0
  337. package/dist/mjs/tools/SelectionTool/ToPointerAutoscroller.d.ts +23 -0
  338. package/dist/mjs/tools/SelectionTool/ToPointerAutoscroller.mjs +77 -0
  339. package/dist/mjs/tools/SelectionTool/TransformMode.d.ts +42 -0
  340. package/dist/mjs/tools/SelectionTool/TransformMode.mjs +145 -0
  341. package/dist/mjs/tools/SelectionTool/keybindings.d.ts +15 -0
  342. package/dist/mjs/tools/SelectionTool/keybindings.mjs +32 -0
  343. package/dist/mjs/tools/SelectionTool/types.d.ts +35 -0
  344. package/dist/mjs/tools/SelectionTool/types.mjs +11 -0
  345. package/dist/mjs/tools/SelectionTool/util/makeClipboardErrorHandlers.d.ts +6 -0
  346. package/dist/mjs/tools/SelectionTool/util/makeClipboardErrorHandlers.mjs +66 -0
  347. package/dist/mjs/tools/SelectionTool/util/showSelectionContextMenu.d.ts +5 -0
  348. package/dist/mjs/tools/SelectionTool/util/showSelectionContextMenu.mjs +47 -0
  349. package/dist/mjs/tools/SoundUITool.d.ts +26 -0
  350. package/dist/mjs/tools/SoundUITool.mjs +167 -0
  351. package/dist/mjs/tools/TextTool.d.ts +36 -0
  352. package/dist/mjs/tools/TextTool.mjs +281 -0
  353. package/dist/mjs/tools/TextTool.test.d.ts +1 -0
  354. package/dist/mjs/tools/ToolController.d.ts +73 -0
  355. package/dist/mjs/tools/ToolController.mjs +281 -0
  356. package/dist/mjs/tools/ToolController.test.d.ts +1 -0
  357. package/dist/mjs/tools/ToolEnabledGroup.d.ts +6 -0
  358. package/dist/mjs/tools/ToolEnabledGroup.mjs +10 -0
  359. package/dist/mjs/tools/ToolSwitcherShortcut.d.ts +16 -0
  360. package/dist/mjs/tools/ToolSwitcherShortcut.mjs +34 -0
  361. package/dist/mjs/tools/ToolbarShortcutHandler.d.ts +12 -0
  362. package/dist/mjs/tools/ToolbarShortcutHandler.mjs +28 -0
  363. package/dist/mjs/tools/UndoRedoShortcut.d.ts +8 -0
  364. package/dist/mjs/tools/UndoRedoShortcut.mjs +21 -0
  365. package/dist/mjs/tools/UndoRedoShortcut.test.d.ts +1 -0
  366. package/dist/mjs/tools/keybindings.d.ts +16 -0
  367. package/dist/mjs/tools/keybindings.mjs +38 -0
  368. package/dist/mjs/tools/lib.d.ts +14 -0
  369. package/dist/mjs/tools/lib.mjs +14 -0
  370. package/dist/mjs/tools/localization.d.ts +45 -0
  371. package/dist/mjs/tools/localization.mjs +44 -0
  372. package/dist/mjs/tools/util/StationaryPenDetector.d.ts +25 -0
  373. package/dist/mjs/tools/util/StationaryPenDetector.mjs +103 -0
  374. package/dist/mjs/tools/util/createMenuOverlay.d.ts +10 -0
  375. package/dist/mjs/tools/util/createMenuOverlay.mjs +121 -0
  376. package/dist/mjs/tools/util/createMenuOverlay.test.d.ts +1 -0
  377. package/dist/mjs/util/ClipboardHandler.d.ts +1 -1
  378. package/dist/mjs/util/ClipboardHandler.mjs +19 -18
  379. package/dist/mjs/util/ReactiveValue.mjs +16 -12
  380. package/dist/mjs/util/adjustEditorThemeForContrast.mjs +6 -2
  381. package/dist/mjs/util/guessKeyCodeFromKey.mjs +1 -1
  382. package/dist/mjs/util/listenForKeyboardEventsFrom.mjs +8 -6
  383. package/dist/mjs/util/waitForAll.mjs +3 -3
  384. package/dist/mjs/util/waitForImageLoaded.mjs +3 -3
  385. package/dist/mjs/util/waitForTimeout.mjs +1 -1
  386. package/dist/mjs/version.mjs +1 -1
  387. package/package.json +88 -88
  388. package/src/Coloris.css +6 -6
  389. package/src/Editor.scss +7 -5
  390. package/src/dialogs/dialogs.scss +3 -4
  391. package/src/dialogs/makeAboutDialog.scss +2 -2
  392. package/src/dialogs/makeMessageDialog.scss +11 -7
  393. package/src/styles.js +1 -1
  394. package/src/toolbar/AbstractToolbar.scss +20 -12
  395. package/src/toolbar/DropdownToolbar.scss +5 -4
  396. package/src/toolbar/EdgeToolbar.scss +65 -31
  397. package/src/toolbar/toolbar.scss +5 -5
  398. package/src/toolbar/utils/HelpDisplay.scss +48 -25
  399. package/src/toolbar/utils/labelVisibleOnHover.scss +39 -16
  400. package/src/toolbar/widgets/DocumentPropertiesWidget.scss +0 -1
  401. package/src/toolbar/widgets/HandToolWidget.scss +0 -1
  402. package/src/toolbar/widgets/InsertImageWidget/InsertImageWidget.scss +2 -3
  403. package/src/toolbar/widgets/OverflowWidget.css +1 -2
  404. package/src/toolbar/widgets/PenToolWidget.scss +0 -2
  405. package/src/toolbar/widgets/SelectionToolWidget.scss +1 -2
  406. package/src/toolbar/widgets/components/components.scss +6 -6
  407. package/src/toolbar/widgets/components/makeColorInput.scss +0 -2
  408. package/src/toolbar/widgets/components/makeFileInput.scss +5 -7
  409. package/src/toolbar/widgets/components/makeGridSelector.scss +6 -9
  410. package/src/toolbar/widgets/components/makeSnappedList.scss +3 -4
  411. package/src/toolbar/widgets/components/makeThicknessSlider.scss +1 -2
  412. package/src/toolbar/widgets/widgets.scss +7 -7
  413. package/src/tools/FindTool.css +6 -0
  414. package/src/tools/ScrollbarTool.scss +61 -0
  415. package/src/tools/SelectionTool/SelectionTool.scss +173 -0
  416. package/src/tools/SelectionTool/util/makeClipboardErrorHandlers.scss +14 -0
  417. package/src/tools/SoundUITool.scss +22 -0
  418. package/src/tools/tools.scss +5 -0
  419. package/src/tools/util/createMenuOverlay.scss +73 -0
  420. package/tsconfig.json +1 -3
  421. package/typedoc.json +1 -1
@@ -0,0 +1,281 @@
1
+ import TextComponent from '../components/TextComponent.mjs';
2
+ import EditorImage from '../image/EditorImage.mjs';
3
+ import { Rect2, Mat33, Vec2, Color4 } from '@js-draw/math';
4
+ import { PointerDevice } from '../Pointer.mjs';
5
+ import { EditorEventType } from '../types.mjs';
6
+ import BaseTool from './BaseTool.mjs';
7
+ import Erase from '../commands/Erase.mjs';
8
+ import uniteCommands from '../commands/uniteCommands.mjs';
9
+ import { ReactiveValue } from '../util/ReactiveValue.mjs';
10
+ const overlayCSSClass = 'textEditorOverlay';
11
+ export default class TextTool extends BaseTool {
12
+ constructor(editor, description, localizationTable) {
13
+ super(editor.notifier, description);
14
+ this.editor = editor;
15
+ this.localizationTable = localizationTable;
16
+ this.textInputElem = null;
17
+ this.textTargetPosition = null;
18
+ this.textMeasuringCtx = null;
19
+ this.textScale = Vec2.of(1, 1);
20
+ this.removeExistingCommand = null;
21
+ const editorFonts = editor.getCurrentSettings().text?.fonts ?? [];
22
+ this.textStyleValue = ReactiveValue.fromInitialValue({
23
+ size: 32,
24
+ fontFamily: editorFonts.length > 0 ? editorFonts[0] : 'sans-serif',
25
+ renderingStyle: {
26
+ fill: Color4.purple,
27
+ },
28
+ });
29
+ this.textStyleValue.onUpdateAndNow(() => {
30
+ this.textStyle = this.textStyleValue.get();
31
+ this.updateTextInput();
32
+ this.editor.notifier.dispatch(EditorEventType.ToolUpdated, {
33
+ kind: EditorEventType.ToolUpdated,
34
+ tool: this,
35
+ });
36
+ });
37
+ this.textEditOverlay = document.createElement('div');
38
+ this.textEditOverlay.classList.add(overlayCSSClass);
39
+ this.editor.addStyleSheet(`
40
+ .${overlayCSSClass} {
41
+ height: 0;
42
+ overflow: visible;
43
+
44
+ /* Allows absolutely-positioned textareas to scroll with
45
+ the containing overlay. */
46
+ position: relative;
47
+ }
48
+
49
+ .${overlayCSSClass} textarea {
50
+ background-color: rgba(0, 0, 0, 0);
51
+
52
+ white-space: pre;
53
+ overflow: hidden;
54
+
55
+ padding: 0;
56
+ margin: 0;
57
+ border: none;
58
+ padding: 0;
59
+
60
+ min-width: 100px;
61
+ min-height: 1.1em;
62
+ }
63
+ `);
64
+ this.editor.createHTMLOverlay(this.textEditOverlay);
65
+ this.editor.notifier.on(EditorEventType.ViewportChanged, () => this.updateTextInput());
66
+ }
67
+ initTextMeasuringCanvas() {
68
+ this.textMeasuringCtx ??= document.createElement('canvas').getContext('2d');
69
+ }
70
+ getTextAscent(text, style) {
71
+ this.initTextMeasuringCanvas();
72
+ if (this.textMeasuringCtx) {
73
+ this.textMeasuringCtx.textBaseline = 'alphabetic';
74
+ TextComponent.applyTextStyles(this.textMeasuringCtx, style);
75
+ const measurement = this.textMeasuringCtx.measureText(text);
76
+ return measurement.fontBoundingBoxAscent ?? measurement.actualBoundingBoxAscent;
77
+ }
78
+ // Estimate
79
+ return (style.size * 2) / 3;
80
+ }
81
+ // Take input from this' textInputElem and add it to the EditorImage.
82
+ // If [removeInput], the HTML input element is removed. Otherwise, its value
83
+ // is cleared.
84
+ flushInput(removeInput = true) {
85
+ if (this.textInputElem && this.textTargetPosition) {
86
+ const content = this.textInputElem.value.trimEnd();
87
+ this.textInputElem.value = '';
88
+ if (removeInput) {
89
+ // In some browsers, .remove() triggers a .blur event (synchronously).
90
+ // Clear this.textInputElem before removal
91
+ const input = this.textInputElem;
92
+ this.textInputElem = null;
93
+ input.remove();
94
+ }
95
+ if (content === '') {
96
+ return;
97
+ }
98
+ const textTransform = Mat33.translation(this.textTargetPosition)
99
+ .rightMul(this.getTextScaleMatrix())
100
+ .rightMul(Mat33.scaling2D(this.editor.viewport.getSizeOfPixelOnCanvas()))
101
+ .rightMul(Mat33.zRotation(this.textRotation));
102
+ const textComponent = TextComponent.fromLines(content.split('\n'), textTransform, this.textStyle);
103
+ const action = EditorImage.addElement(textComponent);
104
+ if (this.removeExistingCommand) {
105
+ // Unapply so that `removeExistingCommand` can be added to the undo stack.
106
+ this.removeExistingCommand.unapply(this.editor);
107
+ this.editor.dispatch(uniteCommands([this.removeExistingCommand, action]));
108
+ this.removeExistingCommand = null;
109
+ }
110
+ else {
111
+ this.editor.dispatch(action);
112
+ }
113
+ }
114
+ }
115
+ getTextScaleMatrix() {
116
+ return Mat33.scaling2D(this.textScale.times(1 / this.editor.viewport.getSizeOfPixelOnCanvas()));
117
+ }
118
+ updateTextInput() {
119
+ if (!this.textInputElem || !this.textTargetPosition) {
120
+ this.textInputElem?.remove();
121
+ return;
122
+ }
123
+ const viewport = this.editor.viewport;
124
+ const textScreenPos = viewport.canvasToScreen(this.textTargetPosition);
125
+ this.textInputElem.placeholder = this.localizationTable.enterTextToInsert;
126
+ this.textInputElem.style.fontFamily = this.textStyle.fontFamily;
127
+ this.textInputElem.style.fontStyle = this.textStyle.fontStyle ?? '';
128
+ this.textInputElem.style.fontVariant = this.textStyle.fontVariant ?? '';
129
+ this.textInputElem.style.fontWeight = this.textStyle.fontWeight ?? '';
130
+ this.textInputElem.style.fontSize = `${this.textStyle.size}px`;
131
+ this.textInputElem.style.color = this.textStyle.renderingStyle.fill.toHexString();
132
+ this.textInputElem.style.position = 'absolute';
133
+ this.textInputElem.style.left = `${textScreenPos.x}px`;
134
+ this.textInputElem.style.top = `${textScreenPos.y}px`;
135
+ this.textInputElem.style.margin = '0';
136
+ this.textInputElem.style.width = `${this.textInputElem.scrollWidth}px`;
137
+ this.textInputElem.style.height = `${this.textInputElem.scrollHeight}px`;
138
+ // Get the ascent based on the font, using a string of characters
139
+ // that is tall in most fonts.
140
+ const tallText = 'Testing!';
141
+ const ascent = this.getTextAscent(tallText, this.textStyle);
142
+ const vertAdjust = ascent;
143
+ const rotation = this.textRotation + viewport.getRotationAngle();
144
+ const scale = this.getTextScaleMatrix();
145
+ this.textInputElem.style.transform = `${scale.toCSSMatrix()} rotate(${(rotation * 180) / Math.PI}deg) translate(0, ${-vertAdjust}px)`;
146
+ this.textInputElem.style.transformOrigin = 'top left';
147
+ // Match the line height of default rendered text.
148
+ const lineHeight = Math.floor(this.textStyle.size);
149
+ this.textInputElem.style.lineHeight = `${lineHeight}px`;
150
+ }
151
+ startTextInput(textCanvasPos, initialText) {
152
+ this.flushInput();
153
+ this.textInputElem = document.createElement('textarea');
154
+ this.textInputElem.value = initialText;
155
+ this.textInputElem.style.display = 'inline-block';
156
+ this.textTargetPosition = this.editor.viewport.roundPoint(textCanvasPos);
157
+ this.textRotation = -this.editor.viewport.getRotationAngle();
158
+ this.textScale = Vec2.of(1, 1).times(this.editor.viewport.getSizeOfPixelOnCanvas());
159
+ this.updateTextInput();
160
+ // Update the input size/position/etc. after the placeHolder has had time to appear.
161
+ setTimeout(() => this.updateTextInput(), 0);
162
+ this.textInputElem.oninput = () => {
163
+ if (this.textInputElem) {
164
+ this.textInputElem.style.width = `${this.textInputElem.scrollWidth}px`;
165
+ this.textInputElem.style.height = `${this.textInputElem.scrollHeight}px`;
166
+ }
167
+ };
168
+ this.textInputElem.onblur = () => {
169
+ // Delay removing the input -- flushInput may be called within a blur()
170
+ // event handler
171
+ const removeInput = false;
172
+ const input = this.textInputElem;
173
+ this.flushInput(removeInput);
174
+ this.textInputElem = null;
175
+ setTimeout(() => {
176
+ input?.remove();
177
+ }, 0);
178
+ };
179
+ this.textInputElem.onkeyup = (evt) => {
180
+ if (evt.key === 'Enter' && !evt.shiftKey) {
181
+ this.flushInput();
182
+ this.editor.focus();
183
+ }
184
+ else if (evt.key === 'Escape') {
185
+ // Cancel input.
186
+ this.textInputElem?.remove();
187
+ this.textInputElem = null;
188
+ this.editor.focus();
189
+ this.removeExistingCommand?.unapply(this.editor);
190
+ this.removeExistingCommand = null;
191
+ }
192
+ };
193
+ this.textEditOverlay.replaceChildren(this.textInputElem);
194
+ setTimeout(() => this.textInputElem?.focus(), 0);
195
+ }
196
+ setEnabled(enabled) {
197
+ super.setEnabled(enabled);
198
+ if (!this.isEnabled()) {
199
+ this.flushInput();
200
+ }
201
+ this.textEditOverlay.style.display = enabled ? 'block' : 'none';
202
+ }
203
+ onPointerDown({ current, allPointers }) {
204
+ if (current.device === PointerDevice.Eraser) {
205
+ return false;
206
+ }
207
+ if (allPointers.length === 1) {
208
+ // Are we clicking on a text node?
209
+ const canvasPos = current.canvasPos;
210
+ const halfTestRegionSize = Vec2.of(2.5, 2.5).times(this.editor.viewport.getSizeOfPixelOnCanvas());
211
+ const testRegion = Rect2.fromCorners(canvasPos.minus(halfTestRegionSize), canvasPos.plus(halfTestRegionSize));
212
+ const targetNodes = this.editor.image.getElementsIntersectingRegion(testRegion);
213
+ let targetTextNodes = targetNodes.filter((node) => node instanceof TextComponent);
214
+ // Don't try to edit text nodes that contain the viewport (this allows us
215
+ // to zoom in on text nodes and add text on top of them.)
216
+ const visibleRect = this.editor.viewport.visibleRect;
217
+ targetTextNodes = targetTextNodes.filter((node) => !node.getBBox().containsRect(visibleRect));
218
+ // End any TextNodes we're currently editing.
219
+ this.flushInput();
220
+ if (targetTextNodes.length > 0) {
221
+ const targetNode = targetTextNodes[targetTextNodes.length - 1];
222
+ this.setTextStyle(targetNode.getTextStyle());
223
+ // Create and temporarily apply removeExistingCommand.
224
+ this.removeExistingCommand = new Erase([targetNode]);
225
+ this.removeExistingCommand.apply(this.editor);
226
+ this.startTextInput(targetNode.getBaselinePos(), targetNode.getText());
227
+ const transform = targetNode.getTransform();
228
+ this.textRotation = transform.transformVec3(Vec2.unitX).angle();
229
+ const scaleFactor = transform.transformVec3(Vec2.unitX).magnitude();
230
+ this.textScale = Vec2.of(1, 1).times(scaleFactor);
231
+ this.updateTextInput();
232
+ }
233
+ else {
234
+ this.removeExistingCommand = null;
235
+ this.startTextInput(current.canvasPos, '');
236
+ }
237
+ return true;
238
+ }
239
+ return false;
240
+ }
241
+ onGestureCancel() {
242
+ this.flushInput();
243
+ this.editor.focus();
244
+ }
245
+ setFontFamily(fontFamily) {
246
+ if (fontFamily !== this.textStyle.fontFamily) {
247
+ this.textStyleValue.set({
248
+ ...this.textStyle,
249
+ fontFamily: fontFamily,
250
+ });
251
+ }
252
+ }
253
+ setColor(color) {
254
+ if (!color.eq(this.textStyle.renderingStyle.fill)) {
255
+ this.textStyleValue.set({
256
+ ...this.textStyle,
257
+ renderingStyle: {
258
+ ...this.textStyle.renderingStyle,
259
+ fill: color,
260
+ },
261
+ });
262
+ }
263
+ }
264
+ setFontSize(size) {
265
+ if (size !== this.textStyle.size) {
266
+ this.textStyleValue.set({
267
+ ...this.textStyle,
268
+ size,
269
+ });
270
+ }
271
+ }
272
+ getTextStyle() {
273
+ return this.textStyle;
274
+ }
275
+ getStyleValue() {
276
+ return this.textStyleValue;
277
+ }
278
+ setTextStyle(style) {
279
+ this.textStyleValue.set(style);
280
+ }
281
+ }
@@ -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,281 @@
1
+ import { EditorEventType } from '../types.mjs';
2
+ import { Color4 } from '@js-draw/math';
3
+ import PanZoom, { PanZoomMode } from './PanZoom.mjs';
4
+ import Pen from './Pen.mjs';
5
+ import ToolEnabledGroup from './ToolEnabledGroup.mjs';
6
+ import Eraser from './Eraser.mjs';
7
+ import SelectionTool from './SelectionTool/SelectionTool.mjs';
8
+ import UndoRedoShortcut from './UndoRedoShortcut.mjs';
9
+ import TextTool from './TextTool.mjs';
10
+ import PipetteTool from './PipetteTool.mjs';
11
+ import ToolSwitcherShortcut from './ToolSwitcherShortcut.mjs';
12
+ import PasteHandler from './PasteHandler.mjs';
13
+ import ToolbarShortcutHandler from './ToolbarShortcutHandler.mjs';
14
+ import { makePressureSensitiveFreehandLineBuilder } from '../components/builders/PressureSensitiveFreehandLineBuilder.mjs';
15
+ import FindTool from './FindTool.mjs';
16
+ import SelectAllShortcutHandler from './SelectionTool/SelectAllShortcutHandler.mjs';
17
+ import SoundUITool from './SoundUITool.mjs';
18
+ import { InputEvtType } from '../inputEvents.mjs';
19
+ import InputPipeline from './InputFilter/InputPipeline.mjs';
20
+ import InputStabilizer from './InputFilter/InputStabilizer.mjs';
21
+ import ScrollbarTool from './ScrollbarTool.mjs';
22
+ export default class ToolController {
23
+ /** @internal */
24
+ constructor(editor, localization) {
25
+ this.activeTool = null;
26
+ this.isEditorReadOnly = editor.isReadOnlyReactiveValue();
27
+ this.inputPipeline = new InputPipeline();
28
+ this.inputPipeline.setEmitListener((event) => this.onEventInternal(event));
29
+ const primaryToolGroup = new ToolEnabledGroup();
30
+ this.primaryToolGroup = primaryToolGroup;
31
+ const panZoomTool = new PanZoom(editor, PanZoomMode.TwoFingerTouchGestures | PanZoomMode.RightClickDrags, localization.touchPanTool);
32
+ const keyboardPanZoomTool = new PanZoom(editor, PanZoomMode.Keyboard, localization.keyboardPanZoom);
33
+ const primaryPenTool = new Pen(editor, localization.penTool(1), {
34
+ color: Color4.purple,
35
+ thickness: 8,
36
+ });
37
+ const secondaryPenTool = new Pen(editor, localization.penTool(2), {
38
+ color: Color4.clay,
39
+ thickness: 4,
40
+ });
41
+ // Stabilize the secondary pen tool.
42
+ secondaryPenTool.setInputMapper(new InputStabilizer(editor.viewport));
43
+ const eraser = new Eraser(editor, localization.eraserTool);
44
+ const primaryTools = [
45
+ // Three pens
46
+ primaryPenTool,
47
+ secondaryPenTool,
48
+ // Highlighter-like pen with width=40
49
+ new Pen(editor, localization.penTool(3), {
50
+ color: Color4.ofRGBA(1, 1, 0, 0.5),
51
+ thickness: 40,
52
+ factory: makePressureSensitiveFreehandLineBuilder,
53
+ }),
54
+ eraser,
55
+ new SelectionTool(editor, localization.selectionTool),
56
+ new TextTool(editor, localization.textTool, localization),
57
+ new PanZoom(editor, PanZoomMode.SinglePointerGestures, localization.anyDevicePanning),
58
+ ];
59
+ // Accessibility tools
60
+ const soundExplorer = new SoundUITool(editor, localization.soundExplorer);
61
+ soundExplorer.setEnabled(false);
62
+ this.tools = [
63
+ new ScrollbarTool(editor),
64
+ new PipetteTool(editor, localization.pipetteTool),
65
+ soundExplorer,
66
+ panZoomTool,
67
+ ...primaryTools,
68
+ keyboardPanZoomTool,
69
+ new UndoRedoShortcut(editor),
70
+ new ToolbarShortcutHandler(editor),
71
+ new ToolSwitcherShortcut(editor),
72
+ eraser.makeEraserSwitcherTool(),
73
+ new FindTool(editor),
74
+ new PasteHandler(editor),
75
+ new SelectAllShortcutHandler(editor),
76
+ ];
77
+ primaryTools.forEach((tool) => tool.setToolGroup(primaryToolGroup));
78
+ panZoomTool.setEnabled(true);
79
+ primaryPenTool.setEnabled(true);
80
+ editor.notifier.on(EditorEventType.ToolEnabled, (event) => {
81
+ if (event.kind === EditorEventType.ToolEnabled) {
82
+ editor.announceForAccessibility(localization.toolEnabledAnnouncement(event.tool.description));
83
+ }
84
+ });
85
+ editor.notifier.on(EditorEventType.ToolDisabled, (event) => {
86
+ if (event.kind === EditorEventType.ToolDisabled) {
87
+ editor.announceForAccessibility(localization.toolDisabledAnnouncement(event.tool.description));
88
+ }
89
+ });
90
+ this.activeTool = null;
91
+ }
92
+ /**
93
+ * Replaces the current set of tools with `tools`. This should only be done before
94
+ * the creation of the app's toolbar (if using `AbstractToolbar`).
95
+ *
96
+ * If no `primaryToolGroup` is given, an empty one will be created.
97
+ */
98
+ setTools(tools, primaryToolGroup) {
99
+ this.tools = tools;
100
+ this.primaryToolGroup = primaryToolGroup ?? new ToolEnabledGroup();
101
+ }
102
+ /**
103
+ * Add a tool that acts like one of the primary tools (only one primary tool can be enabled at a time).
104
+ *
105
+ * If the tool is already added to this, the tool is converted to a primary tool.
106
+ *
107
+ * This should be called before creating the app's toolbar.
108
+ */
109
+ addPrimaryTool(tool) {
110
+ tool.setToolGroup(this.primaryToolGroup);
111
+ if (tool.isEnabled()) {
112
+ this.primaryToolGroup.notifyEnabled(tool);
113
+ }
114
+ if (!this.tools.includes(tool)) {
115
+ this.addTool(tool);
116
+ }
117
+ }
118
+ getPrimaryTools() {
119
+ return this.tools.filter((tool) => {
120
+ return tool.getToolGroup() === this.primaryToolGroup;
121
+ });
122
+ }
123
+ /**
124
+ * Add a tool to the end of this' tool list (the added tool receives events after tools already added to this).
125
+ * This should be called before creating the app's toolbar.
126
+ *
127
+ * If `options.addToFront`, the tool is added to the beginning of this' tool list.
128
+ *
129
+ * Does nothing if the tool is already present.
130
+ */
131
+ addTool(tool, options) {
132
+ // Only add if not already present.
133
+ if (!this.tools.includes(tool)) {
134
+ if (options?.addToFront) {
135
+ this.tools.splice(0, 0, tool);
136
+ }
137
+ else {
138
+ this.tools.push(tool);
139
+ }
140
+ }
141
+ }
142
+ /**
143
+ * Removes **and destroys** all tools in `tools` from this.
144
+ */
145
+ removeAndDestroyTools(tools) {
146
+ const newTools = [];
147
+ for (const tool of this.tools) {
148
+ if (tools.includes(tool)) {
149
+ if (this.activeTool === tool) {
150
+ this.activeTool = null;
151
+ }
152
+ tool.onDestroy();
153
+ }
154
+ else {
155
+ newTools.push(tool);
156
+ }
157
+ }
158
+ this.tools = newTools;
159
+ }
160
+ insertTools(insertNear, toolsToInsert, mode) {
161
+ this.tools = this.tools.filter((tool) => !toolsToInsert.includes(tool));
162
+ const newTools = [];
163
+ for (const tool of this.tools) {
164
+ if (mode === 'after') {
165
+ newTools.push(tool);
166
+ }
167
+ if (tool === insertNear) {
168
+ newTools.push(...toolsToInsert);
169
+ }
170
+ if (mode === 'before') {
171
+ newTools.push(tool);
172
+ }
173
+ }
174
+ this.tools = newTools;
175
+ }
176
+ /**
177
+ * Removes a tool from this' tool list and replaces it with `replaceWith`.
178
+ *
179
+ * If any of `toolsToInsert` have already been added to this, the tools are
180
+ * moved.
181
+ *
182
+ * This should be called before creating the editor's toolbar.
183
+ */
184
+ insertToolsAfter(insertAfter, toolsToInsert) {
185
+ this.insertTools(insertAfter, toolsToInsert, 'after');
186
+ }
187
+ /** @see {@link insertToolsAfter} */
188
+ insertToolsBefore(insertBefore, toolsToInsert) {
189
+ this.insertTools(insertBefore, toolsToInsert, 'before');
190
+ }
191
+ // @internal use `dispatchEvent` rather than calling `onEvent` directly.
192
+ onEventInternal(event) {
193
+ const isEditorReadOnly = this.isEditorReadOnly.get();
194
+ const canToolReceiveInput = (tool) => {
195
+ return tool.isEnabled() && (!isEditorReadOnly || tool.canReceiveInputInReadOnlyEditor());
196
+ };
197
+ let handled = false;
198
+ if (event.kind === InputEvtType.PointerDownEvt) {
199
+ let canOnlySendToActiveTool = false;
200
+ if (this.activeTool && !this.activeTool.eventCanBeDeliveredToNonActiveTool(event)) {
201
+ canOnlySendToActiveTool = true;
202
+ }
203
+ for (const tool of this.tools) {
204
+ if (canOnlySendToActiveTool && tool !== this.activeTool) {
205
+ continue;
206
+ }
207
+ if (canToolReceiveInput(tool) && tool.onEvent(event)) {
208
+ if (this.activeTool !== tool) {
209
+ this.activeTool?.onEvent({ kind: InputEvtType.GestureCancelEvt });
210
+ }
211
+ this.activeTool = tool;
212
+ handled = true;
213
+ break;
214
+ }
215
+ }
216
+ }
217
+ else if (event.kind === InputEvtType.PointerUpEvt) {
218
+ const upResult = this.activeTool?.onEvent(event);
219
+ const continueHandlingEvents = upResult && event.allPointers.length > 1;
220
+ // Should the active tool continue handling events (without an additional pointer down?)
221
+ if (!continueHandlingEvents) {
222
+ // No -- Remove the current tool
223
+ this.activeTool = null;
224
+ }
225
+ handled = true;
226
+ }
227
+ else if (event.kind === InputEvtType.PointerMoveEvt) {
228
+ if (this.activeTool !== null) {
229
+ this.activeTool.onEvent(event);
230
+ handled = true;
231
+ }
232
+ }
233
+ else if (event.kind === InputEvtType.GestureCancelEvt) {
234
+ if (this.activeTool !== null) {
235
+ this.activeTool.onEvent(event);
236
+ this.activeTool = null;
237
+ }
238
+ }
239
+ else {
240
+ for (const tool of this.tools) {
241
+ if (!canToolReceiveInput(tool)) {
242
+ continue;
243
+ }
244
+ handled = tool.onEvent(event);
245
+ if (handled) {
246
+ break;
247
+ }
248
+ }
249
+ }
250
+ return handled;
251
+ }
252
+ /** Alias for {@link dispatchInputEvent}. */
253
+ onEvent(event) {
254
+ return this.dispatchInputEvent(event);
255
+ }
256
+ // Returns true if the event was handled.
257
+ dispatchInputEvent(event) {
258
+ // Feed the event through the input pipeline
259
+ return this.inputPipeline.onEvent(event);
260
+ }
261
+ /**
262
+ * Adds a new `InputMapper` to this' input pipeline.
263
+ *
264
+ * A `mapper` is really a relation that maps each event to no, one,
265
+ * or many other events.
266
+ *
267
+ * @see {@link InputMapper}.
268
+ */
269
+ addInputMapper(mapper) {
270
+ this.inputPipeline.addToTail(mapper);
271
+ }
272
+ getMatchingTools(type) {
273
+ return this.tools.filter((tool) => tool instanceof type);
274
+ }
275
+ // @internal
276
+ onEditorDestroyed() {
277
+ for (const tool of this.tools) {
278
+ tool.onDestroy();
279
+ }
280
+ }
281
+ }
@@ -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,10 @@
1
+ // Connects a group of tools -- at most one tool in the group can be enabled.
2
+ export default class ToolEnabledGroup {
3
+ constructor() { }
4
+ notifyEnabled(tool) {
5
+ if (tool !== this.activeTool) {
6
+ this.activeTool?.setEnabled(false);
7
+ this.activeTool = tool;
8
+ }
9
+ }
10
+ }
@@ -0,0 +1,16 @@
1
+ import Editor from '../Editor';
2
+ import { KeyPressEvent } from '../inputEvents';
3
+ import BaseTool from './BaseTool';
4
+ /**
5
+ * Handles keyboard events used, by default, to select tools. By default,
6
+ * 1 maps to the first primary tool, 2 to the second primary tool, ... .
7
+ *
8
+ * This is in the default set of {@link ToolController} tools.
9
+ *
10
+ */
11
+ export default class ToolSwitcherShortcut extends BaseTool {
12
+ private editor;
13
+ constructor(editor: Editor);
14
+ canReceiveInputInReadOnlyEditor(): boolean;
15
+ onKeyPress({ key }: KeyPressEvent): boolean;
16
+ }