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.
- package/LICENSE +21 -0
- package/dist/Editor.css +1 -0
- package/dist/bundle.js +1 -1
- package/dist/bundledStyles.js +1 -1
- package/dist/cjs/toolbar/AbstractToolbar.d.ts +9 -13
- package/dist/cjs/toolbar/AbstractToolbar.js +14 -19
- package/dist/cjs/toolbar/widgets/SaveActionWidget.d.ts +10 -0
- package/dist/cjs/toolbar/widgets/SaveActionWidget.js +26 -0
- package/dist/cjs/toolbar/widgets/keybindings.d.ts +1 -0
- package/dist/cjs/toolbar/widgets/keybindings.js +4 -1
- package/dist/cjs/version.js +1 -1
- package/dist/mjs/toolbar/AbstractToolbar.d.ts +9 -13
- package/dist/mjs/toolbar/AbstractToolbar.mjs +14 -19
- package/dist/mjs/toolbar/widgets/SaveActionWidget.d.ts +10 -0
- package/dist/mjs/toolbar/widgets/SaveActionWidget.mjs +21 -0
- package/dist/mjs/toolbar/widgets/keybindings.d.ts +1 -0
- package/dist/mjs/toolbar/widgets/keybindings.mjs +3 -0
- package/dist/mjs/version.mjs +1 -1
- package/docs/img/readme-images/js-draw.jpg +0 -0
- package/docs/img/readme-images/unsupported-elements--in-editor.png +0 -0
- package/package.json +5 -4
- package/src/toolbar/EdgeToolbar.scss +1 -0
- package/dist-test/test_imports/package-lock.json +0 -13
- package/dist-test/test_imports/package.json +0 -12
- package/dist-test/test_imports/test-imports.js +0 -11
- package/dist-test/test_imports/test-require.cjs +0 -14
- package/src/Editor.loadFrom.test.ts +0 -24
- package/src/Editor.test.ts +0 -107
- package/src/Editor.toSVG.test.ts +0 -294
- package/src/Editor.ts +0 -1443
- package/src/EditorImage.test.ts +0 -117
- package/src/EditorImage.ts +0 -609
- package/src/EventDispatcher.test.ts +0 -123
- package/src/EventDispatcher.ts +0 -72
- package/src/Pointer.ts +0 -183
- package/src/SVGLoader.test.ts +0 -114
- package/src/SVGLoader.ts +0 -672
- package/src/UndoRedoHistory.test.ts +0 -34
- package/src/UndoRedoHistory.ts +0 -102
- package/src/Viewport.ts +0 -322
- package/src/bundle/bundled.ts +0 -7
- package/src/commands/Command.ts +0 -45
- package/src/commands/Duplicate.ts +0 -75
- package/src/commands/Erase.ts +0 -95
- package/src/commands/SerializableCommand.ts +0 -49
- package/src/commands/UnresolvedCommand.ts +0 -37
- package/src/commands/invertCommand.ts +0 -58
- package/src/commands/lib.ts +0 -16
- package/src/commands/localization.ts +0 -47
- package/src/commands/uniteCommands.test.ts +0 -23
- package/src/commands/uniteCommands.ts +0 -140
- package/src/components/AbstractComponent.transformBy.test.ts +0 -23
- package/src/components/AbstractComponent.ts +0 -383
- package/src/components/BackgroundComponent.test.ts +0 -44
- package/src/components/BackgroundComponent.ts +0 -348
- package/src/components/ImageComponent.ts +0 -176
- package/src/components/RestylableComponent.ts +0 -161
- package/src/components/SVGGlobalAttributesObject.ts +0 -79
- package/src/components/Stroke.test.ts +0 -137
- package/src/components/Stroke.ts +0 -294
- package/src/components/TextComponent.test.ts +0 -202
- package/src/components/TextComponent.ts +0 -429
- package/src/components/UnknownSVGObject.test.ts +0 -10
- package/src/components/UnknownSVGObject.ts +0 -60
- package/src/components/builders/ArrowBuilder.ts +0 -106
- package/src/components/builders/CircleBuilder.ts +0 -100
- package/src/components/builders/FreehandLineBuilder.test.ts +0 -24
- package/src/components/builders/FreehandLineBuilder.ts +0 -210
- package/src/components/builders/LineBuilder.ts +0 -77
- package/src/components/builders/PressureSensitiveFreehandLineBuilder.ts +0 -453
- package/src/components/builders/RectangleBuilder.ts +0 -73
- package/src/components/builders/types.ts +0 -15
- package/src/components/lib.ts +0 -31
- package/src/components/localization.ts +0 -24
- package/src/components/util/StrokeSmoother.ts +0 -302
- package/src/components/util/describeComponentList.ts +0 -18
- package/src/dialogs/makeAboutDialog.ts +0 -82
- package/src/inputEvents.ts +0 -143
- package/src/lib.ts +0 -91
- package/src/localization.ts +0 -34
- package/src/localizations/de.ts +0 -146
- package/src/localizations/en.ts +0 -8
- package/src/localizations/es.ts +0 -74
- package/src/localizations/getLocalizationTable.test.ts +0 -27
- package/src/localizations/getLocalizationTable.ts +0 -74
- package/src/rendering/Display.ts +0 -247
- package/src/rendering/RenderablePathSpec.ts +0 -88
- package/src/rendering/RenderingStyle.test.ts +0 -68
- package/src/rendering/RenderingStyle.ts +0 -55
- package/src/rendering/TextRenderingStyle.ts +0 -55
- package/src/rendering/caching/CacheRecord.test.ts +0 -48
- package/src/rendering/caching/CacheRecord.ts +0 -76
- package/src/rendering/caching/CacheRecordManager.ts +0 -71
- package/src/rendering/caching/RenderingCache.test.ts +0 -43
- package/src/rendering/caching/RenderingCache.ts +0 -66
- package/src/rendering/caching/RenderingCacheNode.ts +0 -404
- package/src/rendering/caching/testUtils.ts +0 -35
- package/src/rendering/caching/types.ts +0 -34
- package/src/rendering/lib.ts +0 -8
- package/src/rendering/localization.ts +0 -20
- package/src/rendering/renderers/AbstractRenderer.ts +0 -232
- package/src/rendering/renderers/CanvasRenderer.ts +0 -312
- package/src/rendering/renderers/DummyRenderer.test.ts +0 -41
- package/src/rendering/renderers/DummyRenderer.ts +0 -142
- package/src/rendering/renderers/SVGRenderer.ts +0 -434
- package/src/rendering/renderers/TextOnlyRenderer.test.ts +0 -34
- package/src/rendering/renderers/TextOnlyRenderer.ts +0 -68
- package/src/shortcuts/KeyBinding.test.ts +0 -61
- package/src/shortcuts/KeyBinding.ts +0 -257
- package/src/shortcuts/KeyboardShortcutManager.test.ts +0 -95
- package/src/shortcuts/KeyboardShortcutManager.ts +0 -163
- package/src/shortcuts/lib.ts +0 -3
- package/src/testing/createEditor.ts +0 -11
- package/src/testing/getUniquePointerId.ts +0 -18
- package/src/testing/lib.ts +0 -3
- package/src/testing/sendPenEvent.ts +0 -36
- package/src/testing/sendTouchEvent.ts +0 -71
- package/src/toolbar/AbstractToolbar.ts +0 -542
- package/src/toolbar/DropdownToolbar.ts +0 -220
- package/src/toolbar/EdgeToolbar.test.ts +0 -54
- package/src/toolbar/EdgeToolbar.ts +0 -543
- package/src/toolbar/IconProvider.ts +0 -861
- package/src/toolbar/constants.ts +0 -1
- package/src/toolbar/lib.ts +0 -6
- package/src/toolbar/localization.ts +0 -136
- package/src/toolbar/types.ts +0 -13
- package/src/toolbar/widgets/ActionButtonWidget.ts +0 -39
- package/src/toolbar/widgets/BaseToolWidget.ts +0 -81
- package/src/toolbar/widgets/BaseWidget.ts +0 -495
- package/src/toolbar/widgets/DocumentPropertiesWidget.ts +0 -250
- package/src/toolbar/widgets/EraserToolWidget.ts +0 -84
- package/src/toolbar/widgets/HandToolWidget.ts +0 -239
- package/src/toolbar/widgets/InsertImageWidget.ts +0 -248
- package/src/toolbar/widgets/OverflowWidget.ts +0 -92
- package/src/toolbar/widgets/PenToolWidget.ts +0 -369
- package/src/toolbar/widgets/SelectionToolWidget.ts +0 -195
- package/src/toolbar/widgets/TextToolWidget.ts +0 -149
- package/src/toolbar/widgets/components/makeColorInput.ts +0 -184
- package/src/toolbar/widgets/components/makeFileInput.ts +0 -128
- package/src/toolbar/widgets/components/makeGridSelector.ts +0 -179
- package/src/toolbar/widgets/components/makeSeparator.ts +0 -17
- package/src/toolbar/widgets/components/makeThicknessSlider.ts +0 -62
- package/src/toolbar/widgets/keybindings.ts +0 -19
- package/src/toolbar/widgets/layout/DropdownLayoutManager.ts +0 -262
- package/src/toolbar/widgets/layout/EdgeToolbarLayoutManager.ts +0 -71
- package/src/toolbar/widgets/layout/types.ts +0 -74
- package/src/toolbar/widgets/lib.ts +0 -13
- package/src/tools/BaseTool.ts +0 -169
- package/src/tools/Eraser.test.ts +0 -103
- package/src/tools/Eraser.ts +0 -173
- package/src/tools/FindTool.test.ts +0 -67
- package/src/tools/FindTool.ts +0 -153
- package/src/tools/InputFilter/FunctionMapper.ts +0 -17
- package/src/tools/InputFilter/InputMapper.ts +0 -41
- package/src/tools/InputFilter/InputPipeline.test.ts +0 -41
- package/src/tools/InputFilter/InputPipeline.ts +0 -34
- package/src/tools/InputFilter/InputStabilizer.ts +0 -254
- package/src/tools/InputFilter/StrokeKeyboardControl.ts +0 -104
- package/src/tools/PanZoom.test.ts +0 -339
- package/src/tools/PanZoom.ts +0 -525
- package/src/tools/PasteHandler.ts +0 -94
- package/src/tools/Pen.test.ts +0 -260
- package/src/tools/Pen.ts +0 -284
- package/src/tools/PipetteTool.ts +0 -84
- package/src/tools/SelectionTool/SelectAllShortcutHandler.ts +0 -29
- package/src/tools/SelectionTool/Selection.ts +0 -647
- package/src/tools/SelectionTool/SelectionHandle.ts +0 -142
- package/src/tools/SelectionTool/SelectionTool.test.ts +0 -370
- package/src/tools/SelectionTool/SelectionTool.ts +0 -510
- package/src/tools/SelectionTool/TransformMode.ts +0 -112
- package/src/tools/SelectionTool/types.ts +0 -11
- package/src/tools/SoundUITool.ts +0 -221
- package/src/tools/TextTool.ts +0 -339
- package/src/tools/ToolController.ts +0 -224
- package/src/tools/ToolEnabledGroup.ts +0 -14
- package/src/tools/ToolSwitcherShortcut.ts +0 -39
- package/src/tools/ToolbarShortcutHandler.ts +0 -39
- package/src/tools/UndoRedoShortcut.test.ts +0 -62
- package/src/tools/UndoRedoShortcut.ts +0 -24
- package/src/tools/keybindings.ts +0 -85
- package/src/tools/lib.ts +0 -22
- package/src/tools/localization.ts +0 -76
- package/src/types.ts +0 -151
- package/src/util/ReactiveValue.test.ts +0 -168
- package/src/util/ReactiveValue.ts +0 -241
- package/src/util/assertions.ts +0 -55
- package/src/util/fileToBase64.ts +0 -18
- package/src/util/guessKeyCodeFromKey.ts +0 -36
- package/src/util/listPrefixMatch.ts +0 -19
- package/src/util/stopPropagationOfScrollingWheelEvents.ts +0 -20
- package/src/util/untilNextAnimationFrame.ts +0 -9
- package/src/util/waitForAll.ts +0 -18
- package/src/util/waitForTimeout.ts +0 -9
- package/src/version.test.ts +0 -12
- package/src/version.ts +0 -3
- package/tools/allLocales.js +0 -4
- package/tools/copyREADME.ts +0 -62
package/src/Editor.ts
DELETED
@@ -1,1443 +0,0 @@
|
|
1
|
-
import EditorImage from './EditorImage';
|
2
|
-
import ToolController from './tools/ToolController';
|
3
|
-
import { EditorNotifier, EditorEventType, ImageLoader } from './types';
|
4
|
-
import { HTMLPointerEventName, HTMLPointerEventFilter, InputEvtType, PointerEvt, keyUpEventFromHTMLEvent, keyPressEventFromHTMLEvent, KeyPressEvent } from './inputEvents';
|
5
|
-
import Command from './commands/Command';
|
6
|
-
import UndoRedoHistory from './UndoRedoHistory';
|
7
|
-
import Viewport from './Viewport';
|
8
|
-
import EventDispatcher from './EventDispatcher';
|
9
|
-
import { Point2, Vec2, Vec3, Color4, Mat33, Rect2, toRoundedString } from '@js-draw/math';
|
10
|
-
import Display, { RenderingMode } from './rendering/Display';
|
11
|
-
import SVGRenderer from './rendering/renderers/SVGRenderer';
|
12
|
-
import SVGLoader from './SVGLoader';
|
13
|
-
import Pointer from './Pointer';
|
14
|
-
import { EditorLocalization } from './localization';
|
15
|
-
import getLocalizationTable from './localizations/getLocalizationTable';
|
16
|
-
import IconProvider from './toolbar/IconProvider';
|
17
|
-
import CanvasRenderer from './rendering/renderers/CanvasRenderer';
|
18
|
-
import untilNextAnimationFrame from './util/untilNextAnimationFrame';
|
19
|
-
import fileToBase64 from './util/fileToBase64';
|
20
|
-
import uniteCommands from './commands/uniteCommands';
|
21
|
-
import SelectionTool from './tools/SelectionTool/SelectionTool';
|
22
|
-
import AbstractComponent from './components/AbstractComponent';
|
23
|
-
import Erase from './commands/Erase';
|
24
|
-
import BackgroundComponent, { BackgroundType } from './components/BackgroundComponent';
|
25
|
-
import sendPenEvent from './testing/sendPenEvent';
|
26
|
-
import KeyboardShortcutManager from './shortcuts/KeyboardShortcutManager';
|
27
|
-
import KeyBinding from './shortcuts/KeyBinding';
|
28
|
-
import AbstractToolbar from './toolbar/AbstractToolbar';
|
29
|
-
import EdgeToolbar from './toolbar/EdgeToolbar';
|
30
|
-
import StrokeKeyboardControl from './tools/InputFilter/StrokeKeyboardControl';
|
31
|
-
import guessKeyCodeFromKey from './util/guessKeyCodeFromKey';
|
32
|
-
import RenderablePathSpec from './rendering/RenderablePathSpec';
|
33
|
-
import makeAboutDialog, { AboutDialogEntry } from './dialogs/makeAboutDialog';
|
34
|
-
import version from './version';
|
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
|
-
|
53
|
-
/** Maximum zoom fraction (e.g. 2 → 200% zoom). */
|
54
|
-
maxZoom: number,
|
55
|
-
|
56
|
-
/**
|
57
|
-
* Overrides for keyboard shortcuts. For example,
|
58
|
-
* ```ts
|
59
|
-
* {
|
60
|
-
* 'some.shortcut.id': [ ShortcutManager.keyboardShortcutFromString('ctrl+a') ],
|
61
|
-
* 'another.shortcut.id': [ ]
|
62
|
-
* }
|
63
|
-
* ```
|
64
|
-
* where shortcut IDs map to lists of associated keybindings.
|
65
|
-
*/
|
66
|
-
keyboardShortcutOverrides: Record<string, Array<KeyBinding>>,
|
67
|
-
|
68
|
-
/**
|
69
|
-
* Provides a set of icons for the editor.
|
70
|
-
*
|
71
|
-
* See, for example, the `@js-draw/material-icons` package.
|
72
|
-
*
|
73
|
-
* @example
|
74
|
-
* ```ts
|
75
|
-
* import * as jsdraw from 'js-draw';
|
76
|
-
* import MaterialIconProvider from '@js-draw/material-icons';
|
77
|
-
* import 'js-draw/styles';
|
78
|
-
*
|
79
|
-
* const settings: Partial<jsdraw.EditorSettings> = {
|
80
|
-
* // Default to material icons
|
81
|
-
* iconProvider: new MaterialIconProvider(),
|
82
|
-
*
|
83
|
-
* // Only scroll the editor if it's focused.
|
84
|
-
* wheelEventsEnabled: 'only-if-focused',
|
85
|
-
* };
|
86
|
-
*
|
87
|
-
* // Add an editor to the document, using the above settings
|
88
|
-
* const editor = new jsdraw.Editor(document.body, settings);
|
89
|
-
*
|
90
|
-
* // Add a toolbar to the editor
|
91
|
-
* const toolbar = jsdraw.makeEdgeToolbar(editor);
|
92
|
-
*
|
93
|
-
* // Add the default tool items
|
94
|
-
* toolbar.addDefaults();
|
95
|
-
* ```
|
96
|
-
*/
|
97
|
-
iconProvider: IconProvider,
|
98
|
-
|
99
|
-
/**
|
100
|
-
* Additional messages to show in the "about" dialog.
|
101
|
-
*/
|
102
|
-
notices: AboutDialogEntry[],
|
103
|
-
}
|
104
|
-
|
105
|
-
/**
|
106
|
-
* The main entrypoint for the full editor.
|
107
|
-
*
|
108
|
-
* @example
|
109
|
-
* To create an editor with a toolbar,
|
110
|
-
* ```
|
111
|
-
* const editor = new Editor(document.body);
|
112
|
-
*
|
113
|
-
* const toolbar = editor.addToolbar();
|
114
|
-
* toolbar.addActionButton('Save', () => {
|
115
|
-
* const saveData = editor.toSVG().outerHTML;
|
116
|
-
* // Do something with saveData...
|
117
|
-
* });
|
118
|
-
* ```
|
119
|
-
*
|
120
|
-
* See also
|
121
|
-
* [`docs/example/example.ts`](https://github.com/personalizedrefrigerator/js-draw/blob/main/docs/demo/example.ts#L15).
|
122
|
-
*/
|
123
|
-
export class Editor {
|
124
|
-
// Wrapper around the viewport and toolbar
|
125
|
-
private container: HTMLElement;
|
126
|
-
private renderingRegion: HTMLElement;
|
127
|
-
|
128
|
-
/** Manages drawing surfaces/{@link AbstractRenderer}s. */
|
129
|
-
public display: Display;
|
130
|
-
|
131
|
-
/**
|
132
|
-
* Handles undo/redo.
|
133
|
-
*
|
134
|
-
* @example
|
135
|
-
* ```
|
136
|
-
* const editor = new Editor(document.body);
|
137
|
-
*
|
138
|
-
* // Do something undoable.
|
139
|
-
* // ...
|
140
|
-
*
|
141
|
-
* // Undo the last action
|
142
|
-
* editor.history.undo();
|
143
|
-
* ```
|
144
|
-
*/
|
145
|
-
public history: UndoRedoHistory;
|
146
|
-
|
147
|
-
/**
|
148
|
-
* Data structure for adding/removing/querying objects in the image.
|
149
|
-
*
|
150
|
-
* @example
|
151
|
-
* ```
|
152
|
-
* const editor = new Editor(document.body);
|
153
|
-
*
|
154
|
-
* // Create a path.
|
155
|
-
* const stroke = new Stroke([
|
156
|
-
* Path.fromString('M0,0 L30,30 z').toRenderable({ fill: Color4.black }),
|
157
|
-
* ]);
|
158
|
-
* const addElementCommand = editor.image.addElement(stroke);
|
159
|
-
*
|
160
|
-
* // Add the stroke to the editor
|
161
|
-
* editor.dispatch(addElementCommand);
|
162
|
-
* ```
|
163
|
-
*/
|
164
|
-
public readonly image: EditorImage;
|
165
|
-
|
166
|
-
/**
|
167
|
-
* Allows transforming the view and querying information about
|
168
|
-
* what is currently visible.
|
169
|
-
*/
|
170
|
-
public readonly viewport: Viewport;
|
171
|
-
|
172
|
-
/** @internal */
|
173
|
-
public readonly localization: EditorLocalization;
|
174
|
-
|
175
|
-
/** {@link EditorSettings.iconProvider} */
|
176
|
-
public readonly icons: IconProvider;
|
177
|
-
|
178
|
-
/**
|
179
|
-
* Manages and allows overriding of keyboard shortcuts.
|
180
|
-
*
|
181
|
-
* @internal
|
182
|
-
*/
|
183
|
-
public readonly shortcuts: KeyboardShortcutManager;
|
184
|
-
|
185
|
-
/**
|
186
|
-
* Controls the list of tools. See
|
187
|
-
* [the custom tool example](https://github.com/personalizedrefrigerator/js-draw/tree/main/docs/examples/example-custom-tools)
|
188
|
-
* for more.
|
189
|
-
*/
|
190
|
-
public readonly toolController: ToolController;
|
191
|
-
|
192
|
-
/**
|
193
|
-
* Global event dispatcher/subscriber.
|
194
|
-
*/
|
195
|
-
public readonly notifier: EditorNotifier;
|
196
|
-
|
197
|
-
private loadingWarning: HTMLElement;
|
198
|
-
private accessibilityAnnounceArea: HTMLElement;
|
199
|
-
private accessibilityControlArea: HTMLTextAreaElement;
|
200
|
-
private eventListenerTargets: HTMLElement[] = [];
|
201
|
-
|
202
|
-
private settings: EditorSettings;
|
203
|
-
|
204
|
-
/**
|
205
|
-
* @example
|
206
|
-
* ```
|
207
|
-
* const container = document.body;
|
208
|
-
*
|
209
|
-
* // Create an editor
|
210
|
-
* const editor = new Editor(container, {
|
211
|
-
* // 2e-10 and 1e12 are the default values for minimum/maximum zoom.
|
212
|
-
* minZoom: 2e-10,
|
213
|
-
* maxZoom: 1e12,
|
214
|
-
* });
|
215
|
-
*
|
216
|
-
* // Add the default toolbar
|
217
|
-
* const toolbar = editor.addToolbar();
|
218
|
-
*
|
219
|
-
* // Add a save button
|
220
|
-
* toolbar.addActionButton({
|
221
|
-
* label: 'Save'
|
222
|
-
* icon: createSaveIcon(),
|
223
|
-
* }, () => {
|
224
|
-
* const saveData = editor.toSVG().outerHTML;
|
225
|
-
* // Do something with saveData
|
226
|
-
* });
|
227
|
-
* ```
|
228
|
-
*/
|
229
|
-
public constructor(
|
230
|
-
parent: HTMLElement,
|
231
|
-
settings: Partial<EditorSettings> = {},
|
232
|
-
) {
|
233
|
-
this.localization = {
|
234
|
-
...getLocalizationTable(),
|
235
|
-
...settings.localization,
|
236
|
-
};
|
237
|
-
|
238
|
-
// Fill default settings.
|
239
|
-
this.settings = {
|
240
|
-
wheelEventsEnabled: settings.wheelEventsEnabled ?? true,
|
241
|
-
renderingMode: settings.renderingMode ?? RenderingMode.CanvasRenderer,
|
242
|
-
localization: this.localization,
|
243
|
-
minZoom: settings.minZoom ?? 2e-10,
|
244
|
-
maxZoom: settings.maxZoom ?? 1e12,
|
245
|
-
keyboardShortcutOverrides: settings.keyboardShortcutOverrides ?? {},
|
246
|
-
iconProvider: settings.iconProvider ?? new IconProvider(),
|
247
|
-
notices: [],
|
248
|
-
};
|
249
|
-
this.icons = this.settings.iconProvider;
|
250
|
-
|
251
|
-
this.shortcuts = new KeyboardShortcutManager(this.settings.keyboardShortcutOverrides);
|
252
|
-
|
253
|
-
this.container = document.createElement('div');
|
254
|
-
this.renderingRegion = document.createElement('div');
|
255
|
-
this.container.appendChild(this.renderingRegion);
|
256
|
-
this.container.classList.add('imageEditorContainer', 'js-draw');
|
257
|
-
|
258
|
-
this.loadingWarning = document.createElement('div');
|
259
|
-
this.loadingWarning.classList.add('loadingMessage');
|
260
|
-
this.loadingWarning.ariaLive = 'polite';
|
261
|
-
this.container.appendChild(this.loadingWarning);
|
262
|
-
|
263
|
-
this.accessibilityControlArea = document.createElement('textarea');
|
264
|
-
this.accessibilityControlArea.setAttribute('placeholder', this.localization.accessibilityInputInstructions);
|
265
|
-
this.accessibilityControlArea.style.opacity = '0';
|
266
|
-
this.accessibilityControlArea.style.width = '0';
|
267
|
-
this.accessibilityControlArea.style.height = '0';
|
268
|
-
this.accessibilityControlArea.style.position = 'absolute';
|
269
|
-
|
270
|
-
this.accessibilityAnnounceArea = document.createElement('div');
|
271
|
-
this.accessibilityAnnounceArea.setAttribute('aria-live', 'assertive');
|
272
|
-
this.accessibilityAnnounceArea.className = 'accessibilityAnnouncement';
|
273
|
-
this.container.appendChild(this.accessibilityAnnounceArea);
|
274
|
-
|
275
|
-
this.renderingRegion.style.touchAction = 'none';
|
276
|
-
this.renderingRegion.className = 'imageEditorRenderArea';
|
277
|
-
this.renderingRegion.appendChild(this.accessibilityControlArea);
|
278
|
-
this.renderingRegion.setAttribute('tabIndex', '0');
|
279
|
-
this.renderingRegion.setAttribute('alt', '');
|
280
|
-
|
281
|
-
this.notifier = new EventDispatcher();
|
282
|
-
this.viewport = new Viewport((oldTransform, newTransform) => {
|
283
|
-
this.notifier.dispatch(EditorEventType.ViewportChanged, {
|
284
|
-
kind: EditorEventType.ViewportChanged,
|
285
|
-
newTransform,
|
286
|
-
oldTransform,
|
287
|
-
});
|
288
|
-
});
|
289
|
-
this.display = new Display(this, this.settings.renderingMode, this.renderingRegion);
|
290
|
-
this.image = new EditorImage();
|
291
|
-
this.history = new UndoRedoHistory(this, this.announceRedoCallback, this.announceUndoCallback);
|
292
|
-
this.toolController = new ToolController(this, this.localization);
|
293
|
-
|
294
|
-
// TODO: Make this pipeline configurable (e.g. allow users to add global input stabilization)
|
295
|
-
this.toolController.addInputMapper(StrokeKeyboardControl.fromEditor(this));
|
296
|
-
|
297
|
-
parent.appendChild(this.container);
|
298
|
-
|
299
|
-
this.viewport.updateScreenSize(
|
300
|
-
Vec2.of(this.display.width, this.display.height)
|
301
|
-
);
|
302
|
-
|
303
|
-
this.registerListeners();
|
304
|
-
this.queueRerender();
|
305
|
-
this.hideLoadingWarning();
|
306
|
-
|
307
|
-
|
308
|
-
// Enforce zoom limits.
|
309
|
-
this.notifier.on(EditorEventType.ViewportChanged, evt => {
|
310
|
-
if (evt.kind === EditorEventType.ViewportChanged) {
|
311
|
-
const zoom = evt.newTransform.transformVec3(Vec2.unitX).length();
|
312
|
-
if (zoom > this.settings.maxZoom || zoom < this.settings.minZoom) {
|
313
|
-
const oldZoom = evt.oldTransform.transformVec3(Vec2.unitX).length();
|
314
|
-
let resetTransform = Mat33.identity;
|
315
|
-
|
316
|
-
if (oldZoom <= this.settings.maxZoom && oldZoom >= this.settings.minZoom) {
|
317
|
-
resetTransform = evt.oldTransform;
|
318
|
-
}
|
319
|
-
|
320
|
-
this.viewport.resetTransform(resetTransform);
|
321
|
-
}
|
322
|
-
}
|
323
|
-
});
|
324
|
-
}
|
325
|
-
|
326
|
-
/**
|
327
|
-
* @returns a reference to the editor's container.
|
328
|
-
*
|
329
|
-
* @example
|
330
|
-
* ```
|
331
|
-
* // Set the editor's height to 500px
|
332
|
-
* editor.getRootElement().style.height = '500px';
|
333
|
-
* ```
|
334
|
-
*/
|
335
|
-
public getRootElement(): HTMLElement {
|
336
|
-
return this.container;
|
337
|
-
}
|
338
|
-
|
339
|
-
/** @param fractionLoaded - should be a number from 0 to 1, where 1 represents completely loaded. */
|
340
|
-
public showLoadingWarning(fractionLoaded: number) {
|
341
|
-
const loadingPercent = Math.round(fractionLoaded * 100);
|
342
|
-
this.loadingWarning.innerText = this.localization.loading(loadingPercent);
|
343
|
-
this.loadingWarning.style.display = 'block';
|
344
|
-
}
|
345
|
-
|
346
|
-
public hideLoadingWarning() {
|
347
|
-
this.loadingWarning.style.display = 'none';
|
348
|
-
|
349
|
-
this.announceForAccessibility(this.localization.doneLoading);
|
350
|
-
}
|
351
|
-
|
352
|
-
private previousAccessibilityAnnouncement: string = '';
|
353
|
-
|
354
|
-
/**
|
355
|
-
* Announce `message` for screen readers. If `message` is the same as the previous
|
356
|
-
* message, it is re-announced.
|
357
|
-
*/
|
358
|
-
public announceForAccessibility(message: string) {
|
359
|
-
// Force re-announcing an announcement if announced again.
|
360
|
-
if (message === this.previousAccessibilityAnnouncement) {
|
361
|
-
message = message + '. ';
|
362
|
-
}
|
363
|
-
this.accessibilityAnnounceArea.innerText = message;
|
364
|
-
this.previousAccessibilityAnnouncement = message;
|
365
|
-
}
|
366
|
-
|
367
|
-
/**
|
368
|
-
* Creates a toolbar. If `defaultLayout` is true, default buttons are used.
|
369
|
-
* @returns a reference to the toolbar.
|
370
|
-
*/
|
371
|
-
public addToolbar(defaultLayout: boolean = true): AbstractToolbar {
|
372
|
-
const toolbar = new EdgeToolbar(this, this.container, this.localization);
|
373
|
-
|
374
|
-
if (defaultLayout) {
|
375
|
-
toolbar.addDefaults();
|
376
|
-
}
|
377
|
-
|
378
|
-
return toolbar;
|
379
|
-
}
|
380
|
-
|
381
|
-
private registerListeners() {
|
382
|
-
this.handlePointerEventsFrom(this.renderingRegion);
|
383
|
-
this.handleKeyEventsFrom(this.renderingRegion);
|
384
|
-
|
385
|
-
this.container.addEventListener('wheel', evt => {
|
386
|
-
let delta = Vec3.of(evt.deltaX, evt.deltaY, evt.deltaZ);
|
387
|
-
|
388
|
-
// Process wheel events if the ctrl key is down, even if disabled -- we do want to handle
|
389
|
-
// pinch-zooming.
|
390
|
-
if (!evt.ctrlKey && !evt.metaKey) {
|
391
|
-
if (!this.settings.wheelEventsEnabled) {
|
392
|
-
return;
|
393
|
-
} else if (this.settings.wheelEventsEnabled === 'only-if-focused') {
|
394
|
-
const focusedChild = this.container.querySelector(':focus');
|
395
|
-
|
396
|
-
if (!focusedChild) {
|
397
|
-
return;
|
398
|
-
}
|
399
|
-
}
|
400
|
-
}
|
401
|
-
|
402
|
-
if (evt.deltaMode === WheelEvent.DOM_DELTA_LINE) {
|
403
|
-
delta = delta.times(15);
|
404
|
-
} else if (evt.deltaMode === WheelEvent.DOM_DELTA_PAGE) {
|
405
|
-
delta = delta.times(100);
|
406
|
-
}
|
407
|
-
|
408
|
-
if (evt.ctrlKey || evt.metaKey) {
|
409
|
-
delta = Vec3.of(0, 0, evt.deltaY);
|
410
|
-
}
|
411
|
-
|
412
|
-
// Ensure that `pos` is relative to `this.renderingRegion`
|
413
|
-
const bbox = this.renderingRegion.getBoundingClientRect();
|
414
|
-
const pos = Vec2.of(evt.clientX, evt.clientY).minus(Vec2.of(bbox.left, bbox.top));
|
415
|
-
|
416
|
-
if (this.toolController.dispatchInputEvent({
|
417
|
-
kind: InputEvtType.WheelEvt,
|
418
|
-
delta,
|
419
|
-
screenPos: pos,
|
420
|
-
})) {
|
421
|
-
evt.preventDefault();
|
422
|
-
return true;
|
423
|
-
}
|
424
|
-
return false;
|
425
|
-
});
|
426
|
-
|
427
|
-
const handleResize = () => {
|
428
|
-
this.viewport.updateScreenSize(
|
429
|
-
Vec2.of(this.display.width, this.display.height)
|
430
|
-
);
|
431
|
-
this.rerender();
|
432
|
-
this.updateEditorSizeVariables();
|
433
|
-
};
|
434
|
-
|
435
|
-
if ('ResizeObserver' in (window as any)) {
|
436
|
-
const resizeObserver = new ResizeObserver(handleResize);
|
437
|
-
resizeObserver.observe(this.renderingRegion);
|
438
|
-
resizeObserver.observe(this.container);
|
439
|
-
} else {
|
440
|
-
addEventListener('resize', handleResize);
|
441
|
-
}
|
442
|
-
|
443
|
-
this.accessibilityControlArea.addEventListener('input', () => {
|
444
|
-
this.accessibilityControlArea.value = '';
|
445
|
-
});
|
446
|
-
|
447
|
-
document.addEventListener('copy', evt => {
|
448
|
-
if (!this.isEventSink(document.querySelector(':focus'))) {
|
449
|
-
return;
|
450
|
-
}
|
451
|
-
|
452
|
-
const clipboardData = evt.clipboardData;
|
453
|
-
|
454
|
-
if (this.toolController.dispatchInputEvent({
|
455
|
-
kind: InputEvtType.CopyEvent,
|
456
|
-
setData: (mime, data) => {
|
457
|
-
clipboardData?.setData(mime, data);
|
458
|
-
},
|
459
|
-
})) {
|
460
|
-
evt.preventDefault();
|
461
|
-
}
|
462
|
-
});
|
463
|
-
|
464
|
-
document.addEventListener('paste', evt => {
|
465
|
-
this.handlePaste(evt);
|
466
|
-
});
|
467
|
-
}
|
468
|
-
|
469
|
-
private updateEditorSizeVariables() {
|
470
|
-
// Add CSS variables so that absolutely-positioned children of the editor can
|
471
|
-
// still fill the screen.
|
472
|
-
this.container.style.setProperty(
|
473
|
-
'--editor-current-width-px', `${this.container.clientWidth}px`
|
474
|
-
);
|
475
|
-
this.container.style.setProperty(
|
476
|
-
'--editor-current-height-px', `${this.container.clientHeight}px`
|
477
|
-
);
|
478
|
-
}
|
479
|
-
|
480
|
-
private pointers: Record<number, Pointer> = {};
|
481
|
-
private getPointerList() {
|
482
|
-
const nowTime = performance.now();
|
483
|
-
|
484
|
-
const res: Pointer[] = [];
|
485
|
-
for (const id in this.pointers) {
|
486
|
-
const maxUnupdatedTime = 2000; // Maximum time without a pointer update (ms)
|
487
|
-
if (this.pointers[id] && (nowTime - this.pointers[id].timeStamp) < maxUnupdatedTime) {
|
488
|
-
res.push(this.pointers[id]);
|
489
|
-
}
|
490
|
-
}
|
491
|
-
return res;
|
492
|
-
}
|
493
|
-
|
494
|
-
/**
|
495
|
-
* Dispatches a `PointerEvent` to the editor. The target element for `evt` must have the same top left
|
496
|
-
* as the content of the editor.
|
497
|
-
*/
|
498
|
-
public handleHTMLPointerEvent(eventType: 'pointerdown'|'pointermove'|'pointerup'|'pointercancel', evt: PointerEvent): boolean {
|
499
|
-
const eventsRelativeTo = this.renderingRegion;
|
500
|
-
const eventTarget = (evt.target as HTMLElement|null) ?? this.renderingRegion;
|
501
|
-
|
502
|
-
if (eventType === 'pointerdown') {
|
503
|
-
const pointer = Pointer.ofEvent(evt, true, this.viewport, eventsRelativeTo);
|
504
|
-
this.pointers[pointer.id] = pointer;
|
505
|
-
|
506
|
-
eventTarget.setPointerCapture(pointer.id);
|
507
|
-
const event: PointerEvt = {
|
508
|
-
kind: InputEvtType.PointerDownEvt,
|
509
|
-
current: pointer,
|
510
|
-
allPointers: this.getPointerList(),
|
511
|
-
};
|
512
|
-
this.toolController.dispatchInputEvent(event);
|
513
|
-
|
514
|
-
return true;
|
515
|
-
}
|
516
|
-
else if (eventType === 'pointermove') {
|
517
|
-
const pointer = Pointer.ofEvent(
|
518
|
-
evt, this.pointers[evt.pointerId]?.down ?? false, this.viewport, eventsRelativeTo
|
519
|
-
);
|
520
|
-
if (pointer.down) {
|
521
|
-
const prevData = this.pointers[pointer.id];
|
522
|
-
|
523
|
-
if (prevData) {
|
524
|
-
const distanceMoved = pointer.screenPos.minus(prevData.screenPos).magnitude();
|
525
|
-
|
526
|
-
// If the pointer moved less than two pixels, don't send a new event.
|
527
|
-
if (distanceMoved < 2) {
|
528
|
-
return false;
|
529
|
-
}
|
530
|
-
}
|
531
|
-
|
532
|
-
this.pointers[pointer.id] = pointer;
|
533
|
-
if (this.toolController.dispatchInputEvent({
|
534
|
-
kind: InputEvtType.PointerMoveEvt,
|
535
|
-
current: pointer,
|
536
|
-
allPointers: this.getPointerList(),
|
537
|
-
})) {
|
538
|
-
evt.preventDefault();
|
539
|
-
}
|
540
|
-
}
|
541
|
-
return true;
|
542
|
-
}
|
543
|
-
else if (eventType === 'pointercancel' || eventType === 'pointerup') {
|
544
|
-
const pointer = Pointer.ofEvent(evt, false, this.viewport, eventsRelativeTo);
|
545
|
-
if (!this.pointers[pointer.id]) {
|
546
|
-
return false;
|
547
|
-
}
|
548
|
-
|
549
|
-
this.pointers[pointer.id] = pointer;
|
550
|
-
eventTarget.releasePointerCapture(pointer.id);
|
551
|
-
if (this.toolController.dispatchInputEvent({
|
552
|
-
kind: InputEvtType.PointerUpEvt,
|
553
|
-
current: pointer,
|
554
|
-
allPointers: this.getPointerList(),
|
555
|
-
})) {
|
556
|
-
evt.preventDefault();
|
557
|
-
}
|
558
|
-
delete this.pointers[pointer.id];
|
559
|
-
return true;
|
560
|
-
}
|
561
|
-
|
562
|
-
return eventType;
|
563
|
-
}
|
564
|
-
|
565
|
-
private isEventSink(evtTarget: Element|EventTarget|null) {
|
566
|
-
let currentElem: Element|null = evtTarget as Element|null;
|
567
|
-
while (currentElem !== null) {
|
568
|
-
for (const elem of this.eventListenerTargets) {
|
569
|
-
if (elem === currentElem) {
|
570
|
-
return true;
|
571
|
-
}
|
572
|
-
}
|
573
|
-
|
574
|
-
currentElem = (currentElem as Element).parentElement;
|
575
|
-
}
|
576
|
-
return false;
|
577
|
-
}
|
578
|
-
|
579
|
-
private async handlePaste(evt: DragEvent|ClipboardEvent) {
|
580
|
-
const target = document.querySelector(':focus') ?? evt.target;
|
581
|
-
if (!this.isEventSink(target)) {
|
582
|
-
return;
|
583
|
-
}
|
584
|
-
|
585
|
-
const clipboardData: DataTransfer = (evt as any).dataTransfer ?? (evt as any).clipboardData;
|
586
|
-
if (!clipboardData) {
|
587
|
-
return;
|
588
|
-
}
|
589
|
-
|
590
|
-
// Handle SVG files (prefer to PNG/JPEG)
|
591
|
-
for (const file of clipboardData.files) {
|
592
|
-
if (file.type.toLowerCase() === 'image/svg+xml') {
|
593
|
-
const text = await file.text();
|
594
|
-
if (this.toolController.dispatchInputEvent({
|
595
|
-
kind: InputEvtType.PasteEvent,
|
596
|
-
mime: file.type,
|
597
|
-
data: text,
|
598
|
-
})) {
|
599
|
-
evt.preventDefault();
|
600
|
-
return;
|
601
|
-
}
|
602
|
-
}
|
603
|
-
}
|
604
|
-
|
605
|
-
// Handle image files.
|
606
|
-
for (const file of clipboardData.files) {
|
607
|
-
const fileType = file.type.toLowerCase();
|
608
|
-
if (fileType === 'image/png' || fileType === 'image/jpg') {
|
609
|
-
this.showLoadingWarning(0);
|
610
|
-
const onprogress = (evt: ProgressEvent<FileReader>) => {
|
611
|
-
this.showLoadingWarning(evt.loaded / evt.total);
|
612
|
-
};
|
613
|
-
|
614
|
-
try {
|
615
|
-
const data = await fileToBase64(file, onprogress);
|
616
|
-
|
617
|
-
if (data && this.toolController.dispatchInputEvent({
|
618
|
-
kind: InputEvtType.PasteEvent,
|
619
|
-
mime: fileType,
|
620
|
-
data: data,
|
621
|
-
})) {
|
622
|
-
evt.preventDefault();
|
623
|
-
this.hideLoadingWarning();
|
624
|
-
return;
|
625
|
-
}
|
626
|
-
} catch (e) {
|
627
|
-
console.error('Error reading image:', e);
|
628
|
-
}
|
629
|
-
this.hideLoadingWarning();
|
630
|
-
}
|
631
|
-
}
|
632
|
-
|
633
|
-
// Supported MIMEs for text data, in order of preference
|
634
|
-
const supportedMIMEs = [
|
635
|
-
'image/svg+xml',
|
636
|
-
'text/plain',
|
637
|
-
];
|
638
|
-
|
639
|
-
for (const mime of supportedMIMEs) {
|
640
|
-
const data = clipboardData.getData(mime);
|
641
|
-
|
642
|
-
if (data && this.toolController.dispatchInputEvent({
|
643
|
-
kind: InputEvtType.PasteEvent,
|
644
|
-
mime,
|
645
|
-
data,
|
646
|
-
})) {
|
647
|
-
evt.preventDefault();
|
648
|
-
return;
|
649
|
-
}
|
650
|
-
}
|
651
|
-
}
|
652
|
-
|
653
|
-
/**
|
654
|
-
* Forward pointer events from `elem` to this editor. Such that right-click/right-click drag
|
655
|
-
* events are also forwarded, `elem`'s contextmenu is disabled.
|
656
|
-
*
|
657
|
-
* `filter` is called once per pointer event, before doing any other processing. If `filter` returns `true` the event is
|
658
|
-
* forwarded to the editor.
|
659
|
-
*
|
660
|
-
* **Note**: `otherEventsFilter` is like `filter`, but is called for other pointer-related
|
661
|
-
* events that could also be forwarded to the editor. To forward just pointer events,
|
662
|
-
* for example, `otherEventsFilter` could be given as `()=>false`.
|
663
|
-
*
|
664
|
-
* @example
|
665
|
-
* ```ts
|
666
|
-
* const overlay = document.createElement('div');
|
667
|
-
* editor.createHTMLOverlay(overlay);
|
668
|
-
*
|
669
|
-
* // Send all pointer events that don't have the control key pressed
|
670
|
-
* // to the editor.
|
671
|
-
* editor.handlePointerEventsFrom(overlay, (event) => {
|
672
|
-
* if (event.ctrlKey) {
|
673
|
-
* return false;
|
674
|
-
* }
|
675
|
-
* return true;
|
676
|
-
* });
|
677
|
-
* ```
|
678
|
-
*/
|
679
|
-
public handlePointerEventsFrom(
|
680
|
-
elem: HTMLElement,
|
681
|
-
filter?: HTMLPointerEventFilter,
|
682
|
-
otherEventsFilter?: (eventName: string, event: Event)=>boolean,
|
683
|
-
) {
|
684
|
-
// May be required to prevent text selection on iOS/Safari:
|
685
|
-
// See https://stackoverflow.com/a/70992717/17055750
|
686
|
-
const touchstartListener = (evt: Event) => {
|
687
|
-
if (otherEventsFilter && !otherEventsFilter('touchstart', evt)) {
|
688
|
-
return;
|
689
|
-
}
|
690
|
-
|
691
|
-
evt.preventDefault();
|
692
|
-
};
|
693
|
-
const contextmenuListener = (evt: Event) => {
|
694
|
-
if (otherEventsFilter && !otherEventsFilter('contextmenu', evt)) {
|
695
|
-
return;
|
696
|
-
}
|
697
|
-
|
698
|
-
// Don't show a context menu
|
699
|
-
evt.preventDefault();
|
700
|
-
};
|
701
|
-
|
702
|
-
const listeners: Record<string, (event: Event)=>void> = {
|
703
|
-
'touchstart': touchstartListener,
|
704
|
-
'contextmenu': contextmenuListener,
|
705
|
-
};
|
706
|
-
|
707
|
-
const eventNames: HTMLPointerEventName[] = ['pointerdown', 'pointermove', 'pointerup', 'pointercancel'];
|
708
|
-
for (const eventName of eventNames) {
|
709
|
-
listeners[eventName] = (evt: Event) => {
|
710
|
-
// This listener will only be called in the context of PointerEvents.
|
711
|
-
const event = evt as PointerEvent;
|
712
|
-
|
713
|
-
if (filter && !filter(eventName, event)) {
|
714
|
-
return undefined;
|
715
|
-
}
|
716
|
-
|
717
|
-
return this.handleHTMLPointerEvent(eventName, event);
|
718
|
-
};
|
719
|
-
}
|
720
|
-
|
721
|
-
// Add all listeners.
|
722
|
-
for (const eventName in listeners) {
|
723
|
-
elem.addEventListener(eventName, listeners[eventName]);
|
724
|
-
}
|
725
|
-
|
726
|
-
return {
|
727
|
-
/** Remove all event listeners registered by this function. */
|
728
|
-
remove: () => {
|
729
|
-
for (const eventName in listeners) {
|
730
|
-
elem.removeEventListener(eventName, listeners[eventName]);
|
731
|
-
}
|
732
|
-
},
|
733
|
-
};
|
734
|
-
}
|
735
|
-
|
736
|
-
/**
|
737
|
-
* Like {@link handlePointerEventsFrom} except ignores short input gestures like clicks.
|
738
|
-
*
|
739
|
-
* `filter` is called once per event, before doing any other processing. If `filter` returns `true` the event is
|
740
|
-
* forwarded to the editor.
|
741
|
-
*
|
742
|
-
* `otherEventsFilter` is passed unmodified to `handlePointerEventsFrom`.
|
743
|
-
*/
|
744
|
-
public handlePointerEventsExceptClicksFrom(
|
745
|
-
elem: HTMLElement,
|
746
|
-
filter?: HTMLPointerEventFilter,
|
747
|
-
otherEventsFilter?: (eventName: string, event: Event)=>boolean,
|
748
|
-
) {
|
749
|
-
type GestureRecord = {
|
750
|
-
// Buffer events: Send events to the editor only if the pointer has moved enough to
|
751
|
-
// suggest that the user is attempting to draw, rather than click to close the color picker.
|
752
|
-
eventBuffer: [ HTMLPointerEventName, PointerEvent ][];
|
753
|
-
startPoint: Point2;
|
754
|
-
};
|
755
|
-
|
756
|
-
// Maps pointer IDs to gesture start points
|
757
|
-
const gestureData: Record<number, GestureRecord> = Object.create(null);
|
758
|
-
|
759
|
-
return this.handlePointerEventsFrom(elem, (eventName, event) => {
|
760
|
-
if (filter && !filter(eventName, event)) {
|
761
|
-
return false;
|
762
|
-
}
|
763
|
-
|
764
|
-
// Position of the current event.
|
765
|
-
const currentPos = Vec2.of(event.pageX, event.pageY);
|
766
|
-
|
767
|
-
const pointerId = event.pointerId ?? 0;
|
768
|
-
|
769
|
-
// Whether to send the current event to the editor
|
770
|
-
let sendToEditor = true;
|
771
|
-
|
772
|
-
if (eventName === 'pointerdown') {
|
773
|
-
// Buffer the event, but don't send it to the editor yet.
|
774
|
-
// We don't want to send single-click events, but we do want to send full strokes.
|
775
|
-
gestureData[pointerId] = {
|
776
|
-
eventBuffer: [ [eventName, event] ],
|
777
|
-
startPoint: currentPos,
|
778
|
-
};
|
779
|
-
|
780
|
-
// Capture the pointer so we receive future events even if the overlay is hidden.
|
781
|
-
elem.setPointerCapture(event.pointerId);
|
782
|
-
|
783
|
-
// Don't send to the editor.
|
784
|
-
sendToEditor = false;
|
785
|
-
}
|
786
|
-
else if (eventName === 'pointermove' && gestureData[pointerId]) {
|
787
|
-
const gestureStartPos = gestureData[pointerId].startPoint;
|
788
|
-
const eventBuffer = gestureData[pointerId].eventBuffer;
|
789
|
-
|
790
|
-
// Skip if the pointer hasn't moved enough to not be a "click".
|
791
|
-
const strokeStartThreshold = 10;
|
792
|
-
const isWithinClickThreshold = gestureStartPos && currentPos.minus(gestureStartPos).magnitude() < strokeStartThreshold;
|
793
|
-
if (isWithinClickThreshold) {
|
794
|
-
eventBuffer.push([ eventName, event ]);
|
795
|
-
sendToEditor = false;
|
796
|
-
} else {
|
797
|
-
// Send all buffered events to the editor -- start the stroke.
|
798
|
-
for (const [ eventName, event ] of eventBuffer) {
|
799
|
-
this.handleHTMLPointerEvent(eventName, event);
|
800
|
-
}
|
801
|
-
|
802
|
-
gestureData[pointerId].eventBuffer = [];
|
803
|
-
sendToEditor = true;
|
804
|
-
}
|
805
|
-
}
|
806
|
-
// Pointers that aren't down -- send to the editor.
|
807
|
-
else if (eventName === 'pointermove') {
|
808
|
-
sendToEditor = true;
|
809
|
-
}
|
810
|
-
// Otherwise, if we received a pointerup/pointercancel without flushing all pointerevents from the
|
811
|
-
// buffer, the gesture wasn't recognised as a stroke. Thus, the editor isn't expecting a pointerup/
|
812
|
-
// pointercancel event.
|
813
|
-
else if (
|
814
|
-
(eventName === 'pointerup' || eventName === 'pointercancel')
|
815
|
-
&& gestureData[pointerId] && gestureData[pointerId].eventBuffer.length > 0
|
816
|
-
) {
|
817
|
-
elem.releasePointerCapture(event.pointerId);
|
818
|
-
|
819
|
-
// Don't send to the editor.
|
820
|
-
sendToEditor = false;
|
821
|
-
|
822
|
-
delete gestureData[pointerId];
|
823
|
-
}
|
824
|
-
|
825
|
-
// Forward all other events to the editor.
|
826
|
-
return sendToEditor;
|
827
|
-
}, otherEventsFilter);
|
828
|
-
}
|
829
|
-
|
830
|
-
/**
|
831
|
-
* Adds event listners for keypresses (and drop events) on `elem` and forwards those
|
832
|
-
* events to the editor.
|
833
|
-
*
|
834
|
-
* If the given `filter` returns `false` for an event, the event is ignored and not
|
835
|
-
* passed to the editor.
|
836
|
-
*/
|
837
|
-
public handleKeyEventsFrom(
|
838
|
-
elem: HTMLElement,
|
839
|
-
filter: (event: KeyboardEvent)=>boolean = ()=>true
|
840
|
-
) {
|
841
|
-
// Track which keys are down so we can release them when the element
|
842
|
-
// loses focus. This is particularly important for keys like Control
|
843
|
-
// that can trigger shortcuts that cause the editor to lose focus before
|
844
|
-
// the keyup event is triggered.
|
845
|
-
let keysDown: KeyPressEvent[] = [];
|
846
|
-
|
847
|
-
type KeyEventLike = { key: string; code: string };
|
848
|
-
|
849
|
-
// Return whether two objects that are similar to keyboard events represent the
|
850
|
-
// same key.
|
851
|
-
const keyEventsMatch = (a: KeyEventLike, b: KeyEventLike) => {
|
852
|
-
return a.key === b.key && a.code === b.code;
|
853
|
-
};
|
854
|
-
|
855
|
-
elem.addEventListener('keydown', htmlEvent => {
|
856
|
-
if (!filter(htmlEvent)) {
|
857
|
-
return;
|
858
|
-
}
|
859
|
-
|
860
|
-
const event = keyPressEventFromHTMLEvent(htmlEvent);
|
861
|
-
|
862
|
-
// Add event to the list of keys that are down (so long as it
|
863
|
-
// isn't a duplicate).
|
864
|
-
if (!keysDown.some(other => keyEventsMatch(other, event))) {
|
865
|
-
keysDown.push(event);
|
866
|
-
}
|
867
|
-
|
868
|
-
if (event.key === 't' || event.key === 'T') {
|
869
|
-
htmlEvent.preventDefault();
|
870
|
-
this.display.rerenderAsText();
|
871
|
-
} else if (this.toolController.dispatchInputEvent(event)) {
|
872
|
-
htmlEvent.preventDefault();
|
873
|
-
} else if (event.key === 'Escape') {
|
874
|
-
this.renderingRegion.blur();
|
875
|
-
}
|
876
|
-
});
|
877
|
-
|
878
|
-
elem.addEventListener('keyup', htmlEvent => {
|
879
|
-
// Remove the key from keysDown -- it's no longer down.
|
880
|
-
keysDown = keysDown.filter(event => {
|
881
|
-
const matches = keyEventsMatch(event, htmlEvent);
|
882
|
-
return !matches;
|
883
|
-
});
|
884
|
-
|
885
|
-
if (!filter(htmlEvent)) {
|
886
|
-
return;
|
887
|
-
}
|
888
|
-
|
889
|
-
const event = keyUpEventFromHTMLEvent(htmlEvent);
|
890
|
-
if (this.toolController.dispatchInputEvent(event)) {
|
891
|
-
htmlEvent.preventDefault();
|
892
|
-
}
|
893
|
-
});
|
894
|
-
|
895
|
-
elem.addEventListener('blur', () => {
|
896
|
-
for (const event of keysDown) {
|
897
|
-
this.toolController.dispatchInputEvent({
|
898
|
-
...event,
|
899
|
-
kind: InputEvtType.KeyUpEvent,
|
900
|
-
});
|
901
|
-
}
|
902
|
-
});
|
903
|
-
|
904
|
-
// Allow drop.
|
905
|
-
elem.ondragover = evt => {
|
906
|
-
evt.preventDefault();
|
907
|
-
};
|
908
|
-
|
909
|
-
elem.ondrop = evt => {
|
910
|
-
evt.preventDefault();
|
911
|
-
this.handlePaste(evt);
|
912
|
-
};
|
913
|
-
|
914
|
-
this.eventListenerTargets.push(elem);
|
915
|
-
}
|
916
|
-
|
917
|
-
/** `apply` a command. `command` will be announced for accessibility. */
|
918
|
-
public dispatch(command: Command, addToHistory: boolean = true) {
|
919
|
-
const dispatchResult = this.dispatchNoAnnounce(command, addToHistory);
|
920
|
-
this.announceForAccessibility(command.description(this, this.localization));
|
921
|
-
|
922
|
-
return dispatchResult;
|
923
|
-
}
|
924
|
-
|
925
|
-
/**
|
926
|
-
* Dispatches a command without announcing it. By default, does not add to history.
|
927
|
-
* Use this to show finalized commands that don't need to have `announceForAccessibility`
|
928
|
-
* called.
|
929
|
-
*
|
930
|
-
* Prefer `command.apply(editor)` for incomplete commands. `dispatchNoAnnounce` may allow
|
931
|
-
* clients to listen for the application of commands (e.g. `SerializableCommand`s so they can
|
932
|
-
* be sent across the network), while `apply` does not.
|
933
|
-
*
|
934
|
-
* @example
|
935
|
-
* ```
|
936
|
-
* const addToHistory = false;
|
937
|
-
* editor.dispatchNoAnnounce(editor.viewport.zoomTo(someRectangle), addToHistory);
|
938
|
-
* ```
|
939
|
-
*/
|
940
|
-
public dispatchNoAnnounce(command: Command, addToHistory: boolean = false) {
|
941
|
-
const result = command.apply(this);
|
942
|
-
|
943
|
-
if (addToHistory) {
|
944
|
-
const apply = false; // Don't double-apply
|
945
|
-
this.history.push(command, apply);
|
946
|
-
}
|
947
|
-
|
948
|
-
return result;
|
949
|
-
}
|
950
|
-
|
951
|
-
/**
|
952
|
-
* Apply a large transformation in chunks.
|
953
|
-
* If `apply` is `false`, the commands are unapplied.
|
954
|
-
* Triggers a re-render after each `updateChunkSize`-sized group of commands
|
955
|
-
* has been applied.
|
956
|
-
*/
|
957
|
-
public async asyncApplyOrUnapplyCommands(
|
958
|
-
commands: Command[], apply: boolean, updateChunkSize: number
|
959
|
-
) {
|
960
|
-
console.assert(updateChunkSize > 0);
|
961
|
-
this.display.setDraftMode(true);
|
962
|
-
for (let i = 0; i < commands.length; i += updateChunkSize) {
|
963
|
-
this.showLoadingWarning(i / commands.length);
|
964
|
-
|
965
|
-
for (let j = i; j < commands.length && j < i + updateChunkSize; j++) {
|
966
|
-
const cmd = commands[j];
|
967
|
-
|
968
|
-
if (apply) {
|
969
|
-
cmd.apply(this);
|
970
|
-
} else {
|
971
|
-
cmd.unapply(this);
|
972
|
-
}
|
973
|
-
}
|
974
|
-
|
975
|
-
// Re-render to show progress, but only if we're not done.
|
976
|
-
if (i + updateChunkSize < commands.length) {
|
977
|
-
await new Promise(resolve => {
|
978
|
-
this.rerender();
|
979
|
-
requestAnimationFrame(resolve);
|
980
|
-
});
|
981
|
-
}
|
982
|
-
}
|
983
|
-
this.display.setDraftMode(false);
|
984
|
-
this.hideLoadingWarning();
|
985
|
-
}
|
986
|
-
|
987
|
-
// @see {@link #asyncApplyOrUnapplyCommands }
|
988
|
-
public asyncApplyCommands(commands: Command[], chunkSize: number) {
|
989
|
-
return this.asyncApplyOrUnapplyCommands(commands, true, chunkSize);
|
990
|
-
}
|
991
|
-
|
992
|
-
// If `unapplyInReverseOrder`, commands are reversed before unapplying.
|
993
|
-
// @see {@link #asyncApplyOrUnapplyCommands }
|
994
|
-
public asyncUnapplyCommands(commands: Command[], chunkSize: number, unapplyInReverseOrder: boolean = false) {
|
995
|
-
if (unapplyInReverseOrder) {
|
996
|
-
commands = [ ...commands ]; // copy
|
997
|
-
commands.reverse();
|
998
|
-
}
|
999
|
-
|
1000
|
-
return this.asyncApplyOrUnapplyCommands(commands, false, chunkSize);
|
1001
|
-
}
|
1002
|
-
|
1003
|
-
private announceUndoCallback = (command: Command) => {
|
1004
|
-
this.announceForAccessibility(this.localization.undoAnnouncement(command.description(this, this.localization)));
|
1005
|
-
};
|
1006
|
-
|
1007
|
-
private announceRedoCallback = (command: Command) => {
|
1008
|
-
this.announceForAccessibility(this.localization.redoAnnouncement(command.description(this, this.localization)));
|
1009
|
-
};
|
1010
|
-
|
1011
|
-
// Listeners to be called once at the end of the next re-render.
|
1012
|
-
private nextRerenderListeners: Array<()=> void> = [];
|
1013
|
-
private rerenderQueued: boolean = false;
|
1014
|
-
|
1015
|
-
/**
|
1016
|
-
* Schedule a re-render for some time in the near future. Does not schedule an additional
|
1017
|
-
* re-render if a re-render is already queued.
|
1018
|
-
*
|
1019
|
-
* @returns a promise that resolves when re-rendering has completed.
|
1020
|
-
*/
|
1021
|
-
public queueRerender(): Promise<void> {
|
1022
|
-
if (!this.rerenderQueued) {
|
1023
|
-
this.rerenderQueued = true;
|
1024
|
-
requestAnimationFrame(() => {
|
1025
|
-
// If .rerender was called manually, we might not need to
|
1026
|
-
// re-render.
|
1027
|
-
if (this.rerenderQueued) {
|
1028
|
-
this.rerender();
|
1029
|
-
this.rerenderQueued = false;
|
1030
|
-
}
|
1031
|
-
});
|
1032
|
-
}
|
1033
|
-
|
1034
|
-
return new Promise(resolve => {
|
1035
|
-
this.nextRerenderListeners.push(() => resolve());
|
1036
|
-
});
|
1037
|
-
}
|
1038
|
-
|
1039
|
-
// @internal
|
1040
|
-
public isRerenderQueued() {
|
1041
|
-
return this.rerenderQueued;
|
1042
|
-
}
|
1043
|
-
|
1044
|
-
/**
|
1045
|
-
* Re-renders the entire image.
|
1046
|
-
*
|
1047
|
-
* @see {@link Editor.queueRerender}
|
1048
|
-
*/
|
1049
|
-
public rerender(showImageBounds: boolean = true) {
|
1050
|
-
this.display.startRerender();
|
1051
|
-
|
1052
|
-
// Don't render if the display has zero size.
|
1053
|
-
if (this.display.width === 0 || this.display.height === 0) {
|
1054
|
-
return;
|
1055
|
-
}
|
1056
|
-
|
1057
|
-
const renderer = this.display.getDryInkRenderer();
|
1058
|
-
|
1059
|
-
this.image.renderWithCache(renderer, this.display.getCache(), this.viewport);
|
1060
|
-
|
1061
|
-
if (showImageBounds) {
|
1062
|
-
// Draw a rectangle around the region that will be visible on save
|
1063
|
-
const exportRectFill = { fill: Color4.fromHex('#44444455') };
|
1064
|
-
const exportRectStrokeWidth = 5 * this.viewport.getSizeOfPixelOnCanvas();
|
1065
|
-
renderer.drawRect(
|
1066
|
-
this.getImportExportRect(),
|
1067
|
-
exportRectStrokeWidth,
|
1068
|
-
exportRectFill
|
1069
|
-
);
|
1070
|
-
}
|
1071
|
-
|
1072
|
-
this.rerenderQueued = false;
|
1073
|
-
this.nextRerenderListeners.forEach(listener => listener());
|
1074
|
-
this.nextRerenderListeners = [];
|
1075
|
-
}
|
1076
|
-
|
1077
|
-
/**
|
1078
|
-
* Draws the given path onto the wet ink renderer. The given path will
|
1079
|
-
* be displayed on top of the main image.
|
1080
|
-
*
|
1081
|
-
* @see {@link Display.getWetInkRenderer} {@link Display.flatten}
|
1082
|
-
*/
|
1083
|
-
public drawWetInk(...path: RenderablePathSpec[]) {
|
1084
|
-
for (const part of path) {
|
1085
|
-
this.display.getWetInkRenderer().drawPath(part);
|
1086
|
-
}
|
1087
|
-
}
|
1088
|
-
|
1089
|
-
/**
|
1090
|
-
* Clears the wet ink display.
|
1091
|
-
*
|
1092
|
-
* @see {@link Display.getWetInkRenderer}
|
1093
|
-
*/
|
1094
|
-
public clearWetInk() {
|
1095
|
-
this.display.getWetInkRenderer().clear();
|
1096
|
-
}
|
1097
|
-
|
1098
|
-
/**
|
1099
|
-
* Focuses the region used for text input/key commands.
|
1100
|
-
*/
|
1101
|
-
public focus() {
|
1102
|
-
this.renderingRegion.focus();
|
1103
|
-
}
|
1104
|
-
|
1105
|
-
/**
|
1106
|
-
* Creates an element that will be positioned on top of the dry/wet ink
|
1107
|
-
* renderers.
|
1108
|
-
*
|
1109
|
-
* This is useful for displaying content on top of the rendered content
|
1110
|
-
* (e.g. a selection box).
|
1111
|
-
*/
|
1112
|
-
public createHTMLOverlay(overlay: HTMLElement) {
|
1113
|
-
overlay.classList.add('overlay');
|
1114
|
-
this.container.appendChild(overlay);
|
1115
|
-
|
1116
|
-
return {
|
1117
|
-
remove: () => overlay.remove(),
|
1118
|
-
};
|
1119
|
-
}
|
1120
|
-
|
1121
|
-
public addStyleSheet(content: string): HTMLStyleElement {
|
1122
|
-
const styleSheet = document.createElement('style');
|
1123
|
-
styleSheet.innerText = content;
|
1124
|
-
this.container.appendChild(styleSheet);
|
1125
|
-
|
1126
|
-
return styleSheet;
|
1127
|
-
}
|
1128
|
-
|
1129
|
-
/**
|
1130
|
-
* Dispatch a keyboard event to the currently selected tool.
|
1131
|
-
* Intended for unit testing.
|
1132
|
-
*
|
1133
|
-
* If `shiftKey` is undefined, it is guessed from `key`.
|
1134
|
-
*
|
1135
|
-
* At present, the **key code** dispatched is guessed from the given key and,
|
1136
|
-
* while this works for ASCII alphanumeric characters, this does not work for
|
1137
|
-
* most non-alphanumeric keys.
|
1138
|
-
*
|
1139
|
-
* Because guessing the key code from `key` is problematic, **only use this for testing**.
|
1140
|
-
*/
|
1141
|
-
public sendKeyboardEvent(
|
1142
|
-
eventType: InputEvtType.KeyPressEvent|InputEvtType.KeyUpEvent,
|
1143
|
-
key: string,
|
1144
|
-
ctrlKey: boolean = false,
|
1145
|
-
altKey: boolean = false,
|
1146
|
-
shiftKey: boolean|undefined = undefined,
|
1147
|
-
) {
|
1148
|
-
shiftKey ??= key.toUpperCase() === key && key.toLowerCase() !== key;
|
1149
|
-
|
1150
|
-
this.toolController.dispatchInputEvent({
|
1151
|
-
kind: eventType,
|
1152
|
-
key,
|
1153
|
-
code: guessKeyCodeFromKey(key),
|
1154
|
-
ctrlKey,
|
1155
|
-
altKey,
|
1156
|
-
shiftKey,
|
1157
|
-
});
|
1158
|
-
}
|
1159
|
-
|
1160
|
-
/**
|
1161
|
-
* Dispatch a pen event to the currently selected tool.
|
1162
|
-
* Intended primarially for unit tests.
|
1163
|
-
*
|
1164
|
-
* @deprecated
|
1165
|
-
* @see {@link sendPenEvent} {@link sendTouchEvent}
|
1166
|
-
*/
|
1167
|
-
public sendPenEvent(
|
1168
|
-
eventType: InputEvtType.PointerDownEvt|InputEvtType.PointerMoveEvt|InputEvtType.PointerUpEvt,
|
1169
|
-
point: Point2,
|
1170
|
-
|
1171
|
-
// @deprecated
|
1172
|
-
allPointers?: Pointer[]
|
1173
|
-
) {
|
1174
|
-
sendPenEvent(this, eventType, point, allPointers);
|
1175
|
-
}
|
1176
|
-
|
1177
|
-
public async addAndCenterComponents(components: AbstractComponent[], selectComponents: boolean = true) {
|
1178
|
-
let bbox: Rect2|null = null;
|
1179
|
-
for (const component of components) {
|
1180
|
-
if (bbox) {
|
1181
|
-
bbox = bbox.union(component.getBBox());
|
1182
|
-
} else {
|
1183
|
-
bbox = component.getBBox();
|
1184
|
-
}
|
1185
|
-
}
|
1186
|
-
|
1187
|
-
if (!bbox) {
|
1188
|
-
return;
|
1189
|
-
}
|
1190
|
-
|
1191
|
-
// Find a transform that scales/moves bbox onto the screen.
|
1192
|
-
const visibleRect = this.viewport.visibleRect;
|
1193
|
-
const scaleRatioX = visibleRect.width / bbox.width;
|
1194
|
-
const scaleRatioY = visibleRect.height / bbox.height;
|
1195
|
-
|
1196
|
-
let scaleRatio = scaleRatioX;
|
1197
|
-
if (bbox.width * scaleRatio > visibleRect.width || bbox.height * scaleRatio > visibleRect.height) {
|
1198
|
-
scaleRatio = scaleRatioY;
|
1199
|
-
}
|
1200
|
-
scaleRatio *= 2 / 3;
|
1201
|
-
|
1202
|
-
scaleRatio = Viewport.roundScaleRatio(scaleRatio);
|
1203
|
-
|
1204
|
-
const transfm = Mat33.translation(
|
1205
|
-
visibleRect.center.minus(bbox.center)
|
1206
|
-
).rightMul(
|
1207
|
-
Mat33.scaling2D(scaleRatio, bbox.center)
|
1208
|
-
);
|
1209
|
-
|
1210
|
-
const commands: Command[] = [];
|
1211
|
-
for (const component of components) {
|
1212
|
-
// To allow deserialization, we need to add first, then transform.
|
1213
|
-
commands.push(EditorImage.addElement(component));
|
1214
|
-
commands.push(component.transformBy(transfm));
|
1215
|
-
}
|
1216
|
-
|
1217
|
-
const applyChunkSize = 100;
|
1218
|
-
await this.dispatch(uniteCommands(commands, applyChunkSize), true);
|
1219
|
-
|
1220
|
-
if (selectComponents) {
|
1221
|
-
for (const selectionTool of this.toolController.getMatchingTools(SelectionTool)) {
|
1222
|
-
selectionTool.setEnabled(true);
|
1223
|
-
selectionTool.setSelection(components);
|
1224
|
-
}
|
1225
|
-
}
|
1226
|
-
}
|
1227
|
-
|
1228
|
-
// Get a data URL (e.g. as produced by `HTMLCanvasElement::toDataURL`).
|
1229
|
-
// If `format` is not `image/png`, a PNG image URL may still be returned (as in the
|
1230
|
-
// case of `HTMLCanvasElement::toDataURL`).
|
1231
|
-
//
|
1232
|
-
// The export resolution is the same as the size of the drawing canvas.
|
1233
|
-
public toDataURL(format: 'image/png'|'image/jpeg'|'image/webp' = 'image/png', outputSize?: Vec2): string {
|
1234
|
-
const canvas = document.createElement('canvas');
|
1235
|
-
|
1236
|
-
const importExportViewport = this.image.getImportExportViewport();
|
1237
|
-
const exportRectSize = importExportViewport.getScreenRectSize();
|
1238
|
-
const resolution = outputSize ?? exportRectSize;
|
1239
|
-
|
1240
|
-
canvas.width = resolution.x;
|
1241
|
-
canvas.height = resolution.y;
|
1242
|
-
|
1243
|
-
const ctx = canvas.getContext('2d')!;
|
1244
|
-
|
1245
|
-
// Scale to ensure that the entire output is visible.
|
1246
|
-
const scaleFactor = Math.min(resolution.x / exportRectSize.x, resolution.y / exportRectSize.y);
|
1247
|
-
ctx.scale(scaleFactor, scaleFactor);
|
1248
|
-
|
1249
|
-
const renderer = new CanvasRenderer(ctx, importExportViewport);
|
1250
|
-
|
1251
|
-
this.image.renderAll(renderer);
|
1252
|
-
|
1253
|
-
const dataURL = canvas.toDataURL(format);
|
1254
|
-
return dataURL;
|
1255
|
-
}
|
1256
|
-
|
1257
|
-
public toSVG(): SVGElement {
|
1258
|
-
const importExportViewport = this.image.getImportExportViewport().getTemporaryClone();
|
1259
|
-
|
1260
|
-
const sanitize = false;
|
1261
|
-
const { element: result, renderer } = SVGRenderer.fromViewport(importExportViewport, sanitize);
|
1262
|
-
|
1263
|
-
const origTransform = importExportViewport.canvasToScreenTransform;
|
1264
|
-
// Render with (0,0) at (0,0) — we'll handle translation with
|
1265
|
-
// the viewBox property.
|
1266
|
-
importExportViewport.resetTransform(Mat33.identity);
|
1267
|
-
|
1268
|
-
this.image.renderAll(renderer);
|
1269
|
-
|
1270
|
-
importExportViewport.resetTransform(origTransform);
|
1271
|
-
|
1272
|
-
|
1273
|
-
// Just show the main region
|
1274
|
-
const rect = importExportViewport.visibleRect;
|
1275
|
-
result.setAttribute('viewBox', [rect.x, rect.y, rect.w, rect.h].map(part => toRoundedString(part)).join(' '));
|
1276
|
-
result.setAttribute('width', toRoundedString(rect.w));
|
1277
|
-
result.setAttribute('height', toRoundedString(rect.h));
|
1278
|
-
|
1279
|
-
return result;
|
1280
|
-
}
|
1281
|
-
|
1282
|
-
/**
|
1283
|
-
* Load editor data from an `ImageLoader` (e.g. an {@link SVGLoader}).
|
1284
|
-
*
|
1285
|
-
* @see loadFromSVG
|
1286
|
-
*/
|
1287
|
-
public async loadFrom(loader: ImageLoader) {
|
1288
|
-
this.showLoadingWarning(0);
|
1289
|
-
this.display.setDraftMode(true);
|
1290
|
-
|
1291
|
-
const originalBackgrounds = this.image.getBackgroundComponents();
|
1292
|
-
const eraseBackgroundCommand = new Erase(originalBackgrounds);
|
1293
|
-
|
1294
|
-
await loader.start(async (component) => {
|
1295
|
-
await this.dispatchNoAnnounce(EditorImage.addElement(component));
|
1296
|
-
}, (countProcessed: number, totalToProcess: number) => {
|
1297
|
-
if (countProcessed % 500 === 0) {
|
1298
|
-
this.showLoadingWarning(countProcessed / totalToProcess);
|
1299
|
-
this.rerender();
|
1300
|
-
return untilNextAnimationFrame();
|
1301
|
-
}
|
1302
|
-
|
1303
|
-
return null;
|
1304
|
-
}, (importExportRect: Rect2) => {
|
1305
|
-
this.dispatchNoAnnounce(this.setImportExportRect(importExportRect), false);
|
1306
|
-
this.dispatchNoAnnounce(this.viewport.zoomTo(importExportRect), false);
|
1307
|
-
});
|
1308
|
-
|
1309
|
-
// Ensure that we don't have multiple overlapping BackgroundComponents. Remove
|
1310
|
-
// old BackgroundComponents.
|
1311
|
-
// Overlapping BackgroundComponents may cause changing the background color to
|
1312
|
-
// not work properly.
|
1313
|
-
if (this.image.getBackgroundComponents().length !== originalBackgrounds.length) {
|
1314
|
-
await this.dispatchNoAnnounce(eraseBackgroundCommand);
|
1315
|
-
}
|
1316
|
-
|
1317
|
-
this.hideLoadingWarning();
|
1318
|
-
|
1319
|
-
this.display.setDraftMode(false);
|
1320
|
-
this.queueRerender();
|
1321
|
-
}
|
1322
|
-
|
1323
|
-
private getTopmostBackgroundComponent(): BackgroundComponent|null {
|
1324
|
-
let background: BackgroundComponent|null = null;
|
1325
|
-
|
1326
|
-
// Find a background component, if one exists.
|
1327
|
-
// Use the last (topmost) background component if there are multiple.
|
1328
|
-
for (const component of this.image.getBackgroundComponents()) {
|
1329
|
-
if (component instanceof BackgroundComponent) {
|
1330
|
-
background = component;
|
1331
|
-
}
|
1332
|
-
}
|
1333
|
-
|
1334
|
-
return background;
|
1335
|
-
}
|
1336
|
-
|
1337
|
-
/**
|
1338
|
-
* Set the background color of the image.
|
1339
|
-
*/
|
1340
|
-
public setBackgroundColor(color: Color4): Command {
|
1341
|
-
let background = this.getTopmostBackgroundComponent();
|
1342
|
-
|
1343
|
-
if (!background) {
|
1344
|
-
const backgroundType = color.eq(Color4.transparent) ? BackgroundType.None : BackgroundType.SolidColor;
|
1345
|
-
background = new BackgroundComponent(backgroundType, color);
|
1346
|
-
return this.image.addElement(background);
|
1347
|
-
} else {
|
1348
|
-
return background.updateStyle({ color });
|
1349
|
-
}
|
1350
|
-
}
|
1351
|
-
|
1352
|
-
/**
|
1353
|
-
* @returns the average of the colors of all background components. Use this to get the current background
|
1354
|
-
* color.
|
1355
|
-
*/
|
1356
|
-
public estimateBackgroundColor(): Color4 {
|
1357
|
-
const backgroundColors = [];
|
1358
|
-
|
1359
|
-
for (const component of this.image.getBackgroundComponents()) {
|
1360
|
-
if (component instanceof BackgroundComponent) {
|
1361
|
-
backgroundColors.push(component.getStyle().color ?? Color4.transparent);
|
1362
|
-
}
|
1363
|
-
}
|
1364
|
-
|
1365
|
-
return Color4.average(backgroundColors);
|
1366
|
-
}
|
1367
|
-
|
1368
|
-
// Returns the size of the visible region of the output SVG
|
1369
|
-
public getImportExportRect(): Rect2 {
|
1370
|
-
return this.image.getImportExportViewport().visibleRect;
|
1371
|
-
}
|
1372
|
-
|
1373
|
-
// Resize the output SVG to match `imageRect`.
|
1374
|
-
public setImportExportRect(imageRect: Rect2): Command {
|
1375
|
-
return this.image.setImportExportRect(imageRect);
|
1376
|
-
}
|
1377
|
-
|
1378
|
-
/**
|
1379
|
-
* Alias for `loadFrom(SVGLoader.fromString)`.
|
1380
|
-
*
|
1381
|
-
* This is particularly useful when accessing a bundled version of the editor,
|
1382
|
-
* where `SVGLoader.fromString` is unavailable.
|
1383
|
-
*/
|
1384
|
-
public async loadFromSVG(svgData: string, sanitize: boolean = false) {
|
1385
|
-
const loader = SVGLoader.fromString(svgData, sanitize);
|
1386
|
-
await this.loadFrom(loader);
|
1387
|
-
}
|
1388
|
-
|
1389
|
-
private closeAboutDialog: (()=>void)|null = null;
|
1390
|
-
|
1391
|
-
/**
|
1392
|
-
* Shows an information dialog with legal notices.
|
1393
|
-
*/
|
1394
|
-
public showAboutDialog() {
|
1395
|
-
const iconLicenseText = this.icons.licenseInfo();
|
1396
|
-
|
1397
|
-
const notices: AboutDialogEntry[] = [];
|
1398
|
-
notices.push({
|
1399
|
-
heading: 'js-draw',
|
1400
|
-
text: [
|
1401
|
-
`v${version.number}`,
|
1402
|
-
'',
|
1403
|
-
'Image debug information (from when this dialog was opened):',
|
1404
|
-
` ${this.viewport.getScaleFactor()}x zoom, ${180/Math.PI * this.viewport.getRotationAngle()} rotation`,
|
1405
|
-
` ${this.image.estimateNumElements()} components`,
|
1406
|
-
` ${this.getImportExportRect().w}x${this.getImportExportRect().h} size`,
|
1407
|
-
].join('\n'),
|
1408
|
-
});
|
1409
|
-
|
1410
|
-
notices.push({
|
1411
|
-
heading: 'Libraries',
|
1412
|
-
text: [
|
1413
|
-
'js-draw uses several libraries at runtime. Particularly noteworthy are:',
|
1414
|
-
' - The Coloris color picker: https://github.com/mdbassit/Coloris',
|
1415
|
-
' - The bezier.js Bézier curve library: https://github.com/Pomax/bezierjs'
|
1416
|
-
].join('\n'),
|
1417
|
-
minimized: true,
|
1418
|
-
});
|
1419
|
-
|
1420
|
-
if (iconLicenseText) {
|
1421
|
-
notices.push({
|
1422
|
-
heading: 'Icon Pack',
|
1423
|
-
text: iconLicenseText,
|
1424
|
-
minimized: true,
|
1425
|
-
});
|
1426
|
-
}
|
1427
|
-
|
1428
|
-
notices.push(...this.settings.notices);
|
1429
|
-
|
1430
|
-
|
1431
|
-
this.closeAboutDialog?.();
|
1432
|
-
this.closeAboutDialog = makeAboutDialog(this, notices).close;
|
1433
|
-
}
|
1434
|
-
|
1435
|
-
/** Removes and destroys the editor */
|
1436
|
-
public remove() {
|
1437
|
-
this.container.remove();
|
1438
|
-
|
1439
|
-
// TODO: Is additional cleanup necessary here?
|
1440
|
-
}
|
1441
|
-
}
|
1442
|
-
|
1443
|
-
export default Editor;
|