js-draw 0.18.2 → 0.20.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 (269) hide show
  1. package/.eslintrc.js +1 -0
  2. package/CHANGELOG.md +10 -0
  3. package/dist/bundle.js +2 -2
  4. package/dist/bundledStyles.js +1 -0
  5. package/dist/cjs/src/Color4.d.ts +8 -0
  6. package/dist/cjs/src/Color4.js +67 -0
  7. package/dist/cjs/src/Editor.d.ts +2 -2
  8. package/dist/cjs/src/Editor.js +7 -7
  9. package/dist/cjs/src/SVGLoader.js +77 -12
  10. package/dist/cjs/src/Viewport.d.ts +2 -0
  11. package/dist/cjs/src/Viewport.js +6 -2
  12. package/dist/cjs/src/components/AbstractComponent.d.ts +2 -2
  13. package/dist/cjs/src/components/AbstractComponent.js +3 -3
  14. package/dist/cjs/src/components/{ImageBackground.d.ts → BackgroundComponent.d.ts} +23 -3
  15. package/dist/cjs/src/components/BackgroundComponent.js +309 -0
  16. package/dist/cjs/src/components/RestylableComponent.d.ts +21 -2
  17. package/dist/cjs/src/components/Stroke.d.ts +35 -0
  18. package/dist/cjs/src/components/Stroke.js +37 -3
  19. package/dist/cjs/src/components/TextComponent.d.ts +27 -17
  20. package/dist/cjs/src/components/TextComponent.js +23 -1
  21. package/dist/cjs/src/components/lib.d.ts +4 -3
  22. package/dist/cjs/src/components/lib.js +2 -2
  23. package/dist/cjs/src/components/util/StrokeSmoother.js +25 -15
  24. package/dist/cjs/src/lib.d.ts +30 -0
  25. package/dist/cjs/src/lib.js +30 -0
  26. package/dist/cjs/src/localizations/de.js +1 -1
  27. package/dist/cjs/src/localizations/es.js +1 -1
  28. package/dist/cjs/src/math/Path.js +1 -1
  29. package/dist/cjs/src/math/polynomial/QuadraticBezier.d.ts +28 -0
  30. package/dist/cjs/src/math/polynomial/QuadraticBezier.js +115 -0
  31. package/dist/cjs/src/math/polynomial/solveQuadratic.d.ts +6 -0
  32. package/dist/cjs/src/math/polynomial/solveQuadratic.js +36 -0
  33. package/dist/cjs/src/rendering/RenderingStyle.d.ts +4 -4
  34. package/dist/cjs/src/rendering/TextRenderingStyle.d.ts +10 -10
  35. package/dist/cjs/src/rendering/lib.d.ts +2 -0
  36. package/dist/cjs/src/rendering/renderers/AbstractRenderer.d.ts +2 -2
  37. package/dist/cjs/src/rendering/renderers/CanvasRenderer.d.ts +2 -2
  38. package/dist/cjs/src/rendering/renderers/CanvasRenderer.js +5 -3
  39. package/dist/cjs/src/rendering/renderers/DummyRenderer.d.ts +2 -2
  40. package/dist/cjs/src/rendering/renderers/SVGRenderer.d.ts +2 -2
  41. package/dist/cjs/src/rendering/renderers/SVGRenderer.js +15 -6
  42. package/dist/cjs/src/rendering/renderers/TextOnlyRenderer.d.ts +2 -2
  43. package/dist/cjs/src/toolbar/IconProvider.d.ts +2 -2
  44. package/dist/cjs/src/toolbar/localization.d.ts +2 -1
  45. package/dist/cjs/src/toolbar/localization.js +3 -2
  46. package/dist/cjs/src/toolbar/widgets/BaseWidget.js +1 -1
  47. package/dist/cjs/src/toolbar/widgets/DocumentPropertiesWidget.d.ts +5 -0
  48. package/dist/cjs/src/toolbar/widgets/DocumentPropertiesWidget.js +77 -2
  49. package/dist/cjs/src/toolbar/widgets/PenToolWidget.js +1 -1
  50. package/dist/cjs/src/tools/FindTool.js +1 -1
  51. package/dist/cjs/src/tools/SoundUITool.d.ts +24 -0
  52. package/dist/cjs/src/tools/SoundUITool.js +164 -0
  53. package/dist/cjs/src/tools/TextTool.d.ts +2 -2
  54. package/dist/cjs/src/tools/ToolController.js +6 -1
  55. package/dist/cjs/src/tools/lib.d.ts +1 -0
  56. package/dist/cjs/src/tools/lib.js +3 -1
  57. package/dist/cjs/src/tools/localization.d.ts +3 -0
  58. package/dist/cjs/src/tools/localization.js +3 -0
  59. package/dist/mjs/src/Color4.d.ts +8 -0
  60. package/dist/mjs/src/Color4.mjs +64 -0
  61. package/dist/mjs/src/Editor.d.ts +2 -2
  62. package/dist/mjs/src/Editor.mjs +6 -6
  63. package/dist/mjs/src/SVGLoader.mjs +76 -11
  64. package/dist/mjs/src/Viewport.d.ts +2 -0
  65. package/dist/mjs/src/Viewport.mjs +6 -2
  66. package/dist/mjs/src/components/AbstractComponent.d.ts +2 -2
  67. package/dist/mjs/src/components/AbstractComponent.mjs +3 -3
  68. package/dist/mjs/src/components/{ImageBackground.d.ts → BackgroundComponent.d.ts} +23 -3
  69. package/dist/mjs/src/components/BackgroundComponent.mjs +279 -0
  70. package/dist/mjs/src/components/RestylableComponent.d.ts +21 -2
  71. package/dist/mjs/src/components/Stroke.d.ts +35 -0
  72. package/dist/mjs/src/components/Stroke.mjs +37 -3
  73. package/dist/mjs/src/components/TextComponent.d.ts +27 -17
  74. package/dist/mjs/src/components/TextComponent.mjs +23 -1
  75. package/dist/mjs/src/components/lib.d.ts +4 -3
  76. package/dist/mjs/src/components/lib.mjs +2 -2
  77. package/dist/mjs/src/components/util/StrokeSmoother.mjs +25 -15
  78. package/dist/mjs/src/lib.d.ts +30 -0
  79. package/dist/mjs/src/lib.mjs +30 -0
  80. package/dist/mjs/src/localizations/de.mjs +1 -1
  81. package/dist/mjs/src/localizations/es.mjs +1 -1
  82. package/dist/mjs/src/math/Path.mjs +1 -1
  83. package/dist/mjs/src/math/polynomial/QuadraticBezier.d.ts +28 -0
  84. package/dist/mjs/src/math/polynomial/QuadraticBezier.mjs +109 -0
  85. package/dist/mjs/src/math/polynomial/solveQuadratic.d.ts +6 -0
  86. package/dist/mjs/src/math/polynomial/solveQuadratic.mjs +34 -0
  87. package/dist/mjs/src/rendering/RenderingStyle.d.ts +4 -4
  88. package/dist/mjs/src/rendering/TextRenderingStyle.d.ts +10 -10
  89. package/dist/mjs/src/rendering/lib.d.ts +2 -0
  90. package/dist/mjs/src/rendering/renderers/AbstractRenderer.d.ts +2 -2
  91. package/dist/mjs/src/rendering/renderers/CanvasRenderer.d.ts +2 -2
  92. package/dist/mjs/src/rendering/renderers/CanvasRenderer.mjs +5 -3
  93. package/dist/mjs/src/rendering/renderers/DummyRenderer.d.ts +2 -2
  94. package/dist/mjs/src/rendering/renderers/SVGRenderer.d.ts +2 -2
  95. package/dist/mjs/src/rendering/renderers/SVGRenderer.mjs +15 -6
  96. package/dist/mjs/src/rendering/renderers/TextOnlyRenderer.d.ts +2 -2
  97. package/dist/mjs/src/toolbar/IconProvider.d.ts +2 -2
  98. package/dist/mjs/src/toolbar/localization.d.ts +2 -1
  99. package/dist/mjs/src/toolbar/localization.mjs +3 -2
  100. package/dist/mjs/src/toolbar/widgets/BaseWidget.mjs +1 -1
  101. package/dist/mjs/src/toolbar/widgets/DocumentPropertiesWidget.d.ts +5 -0
  102. package/dist/mjs/src/toolbar/widgets/DocumentPropertiesWidget.mjs +54 -2
  103. package/dist/mjs/src/toolbar/widgets/PenToolWidget.mjs +1 -1
  104. package/dist/mjs/src/tools/FindTool.mjs +1 -1
  105. package/dist/mjs/src/tools/SoundUITool.d.ts +24 -0
  106. package/dist/mjs/src/tools/SoundUITool.mjs +158 -0
  107. package/dist/mjs/src/tools/TextTool.d.ts +2 -2
  108. package/dist/mjs/src/tools/ToolController.mjs +6 -1
  109. package/dist/mjs/src/tools/lib.d.ts +1 -0
  110. package/dist/mjs/src/tools/lib.mjs +1 -0
  111. package/dist/mjs/src/tools/localization.d.ts +3 -0
  112. package/dist/mjs/src/tools/localization.mjs +3 -0
  113. package/jest.config.js +1 -1
  114. package/package.json +19 -17
  115. package/src/Editor.css +2 -2
  116. package/src/tools/SoundUITool.css +15 -0
  117. package/src/tools/tools.css +4 -0
  118. package/dist/cjs/src/components/ImageBackground.js +0 -146
  119. package/dist/mjs/src/components/ImageBackground.mjs +0 -139
  120. package/src/Color4.test.ts +0 -40
  121. package/src/Color4.ts +0 -236
  122. package/src/Editor.loadFrom.test.ts +0 -24
  123. package/src/Editor.toSVG.test.ts +0 -111
  124. package/src/Editor.ts +0 -1122
  125. package/src/EditorImage.test.ts +0 -120
  126. package/src/EditorImage.ts +0 -603
  127. package/src/EventDispatcher.test.ts +0 -123
  128. package/src/EventDispatcher.ts +0 -71
  129. package/src/Pointer.ts +0 -127
  130. package/src/SVGLoader.test.ts +0 -114
  131. package/src/SVGLoader.ts +0 -511
  132. package/src/UndoRedoHistory.test.ts +0 -33
  133. package/src/UndoRedoHistory.ts +0 -102
  134. package/src/Viewport.ts +0 -319
  135. package/src/bundle/bundled.ts +0 -7
  136. package/src/commands/Command.ts +0 -45
  137. package/src/commands/Duplicate.ts +0 -48
  138. package/src/commands/Erase.ts +0 -74
  139. package/src/commands/SerializableCommand.ts +0 -49
  140. package/src/commands/UnresolvedCommand.ts +0 -37
  141. package/src/commands/invertCommand.ts +0 -51
  142. package/src/commands/lib.ts +0 -16
  143. package/src/commands/localization.ts +0 -47
  144. package/src/commands/uniteCommands.test.ts +0 -23
  145. package/src/commands/uniteCommands.ts +0 -135
  146. package/src/components/AbstractComponent.transformBy.test.ts +0 -22
  147. package/src/components/AbstractComponent.ts +0 -364
  148. package/src/components/ImageBackground.test.ts +0 -35
  149. package/src/components/ImageBackground.ts +0 -176
  150. package/src/components/ImageComponent.ts +0 -171
  151. package/src/components/RestylableComponent.ts +0 -142
  152. package/src/components/SVGGlobalAttributesObject.ts +0 -81
  153. package/src/components/Stroke.test.ts +0 -139
  154. package/src/components/Stroke.ts +0 -245
  155. package/src/components/TextComponent.test.ts +0 -99
  156. package/src/components/TextComponent.ts +0 -315
  157. package/src/components/UnknownSVGObject.test.ts +0 -10
  158. package/src/components/UnknownSVGObject.ts +0 -60
  159. package/src/components/builders/ArrowBuilder.ts +0 -107
  160. package/src/components/builders/FreehandLineBuilder.ts +0 -212
  161. package/src/components/builders/LineBuilder.ts +0 -77
  162. package/src/components/builders/PressureSensitiveFreehandLineBuilder.ts +0 -454
  163. package/src/components/builders/RectangleBuilder.ts +0 -74
  164. package/src/components/builders/types.ts +0 -15
  165. package/src/components/lib.ts +0 -25
  166. package/src/components/localization.ts +0 -22
  167. package/src/components/util/StrokeSmoother.ts +0 -293
  168. package/src/components/util/describeComponentList.ts +0 -18
  169. package/src/lib.ts +0 -37
  170. package/src/localization.ts +0 -34
  171. package/src/localizations/de.ts +0 -98
  172. package/src/localizations/en.ts +0 -8
  173. package/src/localizations/es.ts +0 -74
  174. package/src/localizations/getLocalizationTable.test.ts +0 -27
  175. package/src/localizations/getLocalizationTable.ts +0 -55
  176. package/src/math/LineSegment2.test.ts +0 -99
  177. package/src/math/LineSegment2.ts +0 -160
  178. package/src/math/Mat33.test.ts +0 -244
  179. package/src/math/Mat33.ts +0 -437
  180. package/src/math/Path.fromString.test.ts +0 -223
  181. package/src/math/Path.test.ts +0 -198
  182. package/src/math/Path.toString.test.ts +0 -77
  183. package/src/math/Path.ts +0 -790
  184. package/src/math/Rect2.test.ts +0 -204
  185. package/src/math/Rect2.ts +0 -315
  186. package/src/math/Triangle.ts +0 -29
  187. package/src/math/Vec2.test.ts +0 -30
  188. package/src/math/Vec2.ts +0 -18
  189. package/src/math/Vec3.test.ts +0 -44
  190. package/src/math/Vec3.ts +0 -218
  191. package/src/math/lib.ts +0 -15
  192. package/src/math/rounding.test.ts +0 -65
  193. package/src/math/rounding.ts +0 -156
  194. package/src/rendering/Display.ts +0 -249
  195. package/src/rendering/RenderingStyle.test.ts +0 -68
  196. package/src/rendering/RenderingStyle.ts +0 -55
  197. package/src/rendering/TextRenderingStyle.ts +0 -45
  198. package/src/rendering/caching/CacheRecord.test.ts +0 -49
  199. package/src/rendering/caching/CacheRecord.ts +0 -77
  200. package/src/rendering/caching/CacheRecordManager.ts +0 -71
  201. package/src/rendering/caching/RenderingCache.test.ts +0 -44
  202. package/src/rendering/caching/RenderingCache.ts +0 -66
  203. package/src/rendering/caching/RenderingCacheNode.ts +0 -405
  204. package/src/rendering/caching/testUtils.ts +0 -35
  205. package/src/rendering/caching/types.ts +0 -34
  206. package/src/rendering/lib.ts +0 -6
  207. package/src/rendering/localization.ts +0 -20
  208. package/src/rendering/renderers/AbstractRenderer.ts +0 -222
  209. package/src/rendering/renderers/CanvasRenderer.ts +0 -296
  210. package/src/rendering/renderers/DummyRenderer.test.ts +0 -42
  211. package/src/rendering/renderers/DummyRenderer.ts +0 -136
  212. package/src/rendering/renderers/SVGRenderer.ts +0 -354
  213. package/src/rendering/renderers/TextOnlyRenderer.ts +0 -70
  214. package/src/testing/beforeEachFile.ts +0 -8
  215. package/src/testing/createEditor.ts +0 -11
  216. package/src/testing/global.d.ts +0 -17
  217. package/src/testing/lib.ts +0 -3
  218. package/src/testing/loadExpectExtensions.ts +0 -25
  219. package/src/testing/sendPenEvent.ts +0 -31
  220. package/src/testing/sendTouchEvent.ts +0 -78
  221. package/src/toolbar/HTMLToolbar.ts +0 -492
  222. package/src/toolbar/IconProvider.ts +0 -736
  223. package/src/toolbar/lib.ts +0 -4
  224. package/src/toolbar/localization.ts +0 -106
  225. package/src/toolbar/makeColorInput.ts +0 -145
  226. package/src/toolbar/types.ts +0 -5
  227. package/src/toolbar/widgets/ActionButtonWidget.ts +0 -39
  228. package/src/toolbar/widgets/BaseToolWidget.ts +0 -56
  229. package/src/toolbar/widgets/BaseWidget.ts +0 -377
  230. package/src/toolbar/widgets/DocumentPropertiesWidget.ts +0 -167
  231. package/src/toolbar/widgets/EraserToolWidget.ts +0 -85
  232. package/src/toolbar/widgets/HandToolWidget.ts +0 -250
  233. package/src/toolbar/widgets/InsertImageWidget.ts +0 -223
  234. package/src/toolbar/widgets/OverflowWidget.ts +0 -92
  235. package/src/toolbar/widgets/PenToolWidget.ts +0 -288
  236. package/src/toolbar/widgets/SelectionToolWidget.ts +0 -190
  237. package/src/toolbar/widgets/TextToolWidget.ts +0 -145
  238. package/src/toolbar/widgets/lib.ts +0 -13
  239. package/src/tools/BaseTool.ts +0 -76
  240. package/src/tools/Eraser.test.ts +0 -103
  241. package/src/tools/Eraser.ts +0 -139
  242. package/src/tools/FindTool.ts +0 -152
  243. package/src/tools/PanZoom.test.ts +0 -310
  244. package/src/tools/PanZoom.ts +0 -520
  245. package/src/tools/PasteHandler.ts +0 -95
  246. package/src/tools/Pen.test.ts +0 -194
  247. package/src/tools/Pen.ts +0 -226
  248. package/src/tools/PipetteTool.ts +0 -55
  249. package/src/tools/SelectionTool/SelectAllShortcutHandler.ts +0 -28
  250. package/src/tools/SelectionTool/Selection.ts +0 -607
  251. package/src/tools/SelectionTool/SelectionHandle.ts +0 -108
  252. package/src/tools/SelectionTool/SelectionTool.test.ts +0 -261
  253. package/src/tools/SelectionTool/SelectionTool.ts +0 -480
  254. package/src/tools/SelectionTool/TransformMode.ts +0 -114
  255. package/src/tools/SelectionTool/types.ts +0 -11
  256. package/src/tools/TextTool.ts +0 -326
  257. package/src/tools/ToolController.ts +0 -178
  258. package/src/tools/ToolEnabledGroup.ts +0 -14
  259. package/src/tools/ToolSwitcherShortcut.ts +0 -39
  260. package/src/tools/ToolbarShortcutHandler.ts +0 -34
  261. package/src/tools/UndoRedoShortcut.test.ts +0 -56
  262. package/src/tools/UndoRedoShortcut.ts +0 -25
  263. package/src/tools/lib.ts +0 -21
  264. package/src/tools/localization.ts +0 -66
  265. package/src/types.ts +0 -234
  266. package/src/util/assertions.ts +0 -55
  267. package/src/util/fileToBase64.ts +0 -18
  268. package/src/util/untilNextAnimationFrame.ts +0 -9
  269. 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;