js-draw 0.17.4 → 0.18.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (413) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/dist/bundle.js +9 -1
  3. package/package.json +35 -33
  4. package/tsconfig.json +3 -2
  5. package/tsconfig.mjs.json +9 -0
  6. package/.github/ISSUE_TEMPLATE/bug_report.md +0 -34
  7. package/.github/ISSUE_TEMPLATE/feature_request.md +0 -20
  8. package/.github/ISSUE_TEMPLATE/translation.yml +0 -902
  9. package/.github/pull_request_template.md +0 -15
  10. package/.github/workflows/firebase-hosting-merge.yml +0 -32
  11. package/.github/workflows/firebase-hosting-pull-request.yml +0 -32
  12. package/.github/workflows/github-pages.yml +0 -56
  13. package/.husky/pre-commit +0 -4
  14. package/build_tools/BundledFile.ts +0 -167
  15. package/build_tools/buildTranslationTemplate.ts +0 -121
  16. package/build_tools/bundle.ts +0 -11
  17. package/dist/build_tools/BundledFile.d.ts +0 -13
  18. package/dist/build_tools/BundledFile.js +0 -157
  19. package/dist/build_tools/buildTranslationTemplate.d.ts +0 -1
  20. package/dist/build_tools/buildTranslationTemplate.js +0 -94
  21. package/dist/build_tools/bundle.d.ts +0 -1
  22. package/dist/build_tools/bundle.js +0 -5
  23. package/dist/src/Color4.d.ts +0 -60
  24. package/dist/src/Color4.js +0 -192
  25. package/dist/src/Editor.d.ts +0 -308
  26. package/dist/src/Editor.js +0 -874
  27. package/dist/src/EditorImage.d.ts +0 -97
  28. package/dist/src/EditorImage.js +0 -477
  29. package/dist/src/EventDispatcher.d.ts +0 -30
  30. package/dist/src/EventDispatcher.js +0 -54
  31. package/dist/src/Pointer.d.ts +0 -24
  32. package/dist/src/Pointer.js +0 -80
  33. package/dist/src/SVGLoader.d.ts +0 -48
  34. package/dist/src/SVGLoader.js +0 -442
  35. package/dist/src/UndoRedoHistory.d.ts +0 -19
  36. package/dist/src/UndoRedoHistory.js +0 -91
  37. package/dist/src/Viewport.d.ts +0 -71
  38. package/dist/src/Viewport.js +0 -256
  39. package/dist/src/bundle/bundled.d.ts +0 -4
  40. package/dist/src/bundle/bundled.js +0 -5
  41. package/dist/src/commands/Command.d.ts +0 -16
  42. package/dist/src/commands/Command.js +0 -30
  43. package/dist/src/commands/Duplicate.d.ts +0 -14
  44. package/dist/src/commands/Duplicate.js +0 -33
  45. package/dist/src/commands/Erase.d.ts +0 -14
  46. package/dist/src/commands/Erase.js +0 -57
  47. package/dist/src/commands/SerializableCommand.d.ts +0 -12
  48. package/dist/src/commands/SerializableCommand.js +0 -36
  49. package/dist/src/commands/UnresolvedCommand.d.ts +0 -14
  50. package/dist/src/commands/UnresolvedCommand.js +0 -22
  51. package/dist/src/commands/invertCommand.d.ts +0 -4
  52. package/dist/src/commands/invertCommand.js +0 -44
  53. package/dist/src/commands/lib.d.ts +0 -7
  54. package/dist/src/commands/lib.js +0 -7
  55. package/dist/src/commands/localization.d.ts +0 -23
  56. package/dist/src/commands/localization.js +0 -21
  57. package/dist/src/commands/uniteCommands.d.ts +0 -4
  58. package/dist/src/commands/uniteCommands.js +0 -116
  59. package/dist/src/components/AbstractComponent.d.ts +0 -73
  60. package/dist/src/components/AbstractComponent.js +0 -252
  61. package/dist/src/components/ImageBackground.d.ts +0 -42
  62. package/dist/src/components/ImageBackground.js +0 -139
  63. package/dist/src/components/ImageComponent.d.ts +0 -31
  64. package/dist/src/components/ImageComponent.js +0 -146
  65. package/dist/src/components/RestylableComponent.d.ts +0 -24
  66. package/dist/src/components/RestylableComponent.js +0 -80
  67. package/dist/src/components/SVGGlobalAttributesObject.d.ts +0 -21
  68. package/dist/src/components/SVGGlobalAttributesObject.js +0 -59
  69. package/dist/src/components/Stroke.d.ts +0 -40
  70. package/dist/src/components/Stroke.js +0 -185
  71. package/dist/src/components/TextComponent.d.ts +0 -53
  72. package/dist/src/components/TextComponent.js +0 -252
  73. package/dist/src/components/UnknownSVGObject.d.ts +0 -18
  74. package/dist/src/components/UnknownSVGObject.js +0 -44
  75. package/dist/src/components/builders/ArrowBuilder.d.ts +0 -19
  76. package/dist/src/components/builders/ArrowBuilder.js +0 -86
  77. package/dist/src/components/builders/FreehandLineBuilder.d.ts +0 -33
  78. package/dist/src/components/builders/FreehandLineBuilder.js +0 -165
  79. package/dist/src/components/builders/LineBuilder.d.ts +0 -18
  80. package/dist/src/components/builders/LineBuilder.js +0 -58
  81. package/dist/src/components/builders/PressureSensitiveFreehandLineBuilder.d.ts +0 -36
  82. package/dist/src/components/builders/PressureSensitiveFreehandLineBuilder.js +0 -339
  83. package/dist/src/components/builders/RectangleBuilder.d.ts +0 -20
  84. package/dist/src/components/builders/RectangleBuilder.js +0 -50
  85. package/dist/src/components/builders/types.d.ts +0 -12
  86. package/dist/src/components/builders/types.js +0 -1
  87. package/dist/src/components/lib.d.ts +0 -12
  88. package/dist/src/components/lib.js +0 -12
  89. package/dist/src/components/localization.d.ts +0 -11
  90. package/dist/src/components/localization.js +0 -10
  91. package/dist/src/components/util/StrokeSmoother.d.ts +0 -35
  92. package/dist/src/components/util/StrokeSmoother.js +0 -210
  93. package/dist/src/components/util/describeComponentList.d.ts +0 -4
  94. package/dist/src/components/util/describeComponentList.js +0 -14
  95. package/dist/src/lib.d.ts +0 -34
  96. package/dist/src/lib.js +0 -34
  97. package/dist/src/localization.d.ts +0 -14
  98. package/dist/src/localization.js +0 -10
  99. package/dist/src/localizations/de.d.ts +0 -3
  100. package/dist/src/localizations/de.js +0 -4
  101. package/dist/src/localizations/en.d.ts +0 -3
  102. package/dist/src/localizations/en.js +0 -4
  103. package/dist/src/localizations/es.d.ts +0 -3
  104. package/dist/src/localizations/es.js +0 -18
  105. package/dist/src/localizations/getLocalizationTable.d.ts +0 -3
  106. package/dist/src/localizations/getLocalizationTable.js +0 -45
  107. package/dist/src/math/LineSegment2.d.ts +0 -24
  108. package/dist/src/math/LineSegment2.js +0 -125
  109. package/dist/src/math/Mat33.d.ts +0 -118
  110. package/dist/src/math/Mat33.js +0 -326
  111. package/dist/src/math/Path.d.ts +0 -71
  112. package/dist/src/math/Path.js +0 -648
  113. package/dist/src/math/Rect2.d.ts +0 -52
  114. package/dist/src/math/Rect2.js +0 -228
  115. package/dist/src/math/Triangle.d.ts +0 -11
  116. package/dist/src/math/Triangle.js +0 -19
  117. package/dist/src/math/Vec2.d.ts +0 -13
  118. package/dist/src/math/Vec2.js +0 -13
  119. package/dist/src/math/Vec3.d.ts +0 -106
  120. package/dist/src/math/Vec3.js +0 -174
  121. package/dist/src/math/lib.d.ts +0 -7
  122. package/dist/src/math/lib.js +0 -7
  123. package/dist/src/math/rounding.d.ts +0 -4
  124. package/dist/src/math/rounding.js +0 -128
  125. package/dist/src/rendering/Display.d.ts +0 -75
  126. package/dist/src/rendering/Display.js +0 -207
  127. package/dist/src/rendering/RenderingStyle.d.ts +0 -31
  128. package/dist/src/rendering/RenderingStyle.js +0 -38
  129. package/dist/src/rendering/TextRenderingStyle.d.ts +0 -36
  130. package/dist/src/rendering/TextRenderingStyle.js +0 -23
  131. package/dist/src/rendering/caching/CacheRecord.d.ts +0 -20
  132. package/dist/src/rendering/caching/CacheRecord.js +0 -55
  133. package/dist/src/rendering/caching/CacheRecordManager.d.ts +0 -12
  134. package/dist/src/rendering/caching/CacheRecordManager.js +0 -43
  135. package/dist/src/rendering/caching/RenderingCache.d.ts +0 -11
  136. package/dist/src/rendering/caching/RenderingCache.js +0 -45
  137. package/dist/src/rendering/caching/RenderingCacheNode.d.ts +0 -29
  138. package/dist/src/rendering/caching/RenderingCacheNode.js +0 -320
  139. package/dist/src/rendering/caching/testUtils.d.ts +0 -9
  140. package/dist/src/rendering/caching/testUtils.js +0 -20
  141. package/dist/src/rendering/caching/types.d.ts +0 -19
  142. package/dist/src/rendering/caching/types.js +0 -1
  143. package/dist/src/rendering/lib.d.ts +0 -5
  144. package/dist/src/rendering/lib.js +0 -5
  145. package/dist/src/rendering/localization.d.ts +0 -10
  146. package/dist/src/rendering/localization.js +0 -9
  147. package/dist/src/rendering/renderers/AbstractRenderer.d.ts +0 -68
  148. package/dist/src/rendering/renderers/AbstractRenderer.js +0 -144
  149. package/dist/src/rendering/renderers/CanvasRenderer.d.ts +0 -63
  150. package/dist/src/rendering/renderers/CanvasRenderer.js +0 -230
  151. package/dist/src/rendering/renderers/DummyRenderer.d.ts +0 -35
  152. package/dist/src/rendering/renderers/DummyRenderer.js +0 -106
  153. package/dist/src/rendering/renderers/SVGRenderer.d.ts +0 -57
  154. package/dist/src/rendering/renderers/SVGRenderer.js +0 -304
  155. package/dist/src/rendering/renderers/TextOnlyRenderer.d.ts +0 -29
  156. package/dist/src/rendering/renderers/TextOnlyRenderer.js +0 -57
  157. package/dist/src/testing/beforeEachFile.d.ts +0 -1
  158. package/dist/src/testing/beforeEachFile.js +0 -7
  159. package/dist/src/testing/createEditor.d.ts +0 -4
  160. package/dist/src/testing/createEditor.js +0 -9
  161. package/dist/src/testing/lib.d.ts +0 -2
  162. package/dist/src/testing/lib.js +0 -2
  163. package/dist/src/testing/loadExpectExtensions.d.ts +0 -2
  164. package/dist/src/testing/loadExpectExtensions.js +0 -24
  165. package/dist/src/testing/sendPenEvent.d.ts +0 -12
  166. package/dist/src/testing/sendPenEvent.js +0 -19
  167. package/dist/src/testing/sendTouchEvent.d.ts +0 -42
  168. package/dist/src/testing/sendTouchEvent.js +0 -62
  169. package/dist/src/toolbar/HTMLToolbar.d.ts +0 -103
  170. package/dist/src/toolbar/HTMLToolbar.js +0 -376
  171. package/dist/src/toolbar/IconProvider.d.ts +0 -62
  172. package/dist/src/toolbar/IconProvider.js +0 -654
  173. package/dist/src/toolbar/lib.d.ts +0 -3
  174. package/dist/src/toolbar/lib.js +0 -3
  175. package/dist/src/toolbar/localization.d.ts +0 -49
  176. package/dist/src/toolbar/localization.js +0 -48
  177. package/dist/src/toolbar/makeColorInput.d.ts +0 -6
  178. package/dist/src/toolbar/makeColorInput.js +0 -113
  179. package/dist/src/toolbar/types.d.ts +0 -4
  180. package/dist/src/toolbar/types.js +0 -1
  181. package/dist/src/toolbar/widgets/ActionButtonWidget.d.ts +0 -15
  182. package/dist/src/toolbar/widgets/ActionButtonWidget.js +0 -25
  183. package/dist/src/toolbar/widgets/BaseToolWidget.d.ts +0 -11
  184. package/dist/src/toolbar/widgets/BaseToolWidget.js +0 -44
  185. package/dist/src/toolbar/widgets/BaseWidget.d.ts +0 -72
  186. package/dist/src/toolbar/widgets/BaseWidget.js +0 -307
  187. package/dist/src/toolbar/widgets/DocumentPropertiesWidget.d.ts +0 -18
  188. package/dist/src/toolbar/widgets/DocumentPropertiesWidget.js +0 -120
  189. package/dist/src/toolbar/widgets/EraserToolWidget.d.ts +0 -17
  190. package/dist/src/toolbar/widgets/EraserToolWidget.js +0 -57
  191. package/dist/src/toolbar/widgets/HandToolWidget.d.ts +0 -17
  192. package/dist/src/toolbar/widgets/HandToolWidget.js +0 -172
  193. package/dist/src/toolbar/widgets/InsertImageWidget.d.ts +0 -19
  194. package/dist/src/toolbar/widgets/InsertImageWidget.js +0 -170
  195. package/dist/src/toolbar/widgets/OverflowWidget.d.ts +0 -25
  196. package/dist/src/toolbar/widgets/OverflowWidget.js +0 -71
  197. package/dist/src/toolbar/widgets/PenToolWidget.d.ts +0 -27
  198. package/dist/src/toolbar/widgets/PenToolWidget.js +0 -220
  199. package/dist/src/toolbar/widgets/SelectionToolWidget.d.ts +0 -13
  200. package/dist/src/toolbar/widgets/SelectionToolWidget.js +0 -147
  201. package/dist/src/toolbar/widgets/TextToolWidget.d.ts +0 -16
  202. package/dist/src/toolbar/widgets/TextToolWidget.js +0 -109
  203. package/dist/src/toolbar/widgets/lib.d.ts +0 -10
  204. package/dist/src/toolbar/widgets/lib.js +0 -10
  205. package/dist/src/tools/BaseTool.d.ts +0 -22
  206. package/dist/src/tools/BaseTool.js +0 -63
  207. package/dist/src/tools/Eraser.d.ts +0 -23
  208. package/dist/src/tools/Eraser.js +0 -106
  209. package/dist/src/tools/FindTool.d.ts +0 -21
  210. package/dist/src/tools/FindTool.js +0 -114
  211. package/dist/src/tools/PanZoom.d.ts +0 -52
  212. package/dist/src/tools/PanZoom.js +0 -414
  213. package/dist/src/tools/PasteHandler.d.ts +0 -23
  214. package/dist/src/tools/PasteHandler.js +0 -93
  215. package/dist/src/tools/Pen.d.ts +0 -39
  216. package/dist/src/tools/Pen.js +0 -173
  217. package/dist/src/tools/PipetteTool.d.ts +0 -18
  218. package/dist/src/tools/PipetteTool.js +0 -39
  219. package/dist/src/tools/SelectionTool/SelectAllShortcutHandler.d.ts +0 -8
  220. package/dist/src/tools/SelectionTool/SelectAllShortcutHandler.js +0 -22
  221. package/dist/src/tools/SelectionTool/Selection.d.ts +0 -64
  222. package/dist/src/tools/SelectionTool/Selection.js +0 -459
  223. package/dist/src/tools/SelectionTool/SelectionHandle.d.ts +0 -38
  224. package/dist/src/tools/SelectionTool/SelectionHandle.js +0 -81
  225. package/dist/src/tools/SelectionTool/SelectionTool.d.ts +0 -36
  226. package/dist/src/tools/SelectionTool/SelectionTool.js +0 -398
  227. package/dist/src/tools/SelectionTool/TransformMode.d.ts +0 -34
  228. package/dist/src/tools/SelectionTool/TransformMode.js +0 -98
  229. package/dist/src/tools/SelectionTool/types.d.ts +0 -9
  230. package/dist/src/tools/SelectionTool/types.js +0 -11
  231. package/dist/src/tools/TextTool.d.ts +0 -33
  232. package/dist/src/tools/TextTool.js +0 -256
  233. package/dist/src/tools/ToolController.d.ts +0 -18
  234. package/dist/src/tools/ToolController.js +0 -158
  235. package/dist/src/tools/ToolEnabledGroup.d.ts +0 -6
  236. package/dist/src/tools/ToolEnabledGroup.js +0 -11
  237. package/dist/src/tools/ToolSwitcherShortcut.d.ts +0 -16
  238. package/dist/src/tools/ToolSwitcherShortcut.js +0 -32
  239. package/dist/src/tools/ToolbarShortcutHandler.d.ts +0 -12
  240. package/dist/src/tools/ToolbarShortcutHandler.js +0 -23
  241. package/dist/src/tools/UndoRedoShortcut.d.ts +0 -8
  242. package/dist/src/tools/UndoRedoShortcut.js +0 -22
  243. package/dist/src/tools/lib.d.ts +0 -16
  244. package/dist/src/tools/lib.js +0 -16
  245. package/dist/src/tools/localization.d.ts +0 -28
  246. package/dist/src/tools/localization.js +0 -27
  247. package/dist/src/types.d.ts +0 -151
  248. package/dist/src/types.js +0 -35
  249. package/dist/src/util/assertions.d.ts +0 -23
  250. package/dist/src/util/assertions.js +0 -45
  251. package/dist/src/util/fileToBase64.d.ts +0 -3
  252. package/dist/src/util/fileToBase64.js +0 -13
  253. package/dist/src/util/untilNextAnimationFrame.d.ts +0 -3
  254. package/dist/src/util/untilNextAnimationFrame.js +0 -7
  255. package/dist/src/util/waitForTimeout.d.ts +0 -2
  256. package/dist/src/util/waitForTimeout.js +0 -7
  257. package/src/Color4.test.ts +0 -40
  258. package/src/Color4.ts +0 -234
  259. package/src/Editor.css +0 -86
  260. package/src/Editor.loadFrom.test.ts +0 -24
  261. package/src/Editor.toSVG.test.ts +0 -111
  262. package/src/Editor.ts +0 -1122
  263. package/src/EditorImage.test.ts +0 -120
  264. package/src/EditorImage.ts +0 -603
  265. package/src/EventDispatcher.test.ts +0 -123
  266. package/src/EventDispatcher.ts +0 -71
  267. package/src/Pointer.ts +0 -127
  268. package/src/SVGLoader.test.ts +0 -114
  269. package/src/SVGLoader.ts +0 -511
  270. package/src/UndoRedoHistory.test.ts +0 -33
  271. package/src/UndoRedoHistory.ts +0 -102
  272. package/src/Viewport.ts +0 -319
  273. package/src/bundle/bundled.ts +0 -7
  274. package/src/commands/Command.ts +0 -45
  275. package/src/commands/Duplicate.ts +0 -48
  276. package/src/commands/Erase.ts +0 -74
  277. package/src/commands/SerializableCommand.ts +0 -49
  278. package/src/commands/UnresolvedCommand.ts +0 -37
  279. package/src/commands/invertCommand.ts +0 -51
  280. package/src/commands/lib.ts +0 -16
  281. package/src/commands/localization.ts +0 -47
  282. package/src/commands/uniteCommands.test.ts +0 -23
  283. package/src/commands/uniteCommands.ts +0 -135
  284. package/src/components/AbstractComponent.transformBy.test.ts +0 -22
  285. package/src/components/AbstractComponent.ts +0 -364
  286. package/src/components/ImageBackground.test.ts +0 -35
  287. package/src/components/ImageBackground.ts +0 -176
  288. package/src/components/ImageComponent.ts +0 -171
  289. package/src/components/RestylableComponent.ts +0 -142
  290. package/src/components/SVGGlobalAttributesObject.ts +0 -81
  291. package/src/components/Stroke.test.ts +0 -139
  292. package/src/components/Stroke.ts +0 -245
  293. package/src/components/TextComponent.test.ts +0 -99
  294. package/src/components/TextComponent.ts +0 -315
  295. package/src/components/UnknownSVGObject.test.ts +0 -10
  296. package/src/components/UnknownSVGObject.ts +0 -60
  297. package/src/components/builders/ArrowBuilder.ts +0 -107
  298. package/src/components/builders/FreehandLineBuilder.ts +0 -212
  299. package/src/components/builders/LineBuilder.ts +0 -77
  300. package/src/components/builders/PressureSensitiveFreehandLineBuilder.ts +0 -454
  301. package/src/components/builders/RectangleBuilder.ts +0 -74
  302. package/src/components/builders/types.ts +0 -15
  303. package/src/components/lib.ts +0 -25
  304. package/src/components/localization.ts +0 -22
  305. package/src/components/util/StrokeSmoother.ts +0 -293
  306. package/src/components/util/describeComponentList.ts +0 -18
  307. package/src/lib.ts +0 -37
  308. package/src/localization.ts +0 -34
  309. package/src/localizations/de.ts +0 -98
  310. package/src/localizations/en.ts +0 -8
  311. package/src/localizations/es.ts +0 -74
  312. package/src/localizations/getLocalizationTable.test.ts +0 -27
  313. package/src/localizations/getLocalizationTable.ts +0 -55
  314. package/src/math/LineSegment2.test.ts +0 -99
  315. package/src/math/LineSegment2.ts +0 -160
  316. package/src/math/Mat33.test.ts +0 -244
  317. package/src/math/Mat33.ts +0 -437
  318. package/src/math/Path.fromString.test.ts +0 -223
  319. package/src/math/Path.test.ts +0 -198
  320. package/src/math/Path.toString.test.ts +0 -77
  321. package/src/math/Path.ts +0 -790
  322. package/src/math/Rect2.test.ts +0 -204
  323. package/src/math/Rect2.ts +0 -315
  324. package/src/math/Triangle.ts +0 -29
  325. package/src/math/Vec2.test.ts +0 -30
  326. package/src/math/Vec2.ts +0 -18
  327. package/src/math/Vec3.test.ts +0 -44
  328. package/src/math/Vec3.ts +0 -218
  329. package/src/math/lib.ts +0 -15
  330. package/src/math/rounding.test.ts +0 -65
  331. package/src/math/rounding.ts +0 -156
  332. package/src/rendering/Display.ts +0 -249
  333. package/src/rendering/RenderingStyle.test.ts +0 -68
  334. package/src/rendering/RenderingStyle.ts +0 -55
  335. package/src/rendering/TextRenderingStyle.ts +0 -45
  336. package/src/rendering/caching/CacheRecord.test.ts +0 -49
  337. package/src/rendering/caching/CacheRecord.ts +0 -77
  338. package/src/rendering/caching/CacheRecordManager.ts +0 -71
  339. package/src/rendering/caching/RenderingCache.test.ts +0 -44
  340. package/src/rendering/caching/RenderingCache.ts +0 -66
  341. package/src/rendering/caching/RenderingCacheNode.ts +0 -405
  342. package/src/rendering/caching/testUtils.ts +0 -35
  343. package/src/rendering/caching/types.ts +0 -34
  344. package/src/rendering/lib.ts +0 -6
  345. package/src/rendering/localization.ts +0 -20
  346. package/src/rendering/renderers/AbstractRenderer.ts +0 -222
  347. package/src/rendering/renderers/CanvasRenderer.ts +0 -296
  348. package/src/rendering/renderers/DummyRenderer.test.ts +0 -42
  349. package/src/rendering/renderers/DummyRenderer.ts +0 -136
  350. package/src/rendering/renderers/SVGRenderer.ts +0 -354
  351. package/src/rendering/renderers/TextOnlyRenderer.ts +0 -70
  352. package/src/styles.js +0 -7
  353. package/src/testing/beforeEachFile.ts +0 -8
  354. package/src/testing/createEditor.ts +0 -11
  355. package/src/testing/global.d.ts +0 -17
  356. package/src/testing/lib.ts +0 -3
  357. package/src/testing/loadExpectExtensions.ts +0 -25
  358. package/src/testing/sendPenEvent.ts +0 -31
  359. package/src/testing/sendTouchEvent.ts +0 -78
  360. package/src/toolbar/HTMLToolbar.ts +0 -492
  361. package/src/toolbar/IconProvider.ts +0 -736
  362. package/src/toolbar/lib.ts +0 -4
  363. package/src/toolbar/localization.ts +0 -106
  364. package/src/toolbar/makeColorInput.ts +0 -145
  365. package/src/toolbar/toolbar.css +0 -213
  366. package/src/toolbar/types.ts +0 -5
  367. package/src/toolbar/widgets/ActionButtonWidget.ts +0 -39
  368. package/src/toolbar/widgets/BaseToolWidget.ts +0 -56
  369. package/src/toolbar/widgets/BaseWidget.ts +0 -377
  370. package/src/toolbar/widgets/DocumentPropertiesWidget.ts +0 -167
  371. package/src/toolbar/widgets/EraserToolWidget.ts +0 -85
  372. package/src/toolbar/widgets/HandToolWidget.ts +0 -250
  373. package/src/toolbar/widgets/InsertImageWidget.css +0 -44
  374. package/src/toolbar/widgets/InsertImageWidget.ts +0 -223
  375. package/src/toolbar/widgets/OverflowWidget.css +0 -27
  376. package/src/toolbar/widgets/OverflowWidget.ts +0 -92
  377. package/src/toolbar/widgets/PenToolWidget.ts +0 -288
  378. package/src/toolbar/widgets/SelectionToolWidget.ts +0 -190
  379. package/src/toolbar/widgets/TextToolWidget.ts +0 -145
  380. package/src/toolbar/widgets/lib.ts +0 -13
  381. package/src/tools/BaseTool.ts +0 -76
  382. package/src/tools/Eraser.test.ts +0 -103
  383. package/src/tools/Eraser.ts +0 -139
  384. package/src/tools/FindTool.css +0 -7
  385. package/src/tools/FindTool.ts +0 -152
  386. package/src/tools/PanZoom.test.ts +0 -310
  387. package/src/tools/PanZoom.ts +0 -520
  388. package/src/tools/PasteHandler.ts +0 -95
  389. package/src/tools/Pen.test.ts +0 -194
  390. package/src/tools/Pen.ts +0 -226
  391. package/src/tools/PipetteTool.ts +0 -55
  392. package/src/tools/SelectionTool/SelectAllShortcutHandler.ts +0 -28
  393. package/src/tools/SelectionTool/Selection.ts +0 -607
  394. package/src/tools/SelectionTool/SelectionHandle.ts +0 -108
  395. package/src/tools/SelectionTool/SelectionTool.css +0 -23
  396. package/src/tools/SelectionTool/SelectionTool.test.ts +0 -261
  397. package/src/tools/SelectionTool/SelectionTool.ts +0 -480
  398. package/src/tools/SelectionTool/TransformMode.ts +0 -114
  399. package/src/tools/SelectionTool/types.ts +0 -11
  400. package/src/tools/TextTool.ts +0 -326
  401. package/src/tools/ToolController.ts +0 -178
  402. package/src/tools/ToolEnabledGroup.ts +0 -14
  403. package/src/tools/ToolSwitcherShortcut.ts +0 -39
  404. package/src/tools/ToolbarShortcutHandler.ts +0 -34
  405. package/src/tools/UndoRedoShortcut.test.ts +0 -56
  406. package/src/tools/UndoRedoShortcut.ts +0 -25
  407. package/src/tools/lib.ts +0 -21
  408. package/src/tools/localization.ts +0 -66
  409. package/src/types.ts +0 -234
  410. package/src/util/assertions.ts +0 -55
  411. package/src/util/fileToBase64.ts +0 -18
  412. package/src/util/untilNextAnimationFrame.ts +0 -9
  413. package/src/util/waitForTimeout.ts +0 -9
package/src/Editor.ts DELETED
@@ -1,1122 +0,0 @@
1
- import EditorImage from './EditorImage';
2
- import ToolController from './tools/ToolController';
3
- import { InputEvtType, PointerEvt, EditorNotifier, EditorEventType, ImageLoader } from './types';
4
- import Command from './commands/Command';
5
- import UndoRedoHistory from './UndoRedoHistory';
6
- import Viewport from './Viewport';
7
- import EventDispatcher from './EventDispatcher';
8
- import { Point2, Vec2 } from './math/Vec2';
9
- import Vec3 from './math/Vec3';
10
- import HTMLToolbar from './toolbar/HTMLToolbar';
11
- import { RenderablePathSpec } from './rendering/renderers/AbstractRenderer';
12
- import Display, { RenderingMode } from './rendering/Display';
13
- import SVGRenderer from './rendering/renderers/SVGRenderer';
14
- import Color4 from './Color4';
15
- import SVGLoader from './SVGLoader';
16
- import Pointer from './Pointer';
17
- import Mat33 from './math/Mat33';
18
- import Rect2 from './math/Rect2';
19
- import { EditorLocalization } from './localization';
20
- import getLocalizationTable from './localizations/getLocalizationTable';
21
- import IconProvider from './toolbar/IconProvider';
22
- import { toRoundedString } from './math/rounding';
23
- import CanvasRenderer from './rendering/renderers/CanvasRenderer';
24
- import untilNextAnimationFrame from './util/untilNextAnimationFrame';
25
- import fileToBase64 from './util/fileToBase64';
26
- import uniteCommands from './commands/uniteCommands';
27
- import SelectionTool from './tools/SelectionTool/SelectionTool';
28
- import AbstractComponent from './components/AbstractComponent';
29
- import Erase from './commands/Erase';
30
- import ImageBackground, { BackgroundType } from './components/ImageBackground';
31
- import sendPenEvent from './testing/sendPenEvent';
32
-
33
- type HTMLPointerEventType = 'pointerdown'|'pointermove'|'pointerup'|'pointercancel';
34
- type HTMLPointerEventFilter = (eventName: HTMLPointerEventType, event: PointerEvent)=>boolean;
35
-
36
- export interface EditorSettings {
37
- /** Defaults to `RenderingMode.CanvasRenderer` */
38
- renderingMode: RenderingMode,
39
-
40
- /** Uses a default English localization if a translation is not given. */
41
- localization: Partial<EditorLocalization>,
42
-
43
- /**
44
- * `true` if touchpad/mousewheel scrolling should scroll the editor instead of the document.
45
- * This does not include pinch-zoom events.
46
- * Defaults to true.
47
- */
48
- wheelEventsEnabled: boolean|'only-if-focused';
49
-
50
- /** Minimum zoom fraction (e.g. 0.5 → 50% zoom). */
51
- minZoom: number,
52
- maxZoom: number,
53
-
54
- iconProvider: IconProvider,
55
- }
56
-
57
- /**
58
- * The main entrypoint for the full editor.
59
- *
60
- * @example
61
- * To create an editor with a toolbar,
62
- * ```
63
- * const editor = new Editor(document.body);
64
- *
65
- * const toolbar = editor.addToolbar();
66
- * toolbar.addActionButton('Save', () => {
67
- * const saveData = editor.toSVG().outerHTML;
68
- * // Do something with saveData...
69
- * });
70
- * ```
71
- *
72
- * See also
73
- * [`docs/example/example.ts`](https://github.com/personalizedrefrigerator/js-draw/blob/main/docs/example/example.ts#L15).
74
- */
75
- export class Editor {
76
- // Wrapper around the viewport and toolbar
77
- private container: HTMLElement;
78
- private renderingRegion: HTMLElement;
79
-
80
- /** Manages drawing surfaces/{@link lib!AbstractRenderer}s. */
81
- public display: Display;
82
-
83
- /**
84
- * Handles undo/redo.
85
- *
86
- * @example
87
- * ```
88
- * const editor = new Editor(document.body);
89
- *
90
- * // Do something undoable.
91
- * // ...
92
- *
93
- * // Undo the last action
94
- * editor.history.undo();
95
- * ```
96
- */
97
- public history: UndoRedoHistory;
98
-
99
- /**
100
- * Data structure for adding/removing/querying objects in the image.
101
- *
102
- * @example
103
- * ```
104
- * const editor = new Editor(document.body);
105
- *
106
- * // Create a path.
107
- * const stroke = new Stroke([
108
- * Path.fromString('M0,0 L30,30 z').toRenderable({ fill: Color4.black }),
109
- * ]);
110
- * const addElementCommand = editor.image.addElement(stroke);
111
- *
112
- * // Add the stroke to the editor
113
- * editor.dispatch(addElementCommand);
114
- * ```
115
- */
116
- public readonly image: EditorImage;
117
-
118
- /**
119
- * Allows transforming the view and querying information about
120
- * what is currently visible.
121
- */
122
- public readonly viewport: Viewport;
123
-
124
- /** @internal */
125
- public readonly localization: EditorLocalization;
126
-
127
- /** {@link lib!EditorSettings.iconProvider} */
128
- public readonly icons: IconProvider;
129
-
130
- /**
131
- * Controls the list of tools. See
132
- * [the custom tool example](https://github.com/personalizedrefrigerator/js-draw/tree/main/docs/example-custom-tools)
133
- * for more.
134
- */
135
- public readonly toolController: ToolController;
136
-
137
- /**
138
- * Global event dispatcher/subscriber.
139
- */
140
- public readonly notifier: EditorNotifier;
141
-
142
- private loadingWarning: HTMLElement;
143
- private accessibilityAnnounceArea: HTMLElement;
144
- private accessibilityControlArea: HTMLTextAreaElement;
145
- private eventListenerTargets: HTMLElement[] = [];
146
-
147
- private settings: EditorSettings;
148
-
149
- /**
150
- * @example
151
- * ```
152
- * const container = document.body;
153
- *
154
- * // Create an editor
155
- * const editor = new Editor(container, {
156
- * // 2e-10 and 1e12 are the default values for minimum/maximum zoom.
157
- * minZoom: 2e-10,
158
- * maxZoom: 1e12,
159
- * });
160
- *
161
- * // Add the default toolbar
162
- * const toolbar = editor.addToolbar();
163
- *
164
- * // Add a save button
165
- * toolbar.addActionButton({
166
- * label: 'Save'
167
- * icon: createSaveIcon(),
168
- * }, () => {
169
- * const saveData = editor.toSVG().outerHTML;
170
- * // Do something with saveData
171
- * });
172
- * ```
173
- */
174
- public constructor(
175
- parent: HTMLElement,
176
- settings: Partial<EditorSettings> = {},
177
- ) {
178
- this.localization = {
179
- ...getLocalizationTable(),
180
- ...settings.localization,
181
- };
182
-
183
- // Fill default settings.
184
- this.settings = {
185
- wheelEventsEnabled: settings.wheelEventsEnabled ?? true,
186
- renderingMode: settings.renderingMode ?? RenderingMode.CanvasRenderer,
187
- localization: this.localization,
188
- minZoom: settings.minZoom ?? 2e-10,
189
- maxZoom: settings.maxZoom ?? 1e12,
190
- iconProvider: settings.iconProvider ?? new IconProvider(),
191
- };
192
- this.icons = this.settings.iconProvider;
193
-
194
- this.container = document.createElement('div');
195
- this.renderingRegion = document.createElement('div');
196
- this.container.appendChild(this.renderingRegion);
197
- this.container.className = 'imageEditorContainer';
198
-
199
- this.loadingWarning = document.createElement('div');
200
- this.loadingWarning.classList.add('loadingMessage');
201
- this.loadingWarning.ariaLive = 'polite';
202
- this.container.appendChild(this.loadingWarning);
203
-
204
- this.accessibilityControlArea = document.createElement('textarea');
205
- this.accessibilityControlArea.setAttribute('placeholder', this.localization.accessibilityInputInstructions);
206
- this.accessibilityControlArea.style.opacity = '0';
207
- this.accessibilityControlArea.style.width = '0';
208
- this.accessibilityControlArea.style.height = '0';
209
- this.accessibilityControlArea.style.position = 'absolute';
210
-
211
- this.accessibilityAnnounceArea = document.createElement('div');
212
- this.accessibilityAnnounceArea.setAttribute('aria-live', 'assertive');
213
- this.accessibilityAnnounceArea.className = 'accessibilityAnnouncement';
214
- this.container.appendChild(this.accessibilityAnnounceArea);
215
-
216
- this.renderingRegion.style.touchAction = 'none';
217
- this.renderingRegion.className = 'imageEditorRenderArea';
218
- this.renderingRegion.appendChild(this.accessibilityControlArea);
219
- this.renderingRegion.setAttribute('tabIndex', '0');
220
- this.renderingRegion.setAttribute('alt', '');
221
-
222
- this.notifier = new EventDispatcher();
223
- this.viewport = new Viewport((oldTransform, newTransform) => {
224
- this.notifier.dispatch(EditorEventType.ViewportChanged, {
225
- kind: EditorEventType.ViewportChanged,
226
- newTransform,
227
- oldTransform,
228
- });
229
- });
230
- this.display = new Display(this, this.settings.renderingMode, this.renderingRegion);
231
- this.image = new EditorImage();
232
- this.history = new UndoRedoHistory(this, this.announceRedoCallback, this.announceUndoCallback);
233
- this.toolController = new ToolController(this, this.localization);
234
-
235
- parent.appendChild(this.container);
236
-
237
- this.viewport.updateScreenSize(
238
- Vec2.of(this.display.width, this.display.height)
239
- );
240
-
241
- this.registerListeners();
242
- this.queueRerender();
243
- this.hideLoadingWarning();
244
-
245
-
246
- // Enforce zoom limits.
247
- this.notifier.on(EditorEventType.ViewportChanged, evt => {
248
- if (evt.kind === EditorEventType.ViewportChanged) {
249
- const zoom = evt.newTransform.transformVec3(Vec2.unitX).length();
250
- if (zoom > this.settings.maxZoom || zoom < this.settings.minZoom) {
251
- const oldZoom = evt.oldTransform.transformVec3(Vec2.unitX).length();
252
- let resetTransform = Mat33.identity;
253
-
254
- if (oldZoom <= this.settings.maxZoom && oldZoom >= this.settings.minZoom) {
255
- resetTransform = evt.oldTransform;
256
- }
257
-
258
- this.viewport.resetTransform(resetTransform);
259
- }
260
- }
261
- });
262
- }
263
-
264
- /**
265
- * @returns a reference to the editor's container.
266
- *
267
- * @example
268
- * ```
269
- * // Set the editor's height to 500px
270
- * editor.getRootElement().style.height = '500px';
271
- * ```
272
- */
273
- public getRootElement(): HTMLElement {
274
- return this.container;
275
- }
276
-
277
- /** @param fractionLoaded - should be a number from 0 to 1, where 1 represents completely loaded. */
278
- public showLoadingWarning(fractionLoaded: number) {
279
- const loadingPercent = Math.round(fractionLoaded * 100);
280
- this.loadingWarning.innerText = this.localization.loading(loadingPercent);
281
- this.loadingWarning.style.display = 'block';
282
- }
283
-
284
- public hideLoadingWarning() {
285
- this.loadingWarning.style.display = 'none';
286
-
287
- this.announceForAccessibility(this.localization.doneLoading);
288
- }
289
-
290
- private previousAccessibilityAnnouncement: string = '';
291
-
292
- /**
293
- * Announce `message` for screen readers. If `message` is the same as the previous
294
- * message, it is re-announced.
295
- */
296
- public announceForAccessibility(message: string) {
297
- // Force re-announcing an announcement if announced again.
298
- if (message === this.previousAccessibilityAnnouncement) {
299
- message = message + '. ';
300
- }
301
- this.accessibilityAnnounceArea.innerText = message;
302
- this.previousAccessibilityAnnouncement = message;
303
- }
304
-
305
- /**
306
- * Creates a toolbar. If `defaultLayout` is true, default buttons are used.
307
- * @returns a reference to the toolbar.
308
- */
309
- public addToolbar(defaultLayout: boolean = true): HTMLToolbar {
310
- const toolbar = new HTMLToolbar(this, this.container, this.localization);
311
-
312
- if (defaultLayout) {
313
- toolbar.addDefaults();
314
- }
315
-
316
- return toolbar;
317
- }
318
-
319
- private registerListeners() {
320
- this.handlePointerEventsFrom(this.renderingRegion);
321
- this.handleKeyEventsFrom(this.renderingRegion);
322
-
323
- this.container.addEventListener('wheel', evt => {
324
- let delta = Vec3.of(evt.deltaX, evt.deltaY, evt.deltaZ);
325
-
326
- // Process wheel events if the ctrl key is down, even if disabled -- we do want to handle
327
- // pinch-zooming.
328
- if (!evt.ctrlKey && !evt.metaKey) {
329
- if (!this.settings.wheelEventsEnabled) {
330
- return;
331
- } else if (this.settings.wheelEventsEnabled === 'only-if-focused') {
332
- const focusedChild = this.container.querySelector(':focus');
333
-
334
- if (!focusedChild) {
335
- return;
336
- }
337
- }
338
- }
339
-
340
- if (evt.deltaMode === WheelEvent.DOM_DELTA_LINE) {
341
- delta = delta.times(15);
342
- } else if (evt.deltaMode === WheelEvent.DOM_DELTA_PAGE) {
343
- delta = delta.times(100);
344
- }
345
-
346
- if (evt.ctrlKey || evt.metaKey) {
347
- delta = Vec3.of(0, 0, evt.deltaY);
348
- }
349
-
350
- // Ensure that `pos` is relative to `this.container`
351
- const bbox = this.container.getBoundingClientRect();
352
- const pos = Vec2.of(evt.clientX, evt.clientY).minus(Vec2.of(bbox.left, bbox.top));
353
-
354
- if (this.toolController.dispatchInputEvent({
355
- kind: InputEvtType.WheelEvt,
356
- delta,
357
- screenPos: pos,
358
- })) {
359
- evt.preventDefault();
360
- return true;
361
- }
362
- return false;
363
- });
364
-
365
- this.notifier.on(EditorEventType.DisplayResized, _event => {
366
- this.viewport.updateScreenSize(
367
- Vec2.of(this.display.width, this.display.height)
368
- );
369
- this.queueRerender();
370
- });
371
-
372
- const handleResize = () => {
373
- this.notifier.dispatch(EditorEventType.DisplayResized, {
374
- kind: EditorEventType.DisplayResized,
375
- newSize: Vec2.of(
376
- this.display.width,
377
- this.display.height
378
- ),
379
- });
380
- };
381
-
382
- if ('ResizeObserver' in (window as any)) {
383
- const resizeObserver = new ResizeObserver(handleResize);
384
- resizeObserver.observe(this.container);
385
- } else {
386
- addEventListener('resize', handleResize);
387
- }
388
-
389
- this.accessibilityControlArea.addEventListener('input', () => {
390
- this.accessibilityControlArea.value = '';
391
- });
392
-
393
- document.addEventListener('copy', evt => {
394
- if (!this.isEventSink(document.querySelector(':focus'))) {
395
- return;
396
- }
397
-
398
- const clipboardData = evt.clipboardData;
399
-
400
- if (this.toolController.dispatchInputEvent({
401
- kind: InputEvtType.CopyEvent,
402
- setData: (mime, data) => {
403
- clipboardData?.setData(mime, data);
404
- },
405
- })) {
406
- evt.preventDefault();
407
- }
408
- });
409
-
410
- document.addEventListener('paste', evt => {
411
- this.handlePaste(evt);
412
- });
413
- }
414
-
415
- private pointers: Record<number, Pointer> = {};
416
- private getPointerList() {
417
- const nowTime = (new Date()).getTime();
418
-
419
- const res: Pointer[] = [];
420
- for (const id in this.pointers) {
421
- const maxUnupdatedTime = 2000; // Maximum time without a pointer update (ms)
422
- if (this.pointers[id] && (nowTime - this.pointers[id].timeStamp) < maxUnupdatedTime) {
423
- res.push(this.pointers[id]);
424
- }
425
- }
426
- return res;
427
- }
428
-
429
- /**
430
- * Dispatches a `PointerEvent` to the editor. The target element for `evt` must have the same top left
431
- * as the content of the editor.
432
- */
433
- public handleHTMLPointerEvent(eventType: 'pointerdown'|'pointermove'|'pointerup'|'pointercancel', evt: PointerEvent): boolean {
434
- const eventsRelativeTo = this.renderingRegion;
435
- const eventTarget = (evt.target as HTMLElement|null) ?? this.renderingRegion;
436
-
437
- if (eventType === 'pointerdown') {
438
- const pointer = Pointer.ofEvent(evt, true, this.viewport, eventsRelativeTo);
439
- this.pointers[pointer.id] = pointer;
440
-
441
- eventTarget.setPointerCapture(pointer.id);
442
- const event: PointerEvt = {
443
- kind: InputEvtType.PointerDownEvt,
444
- current: pointer,
445
- allPointers: this.getPointerList(),
446
- };
447
- this.toolController.dispatchInputEvent(event);
448
-
449
- return true;
450
- }
451
- else if (eventType === 'pointermove') {
452
- const pointer = Pointer.ofEvent(
453
- evt, this.pointers[evt.pointerId]?.down ?? false, this.viewport, eventsRelativeTo
454
- );
455
- if (pointer.down) {
456
- const prevData = this.pointers[pointer.id];
457
-
458
- if (prevData) {
459
- const distanceMoved = pointer.screenPos.minus(prevData.screenPos).magnitude();
460
-
461
- // If the pointer moved less than two pixels, don't send a new event.
462
- if (distanceMoved < 2) {
463
- return false;
464
- }
465
- }
466
-
467
- this.pointers[pointer.id] = pointer;
468
- if (this.toolController.dispatchInputEvent({
469
- kind: InputEvtType.PointerMoveEvt,
470
- current: pointer,
471
- allPointers: this.getPointerList(),
472
- })) {
473
- evt.preventDefault();
474
- }
475
- }
476
- return true;
477
- }
478
- else if (eventType === 'pointercancel' || eventType === 'pointerup') {
479
- const pointer = Pointer.ofEvent(evt, false, this.viewport, eventsRelativeTo);
480
- if (!this.pointers[pointer.id]) {
481
- return false;
482
- }
483
-
484
- this.pointers[pointer.id] = pointer;
485
- eventTarget.releasePointerCapture(pointer.id);
486
- if (this.toolController.dispatchInputEvent({
487
- kind: InputEvtType.PointerUpEvt,
488
- current: pointer,
489
- allPointers: this.getPointerList(),
490
- })) {
491
- evt.preventDefault();
492
- }
493
- delete this.pointers[pointer.id];
494
- return true;
495
- }
496
-
497
- return eventType;
498
- }
499
-
500
- private isEventSink(evtTarget: Element|EventTarget|null) {
501
- let currentElem: Element|null = evtTarget as Element|null;
502
- while (currentElem !== null) {
503
- for (const elem of this.eventListenerTargets) {
504
- if (elem === currentElem) {
505
- return true;
506
- }
507
- }
508
-
509
- currentElem = (currentElem as Element).parentElement;
510
- }
511
- return false;
512
- }
513
-
514
- private async handlePaste(evt: DragEvent|ClipboardEvent) {
515
- const target = document.querySelector(':focus') ?? evt.target;
516
- if (!this.isEventSink(target)) {
517
- return;
518
- }
519
-
520
- const clipboardData: DataTransfer = (evt as any).dataTransfer ?? (evt as any).clipboardData;
521
- if (!clipboardData) {
522
- return;
523
- }
524
-
525
- // Handle SVG files (prefer to PNG/JPEG)
526
- for (const file of clipboardData.files) {
527
- if (file.type.toLowerCase() === 'image/svg+xml') {
528
- const text = await file.text();
529
- if (this.toolController.dispatchInputEvent({
530
- kind: InputEvtType.PasteEvent,
531
- mime: file.type,
532
- data: text,
533
- })) {
534
- evt.preventDefault();
535
- return;
536
- }
537
- }
538
- }
539
-
540
- // Handle image files.
541
- for (const file of clipboardData.files) {
542
- const fileType = file.type.toLowerCase();
543
- if (fileType === 'image/png' || fileType === 'image/jpg') {
544
- this.showLoadingWarning(0);
545
- const onprogress = (evt: ProgressEvent<FileReader>) => {
546
- this.showLoadingWarning(evt.loaded / evt.total);
547
- };
548
-
549
- try {
550
- const data = await fileToBase64(file, onprogress);
551
-
552
- if (data && this.toolController.dispatchInputEvent({
553
- kind: InputEvtType.PasteEvent,
554
- mime: fileType,
555
- data: data,
556
- })) {
557
- evt.preventDefault();
558
- this.hideLoadingWarning();
559
- return;
560
- }
561
- } catch (e) {
562
- console.error('Error reading image:', e);
563
- }
564
- this.hideLoadingWarning();
565
- }
566
- }
567
-
568
- // Supported MIMEs for text data, in order of preference
569
- const supportedMIMEs = [
570
- 'image/svg+xml',
571
- 'text/plain',
572
- ];
573
-
574
- for (const mime of supportedMIMEs) {
575
- const data = clipboardData.getData(mime);
576
-
577
- if (data && this.toolController.dispatchInputEvent({
578
- kind: InputEvtType.PasteEvent,
579
- mime,
580
- data,
581
- })) {
582
- evt.preventDefault();
583
- return;
584
- }
585
- }
586
- }
587
-
588
- /**
589
- * Forward pointer events from `elem` to this editor. Such that right-click/right-click drag
590
- * events are also forwarded, `elem`'s contextmenu is disabled.
591
- *
592
- * @example
593
- * ```ts
594
- * const overlay = document.createElement('div');
595
- * editor.createHTMLOverlay(overlay);
596
- *
597
- * // Send all pointer events that don't have the control key pressed
598
- * // to the editor.
599
- * editor.handlePointerEventsFrom(overlay, (event) => {
600
- * if (event.ctrlKey) {
601
- * return false;
602
- * }
603
- * return true;
604
- * });
605
- * ```
606
- */
607
- public handlePointerEventsFrom(elem: HTMLElement, filter?: HTMLPointerEventFilter) {
608
- // May be required to prevent text selection on iOS/Safari:
609
- // See https://stackoverflow.com/a/70992717/17055750
610
- elem.addEventListener('touchstart', evt => evt.preventDefault());
611
- elem.addEventListener('contextmenu', evt => {
612
- // Don't show a context menu
613
- evt.preventDefault();
614
- });
615
-
616
- const eventNames: HTMLPointerEventType[] = ['pointerdown', 'pointermove', 'pointerup', 'pointercancel'];
617
- for (const eventName of eventNames) {
618
- elem.addEventListener(eventName, evt => {
619
- if (filter && !filter(eventName, evt)) {
620
- return true;
621
- }
622
-
623
- return this.handleHTMLPointerEvent(eventName, evt);
624
- });
625
- }
626
- }
627
-
628
- /** Adds event listners for keypresses to `elem` and forwards those events to the editor. */
629
- public handleKeyEventsFrom(elem: HTMLElement) {
630
- elem.addEventListener('keydown', evt => {
631
- if (evt.key === 't' || evt.key === 'T') {
632
- evt.preventDefault();
633
- this.display.rerenderAsText();
634
- } else if (this.toolController.dispatchInputEvent({
635
- kind: InputEvtType.KeyPressEvent,
636
- key: evt.key,
637
- ctrlKey: evt.ctrlKey || evt.metaKey,
638
- altKey: evt.altKey,
639
- })) {
640
- evt.preventDefault();
641
- } else if (evt.key === 'Escape') {
642
- this.renderingRegion.blur();
643
- }
644
- });
645
-
646
- elem.addEventListener('keyup', evt => {
647
- if (this.toolController.dispatchInputEvent({
648
- kind: InputEvtType.KeyUpEvent,
649
- key: evt.key,
650
- ctrlKey: evt.ctrlKey || evt.metaKey,
651
- altKey: evt.altKey,
652
- })) {
653
- evt.preventDefault();
654
- }
655
- });
656
-
657
- // Allow drop.
658
- elem.ondragover = evt => {
659
- evt.preventDefault();
660
- };
661
-
662
- elem.ondrop = evt => {
663
- evt.preventDefault();
664
- this.handlePaste(evt);
665
- };
666
-
667
- this.eventListenerTargets.push(elem);
668
- }
669
-
670
- /** `apply` a command. `command` will be announced for accessibility. */
671
- public dispatch(command: Command, addToHistory: boolean = true) {
672
- const dispatchResult = this.dispatchNoAnnounce(command, addToHistory);
673
- this.announceForAccessibility(command.description(this, this.localization));
674
-
675
- return dispatchResult;
676
- }
677
-
678
- /**
679
- * Dispatches a command without announcing it. By default, does not add to history.
680
- * Use this to show finalized commands that don't need to have `announceForAccessibility`
681
- * called.
682
- *
683
- * Prefer `command.apply(editor)` for incomplete commands. `dispatchNoAnnounce` may allow
684
- * clients to listen for the application of commands (e.g. `SerializableCommand`s so they can
685
- * be sent across the network), while `apply` does not.
686
- *
687
- * @example
688
- * ```
689
- * const addToHistory = false;
690
- * editor.dispatchNoAnnounce(editor.viewport.zoomTo(someRectangle), addToHistory);
691
- * ```
692
- */
693
- public dispatchNoAnnounce(command: Command, addToHistory: boolean = false) {
694
- const result = command.apply(this);
695
-
696
- if (addToHistory) {
697
- const apply = false; // Don't double-apply
698
- this.history.push(command, apply);
699
- }
700
-
701
- return result;
702
- }
703
-
704
- /**
705
- * Apply a large transformation in chunks.
706
- * If `apply` is `false`, the commands are unapplied.
707
- * Triggers a re-render after each `updateChunkSize`-sized group of commands
708
- * has been applied.
709
- */
710
- public async asyncApplyOrUnapplyCommands(
711
- commands: Command[], apply: boolean, updateChunkSize: number
712
- ) {
713
- console.assert(updateChunkSize > 0);
714
- this.display.setDraftMode(true);
715
- for (let i = 0; i < commands.length; i += updateChunkSize) {
716
- this.showLoadingWarning(i / commands.length);
717
-
718
- for (let j = i; j < commands.length && j < i + updateChunkSize; j++) {
719
- const cmd = commands[j];
720
-
721
- if (apply) {
722
- cmd.apply(this);
723
- } else {
724
- cmd.unapply(this);
725
- }
726
- }
727
-
728
- // Re-render to show progress, but only if we're not done.
729
- if (i + updateChunkSize < commands.length) {
730
- await new Promise(resolve => {
731
- this.rerender();
732
- requestAnimationFrame(resolve);
733
- });
734
- }
735
- }
736
- this.display.setDraftMode(false);
737
- this.hideLoadingWarning();
738
- }
739
-
740
- // @see {@link #asyncApplyOrUnapplyCommands }
741
- public asyncApplyCommands(commands: Command[], chunkSize: number) {
742
- return this.asyncApplyOrUnapplyCommands(commands, true, chunkSize);
743
- }
744
-
745
- // If `unapplyInReverseOrder`, commands are reversed before unapplying.
746
- // @see {@link #asyncApplyOrUnapplyCommands }
747
- public asyncUnapplyCommands(commands: Command[], chunkSize: number, unapplyInReverseOrder: boolean = false) {
748
- if (unapplyInReverseOrder) {
749
- commands = [ ...commands ]; // copy
750
- commands.reverse();
751
- }
752
-
753
- return this.asyncApplyOrUnapplyCommands(commands, false, chunkSize);
754
- }
755
-
756
- private announceUndoCallback = (command: Command) => {
757
- this.announceForAccessibility(this.localization.undoAnnouncement(command.description(this, this.localization)));
758
- };
759
-
760
- private announceRedoCallback = (command: Command) => {
761
- this.announceForAccessibility(this.localization.redoAnnouncement(command.description(this, this.localization)));
762
- };
763
-
764
- // Listeners to be called once at the end of the next re-render.
765
- private nextRerenderListeners: Array<()=> void> = [];
766
- private rerenderQueued: boolean = false;
767
-
768
- /**
769
- * Schedule a re-render for some time in the near future. Does not schedule an additional
770
- * re-render if a re-render is already queued.
771
- *
772
- * @returns a promise that resolves when re-rendering has completed.
773
- */
774
- public queueRerender(): Promise<void> {
775
- if (!this.rerenderQueued) {
776
- this.rerenderQueued = true;
777
- requestAnimationFrame(() => {
778
- // If .rerender was called manually, we might not need to
779
- // re-render.
780
- if (this.rerenderQueued) {
781
- this.rerender();
782
- this.rerenderQueued = false;
783
- }
784
- });
785
- }
786
-
787
- return new Promise(resolve => {
788
- this.nextRerenderListeners.push(() => resolve());
789
- });
790
- }
791
-
792
- // @internal
793
- public isRerenderQueued() {
794
- return this.rerenderQueued;
795
- }
796
-
797
- /**
798
- * Re-renders the entire image.
799
- *
800
- * @see {@link Editor.queueRerender}
801
- */
802
- public rerender(showImageBounds: boolean = true) {
803
- this.display.startRerender();
804
-
805
- // Don't render if the display has zero size.
806
- if (this.display.width === 0 || this.display.height === 0) {
807
- return;
808
- }
809
-
810
- // Draw a rectangle around the region that will be visible on save
811
- const renderer = this.display.getDryInkRenderer();
812
-
813
- this.image.renderWithCache(renderer, this.display.getCache(), this.viewport);
814
-
815
- if (showImageBounds) {
816
- const exportRectFill = { fill: Color4.fromHex('#44444455') };
817
- const exportRectStrokeWidth = 5 * this.viewport.getSizeOfPixelOnCanvas();
818
- renderer.drawRect(
819
- this.getImportExportRect(),
820
- exportRectStrokeWidth,
821
- exportRectFill
822
- );
823
- }
824
-
825
- this.rerenderQueued = false;
826
- this.nextRerenderListeners.forEach(listener => listener());
827
- this.nextRerenderListeners = [];
828
- }
829
-
830
- /**
831
- * Draws the given path onto the wet ink renderer. The given path will
832
- * be displayed on top of the main image.
833
- *
834
- * @see {@link Display.getWetInkRenderer} {@link Display.flatten}
835
- */
836
- public drawWetInk(...path: RenderablePathSpec[]) {
837
- for (const part of path) {
838
- this.display.getWetInkRenderer().drawPath(part);
839
- }
840
- }
841
-
842
- /**
843
- * Clears the wet ink display.
844
- *
845
- * @see {@link Display.getWetInkRenderer}
846
- */
847
- public clearWetInk() {
848
- this.display.getWetInkRenderer().clear();
849
- }
850
-
851
- /**
852
- * Focuses the region used for text input/key commands.
853
- */
854
- public focus() {
855
- this.renderingRegion.focus();
856
- }
857
-
858
- /**
859
- * Creates an element that will be positioned on top of the dry/wet ink
860
- * renderers.
861
- *
862
- * This is useful for displaying content on top of the rendered content
863
- * (e.g. a selection box).
864
- */
865
- public createHTMLOverlay(overlay: HTMLElement) {
866
- overlay.classList.add('overlay');
867
- this.container.appendChild(overlay);
868
-
869
- return {
870
- remove: () => overlay.remove(),
871
- };
872
- }
873
-
874
- public addStyleSheet(content: string): HTMLStyleElement {
875
- const styleSheet = document.createElement('style');
876
- styleSheet.innerText = content;
877
- this.container.appendChild(styleSheet);
878
-
879
- return styleSheet;
880
- }
881
-
882
- // Dispatch a keyboard event to the currently selected tool.
883
- // Intended for unit testing
884
- public sendKeyboardEvent(
885
- eventType: InputEvtType.KeyPressEvent|InputEvtType.KeyUpEvent,
886
- key: string,
887
- ctrlKey: boolean = false,
888
- altKey: boolean = false,
889
- ) {
890
- this.toolController.dispatchInputEvent({
891
- kind: eventType,
892
- key,
893
- ctrlKey,
894
- altKey,
895
- });
896
- }
897
-
898
- /**
899
- * Dispatch a pen event to the currently selected tool.
900
- * Intended primarially for unit tests.
901
- *
902
- * @deprecated
903
- * @see {@link sendPenEvent} {@link sendTouchEvent}
904
- */
905
- public sendPenEvent(
906
- eventType: InputEvtType.PointerDownEvt|InputEvtType.PointerMoveEvt|InputEvtType.PointerUpEvt,
907
- point: Point2,
908
-
909
- // @deprecated
910
- allPointers?: Pointer[]
911
- ) {
912
- sendPenEvent(this, eventType, point, allPointers);
913
- }
914
-
915
- public async addAndCenterComponents(components: AbstractComponent[], selectComponents: boolean = true) {
916
- let bbox: Rect2|null = null;
917
- for (const component of components) {
918
- if (bbox) {
919
- bbox = bbox.union(component.getBBox());
920
- } else {
921
- bbox = component.getBBox();
922
- }
923
- }
924
-
925
- if (!bbox) {
926
- return;
927
- }
928
-
929
- // Find a transform that scales/moves bbox onto the screen.
930
- const visibleRect = this.viewport.visibleRect;
931
- const scaleRatioX = visibleRect.width / bbox.width;
932
- const scaleRatioY = visibleRect.height / bbox.height;
933
-
934
- let scaleRatio = scaleRatioX;
935
- if (bbox.width * scaleRatio > visibleRect.width || bbox.height * scaleRatio > visibleRect.height) {
936
- scaleRatio = scaleRatioY;
937
- }
938
- scaleRatio *= 2 / 3;
939
-
940
- scaleRatio = Viewport.roundScaleRatio(scaleRatio);
941
-
942
- const transfm = Mat33.translation(
943
- visibleRect.center.minus(bbox.center)
944
- ).rightMul(
945
- Mat33.scaling2D(scaleRatio, bbox.center)
946
- );
947
-
948
- const commands: Command[] = [];
949
- for (const component of components) {
950
- // To allow deserialization, we need to add first, then transform.
951
- commands.push(EditorImage.addElement(component));
952
- commands.push(component.transformBy(transfm));
953
- }
954
-
955
- const applyChunkSize = 100;
956
- await this.dispatch(uniteCommands(commands, applyChunkSize), true);
957
-
958
- if (selectComponents) {
959
- for (const selectionTool of this.toolController.getMatchingTools(SelectionTool)) {
960
- selectionTool.setEnabled(true);
961
- selectionTool.setSelection(components);
962
- }
963
- }
964
- }
965
-
966
- // Get a data URL (e.g. as produced by `HTMLCanvasElement::toDataURL`).
967
- // If `format` is not `image/png`, a PNG image URL may still be returned (as in the
968
- // case of `HTMLCanvasElement::toDataURL`).
969
- //
970
- // The export resolution is the same as the size of the drawing canvas.
971
- public toDataURL(format: 'image/png'|'image/jpeg'|'image/webp' = 'image/png'): string {
972
- const canvas = document.createElement('canvas');
973
-
974
- const importExportViewport = this.image.getImportExportViewport();
975
- const resolution = importExportViewport.getScreenRectSize();
976
-
977
- canvas.width = resolution.x;
978
- canvas.height = resolution.y;
979
-
980
- const ctx = canvas.getContext('2d')!;
981
- const renderer = new CanvasRenderer(ctx, importExportViewport);
982
-
983
- this.image.renderAll(renderer);
984
-
985
- const dataURL = canvas.toDataURL(format);
986
- return dataURL;
987
- }
988
-
989
- public toSVG(): SVGElement {
990
- const importExportViewport = this.image.getImportExportViewport().getTemporaryClone();
991
-
992
- const sanitize = false;
993
- const { element: result, renderer } = SVGRenderer.fromViewport(importExportViewport, sanitize);
994
-
995
- const origTransform = importExportViewport.canvasToScreenTransform;
996
- // Render with (0,0) at (0,0) — we'll handle translation with
997
- // the viewBox property.
998
- importExportViewport.resetTransform(Mat33.identity);
999
-
1000
- this.image.renderAll(renderer);
1001
-
1002
- importExportViewport.resetTransform(origTransform);
1003
-
1004
-
1005
- // Just show the main region
1006
- const rect = importExportViewport.visibleRect;
1007
- result.setAttribute('viewBox', [rect.x, rect.y, rect.w, rect.h].map(part => toRoundedString(part)).join(' '));
1008
- result.setAttribute('width', toRoundedString(rect.w));
1009
- result.setAttribute('height', toRoundedString(rect.h));
1010
-
1011
- return result;
1012
- }
1013
-
1014
- /**
1015
- * Load editor data from an `ImageLoader` (e.g. an {@link SVGLoader}).
1016
- *
1017
- * @see loadFromSVG
1018
- */
1019
- public async loadFrom(loader: ImageLoader) {
1020
- this.showLoadingWarning(0);
1021
- this.display.setDraftMode(true);
1022
-
1023
- const originalBackgrounds = this.image.getBackgroundComponents();
1024
- const eraseBackgroundCommand = new Erase(originalBackgrounds);
1025
-
1026
- await loader.start(async (component) => {
1027
- await this.dispatchNoAnnounce(EditorImage.addElement(component));
1028
- }, (countProcessed: number, totalToProcess: number) => {
1029
- if (countProcessed % 500 === 0) {
1030
- this.showLoadingWarning(countProcessed / totalToProcess);
1031
- this.rerender();
1032
- return untilNextAnimationFrame();
1033
- }
1034
-
1035
- return null;
1036
- }, (importExportRect: Rect2) => {
1037
- this.dispatchNoAnnounce(this.setImportExportRect(importExportRect), false);
1038
- this.dispatchNoAnnounce(this.viewport.zoomTo(importExportRect), false);
1039
- });
1040
-
1041
- // Ensure that we don't have multiple overlapping BackgroundComponents. Remove
1042
- // old BackgroundComponents.
1043
- // Overlapping BackgroundComponents may cause changing the background color to
1044
- // not work properly.
1045
- if (this.image.getBackgroundComponents().length !== originalBackgrounds.length) {
1046
- await this.dispatchNoAnnounce(eraseBackgroundCommand);
1047
- }
1048
-
1049
- this.hideLoadingWarning();
1050
-
1051
- this.display.setDraftMode(false);
1052
- this.queueRerender();
1053
- }
1054
-
1055
- private getTopmostBackgroundComponent(): ImageBackground|null {
1056
- let background: ImageBackground|null = null;
1057
-
1058
- // Find a background component, if one exists.
1059
- // Use the last (topmost) background component if there are multiple.
1060
- for (const component of this.image.getBackgroundComponents()) {
1061
- if (component instanceof ImageBackground) {
1062
- background = component;
1063
- }
1064
- }
1065
-
1066
- return background;
1067
- }
1068
-
1069
- /**
1070
- * Set the background color of the image.
1071
- */
1072
- public setBackgroundColor(color: Color4): Command {
1073
- let background = this.getTopmostBackgroundComponent();
1074
-
1075
- if (!background) {
1076
- const backgroundType = color.eq(Color4.transparent) ? BackgroundType.None : BackgroundType.SolidColor;
1077
- background = new ImageBackground(backgroundType, color);
1078
- return this.image.addElement(background);
1079
- } else {
1080
- return background.updateStyle({ color });
1081
- }
1082
- }
1083
-
1084
- /**
1085
- * @returns the average of the colors of all background components. Use this to get the current background
1086
- * color.
1087
- */
1088
- public estimateBackgroundColor(): Color4 {
1089
- const backgroundColors = [];
1090
-
1091
- for (const component of this.image.getBackgroundComponents()) {
1092
- if (component instanceof ImageBackground) {
1093
- backgroundColors.push(component.getStyle().color ?? Color4.transparent);
1094
- }
1095
- }
1096
-
1097
- return Color4.average(backgroundColors);
1098
- }
1099
-
1100
- // Returns the size of the visible region of the output SVG
1101
- public getImportExportRect(): Rect2 {
1102
- return this.image.getImportExportViewport().visibleRect;
1103
- }
1104
-
1105
- // Resize the output SVG to match `imageRect`.
1106
- public setImportExportRect(imageRect: Rect2): Command {
1107
- return this.image.setImportExportRect(imageRect);
1108
- }
1109
-
1110
- /**
1111
- * Alias for loadFrom(SVGLoader.fromString).
1112
- *
1113
- * This is particularly useful when accessing a bundled version of the editor,
1114
- * where `SVGLoader.fromString` is unavailable.
1115
- */
1116
- public async loadFromSVG(svgData: string, sanitize: boolean = false) {
1117
- const loader = SVGLoader.fromString(svgData, sanitize);
1118
- await this.loadFrom(loader);
1119
- }
1120
- }
1121
-
1122
- export default Editor;