js-draw 0.18.1 → 0.19.0

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