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,609 @@
|
|
1
|
+
import Editor from './Editor';
|
2
|
+
import AbstractRenderer from './rendering/renderers/AbstractRenderer';
|
3
|
+
import Viewport from './Viewport';
|
4
|
+
import AbstractComponent from './components/AbstractComponent';
|
5
|
+
import { Rect2, Vec2, Mat33, Mat33Array } from '@js-draw/math';
|
6
|
+
import { EditorLocalization } from './localization';
|
7
|
+
import RenderingCache from './rendering/caching/RenderingCache';
|
8
|
+
import SerializableCommand from './commands/SerializableCommand';
|
9
|
+
import EventDispatcher from './EventDispatcher';
|
10
|
+
import { assertIsNumber, assertIsNumberArray } from './util/assertions';
|
11
|
+
|
12
|
+
// @internal Sort by z-index, low to high
|
13
|
+
export const sortLeavesByZIndex = (leaves: Array<ImageNode>) => {
|
14
|
+
leaves.sort((a, b) => a.getContent()!.getZIndex() - b.getContent()!.getZIndex());
|
15
|
+
};
|
16
|
+
|
17
|
+
export enum EditorImageEventType {
|
18
|
+
ExportViewportChanged
|
19
|
+
}
|
20
|
+
|
21
|
+
export type EditorImageNotifier = EventDispatcher<EditorImageEventType, { image: EditorImage }>;
|
22
|
+
|
23
|
+
// Handles lookup/storage of elements in the image
|
24
|
+
export default class EditorImage {
|
25
|
+
private root: ImageNode;
|
26
|
+
private background: ImageNode;
|
27
|
+
private componentsById: Record<string, AbstractComponent>;
|
28
|
+
private componentCount: number = 0;
|
29
|
+
|
30
|
+
/** Viewport for the exported/imported image. */
|
31
|
+
private importExportViewport: Viewport;
|
32
|
+
|
33
|
+
// @internal
|
34
|
+
public readonly notifier: EditorImageNotifier;
|
35
|
+
|
36
|
+
// @internal
|
37
|
+
public constructor() {
|
38
|
+
this.root = new ImageNode();
|
39
|
+
this.background = new ImageNode();
|
40
|
+
this.componentsById = {};
|
41
|
+
|
42
|
+
this.notifier = new EventDispatcher();
|
43
|
+
this.importExportViewport = new Viewport(() => {
|
44
|
+
this.notifier.dispatch(EditorImageEventType.ExportViewportChanged, {
|
45
|
+
image: this,
|
46
|
+
});
|
47
|
+
});
|
48
|
+
|
49
|
+
// Default to a 500x500 image
|
50
|
+
this.importExportViewport.updateScreenSize(Vec2.of(500, 500));
|
51
|
+
}
|
52
|
+
|
53
|
+
// Returns all components that make up the background of this image. These
|
54
|
+
// components are rendered below all other components.
|
55
|
+
public getBackgroundComponents(): AbstractComponent[] {
|
56
|
+
const result = [];
|
57
|
+
|
58
|
+
const leaves = this.background.getLeaves();
|
59
|
+
sortLeavesByZIndex(leaves);
|
60
|
+
|
61
|
+
for (const leaf of leaves) {
|
62
|
+
const content = leaf.getContent();
|
63
|
+
|
64
|
+
if (content) {
|
65
|
+
result.push(content);
|
66
|
+
}
|
67
|
+
}
|
68
|
+
return result;
|
69
|
+
}
|
70
|
+
|
71
|
+
// Returns the parent of the given element, if it exists.
|
72
|
+
public findParent(elem: AbstractComponent): ImageNode|null {
|
73
|
+
return this.background.getChildWithContent(elem) ?? this.root.getChildWithContent(elem);
|
74
|
+
}
|
75
|
+
|
76
|
+
// Forces a re-render of `elem` when the image is next re-rendered as a whole.
|
77
|
+
// Does nothing if `elem` is not in this.
|
78
|
+
public queueRerenderOf(elem: AbstractComponent) {
|
79
|
+
// TODO: Make more efficient (e.g. increase IDs of all parents,
|
80
|
+
// make cache take into account last modified time instead of IDs, etc.)
|
81
|
+
const parent = this.findParent(elem);
|
82
|
+
|
83
|
+
if (parent) {
|
84
|
+
parent.remove();
|
85
|
+
this.addElementDirectly(elem);
|
86
|
+
}
|
87
|
+
}
|
88
|
+
|
89
|
+
/** @internal */
|
90
|
+
public renderWithCache(screenRenderer: AbstractRenderer, cache: RenderingCache, viewport: Viewport) {
|
91
|
+
this.background.render(screenRenderer, viewport.visibleRect);
|
92
|
+
cache.render(screenRenderer, this.root, viewport);
|
93
|
+
}
|
94
|
+
|
95
|
+
/**
|
96
|
+
* Renders all nodes visible from `viewport` (or all nodes if `viewport = null`).
|
97
|
+
*
|
98
|
+
* `viewport` is used to improve rendering performance. If given, it must match
|
99
|
+
* the viewport used by the `renderer` (if any).
|
100
|
+
*/
|
101
|
+
public render(renderer: AbstractRenderer, viewport: Viewport|null) {
|
102
|
+
this.background.render(renderer, viewport?.visibleRect);
|
103
|
+
this.root.render(renderer, viewport?.visibleRect);
|
104
|
+
}
|
105
|
+
|
106
|
+
/** Renders all nodes, even ones not within the viewport. @internal */
|
107
|
+
public renderAll(renderer: AbstractRenderer) {
|
108
|
+
this.render(renderer, null);
|
109
|
+
}
|
110
|
+
|
111
|
+
/** @returns all elements in the image, sorted by z-index. This can be slow for large images. */
|
112
|
+
public getAllElements() {
|
113
|
+
const leaves = this.root.getLeaves();
|
114
|
+
sortLeavesByZIndex(leaves);
|
115
|
+
|
116
|
+
return leaves.map(leaf => leaf.getContent()!);
|
117
|
+
}
|
118
|
+
|
119
|
+
/** Returns the number of elements added to this image. @internal */
|
120
|
+
public estimateNumElements() {
|
121
|
+
return this.componentCount;
|
122
|
+
}
|
123
|
+
|
124
|
+
/** @returns a list of `AbstractComponent`s intersecting `region`, sorted by z-index. */
|
125
|
+
public getElementsIntersectingRegion(region: Rect2): AbstractComponent[] {
|
126
|
+
const leaves = this.root.getLeavesIntersectingRegion(region);
|
127
|
+
sortLeavesByZIndex(leaves);
|
128
|
+
|
129
|
+
return leaves.map(leaf => leaf.getContent()!);
|
130
|
+
}
|
131
|
+
|
132
|
+
/** Called whenever an element is completely removed. @internal */
|
133
|
+
public onDestroyElement(elem: AbstractComponent) {
|
134
|
+
this.componentCount --;
|
135
|
+
delete this.componentsById[elem.getId()];
|
136
|
+
}
|
137
|
+
|
138
|
+
/**
|
139
|
+
* @returns the AbstractComponent with `id`, if it exists.
|
140
|
+
*
|
141
|
+
* @see {@link AbstractComponent.getId}
|
142
|
+
*/
|
143
|
+
public lookupElement(id: string): AbstractComponent|null {
|
144
|
+
return this.componentsById[id] ?? null;
|
145
|
+
}
|
146
|
+
|
147
|
+
private addElementDirectly(elem: AbstractComponent): ImageNode {
|
148
|
+
elem.onAddToImage(this);
|
149
|
+
|
150
|
+
this.componentCount ++;
|
151
|
+
this.componentsById[elem.getId()] = elem;
|
152
|
+
|
153
|
+
// If a background component, add to the background. Else,
|
154
|
+
// add to the normal component tree.
|
155
|
+
const parentTree = elem.isBackground() ? this.background : this.root;
|
156
|
+
return parentTree.addLeaf(elem);
|
157
|
+
}
|
158
|
+
|
159
|
+
private removeElementDirectly(element: AbstractComponent) {
|
160
|
+
const container = this.findParent(element);
|
161
|
+
container?.remove();
|
162
|
+
|
163
|
+
if (container) {
|
164
|
+
this.onDestroyElement(element);
|
165
|
+
return true;
|
166
|
+
}
|
167
|
+
|
168
|
+
return false;
|
169
|
+
}
|
170
|
+
|
171
|
+
/**
|
172
|
+
* Returns a command that adds the given element to the `EditorImage`.
|
173
|
+
* If `applyByFlattening` is true, the content of the wet ink renderer is
|
174
|
+
* rendered onto the main rendering canvas instead of doing a full re-render.
|
175
|
+
*
|
176
|
+
* @see {@link Display.flatten}
|
177
|
+
*/
|
178
|
+
public static addElement(elem: AbstractComponent, applyByFlattening: boolean = false): SerializableCommand {
|
179
|
+
return new EditorImage.AddElementCommand(elem, applyByFlattening);
|
180
|
+
}
|
181
|
+
|
182
|
+
/** @see EditorImage.addElement */
|
183
|
+
public addElement(elem: AbstractComponent, applyByFlattening?: boolean) {
|
184
|
+
return EditorImage.addElement(elem, applyByFlattening);
|
185
|
+
}
|
186
|
+
|
187
|
+
// A Command that can access private [EditorImage] functionality
|
188
|
+
private static AddElementCommand = class extends SerializableCommand {
|
189
|
+
private serializedElem: any|null = null;
|
190
|
+
|
191
|
+
// If [applyByFlattening], then the rendered content of this element
|
192
|
+
// is present on the display's wet ink canvas. As such, no re-render is necessary
|
193
|
+
// the first time this command is applied (the surfaces are joined instead).
|
194
|
+
public constructor(
|
195
|
+
private element: AbstractComponent,
|
196
|
+
private applyByFlattening: boolean = false
|
197
|
+
) {
|
198
|
+
super('add-element');
|
199
|
+
|
200
|
+
// FIXME: The serialized version of this command may be inaccurate if this is
|
201
|
+
// serialized when this command is not on the top of the undo stack.
|
202
|
+
//
|
203
|
+
// Caching the element's serialized data leads to additional memory usage *and*
|
204
|
+
// sometimes incorrect behavior in collaborative editing.
|
205
|
+
this.serializedElem = null;
|
206
|
+
|
207
|
+
if (isNaN(element.getBBox().area)) {
|
208
|
+
throw new Error('Elements in the image cannot have NaN bounding boxes');
|
209
|
+
}
|
210
|
+
}
|
211
|
+
|
212
|
+
public apply(editor: Editor) {
|
213
|
+
editor.image.addElementDirectly(this.element);
|
214
|
+
|
215
|
+
if (!this.applyByFlattening) {
|
216
|
+
editor.queueRerender();
|
217
|
+
} else {
|
218
|
+
this.applyByFlattening = false;
|
219
|
+
editor.display.flatten();
|
220
|
+
}
|
221
|
+
}
|
222
|
+
|
223
|
+
public unapply(editor: Editor) {
|
224
|
+
editor.image.removeElementDirectly(this.element);
|
225
|
+
editor.queueRerender();
|
226
|
+
}
|
227
|
+
|
228
|
+
public description(_editor: Editor, localization: EditorLocalization) {
|
229
|
+
return localization.addElementAction(this.element.description(localization));
|
230
|
+
}
|
231
|
+
|
232
|
+
protected serializeToJSON() {
|
233
|
+
return {
|
234
|
+
elemData: this.serializedElem ?? this.element.serialize(),
|
235
|
+
};
|
236
|
+
}
|
237
|
+
|
238
|
+
static {
|
239
|
+
SerializableCommand.register('add-element', (json: any, editor: Editor) => {
|
240
|
+
const id = json.elemData.id;
|
241
|
+
const foundElem = editor.image.lookupElement(id);
|
242
|
+
const elem = foundElem ?? AbstractComponent.deserialize(json.elemData);
|
243
|
+
const result = new EditorImage.AddElementCommand(elem);
|
244
|
+
result.serializedElem = json.elemData;
|
245
|
+
return result;
|
246
|
+
});
|
247
|
+
}
|
248
|
+
};
|
249
|
+
|
250
|
+
|
251
|
+
|
252
|
+
/**
|
253
|
+
* @returns a `Viewport` for rendering the image when importing/exporting.
|
254
|
+
*/
|
255
|
+
public getImportExportViewport() {
|
256
|
+
return this.importExportViewport;
|
257
|
+
}
|
258
|
+
|
259
|
+
public setImportExportRect(imageRect: Rect2): SerializableCommand {
|
260
|
+
const importExportViewport = this.getImportExportViewport();
|
261
|
+
const origSize = importExportViewport.visibleRect.size;
|
262
|
+
const origTransform = importExportViewport.canvasToScreenTransform;
|
263
|
+
|
264
|
+
return new EditorImage.SetImportExportRectCommand(origSize, origTransform, imageRect);
|
265
|
+
}
|
266
|
+
|
267
|
+
// Handles resizing the background import/export region of the image.
|
268
|
+
private static SetImportExportRectCommand = class extends SerializableCommand {
|
269
|
+
private static commandId = 'set-import-export-rect';
|
270
|
+
|
271
|
+
public constructor(
|
272
|
+
private originalSize: Vec2,
|
273
|
+
private originalTransform: Mat33,
|
274
|
+
private finalRect: Rect2,
|
275
|
+
) {
|
276
|
+
super(EditorImage.SetImportExportRectCommand.commandId);
|
277
|
+
}
|
278
|
+
|
279
|
+
public apply(editor: Editor) {
|
280
|
+
const viewport = editor.image.getImportExportViewport();
|
281
|
+
viewport.updateScreenSize(this.finalRect.size);
|
282
|
+
viewport.resetTransform(Mat33.translation(this.finalRect.topLeft.times(-1)));
|
283
|
+
editor.queueRerender();
|
284
|
+
}
|
285
|
+
|
286
|
+
public unapply(editor: Editor) {
|
287
|
+
const viewport = editor.image.getImportExportViewport();
|
288
|
+
viewport.updateScreenSize(this.originalSize);
|
289
|
+
viewport.resetTransform(this.originalTransform);
|
290
|
+
editor.queueRerender();
|
291
|
+
}
|
292
|
+
|
293
|
+
public description(_editor: Editor, localization: EditorLocalization) {
|
294
|
+
return localization.resizeOutputCommand(this.finalRect);
|
295
|
+
}
|
296
|
+
|
297
|
+
protected serializeToJSON() {
|
298
|
+
return {
|
299
|
+
originalSize: this.originalSize.xy,
|
300
|
+
originalTransform: this.originalTransform.toArray(),
|
301
|
+
newRegion: {
|
302
|
+
x: this.finalRect.x,
|
303
|
+
y: this.finalRect.y,
|
304
|
+
w: this.finalRect.w,
|
305
|
+
h: this.finalRect.h,
|
306
|
+
},
|
307
|
+
};
|
308
|
+
}
|
309
|
+
|
310
|
+
static {
|
311
|
+
const commandId = this.commandId;
|
312
|
+
SerializableCommand.register(commandId, (json: any, _editor: Editor) => {
|
313
|
+
assertIsNumber(json.originalSize.x);
|
314
|
+
assertIsNumber(json.originalSize.y);
|
315
|
+
assertIsNumberArray(json.originalTransform);
|
316
|
+
assertIsNumberArray([
|
317
|
+
json.newRegion.x,
|
318
|
+
json.newRegion.y,
|
319
|
+
json.newRegion.w,
|
320
|
+
json.newRegion.h,
|
321
|
+
]);
|
322
|
+
|
323
|
+
const originalSize = Vec2.ofXY(json.originalSize);
|
324
|
+
const originalTransform = new Mat33(...(json.originalTransform as Mat33Array));
|
325
|
+
const finalRect = new Rect2(
|
326
|
+
json.newRegion.x, json.newRegion.y, json.newRegion.w, json.newRegion.h
|
327
|
+
);
|
328
|
+
return new EditorImage.SetImportExportRectCommand(
|
329
|
+
originalSize, originalTransform, finalRect
|
330
|
+
);
|
331
|
+
});
|
332
|
+
}
|
333
|
+
};
|
334
|
+
}
|
335
|
+
|
336
|
+
type TooSmallToRenderCheck = (rect: Rect2)=> boolean;
|
337
|
+
|
338
|
+
/** Part of the Editor's image. @internal */
|
339
|
+
export class ImageNode {
|
340
|
+
private content: AbstractComponent|null;
|
341
|
+
private bbox: Rect2;
|
342
|
+
private children: ImageNode[];
|
343
|
+
private targetChildCount: number = 30;
|
344
|
+
|
345
|
+
private id: number;
|
346
|
+
private static idCounter: number = 0;
|
347
|
+
|
348
|
+
public constructor(
|
349
|
+
private parent: ImageNode|null = null
|
350
|
+
) {
|
351
|
+
this.children = [];
|
352
|
+
this.bbox = Rect2.empty;
|
353
|
+
this.content = null;
|
354
|
+
|
355
|
+
this.id = ImageNode.idCounter++;
|
356
|
+
}
|
357
|
+
|
358
|
+
public getId() {
|
359
|
+
return this.id;
|
360
|
+
}
|
361
|
+
|
362
|
+
public onContentChange() {
|
363
|
+
this.id = ImageNode.idCounter++;
|
364
|
+
}
|
365
|
+
|
366
|
+
public getContent(): AbstractComponent|null {
|
367
|
+
return this.content;
|
368
|
+
}
|
369
|
+
|
370
|
+
public getParent(): ImageNode|null {
|
371
|
+
return this.parent;
|
372
|
+
}
|
373
|
+
|
374
|
+
private getChildrenIntersectingRegion(region: Rect2): ImageNode[] {
|
375
|
+
return this.children.filter(child => {
|
376
|
+
return child.getBBox().intersects(region);
|
377
|
+
});
|
378
|
+
}
|
379
|
+
|
380
|
+
public getChildrenOrSelfIntersectingRegion(region: Rect2): ImageNode[] {
|
381
|
+
if (this.content) {
|
382
|
+
return [this];
|
383
|
+
}
|
384
|
+
return this.getChildrenIntersectingRegion(region);
|
385
|
+
}
|
386
|
+
|
387
|
+
// Returns a list of `ImageNode`s with content (and thus no children).
|
388
|
+
public getLeavesIntersectingRegion(region: Rect2, isTooSmall?: TooSmallToRenderCheck): ImageNode[] {
|
389
|
+
const result: ImageNode[] = [];
|
390
|
+
let current: ImageNode|undefined;
|
391
|
+
const workList: ImageNode[] = [];
|
392
|
+
|
393
|
+
workList.push(this);
|
394
|
+
const toNext = () => {
|
395
|
+
current = undefined;
|
396
|
+
|
397
|
+
const next = workList.pop();
|
398
|
+
if (next && !isTooSmall?.(next.bbox)) {
|
399
|
+
current = next;
|
400
|
+
|
401
|
+
if (current.content !== null && current.getBBox().intersection(region)) {
|
402
|
+
result.push(current);
|
403
|
+
}
|
404
|
+
|
405
|
+
workList.push(
|
406
|
+
...current.getChildrenIntersectingRegion(region)
|
407
|
+
);
|
408
|
+
}
|
409
|
+
};
|
410
|
+
|
411
|
+
while (workList.length > 0) {
|
412
|
+
toNext();
|
413
|
+
}
|
414
|
+
|
415
|
+
return result;
|
416
|
+
}
|
417
|
+
|
418
|
+
// Returns the child of this with the target content or `null` if no
|
419
|
+
// such child exists.
|
420
|
+
public getChildWithContent(target: AbstractComponent): ImageNode|null {
|
421
|
+
const candidates = this.getLeavesIntersectingRegion(target.getBBox());
|
422
|
+
for (const candidate of candidates) {
|
423
|
+
if (candidate.getContent() === target) {
|
424
|
+
return candidate;
|
425
|
+
}
|
426
|
+
}
|
427
|
+
|
428
|
+
return null;
|
429
|
+
}
|
430
|
+
|
431
|
+
// Returns a list of leaves with this as an ancestor.
|
432
|
+
// Like getLeavesInRegion, but does not check whether ancestors are in a given rectangle
|
433
|
+
public getLeaves(): ImageNode[] {
|
434
|
+
if (this.content) {
|
435
|
+
return [this];
|
436
|
+
}
|
437
|
+
|
438
|
+
const result: ImageNode[] = [];
|
439
|
+
for (const child of this.children) {
|
440
|
+
result.push(...child.getLeaves());
|
441
|
+
}
|
442
|
+
|
443
|
+
return result;
|
444
|
+
}
|
445
|
+
|
446
|
+
public addLeaf(leaf: AbstractComponent): ImageNode {
|
447
|
+
this.onContentChange();
|
448
|
+
|
449
|
+
if (this.content === null && this.children.length === 0) {
|
450
|
+
this.content = leaf;
|
451
|
+
this.recomputeBBox(true);
|
452
|
+
|
453
|
+
return this;
|
454
|
+
}
|
455
|
+
|
456
|
+
if (this.content !== null) {
|
457
|
+
console.assert(this.children.length === 0);
|
458
|
+
|
459
|
+
const contentNode = new ImageNode(this);
|
460
|
+
contentNode.content = this.content;
|
461
|
+
this.content = null;
|
462
|
+
this.children.push(contentNode);
|
463
|
+
contentNode.recomputeBBox(false);
|
464
|
+
}
|
465
|
+
|
466
|
+
// If this node is contained within the leaf, make this and the leaf
|
467
|
+
// share a parent.
|
468
|
+
const leafBBox = leaf.getBBox();
|
469
|
+
if (leafBBox.containsRect(this.getBBox())) {
|
470
|
+
const nodeForNewLeaf = new ImageNode(this);
|
471
|
+
|
472
|
+
if (this.children.length < this.targetChildCount) {
|
473
|
+
this.children.push(nodeForNewLeaf);
|
474
|
+
} else {
|
475
|
+
const nodeForChildren = new ImageNode(this);
|
476
|
+
|
477
|
+
nodeForChildren.children = this.children;
|
478
|
+
this.children = [nodeForNewLeaf, nodeForChildren];
|
479
|
+
nodeForChildren.recomputeBBox(true);
|
480
|
+
nodeForChildren.updateParents();
|
481
|
+
}
|
482
|
+
return nodeForNewLeaf.addLeaf(leaf);
|
483
|
+
}
|
484
|
+
|
485
|
+
const containingNodes = this.children.filter(
|
486
|
+
child => child.getBBox().containsRect(leafBBox)
|
487
|
+
);
|
488
|
+
|
489
|
+
// Does the leaf already fit within one of the children?
|
490
|
+
if (containingNodes.length > 0 && this.children.length >= this.targetChildCount) {
|
491
|
+
// Sort the containers in ascending order by area
|
492
|
+
containingNodes.sort((a, b) => a.getBBox().area - b.getBBox().area);
|
493
|
+
|
494
|
+
// Choose the smallest child that contains the new element.
|
495
|
+
const result = containingNodes[0].addLeaf(leaf);
|
496
|
+
result.rebalance();
|
497
|
+
return result;
|
498
|
+
}
|
499
|
+
|
500
|
+
|
501
|
+
const newNode = new ImageNode(this);
|
502
|
+
this.children.push(newNode);
|
503
|
+
newNode.content = leaf;
|
504
|
+
newNode.recomputeBBox(true);
|
505
|
+
|
506
|
+
return newNode;
|
507
|
+
}
|
508
|
+
|
509
|
+
public getBBox(): Rect2 {
|
510
|
+
return this.bbox;
|
511
|
+
}
|
512
|
+
|
513
|
+
// Recomputes this' bounding box. If [bubbleUp], also recompute
|
514
|
+
// this' ancestors bounding boxes. This also re-computes this' bounding box
|
515
|
+
// in the z-direction (z-indicies).
|
516
|
+
public recomputeBBox(bubbleUp: boolean) {
|
517
|
+
const oldBBox = this.bbox;
|
518
|
+
if (this.content !== null) {
|
519
|
+
this.bbox = this.content.getBBox();
|
520
|
+
} else {
|
521
|
+
this.bbox = Rect2.union(...this.children.map(child => child.getBBox()));
|
522
|
+
}
|
523
|
+
|
524
|
+
if (bubbleUp && !oldBBox.eq(this.bbox)) {
|
525
|
+
this.parent?.recomputeBBox(true);
|
526
|
+
}
|
527
|
+
}
|
528
|
+
|
529
|
+
private updateParents(recursive: boolean = false) {
|
530
|
+
for (const child of this.children) {
|
531
|
+
child.parent = this;
|
532
|
+
|
533
|
+
if (recursive) {
|
534
|
+
child.updateParents(recursive);
|
535
|
+
}
|
536
|
+
}
|
537
|
+
}
|
538
|
+
|
539
|
+
private rebalance() {
|
540
|
+
// If the current node is its parent's only child,
|
541
|
+
if (this.parent && this.parent.children.length === 1) {
|
542
|
+
console.assert(this.parent.content === null);
|
543
|
+
console.assert(this.parent.children[0] === this);
|
544
|
+
|
545
|
+
// Remove this' parent, if this' parent isn't the root.
|
546
|
+
const oldParent = this.parent;
|
547
|
+
if (oldParent.parent !== null) {
|
548
|
+
oldParent.children = [];
|
549
|
+
this.parent = oldParent.parent;
|
550
|
+
this.parent.children.push(this);
|
551
|
+
oldParent.parent = null;
|
552
|
+
this.parent.recomputeBBox(false);
|
553
|
+
} else if (this.content === null) {
|
554
|
+
// Remove this and transfer this' children to the parent.
|
555
|
+
this.parent.children = this.children;
|
556
|
+
this.parent.updateParents();
|
557
|
+
this.parent = null;
|
558
|
+
}
|
559
|
+
}
|
560
|
+
}
|
561
|
+
|
562
|
+
// Remove this node and all of its children
|
563
|
+
public remove() {
|
564
|
+
this.content?.onRemoveFromImage();
|
565
|
+
|
566
|
+
if (!this.parent) {
|
567
|
+
this.content = null;
|
568
|
+
this.children = [];
|
569
|
+
|
570
|
+
return;
|
571
|
+
}
|
572
|
+
|
573
|
+
const oldChildCount = this.parent.children.length;
|
574
|
+
this.parent.children = this.parent.children.filter(node => {
|
575
|
+
return node !== this;
|
576
|
+
});
|
577
|
+
|
578
|
+
console.assert(
|
579
|
+
this.parent.children.length === oldChildCount - 1,
|
580
|
+
`${oldChildCount - 1} ≠ ${this.parent.children.length} after removing all nodes equal to ${this}. Nodes should only be removed once.`
|
581
|
+
);
|
582
|
+
|
583
|
+
this.parent.children.forEach(child => {
|
584
|
+
child.rebalance();
|
585
|
+
});
|
586
|
+
|
587
|
+
this.parent.recomputeBBox(true);
|
588
|
+
|
589
|
+
// Invalidate/disconnect this.
|
590
|
+
this.content = null;
|
591
|
+
this.parent = null;
|
592
|
+
this.children = [];
|
593
|
+
}
|
594
|
+
|
595
|
+
public render(renderer: AbstractRenderer, visibleRect?: Rect2) {
|
596
|
+
let leaves;
|
597
|
+
if (visibleRect) {
|
598
|
+
leaves = this.getLeavesIntersectingRegion(visibleRect, rect => renderer.isTooSmallToRender(rect));
|
599
|
+
} else {
|
600
|
+
leaves = this.getLeaves();
|
601
|
+
}
|
602
|
+
sortLeavesByZIndex(leaves);
|
603
|
+
|
604
|
+
for (const leaf of leaves) {
|
605
|
+
// Leaves by definition have content
|
606
|
+
leaf.getContent()!.render(renderer, visibleRect);
|
607
|
+
}
|
608
|
+
}
|
609
|
+
}
|