js-draw 1.0.1 → 1.1.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 (197) hide show
  1. package/LICENSE +21 -0
  2. package/dist/Editor.css +1 -0
  3. package/dist/bundle.js +1 -1
  4. package/dist/bundledStyles.js +1 -1
  5. package/dist/cjs/toolbar/AbstractToolbar.d.ts +9 -13
  6. package/dist/cjs/toolbar/AbstractToolbar.js +14 -19
  7. package/dist/cjs/toolbar/widgets/SaveActionWidget.d.ts +10 -0
  8. package/dist/cjs/toolbar/widgets/SaveActionWidget.js +26 -0
  9. package/dist/cjs/toolbar/widgets/keybindings.d.ts +1 -0
  10. package/dist/cjs/toolbar/widgets/keybindings.js +4 -1
  11. package/dist/cjs/version.js +1 -1
  12. package/dist/mjs/toolbar/AbstractToolbar.d.ts +9 -13
  13. package/dist/mjs/toolbar/AbstractToolbar.mjs +14 -19
  14. package/dist/mjs/toolbar/widgets/SaveActionWidget.d.ts +10 -0
  15. package/dist/mjs/toolbar/widgets/SaveActionWidget.mjs +21 -0
  16. package/dist/mjs/toolbar/widgets/keybindings.d.ts +1 -0
  17. package/dist/mjs/toolbar/widgets/keybindings.mjs +3 -0
  18. package/dist/mjs/version.mjs +1 -1
  19. package/docs/img/readme-images/js-draw.jpg +0 -0
  20. package/docs/img/readme-images/unsupported-elements--in-editor.png +0 -0
  21. package/package.json +5 -4
  22. package/src/toolbar/EdgeToolbar.scss +1 -0
  23. package/dist-test/test_imports/package-lock.json +0 -13
  24. package/dist-test/test_imports/package.json +0 -12
  25. package/dist-test/test_imports/test-imports.js +0 -11
  26. package/dist-test/test_imports/test-require.cjs +0 -14
  27. package/src/Editor.loadFrom.test.ts +0 -24
  28. package/src/Editor.test.ts +0 -107
  29. package/src/Editor.toSVG.test.ts +0 -294
  30. package/src/Editor.ts +0 -1443
  31. package/src/EditorImage.test.ts +0 -117
  32. package/src/EditorImage.ts +0 -609
  33. package/src/EventDispatcher.test.ts +0 -123
  34. package/src/EventDispatcher.ts +0 -72
  35. package/src/Pointer.ts +0 -183
  36. package/src/SVGLoader.test.ts +0 -114
  37. package/src/SVGLoader.ts +0 -672
  38. package/src/UndoRedoHistory.test.ts +0 -34
  39. package/src/UndoRedoHistory.ts +0 -102
  40. package/src/Viewport.ts +0 -322
  41. package/src/bundle/bundled.ts +0 -7
  42. package/src/commands/Command.ts +0 -45
  43. package/src/commands/Duplicate.ts +0 -75
  44. package/src/commands/Erase.ts +0 -95
  45. package/src/commands/SerializableCommand.ts +0 -49
  46. package/src/commands/UnresolvedCommand.ts +0 -37
  47. package/src/commands/invertCommand.ts +0 -58
  48. package/src/commands/lib.ts +0 -16
  49. package/src/commands/localization.ts +0 -47
  50. package/src/commands/uniteCommands.test.ts +0 -23
  51. package/src/commands/uniteCommands.ts +0 -140
  52. package/src/components/AbstractComponent.transformBy.test.ts +0 -23
  53. package/src/components/AbstractComponent.ts +0 -383
  54. package/src/components/BackgroundComponent.test.ts +0 -44
  55. package/src/components/BackgroundComponent.ts +0 -348
  56. package/src/components/ImageComponent.ts +0 -176
  57. package/src/components/RestylableComponent.ts +0 -161
  58. package/src/components/SVGGlobalAttributesObject.ts +0 -79
  59. package/src/components/Stroke.test.ts +0 -137
  60. package/src/components/Stroke.ts +0 -294
  61. package/src/components/TextComponent.test.ts +0 -202
  62. package/src/components/TextComponent.ts +0 -429
  63. package/src/components/UnknownSVGObject.test.ts +0 -10
  64. package/src/components/UnknownSVGObject.ts +0 -60
  65. package/src/components/builders/ArrowBuilder.ts +0 -106
  66. package/src/components/builders/CircleBuilder.ts +0 -100
  67. package/src/components/builders/FreehandLineBuilder.test.ts +0 -24
  68. package/src/components/builders/FreehandLineBuilder.ts +0 -210
  69. package/src/components/builders/LineBuilder.ts +0 -77
  70. package/src/components/builders/PressureSensitiveFreehandLineBuilder.ts +0 -453
  71. package/src/components/builders/RectangleBuilder.ts +0 -73
  72. package/src/components/builders/types.ts +0 -15
  73. package/src/components/lib.ts +0 -31
  74. package/src/components/localization.ts +0 -24
  75. package/src/components/util/StrokeSmoother.ts +0 -302
  76. package/src/components/util/describeComponentList.ts +0 -18
  77. package/src/dialogs/makeAboutDialog.ts +0 -82
  78. package/src/inputEvents.ts +0 -143
  79. package/src/lib.ts +0 -91
  80. package/src/localization.ts +0 -34
  81. package/src/localizations/de.ts +0 -146
  82. package/src/localizations/en.ts +0 -8
  83. package/src/localizations/es.ts +0 -74
  84. package/src/localizations/getLocalizationTable.test.ts +0 -27
  85. package/src/localizations/getLocalizationTable.ts +0 -74
  86. package/src/rendering/Display.ts +0 -247
  87. package/src/rendering/RenderablePathSpec.ts +0 -88
  88. package/src/rendering/RenderingStyle.test.ts +0 -68
  89. package/src/rendering/RenderingStyle.ts +0 -55
  90. package/src/rendering/TextRenderingStyle.ts +0 -55
  91. package/src/rendering/caching/CacheRecord.test.ts +0 -48
  92. package/src/rendering/caching/CacheRecord.ts +0 -76
  93. package/src/rendering/caching/CacheRecordManager.ts +0 -71
  94. package/src/rendering/caching/RenderingCache.test.ts +0 -43
  95. package/src/rendering/caching/RenderingCache.ts +0 -66
  96. package/src/rendering/caching/RenderingCacheNode.ts +0 -404
  97. package/src/rendering/caching/testUtils.ts +0 -35
  98. package/src/rendering/caching/types.ts +0 -34
  99. package/src/rendering/lib.ts +0 -8
  100. package/src/rendering/localization.ts +0 -20
  101. package/src/rendering/renderers/AbstractRenderer.ts +0 -232
  102. package/src/rendering/renderers/CanvasRenderer.ts +0 -312
  103. package/src/rendering/renderers/DummyRenderer.test.ts +0 -41
  104. package/src/rendering/renderers/DummyRenderer.ts +0 -142
  105. package/src/rendering/renderers/SVGRenderer.ts +0 -434
  106. package/src/rendering/renderers/TextOnlyRenderer.test.ts +0 -34
  107. package/src/rendering/renderers/TextOnlyRenderer.ts +0 -68
  108. package/src/shortcuts/KeyBinding.test.ts +0 -61
  109. package/src/shortcuts/KeyBinding.ts +0 -257
  110. package/src/shortcuts/KeyboardShortcutManager.test.ts +0 -95
  111. package/src/shortcuts/KeyboardShortcutManager.ts +0 -163
  112. package/src/shortcuts/lib.ts +0 -3
  113. package/src/testing/createEditor.ts +0 -11
  114. package/src/testing/getUniquePointerId.ts +0 -18
  115. package/src/testing/lib.ts +0 -3
  116. package/src/testing/sendPenEvent.ts +0 -36
  117. package/src/testing/sendTouchEvent.ts +0 -71
  118. package/src/toolbar/AbstractToolbar.ts +0 -542
  119. package/src/toolbar/DropdownToolbar.ts +0 -220
  120. package/src/toolbar/EdgeToolbar.test.ts +0 -54
  121. package/src/toolbar/EdgeToolbar.ts +0 -543
  122. package/src/toolbar/IconProvider.ts +0 -861
  123. package/src/toolbar/constants.ts +0 -1
  124. package/src/toolbar/lib.ts +0 -6
  125. package/src/toolbar/localization.ts +0 -136
  126. package/src/toolbar/types.ts +0 -13
  127. package/src/toolbar/widgets/ActionButtonWidget.ts +0 -39
  128. package/src/toolbar/widgets/BaseToolWidget.ts +0 -81
  129. package/src/toolbar/widgets/BaseWidget.ts +0 -495
  130. package/src/toolbar/widgets/DocumentPropertiesWidget.ts +0 -250
  131. package/src/toolbar/widgets/EraserToolWidget.ts +0 -84
  132. package/src/toolbar/widgets/HandToolWidget.ts +0 -239
  133. package/src/toolbar/widgets/InsertImageWidget.ts +0 -248
  134. package/src/toolbar/widgets/OverflowWidget.ts +0 -92
  135. package/src/toolbar/widgets/PenToolWidget.ts +0 -369
  136. package/src/toolbar/widgets/SelectionToolWidget.ts +0 -195
  137. package/src/toolbar/widgets/TextToolWidget.ts +0 -149
  138. package/src/toolbar/widgets/components/makeColorInput.ts +0 -184
  139. package/src/toolbar/widgets/components/makeFileInput.ts +0 -128
  140. package/src/toolbar/widgets/components/makeGridSelector.ts +0 -179
  141. package/src/toolbar/widgets/components/makeSeparator.ts +0 -17
  142. package/src/toolbar/widgets/components/makeThicknessSlider.ts +0 -62
  143. package/src/toolbar/widgets/keybindings.ts +0 -19
  144. package/src/toolbar/widgets/layout/DropdownLayoutManager.ts +0 -262
  145. package/src/toolbar/widgets/layout/EdgeToolbarLayoutManager.ts +0 -71
  146. package/src/toolbar/widgets/layout/types.ts +0 -74
  147. package/src/toolbar/widgets/lib.ts +0 -13
  148. package/src/tools/BaseTool.ts +0 -169
  149. package/src/tools/Eraser.test.ts +0 -103
  150. package/src/tools/Eraser.ts +0 -173
  151. package/src/tools/FindTool.test.ts +0 -67
  152. package/src/tools/FindTool.ts +0 -153
  153. package/src/tools/InputFilter/FunctionMapper.ts +0 -17
  154. package/src/tools/InputFilter/InputMapper.ts +0 -41
  155. package/src/tools/InputFilter/InputPipeline.test.ts +0 -41
  156. package/src/tools/InputFilter/InputPipeline.ts +0 -34
  157. package/src/tools/InputFilter/InputStabilizer.ts +0 -254
  158. package/src/tools/InputFilter/StrokeKeyboardControl.ts +0 -104
  159. package/src/tools/PanZoom.test.ts +0 -339
  160. package/src/tools/PanZoom.ts +0 -525
  161. package/src/tools/PasteHandler.ts +0 -94
  162. package/src/tools/Pen.test.ts +0 -260
  163. package/src/tools/Pen.ts +0 -284
  164. package/src/tools/PipetteTool.ts +0 -84
  165. package/src/tools/SelectionTool/SelectAllShortcutHandler.ts +0 -29
  166. package/src/tools/SelectionTool/Selection.ts +0 -647
  167. package/src/tools/SelectionTool/SelectionHandle.ts +0 -142
  168. package/src/tools/SelectionTool/SelectionTool.test.ts +0 -370
  169. package/src/tools/SelectionTool/SelectionTool.ts +0 -510
  170. package/src/tools/SelectionTool/TransformMode.ts +0 -112
  171. package/src/tools/SelectionTool/types.ts +0 -11
  172. package/src/tools/SoundUITool.ts +0 -221
  173. package/src/tools/TextTool.ts +0 -339
  174. package/src/tools/ToolController.ts +0 -224
  175. package/src/tools/ToolEnabledGroup.ts +0 -14
  176. package/src/tools/ToolSwitcherShortcut.ts +0 -39
  177. package/src/tools/ToolbarShortcutHandler.ts +0 -39
  178. package/src/tools/UndoRedoShortcut.test.ts +0 -62
  179. package/src/tools/UndoRedoShortcut.ts +0 -24
  180. package/src/tools/keybindings.ts +0 -85
  181. package/src/tools/lib.ts +0 -22
  182. package/src/tools/localization.ts +0 -76
  183. package/src/types.ts +0 -151
  184. package/src/util/ReactiveValue.test.ts +0 -168
  185. package/src/util/ReactiveValue.ts +0 -241
  186. package/src/util/assertions.ts +0 -55
  187. package/src/util/fileToBase64.ts +0 -18
  188. package/src/util/guessKeyCodeFromKey.ts +0 -36
  189. package/src/util/listPrefixMatch.ts +0 -19
  190. package/src/util/stopPropagationOfScrollingWheelEvents.ts +0 -20
  191. package/src/util/untilNextAnimationFrame.ts +0 -9
  192. package/src/util/waitForAll.ts +0 -18
  193. package/src/util/waitForTimeout.ts +0 -9
  194. package/src/version.test.ts +0 -12
  195. package/src/version.ts +0 -3
  196. package/tools/allLocales.js +0 -4
  197. package/tools/copyREADME.ts +0 -62
@@ -1,71 +0,0 @@
1
- import Editor from '../Editor';
2
- import { Vec2 } from '@js-draw/math';
3
- import Pointer, { PointerDevice } from '../Pointer';
4
- import { InputEvtType } from '../inputEvents';
5
- import getUniquePointerId from './getUniquePointerId';
6
-
7
- /**
8
- * Dispatch a touch event to the currently selected tool. Intended for unit tests.
9
- *
10
- * @see {@link sendPenEvent}
11
- *
12
- * @example
13
- * **Simulating a horizontal swipe gesture:**
14
- * ```ts
15
- * sendTouchEvent(editor, InputEvtType.PointerDownEvt, Vec2.of(0, 0));
16
- * for (let i = 1; i <= 10; i++) {
17
- * jest.advanceTimersByTime(10);
18
- * sendTouchEvent(editor, InputEvtType.PointerMoveEvt, Vec2.of(i * 10, 0));
19
- * }
20
- * ```
21
- *
22
- * @example
23
- * **Simulating a pinch gesture.** This example assumes that you're using [Jest with timer mocks enabled](https://jestjs.io/docs/timer-mocks).
24
- * ```ts
25
- * let firstPointer = sendTouchEvent(editor, InputEvtType.PointerDownEvt, Vec2.of(0, 0));
26
- * let secondPointer = sendTouchEvent(editor, InputEvtType.PointerDownEvt, Vec2.of(100, 0), [ firstPointer ]);
27
- *
28
- * // Simulate a pinch
29
- * const maxIterations = 10;
30
- * for (let i = 0; i < maxIterations; i++) {
31
- * // Use the unit testing framework's tool for increasing the current time
32
- * // returned by (new Date()).getTime(), etc.
33
- * jest.advanceTimersByTime(100);
34
- *
35
- * const point1 = Vec2.of(-i * 5, 0);
36
- * const point2 = Vec2.of(i * 5 + 100, 0);
37
- *
38
- * firstPointer = sendTouchEvent(editor, InputEvtType.PointerMoveEvt, point1, [ secondPointer ]);
39
- * secondPointer = sendTouchEvent(editor, InputEvtType.PointerMoveEvt, point2, [ firstPointer ]);
40
- * }
41
- * ```
42
- */
43
- const sendTouchEvent = (
44
- editor: Editor,
45
- eventType: InputEvtType.PointerDownEvt|InputEvtType.PointerMoveEvt|InputEvtType.PointerUpEvt,
46
- screenPos: Vec2,
47
- allOtherPointers?: Pointer[]
48
- ) => {
49
- const canvasPos = editor.viewport.screenToCanvas(screenPos);
50
-
51
- // Get a unique ID for the main pointer
52
- // (try to use id=0, but don't use it if it's already in use).
53
- const ptrId = getUniquePointerId(allOtherPointers ?? []);
54
-
55
- const mainPointer = Pointer.ofCanvasPoint(
56
- canvasPos, eventType !== InputEvtType.PointerUpEvt, editor.viewport, ptrId, PointerDevice.Touch
57
- );
58
-
59
- editor.toolController.dispatchInputEvent({
60
- kind: eventType,
61
- allPointers: [
62
- ...(allOtherPointers ?? []),
63
- mainPointer,
64
- ],
65
- current: mainPointer,
66
- });
67
-
68
- return mainPointer;
69
- };
70
-
71
- export default sendTouchEvent;
@@ -1,542 +0,0 @@
1
- import Editor from '../Editor';
2
- import { EditorEventType } from '../types';
3
-
4
- import { coloris, close as closeColoris, init as colorisInit } from '@melloware/coloris';
5
- import { defaultToolbarLocalization, ToolbarLocalization } from './localization';
6
- import { ActionButtonIcon } from './types';
7
- import SelectionTool from '../tools/SelectionTool/SelectionTool';
8
- import PanZoomTool from '../tools/PanZoom';
9
- import TextTool from '../tools/TextTool';
10
- import EraserTool from '../tools/Eraser';
11
- import PenTool from '../tools/Pen';
12
- import PenToolWidget from './widgets/PenToolWidget';
13
- import EraserWidget from './widgets/EraserToolWidget';
14
- import SelectionToolWidget from './widgets/SelectionToolWidget';
15
- import TextToolWidget from './widgets/TextToolWidget';
16
- import HandToolWidget from './widgets/HandToolWidget';
17
- import BaseWidget, { ToolbarWidgetTag } from './widgets/BaseWidget';
18
- import ActionButtonWidget from './widgets/ActionButtonWidget';
19
- import InsertImageWidget from './widgets/InsertImageWidget';
20
- import DocumentPropertiesWidget from './widgets/DocumentPropertiesWidget';
21
- import { DispatcherEventListener } from '../EventDispatcher';
22
- import { Color4 } from '@js-draw/math';
23
- import { toolbarCSSPrefix } from './constants';
24
-
25
- type UpdateColorisCallback = ()=>void;
26
- type WidgetByIdMap = Record<string, BaseWidget>;
27
-
28
- export interface SpacerOptions {
29
- // Defaults to 0. If a non-zero number, determines the rate at which the
30
- // spacer should grow (like flexGrow).
31
- grow: number;
32
-
33
- // Minimum size (e.g. "23px")
34
- minSize: string;
35
-
36
- // Maximum size (e.g. "50px")
37
- maxSize: string;
38
- }
39
-
40
- export default abstract class AbstractToolbar {
41
- #listeners: DispatcherEventListener[] = [];
42
-
43
- #widgetsById: WidgetByIdMap = {};
44
- #widgetList: Array<BaseWidget> = [];
45
-
46
- private static colorisStarted: boolean = false;
47
- #updateColoris: UpdateColorisCallback|null = null;
48
-
49
- /** @internal */
50
- public constructor(
51
- protected editor: Editor, protected localizationTable: ToolbarLocalization = defaultToolbarLocalization,
52
- ) {
53
- if (!AbstractToolbar.colorisStarted) {
54
- colorisInit();
55
- AbstractToolbar.colorisStarted = true;
56
- }
57
- this.setupColorPickers();
58
- }
59
-
60
- private closeColorPickerOverlay: HTMLElement|null = null;
61
- private setupCloseColorPickerOverlay() {
62
- if (this.closeColorPickerOverlay) return;
63
-
64
- this.closeColorPickerOverlay = document.createElement('div');
65
- this.closeColorPickerOverlay.className = `${toolbarCSSPrefix}closeColorPickerOverlay`;
66
- this.editor.createHTMLOverlay(this.closeColorPickerOverlay);
67
-
68
- // Hide the color picker when attempting to draw on the overlay.
69
- this.#listeners.push(this.editor.handlePointerEventsExceptClicksFrom(this.closeColorPickerOverlay, (eventName) => {
70
- if (eventName === 'pointerdown') {
71
- closeColoris();
72
- }
73
-
74
- // Transfer focus to the editor to allow keyboard events to be handled.
75
- if (eventName === 'pointerup') {
76
- this.editor.focus();
77
- }
78
-
79
- // Send the event to the editor
80
- return true;
81
- }));
82
- }
83
-
84
- // @internal
85
- public setupColorPickers() {
86
- // Much of the setup only needs to be done once.
87
- if (this.#updateColoris) {
88
- this.#updateColoris();
89
- return;
90
- }
91
-
92
- this.setupCloseColorPickerOverlay();
93
-
94
- const maxSwatchLen = 12;
95
- const swatches = [
96
- Color4.red.toHexString(),
97
- Color4.purple.toHexString(),
98
- Color4.blue.toHexString(),
99
- Color4.clay.toHexString(),
100
- Color4.black.toHexString(),
101
- Color4.white.toHexString(),
102
- ];
103
- const presetColorEnd = swatches.length;
104
-
105
- // Keeps track of whether a Coloris initialization is scheduled.
106
- let colorisInitScheduled = false;
107
-
108
- // (Re)init Coloris -- update the swatches list.
109
- const initColoris = () => {
110
- try {
111
- coloris({
112
- el: '.coloris_input',
113
- format: 'hex',
114
- selectInput: false,
115
- focusInput: false,
116
- themeMode: 'auto',
117
-
118
- swatches,
119
- });
120
- } catch(err) {
121
- console.warn('Failed to initialize Coloris. Error: ', err);
122
-
123
- // Try again --- a known issue is that Coloris fails to load if the document
124
- // isn't ready.
125
- if (!colorisInitScheduled) {
126
- colorisInitScheduled = true;
127
-
128
- // Wait to initialize after the document has loaded
129
- document.addEventListener('load', () => {
130
- initColoris();
131
- }, { once: true });
132
- }
133
- }
134
- };
135
- initColoris();
136
- this.#updateColoris = initColoris;
137
-
138
- const addColorToSwatch = (newColor: string) => {
139
- let alreadyPresent = false;
140
-
141
- for (const color of swatches) {
142
- if (color === newColor) {
143
- alreadyPresent = true;
144
- }
145
- }
146
-
147
- if (!alreadyPresent) {
148
- swatches.push(newColor);
149
- if (swatches.length > maxSwatchLen) {
150
- swatches.splice(presetColorEnd, 1);
151
- }
152
- initColoris();
153
- }
154
- };
155
-
156
- this.#listeners.push(this.editor.notifier.on(EditorEventType.ColorPickerToggled, event => {
157
- if (event.kind !== EditorEventType.ColorPickerToggled) {
158
- return;
159
- }
160
-
161
- // Show/hide the overlay. Making the overlay visible gives users a surface to click
162
- // on that shows/hides the color picker.
163
- if (this.closeColorPickerOverlay) {
164
- this.closeColorPickerOverlay.style.display = event.open ? 'block' : 'none';
165
- }
166
- }));
167
-
168
- // Add newly-selected colors to the swatch.
169
- this.#listeners.push(this.editor.notifier.on(EditorEventType.ColorPickerColorSelected, event => {
170
- if (event.kind === EditorEventType.ColorPickerColorSelected) {
171
- addColorToSwatch(event.color.toHexString());
172
- }
173
- }));
174
- }
175
-
176
- protected closeColorPickers() {
177
- closeColoris?.();
178
- }
179
-
180
- protected getWidgetUniqueId(widget: BaseWidget) {
181
- return widget.getUniqueIdIn(this.#widgetsById);
182
- }
183
-
184
- protected getWidgetFromId(id: string): BaseWidget|undefined {
185
- return this.#widgetsById[id];
186
- }
187
-
188
- /** Do **not** modify the return value. */
189
- protected getAllWidgets(): Array<BaseWidget> {
190
- return this.#widgetList;
191
- }
192
-
193
- /**
194
- * Adds a spacer.
195
- *
196
- * **Toolbars can choose to ignore calls to `addSpacer`**.
197
- *
198
- * @example
199
- * Adding a save button that moves to the very right edge of the toolbar
200
- * while keeping the other buttons centered:
201
- * ```ts
202
- * const toolbar = editor.addToolbar(false);
203
- *
204
- * toolbar.addSpacer({ grow: 1 });
205
- * toolbar.addDefaults();
206
- * toolbar.addSpacer({ grow: 1 });
207
- *
208
- * toolbar.addActionButton({
209
- * label: 'Save',
210
- * icon: editor.icons.makeSaveIcon(),
211
- * }, () => {
212
- * saveCallback();
213
- * });
214
- * ```
215
- */
216
- public abstract addSpacer(options?: Partial<SpacerOptions>): void;
217
-
218
- /**
219
- * Adds an `ActionButtonWidget` or `BaseToolWidget`. The widget should not have already have a parent
220
- * (i.e. its `addTo` method should not have been called).
221
- *
222
- * @example
223
- * ```ts
224
- * const toolbar = editor.addToolbar();
225
- * const insertImageWidget = new InsertImageWidget(editor);
226
- * toolbar.addWidget(insertImageWidget);
227
- * ```
228
- */
229
- public addWidget(widget: BaseWidget) {
230
- // Prevent name collisions
231
- const id = widget.getUniqueIdIn(this.#widgetsById);
232
-
233
- // Add the widget
234
- this.#widgetsById[id] = widget;
235
- this.#widgetList.push(widget);
236
-
237
- this.addWidgetInternal(widget);
238
- this.setupColorPickers();
239
- }
240
-
241
- /** Called by `addWidget`. Implement this to add a new widget to the toolbar. */
242
- protected abstract addWidgetInternal(widget: BaseWidget): void;
243
-
244
- /** Removes the given `widget` from this toolbar. */
245
- public removeWidget(widget: BaseWidget) {
246
- const id = widget.getUniqueIdIn(this.#widgetsById);
247
- this.removeWidgetInternal(widget);
248
-
249
- delete this.#widgetsById[id];
250
- this.#widgetList = this.#widgetList.filter(otherWidget => otherWidget !== widget);
251
- }
252
-
253
-
254
- /** Called by `removeWidget`. Implement this to remove a new widget from the toolbar. */
255
- protected abstract removeWidgetInternal(widget: BaseWidget): void;
256
-
257
- private static rootToolbarId = 'root-toolbar--';
258
-
259
- /** Returns a snapshot of the state of widgets in the toolbar. */
260
- public serializeState(): string {
261
- const result: Record<string, any> = {};
262
-
263
- for (const widgetId in this.#widgetsById) {
264
- result[widgetId] = this.#widgetsById[widgetId].serializeState();
265
- }
266
-
267
- result[AbstractToolbar.rootToolbarId] = this.serializeInternal();
268
-
269
- return JSON.stringify(result);
270
- }
271
-
272
- /**
273
- * Deserialize toolbar widgets from the given state.
274
- * Assumes that toolbar widgets are in the same order as when state was serialized.
275
- */
276
- public deserializeState(state: string) {
277
- const data = JSON.parse(state);
278
-
279
- const rootId = AbstractToolbar.rootToolbarId;
280
- this.deserializeInternal(data[rootId]);
281
-
282
- for (const widgetId in data) {
283
- if (widgetId === rootId) {
284
- continue;
285
- }
286
-
287
- if (!(widgetId in this.#widgetsById)) {
288
- console.warn(`Unable to deserialize widget ${widgetId} ­— no such widget.`);
289
- continue;
290
- }
291
-
292
- this.#widgetsById[widgetId].deserializeFrom(data[widgetId]);
293
- }
294
- }
295
-
296
- /**
297
- * Called by `serializeState` to attach any additional JSONifyable data
298
- * to the serialized result.
299
- *
300
- * @reutrns an object that can be converted to JSON with `JSON.stringify`.
301
- */
302
- protected serializeInternal(): any {}
303
-
304
- /**
305
- * Called by `deserializeState` with a version of the JSON outputted
306
- * previously by `serializeInternal`.
307
- */
308
- protected deserializeInternal(_json: any) {}
309
-
310
- /**
311
- * Creates, but does not add, an action button to this container.
312
- *
313
- * @see
314
- * {@link addActionButton}
315
- */
316
- protected makeActionButton(
317
- title: string|ActionButtonIcon,
318
- command: ()=>void,
319
- mustBeToplevel: boolean = true,
320
- ): BaseWidget {
321
- const titleString = typeof title === 'string' ? title : title.label;
322
- const widgetId = 'action-button';
323
-
324
- const makeIcon = () => {
325
- if (typeof title === 'string') {
326
- return null;
327
- }
328
-
329
- return title.icon;
330
- };
331
-
332
- const widget = new ActionButtonWidget(
333
- this.editor,
334
- widgetId,
335
- makeIcon,
336
- titleString,
337
- command,
338
- this.editor.localization,
339
- mustBeToplevel,
340
- );
341
-
342
- return widget;
343
- }
344
-
345
- /**
346
- * Adds an action button with `title` to this toolbar (or to the given `parent` element).
347
- *
348
- * @return The added button.
349
- */
350
- public addActionButton(
351
- title: string|ActionButtonIcon,
352
- command: ()=> void,
353
- mustBeToplevel: boolean = true
354
- ): BaseWidget {
355
- const widget = this.makeActionButton(title, command, mustBeToplevel);
356
- this.addWidget(widget);
357
- return widget;
358
- }
359
-
360
- /**
361
- * Like {@link addActionButton}, except associates `tags` with the button that allow
362
- * different toolbar styles to give the button tag-dependent styles.
363
- */
364
- public addTaggedActionButton(
365
- tags: (ToolbarWidgetTag|string)[],
366
- title: string|ActionButtonIcon,
367
- command: ()=>void,
368
- mustBeToplevel = true
369
- ): BaseWidget {
370
- const widget = this.makeActionButton(title, command, mustBeToplevel);
371
- widget.setTags(tags);
372
- this.addWidget(widget);
373
-
374
- return widget;
375
- }
376
-
377
- /**
378
- * Adds a save button that, when clicked, calls `saveCallback`.
379
- *
380
- * **Note**: This is equivalent to
381
- * ```ts
382
- * const tags = [
383
- * ToolbarWidgetTag.Save,
384
- * ];
385
- * toolbar.addTaggedActionButton(tags, {
386
- * label: editor.localization.save,
387
- * icon: editor.icons.makeSaveIcon(),
388
- * }, () => {
389
- * saveCallback();
390
- * });
391
- * ```
392
- *
393
- * @final
394
- */
395
- public addSaveButton(saveCallback: ()=>void): BaseWidget {
396
- return this.addTaggedActionButton([ ToolbarWidgetTag.Save ], {
397
- label: this.editor.localization.save,
398
- icon: this.editor.icons.makeSaveIcon(),
399
- }, () => {
400
- saveCallback();
401
- });
402
- }
403
-
404
- /**
405
- * Adds an "Exit" button that, when clicked, calls `exitCallback`.
406
- *
407
- * **Note**: This is equivalent to
408
- * ```ts
409
- * toolbar.addTaggedActionButton([ ToolbarWidgetTag.Exit ], {
410
- * label: this.editor.localization.exit,
411
- * icon: this.editor.icons.makeCloseIcon(),
412
- * }, () => {
413
- * exitCallback();
414
- * });
415
- * ```
416
- *
417
- * @final
418
- */
419
- public addExitButton(exitCallback: ()=>void): BaseWidget {
420
- return this.addTaggedActionButton([ ToolbarWidgetTag.Exit ], {
421
- label: this.editor.localization.exit,
422
- icon: this.editor.icons.makeCloseIcon(),
423
- }, () => {
424
- exitCallback();
425
- });
426
- }
427
-
428
- /**
429
- * Adds undo and redo buttons that trigger the editor's built-in undo and redo
430
- * functionality.
431
- */
432
- public addUndoRedoButtons(undoFirst = true) {
433
- const makeUndo = () => {
434
- return this.addTaggedActionButton([
435
- ToolbarWidgetTag.Undo,
436
- ], {
437
- label: this.localizationTable.undo,
438
- icon: this.editor.icons.makeUndoIcon()
439
- }, () => {
440
- this.editor.history.undo();
441
- });
442
- };
443
- const makeRedo = () => {
444
- return this.addTaggedActionButton([
445
- ToolbarWidgetTag.Redo,
446
- ], {
447
- label: this.localizationTable.redo,
448
- icon: this.editor.icons.makeRedoIcon(),
449
- }, () => {
450
- this.editor.history.redo();
451
- });
452
- };
453
-
454
- let undoButton: BaseWidget;
455
- let redoButton: BaseWidget;
456
- if (undoFirst) {
457
- undoButton = makeUndo();
458
- redoButton = makeRedo();
459
- } else {
460
- redoButton = makeRedo();
461
- undoButton = makeUndo();
462
- }
463
-
464
- undoButton.setDisabled(true);
465
- redoButton.setDisabled(true);
466
- this.editor.notifier.on(EditorEventType.UndoRedoStackUpdated, event => {
467
- if (event.kind !== EditorEventType.UndoRedoStackUpdated) {
468
- throw new Error('Wrong event type!');
469
- }
470
-
471
- undoButton.setDisabled(event.undoStackSize === 0);
472
- redoButton.setDisabled(event.redoStackSize === 0);
473
- });
474
- }
475
-
476
- /**
477
- * Adds toolbar widgets based on the enabled tools.
478
- */
479
- public addDefaultToolWidgets() {
480
- const toolController = this.editor.toolController;
481
- for (const tool of toolController.getMatchingTools(PenTool)) {
482
- const widget = new PenToolWidget(
483
- this.editor, tool, this.localizationTable,
484
- );
485
- this.addWidget(widget);
486
- }
487
-
488
- for (const tool of toolController.getMatchingTools(EraserTool)) {
489
- this.addWidget(new EraserWidget(this.editor, tool, this.localizationTable));
490
- }
491
-
492
- for (const tool of toolController.getMatchingTools(SelectionTool)) {
493
- this.addWidget(new SelectionToolWidget(this.editor, tool, this.localizationTable));
494
- }
495
-
496
- for (const tool of toolController.getMatchingTools(TextTool)) {
497
- this.addWidget(new TextToolWidget(this.editor, tool, this.localizationTable));
498
- }
499
-
500
- const panZoomTool = toolController.getMatchingTools(PanZoomTool)[0];
501
- if (panZoomTool) {
502
- this.addWidget(new HandToolWidget(this.editor, panZoomTool, this.localizationTable));
503
- }
504
-
505
- this.addWidget(new DocumentPropertiesWidget(this.editor, this.localizationTable));
506
- this.addWidget(new InsertImageWidget(this.editor, this.localizationTable));
507
- }
508
-
509
- public addDefaultActionButtons() {
510
- this.addUndoRedoButtons();
511
- }
512
-
513
- /**
514
- * Adds both the default tool widgets and action buttons.
515
- */
516
- public abstract addDefaults(): void;
517
-
518
- /** Remove this toolbar from its container and clean up listeners. */
519
- public remove() {
520
- this.closeColorPickerOverlay?.remove();
521
-
522
- for (const listener of this.#listeners) {
523
- listener.remove();
524
- }
525
- this.#listeners = [];
526
-
527
- this.onRemove();
528
- }
529
-
530
- /**
531
- * Removes `listener` when {@link remove} is called.
532
- */
533
- protected manageListener(listener: DispatcherEventListener) {
534
- this.#listeners.push(listener);
535
- }
536
-
537
- /**
538
- * Internal logic for {@link remove}. Implementers should remove the toolbar
539
- * from its container.
540
- */
541
- protected abstract onRemove(): void;
542
- }