js-draw 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -6
- package/dist/bundle.js +1 -1
- package/dist/cjs/Editor.js +1 -1
- package/dist/cjs/Editor.loadFrom.test.d.ts +1 -0
- package/dist/cjs/Editor.test.d.ts +1 -0
- package/dist/cjs/Editor.toSVG.test.d.ts +1 -0
- package/dist/cjs/EditorImage.test.d.ts +1 -0
- package/dist/cjs/EventDispatcher.test.d.ts +1 -0
- package/dist/cjs/SVGLoader.test.d.ts +1 -0
- package/dist/cjs/UndoRedoHistory.test.d.ts +1 -0
- package/dist/cjs/commands/uniteCommands.test.d.ts +1 -0
- package/dist/cjs/components/AbstractComponent.transformBy.test.d.ts +1 -0
- package/dist/cjs/components/BackgroundComponent.test.d.ts +1 -0
- package/dist/cjs/components/Stroke.test.d.ts +1 -0
- package/dist/cjs/components/TextComponent.test.d.ts +1 -0
- package/dist/cjs/components/UnknownSVGObject.test.d.ts +1 -0
- package/dist/cjs/components/builders/FreehandLineBuilder.test.d.ts +1 -0
- package/dist/cjs/localizations/getLocalizationTable.test.d.ts +1 -0
- package/dist/cjs/rendering/RenderingStyle.test.d.ts +1 -0
- package/dist/cjs/rendering/caching/CacheRecord.test.d.ts +1 -0
- package/dist/cjs/rendering/caching/RenderingCache.test.d.ts +1 -0
- package/dist/cjs/rendering/renderers/DummyRenderer.test.d.ts +1 -0
- package/dist/cjs/rendering/renderers/TextOnlyRenderer.test.d.ts +1 -0
- package/dist/cjs/shortcuts/KeyBinding.test.d.ts +1 -0
- package/dist/cjs/shortcuts/KeyboardShortcutManager.test.d.ts +1 -0
- package/dist/cjs/toolbar/EdgeToolbar.test.d.ts +1 -0
- package/dist/cjs/tools/Eraser.test.d.ts +1 -0
- package/dist/cjs/tools/FindTool.test.d.ts +1 -0
- package/dist/cjs/tools/InputFilter/InputPipeline.test.d.ts +1 -0
- package/dist/cjs/tools/PanZoom.test.d.ts +1 -0
- package/dist/cjs/tools/Pen.test.d.ts +1 -0
- package/dist/cjs/tools/SelectionTool/SelectionTool.test.d.ts +1 -0
- package/dist/cjs/tools/UndoRedoShortcut.test.d.ts +1 -0
- package/dist/cjs/util/ReactiveValue.test.d.ts +1 -0
- package/dist/cjs/version.js +1 -1
- package/dist/cjs/version.test.d.ts +1 -0
- package/dist/mjs/Editor.loadFrom.test.d.ts +1 -0
- package/dist/mjs/Editor.mjs +1 -1
- package/dist/mjs/Editor.test.d.ts +1 -0
- package/dist/mjs/Editor.toSVG.test.d.ts +1 -0
- package/dist/mjs/EditorImage.test.d.ts +1 -0
- package/dist/mjs/EventDispatcher.test.d.ts +1 -0
- package/dist/mjs/SVGLoader.test.d.ts +1 -0
- package/dist/mjs/UndoRedoHistory.test.d.ts +1 -0
- package/dist/mjs/commands/uniteCommands.test.d.ts +1 -0
- package/dist/mjs/components/AbstractComponent.transformBy.test.d.ts +1 -0
- package/dist/mjs/components/BackgroundComponent.test.d.ts +1 -0
- package/dist/mjs/components/Stroke.test.d.ts +1 -0
- package/dist/mjs/components/TextComponent.test.d.ts +1 -0
- package/dist/mjs/components/UnknownSVGObject.test.d.ts +1 -0
- package/dist/mjs/components/builders/FreehandLineBuilder.test.d.ts +1 -0
- package/dist/mjs/localizations/getLocalizationTable.test.d.ts +1 -0
- package/dist/mjs/rendering/RenderingStyle.test.d.ts +1 -0
- package/dist/mjs/rendering/caching/CacheRecord.test.d.ts +1 -0
- package/dist/mjs/rendering/caching/RenderingCache.test.d.ts +1 -0
- package/dist/mjs/rendering/renderers/DummyRenderer.test.d.ts +1 -0
- package/dist/mjs/rendering/renderers/TextOnlyRenderer.test.d.ts +1 -0
- package/dist/mjs/shortcuts/KeyBinding.test.d.ts +1 -0
- package/dist/mjs/shortcuts/KeyboardShortcutManager.test.d.ts +1 -0
- package/dist/mjs/toolbar/EdgeToolbar.test.d.ts +1 -0
- package/dist/mjs/tools/Eraser.test.d.ts +1 -0
- package/dist/mjs/tools/FindTool.test.d.ts +1 -0
- package/dist/mjs/tools/InputFilter/InputPipeline.test.d.ts +1 -0
- package/dist/mjs/tools/PanZoom.test.d.ts +1 -0
- package/dist/mjs/tools/Pen.test.d.ts +1 -0
- package/dist/mjs/tools/SelectionTool/SelectionTool.test.d.ts +1 -0
- package/dist/mjs/tools/UndoRedoShortcut.test.d.ts +1 -0
- package/dist/mjs/util/ReactiveValue.test.d.ts +1 -0
- package/dist/mjs/version.mjs +1 -1
- package/dist/mjs/version.test.d.ts +1 -0
- package/dist-test/test_imports/package-lock.json +13 -0
- package/dist-test/test_imports/package.json +12 -0
- package/dist-test/test_imports/test-imports.js +11 -0
- package/dist-test/test_imports/test-require.cjs +14 -0
- package/package.json +2 -2
- package/src/Editor.loadFrom.test.ts +24 -0
- package/src/Editor.test.ts +107 -0
- package/src/Editor.toSVG.test.ts +294 -0
- package/src/Editor.ts +1443 -0
- package/src/EditorImage.test.ts +117 -0
- package/src/EditorImage.ts +609 -0
- package/src/EventDispatcher.test.ts +123 -0
- package/src/EventDispatcher.ts +72 -0
- package/src/Pointer.ts +183 -0
- package/src/SVGLoader.test.ts +114 -0
- package/src/SVGLoader.ts +672 -0
- package/src/UndoRedoHistory.test.ts +34 -0
- package/src/UndoRedoHistory.ts +102 -0
- package/src/Viewport.ts +322 -0
- package/src/bundle/bundled.ts +7 -0
- package/src/commands/Command.ts +45 -0
- package/src/commands/Duplicate.ts +75 -0
- package/src/commands/Erase.ts +95 -0
- package/src/commands/SerializableCommand.ts +49 -0
- package/src/commands/UnresolvedCommand.ts +37 -0
- package/src/commands/invertCommand.ts +58 -0
- package/src/commands/lib.ts +16 -0
- package/src/commands/localization.ts +47 -0
- package/src/commands/uniteCommands.test.ts +23 -0
- package/src/commands/uniteCommands.ts +140 -0
- package/src/components/AbstractComponent.transformBy.test.ts +23 -0
- package/src/components/AbstractComponent.ts +383 -0
- package/src/components/BackgroundComponent.test.ts +44 -0
- package/src/components/BackgroundComponent.ts +348 -0
- package/src/components/ImageComponent.ts +176 -0
- package/src/components/RestylableComponent.ts +161 -0
- package/src/components/SVGGlobalAttributesObject.ts +79 -0
- package/src/components/Stroke.test.ts +137 -0
- package/src/components/Stroke.ts +294 -0
- package/src/components/TextComponent.test.ts +202 -0
- package/src/components/TextComponent.ts +429 -0
- package/src/components/UnknownSVGObject.test.ts +10 -0
- package/src/components/UnknownSVGObject.ts +60 -0
- package/src/components/builders/ArrowBuilder.ts +106 -0
- package/src/components/builders/CircleBuilder.ts +100 -0
- package/src/components/builders/FreehandLineBuilder.test.ts +24 -0
- package/src/components/builders/FreehandLineBuilder.ts +210 -0
- package/src/components/builders/LineBuilder.ts +77 -0
- package/src/components/builders/PressureSensitiveFreehandLineBuilder.ts +453 -0
- package/src/components/builders/RectangleBuilder.ts +73 -0
- package/src/components/builders/types.ts +15 -0
- package/src/components/lib.ts +31 -0
- package/src/components/localization.ts +24 -0
- package/src/components/util/StrokeSmoother.ts +302 -0
- package/src/components/util/describeComponentList.ts +18 -0
- package/src/dialogs/makeAboutDialog.ts +82 -0
- package/src/inputEvents.ts +143 -0
- package/src/lib.ts +91 -0
- package/src/localization.ts +34 -0
- package/src/localizations/de.ts +146 -0
- package/src/localizations/en.ts +8 -0
- package/src/localizations/es.ts +74 -0
- package/src/localizations/getLocalizationTable.test.ts +27 -0
- package/src/localizations/getLocalizationTable.ts +74 -0
- package/src/rendering/Display.ts +247 -0
- package/src/rendering/RenderablePathSpec.ts +88 -0
- package/src/rendering/RenderingStyle.test.ts +68 -0
- package/src/rendering/RenderingStyle.ts +55 -0
- package/src/rendering/TextRenderingStyle.ts +55 -0
- package/src/rendering/caching/CacheRecord.test.ts +48 -0
- package/src/rendering/caching/CacheRecord.ts +76 -0
- package/src/rendering/caching/CacheRecordManager.ts +71 -0
- package/src/rendering/caching/RenderingCache.test.ts +43 -0
- package/src/rendering/caching/RenderingCache.ts +66 -0
- package/src/rendering/caching/RenderingCacheNode.ts +404 -0
- package/src/rendering/caching/testUtils.ts +35 -0
- package/src/rendering/caching/types.ts +34 -0
- package/src/rendering/lib.ts +8 -0
- package/src/rendering/localization.ts +20 -0
- package/src/rendering/renderers/AbstractRenderer.ts +232 -0
- package/src/rendering/renderers/CanvasRenderer.ts +312 -0
- package/src/rendering/renderers/DummyRenderer.test.ts +41 -0
- package/src/rendering/renderers/DummyRenderer.ts +142 -0
- package/src/rendering/renderers/SVGRenderer.ts +434 -0
- package/src/rendering/renderers/TextOnlyRenderer.test.ts +34 -0
- package/src/rendering/renderers/TextOnlyRenderer.ts +68 -0
- package/src/shortcuts/KeyBinding.test.ts +61 -0
- package/src/shortcuts/KeyBinding.ts +257 -0
- package/src/shortcuts/KeyboardShortcutManager.test.ts +95 -0
- package/src/shortcuts/KeyboardShortcutManager.ts +163 -0
- package/src/shortcuts/lib.ts +3 -0
- package/src/testing/createEditor.ts +11 -0
- package/src/testing/getUniquePointerId.ts +18 -0
- package/src/testing/lib.ts +3 -0
- package/src/testing/sendPenEvent.ts +36 -0
- package/src/testing/sendTouchEvent.ts +71 -0
- package/src/toolbar/AbstractToolbar.ts +542 -0
- package/src/toolbar/DropdownToolbar.ts +220 -0
- package/src/toolbar/EdgeToolbar.test.ts +54 -0
- package/src/toolbar/EdgeToolbar.ts +543 -0
- package/src/toolbar/IconProvider.ts +861 -0
- package/src/toolbar/constants.ts +1 -0
- package/src/toolbar/lib.ts +6 -0
- package/src/toolbar/localization.ts +136 -0
- package/src/toolbar/types.ts +13 -0
- package/src/toolbar/widgets/ActionButtonWidget.ts +39 -0
- package/src/toolbar/widgets/BaseToolWidget.ts +81 -0
- package/src/toolbar/widgets/BaseWidget.ts +495 -0
- package/src/toolbar/widgets/DocumentPropertiesWidget.ts +250 -0
- package/src/toolbar/widgets/EraserToolWidget.ts +84 -0
- package/src/toolbar/widgets/HandToolWidget.ts +239 -0
- package/src/toolbar/widgets/InsertImageWidget.ts +248 -0
- package/src/toolbar/widgets/OverflowWidget.ts +92 -0
- package/src/toolbar/widgets/PenToolWidget.ts +369 -0
- package/src/toolbar/widgets/SelectionToolWidget.ts +195 -0
- package/src/toolbar/widgets/TextToolWidget.ts +149 -0
- package/src/toolbar/widgets/components/makeColorInput.ts +184 -0
- package/src/toolbar/widgets/components/makeFileInput.ts +128 -0
- package/src/toolbar/widgets/components/makeGridSelector.ts +179 -0
- package/src/toolbar/widgets/components/makeSeparator.ts +17 -0
- package/src/toolbar/widgets/components/makeThicknessSlider.ts +62 -0
- package/src/toolbar/widgets/keybindings.ts +19 -0
- package/src/toolbar/widgets/layout/DropdownLayoutManager.ts +262 -0
- package/src/toolbar/widgets/layout/EdgeToolbarLayoutManager.ts +71 -0
- package/src/toolbar/widgets/layout/types.ts +74 -0
- package/src/toolbar/widgets/lib.ts +13 -0
- package/src/tools/BaseTool.ts +169 -0
- package/src/tools/Eraser.test.ts +103 -0
- package/src/tools/Eraser.ts +173 -0
- package/src/tools/FindTool.test.ts +67 -0
- package/src/tools/FindTool.ts +153 -0
- package/src/tools/InputFilter/FunctionMapper.ts +17 -0
- package/src/tools/InputFilter/InputMapper.ts +41 -0
- package/src/tools/InputFilter/InputPipeline.test.ts +41 -0
- package/src/tools/InputFilter/InputPipeline.ts +34 -0
- package/src/tools/InputFilter/InputStabilizer.ts +254 -0
- package/src/tools/InputFilter/StrokeKeyboardControl.ts +104 -0
- package/src/tools/PanZoom.test.ts +339 -0
- package/src/tools/PanZoom.ts +525 -0
- package/src/tools/PasteHandler.ts +94 -0
- package/src/tools/Pen.test.ts +260 -0
- package/src/tools/Pen.ts +284 -0
- package/src/tools/PipetteTool.ts +84 -0
- package/src/tools/SelectionTool/SelectAllShortcutHandler.ts +29 -0
- package/src/tools/SelectionTool/Selection.ts +647 -0
- package/src/tools/SelectionTool/SelectionHandle.ts +142 -0
- package/src/tools/SelectionTool/SelectionTool.test.ts +370 -0
- package/src/tools/SelectionTool/SelectionTool.ts +510 -0
- package/src/tools/SelectionTool/TransformMode.ts +112 -0
- package/src/tools/SelectionTool/types.ts +11 -0
- package/src/tools/SoundUITool.ts +221 -0
- package/src/tools/TextTool.ts +339 -0
- package/src/tools/ToolController.ts +224 -0
- package/src/tools/ToolEnabledGroup.ts +14 -0
- package/src/tools/ToolSwitcherShortcut.ts +39 -0
- package/src/tools/ToolbarShortcutHandler.ts +39 -0
- package/src/tools/UndoRedoShortcut.test.ts +62 -0
- package/src/tools/UndoRedoShortcut.ts +24 -0
- package/src/tools/keybindings.ts +85 -0
- package/src/tools/lib.ts +22 -0
- package/src/tools/localization.ts +76 -0
- package/src/types.ts +151 -0
- package/src/util/ReactiveValue.test.ts +168 -0
- package/src/util/ReactiveValue.ts +241 -0
- package/src/util/assertions.ts +55 -0
- package/src/util/fileToBase64.ts +18 -0
- package/src/util/guessKeyCodeFromKey.ts +36 -0
- package/src/util/listPrefixMatch.ts +19 -0
- package/src/util/stopPropagationOfScrollingWheelEvents.ts +20 -0
- package/src/util/untilNextAnimationFrame.ts +9 -0
- package/src/util/waitForAll.ts +18 -0
- package/src/util/waitForTimeout.ts +9 -0
- package/src/version.test.ts +12 -0
- package/src/version.ts +3 -0
@@ -0,0 +1,71 @@
|
|
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;
|
@@ -0,0 +1,542 @@
|
|
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
|
+
}
|