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,510 @@
|
|
1
|
+
import AbstractComponent from '../../components/AbstractComponent';
|
2
|
+
import Editor from '../../Editor';
|
3
|
+
import { Mat33, Rect2, Point2, Vec2 } from '@js-draw/math';
|
4
|
+
import { EditorEventType } from '../../types';
|
5
|
+
import { CopyEvent, KeyPressEvent, KeyUpEvent, PointerEvt } from '../../inputEvents';
|
6
|
+
import Viewport from '../../Viewport';
|
7
|
+
import BaseTool from '../BaseTool';
|
8
|
+
import SVGRenderer from '../../rendering/renderers/SVGRenderer';
|
9
|
+
import Selection from './Selection';
|
10
|
+
import TextComponent from '../../components/TextComponent';
|
11
|
+
import { duplicateSelectionShortcut, selectAllKeyboardShortcut, snapToGridKeyboardShortcutId } from '../keybindings';
|
12
|
+
|
13
|
+
export const cssPrefix = 'selection-tool-';
|
14
|
+
|
15
|
+
// Allows users to select/transform portions of the `EditorImage`.
|
16
|
+
// With respect to `extend`ing, `SelectionTool` is not stable.
|
17
|
+
export default class SelectionTool extends BaseTool {
|
18
|
+
private handleOverlay: HTMLElement;
|
19
|
+
private prevSelectionBox: Selection | null;
|
20
|
+
private selectionBox: Selection | null;
|
21
|
+
|
22
|
+
private startPoint: Vec2 | null = null; // canvas position
|
23
|
+
private expandingSelectionBox: boolean = false;
|
24
|
+
private shiftKeyPressed: boolean = false;
|
25
|
+
private snapToGrid: boolean = false;
|
26
|
+
|
27
|
+
public constructor(private editor: Editor, description: string) {
|
28
|
+
super(editor.notifier, description);
|
29
|
+
|
30
|
+
this.handleOverlay = document.createElement('div');
|
31
|
+
editor.createHTMLOverlay(this.handleOverlay);
|
32
|
+
|
33
|
+
this.handleOverlay.style.display = 'none';
|
34
|
+
this.handleOverlay.classList.add('handleOverlay');
|
35
|
+
|
36
|
+
editor.notifier.on(EditorEventType.ViewportChanged, _data => {
|
37
|
+
// The selection box could be using the wet ink display if its transformation
|
38
|
+
// hasn't been finalized yet. Clear before updating the UI.
|
39
|
+
this.editor.clearWetInk();
|
40
|
+
|
41
|
+
this.selectionBox?.updateUI();
|
42
|
+
});
|
43
|
+
|
44
|
+
this.editor.handleKeyEventsFrom(this.handleOverlay);
|
45
|
+
this.editor.handlePointerEventsFrom(this.handleOverlay);
|
46
|
+
}
|
47
|
+
|
48
|
+
private makeSelectionBox(selectionStartPos: Point2) {
|
49
|
+
this.prevSelectionBox = this.selectionBox;
|
50
|
+
this.selectionBox = new Selection(
|
51
|
+
selectionStartPos, this.editor
|
52
|
+
);
|
53
|
+
|
54
|
+
if (!this.expandingSelectionBox) {
|
55
|
+
// Remove any previous selection rects
|
56
|
+
this.prevSelectionBox?.cancelSelection();
|
57
|
+
}
|
58
|
+
this.selectionBox.addTo(this.handleOverlay);
|
59
|
+
}
|
60
|
+
|
61
|
+
private snapSelectionToGrid() {
|
62
|
+
if (!this.selectionBox) throw new Error('No selection to snap!');
|
63
|
+
|
64
|
+
// Snap the top left corner of what we have selected.
|
65
|
+
const topLeftOfBBox = this.selectionBox.computeTightBoundingBox().topLeft;
|
66
|
+
const snappedTopLeft = this.editor.viewport.snapToGrid(topLeftOfBBox);
|
67
|
+
const snapDelta = snappedTopLeft.minus(topLeftOfBBox);
|
68
|
+
|
69
|
+
const oldTransform = this.selectionBox.getTransform();
|
70
|
+
this.selectionBox.setTransform(oldTransform.rightMul(Mat33.translation(snapDelta)));
|
71
|
+
this.selectionBox.finalizeTransform();
|
72
|
+
}
|
73
|
+
|
74
|
+
private selectionBoxHandlingEvt: boolean = false;
|
75
|
+
public override onPointerDown({ allPointers, current }: PointerEvt): boolean {
|
76
|
+
const snapToGrid = this.snapToGrid;
|
77
|
+
if (snapToGrid) {
|
78
|
+
current = current.snappedToGrid(this.editor.viewport);
|
79
|
+
}
|
80
|
+
|
81
|
+
if (allPointers.length === 1 && current.isPrimary) {
|
82
|
+
this.startPoint = current.canvasPos;
|
83
|
+
|
84
|
+
let transforming = false;
|
85
|
+
|
86
|
+
if (this.selectionBox) {
|
87
|
+
if (snapToGrid) {
|
88
|
+
this.snapSelectionToGrid();
|
89
|
+
}
|
90
|
+
|
91
|
+
const dragStartResult = this.selectionBox.onDragStart(current);
|
92
|
+
|
93
|
+
if (dragStartResult) {
|
94
|
+
transforming = true;
|
95
|
+
|
96
|
+
this.selectionBoxHandlingEvt = true;
|
97
|
+
this.expandingSelectionBox = false;
|
98
|
+
}
|
99
|
+
}
|
100
|
+
|
101
|
+
if (!transforming) {
|
102
|
+
// Shift key: Combine the new and old selection boxes at the end of the gesture.
|
103
|
+
this.expandingSelectionBox = this.shiftKeyPressed;
|
104
|
+
this.makeSelectionBox(current.canvasPos);
|
105
|
+
}
|
106
|
+
|
107
|
+
return true;
|
108
|
+
}
|
109
|
+
return false;
|
110
|
+
}
|
111
|
+
|
112
|
+
public override onPointerMove(event: PointerEvt): void {
|
113
|
+
if (!this.selectionBox) return;
|
114
|
+
|
115
|
+
let currentPointer = event.current;
|
116
|
+
if (!this.expandingSelectionBox && this.shiftKeyPressed && this.startPoint) {
|
117
|
+
const screenPos = this.editor.viewport.canvasToScreen(this.startPoint);
|
118
|
+
currentPointer = currentPointer.lockedToXYAxesScreen(screenPos, this.editor.viewport);
|
119
|
+
}
|
120
|
+
|
121
|
+
if (this.snapToGrid) {
|
122
|
+
currentPointer = currentPointer.snappedToGrid(this.editor.viewport);
|
123
|
+
}
|
124
|
+
|
125
|
+
if (this.selectionBoxHandlingEvt) {
|
126
|
+
this.selectionBox.onDragUpdate(currentPointer);
|
127
|
+
} else {
|
128
|
+
this.selectionBox!.setToPoint(currentPointer.canvasPos);
|
129
|
+
}
|
130
|
+
}
|
131
|
+
|
132
|
+
// Called after a gestureCancel and a pointerUp
|
133
|
+
private onGestureEnd() {
|
134
|
+
if (!this.selectionBox) return;
|
135
|
+
|
136
|
+
if (!this.selectionBoxHandlingEvt) {
|
137
|
+
// Expand/shrink the selection rectangle, if applicable
|
138
|
+
this.selectionBox.resolveToObjects();
|
139
|
+
this.onSelectionUpdated();
|
140
|
+
} else {
|
141
|
+
this.selectionBox.onDragEnd();
|
142
|
+
}
|
143
|
+
|
144
|
+
|
145
|
+
this.selectionBoxHandlingEvt = false;
|
146
|
+
}
|
147
|
+
|
148
|
+
public override onPointerUp(event: PointerEvt): void {
|
149
|
+
if (!this.selectionBox) return;
|
150
|
+
|
151
|
+
let currentPointer = event.current;
|
152
|
+
if (this.snapToGrid) {
|
153
|
+
currentPointer = currentPointer.snappedToGrid(this.editor.viewport);
|
154
|
+
}
|
155
|
+
|
156
|
+
this.selectionBox.setToPoint(currentPointer.canvasPos);
|
157
|
+
|
158
|
+
// Were we expanding the previous selection?
|
159
|
+
if (this.expandingSelectionBox && this.prevSelectionBox) {
|
160
|
+
// If so, finish expanding.
|
161
|
+
this.expandingSelectionBox = false;
|
162
|
+
this.selectionBox.resolveToObjects();
|
163
|
+
this.setSelection([
|
164
|
+
...this.selectionBox.getSelectedObjects(),
|
165
|
+
...this.prevSelectionBox.getSelectedObjects(),
|
166
|
+
]);
|
167
|
+
} else {
|
168
|
+
this.onGestureEnd();
|
169
|
+
}
|
170
|
+
}
|
171
|
+
|
172
|
+
public override onGestureCancel(): void {
|
173
|
+
if (this.selectionBoxHandlingEvt) {
|
174
|
+
this.selectionBox?.onDragCancel();
|
175
|
+
} else {
|
176
|
+
// Revert to the previous selection, if any.
|
177
|
+
this.selectionBox?.cancelSelection();
|
178
|
+
this.selectionBox = this.prevSelectionBox;
|
179
|
+
this.selectionBox?.addTo(this.handleOverlay);
|
180
|
+
this.selectionBox?.recomputeRegion();
|
181
|
+
this.prevSelectionBox = null;
|
182
|
+
}
|
183
|
+
|
184
|
+
this.expandingSelectionBox = false;
|
185
|
+
}
|
186
|
+
|
187
|
+
private lastSelectedObjects: AbstractComponent[] = [];
|
188
|
+
|
189
|
+
private onSelectionUpdated() {
|
190
|
+
const selectedItemCount = this.selectionBox?.getSelectedItemCount() ?? 0;
|
191
|
+
const selectedObjects = this.selectionBox?.getSelectedObjects() ?? [];
|
192
|
+
const hasDifferentSelection =
|
193
|
+
this.lastSelectedObjects.length !== selectedItemCount
|
194
|
+
|| selectedObjects.some((obj, i) => this.lastSelectedObjects[i] !== obj);
|
195
|
+
|
196
|
+
if (hasDifferentSelection) {
|
197
|
+
this.lastSelectedObjects = selectedObjects;
|
198
|
+
|
199
|
+
// Note that the selection has changed
|
200
|
+
this.editor.notifier.dispatch(EditorEventType.ToolUpdated, {
|
201
|
+
kind: EditorEventType.ToolUpdated,
|
202
|
+
tool: this,
|
203
|
+
});
|
204
|
+
|
205
|
+
// Only fire the SelectionUpdated event if the selection really updated.
|
206
|
+
this.editor.notifier.dispatch(EditorEventType.SelectionUpdated, {
|
207
|
+
kind: EditorEventType.SelectionUpdated,
|
208
|
+
selectedComponents: selectedObjects,
|
209
|
+
tool: this,
|
210
|
+
});
|
211
|
+
|
212
|
+
if (selectedItemCount > 0) {
|
213
|
+
this.editor.announceForAccessibility(
|
214
|
+
this.editor.localization.selectedElements(selectedItemCount)
|
215
|
+
);
|
216
|
+
this.zoomToSelection();
|
217
|
+
}
|
218
|
+
}
|
219
|
+
|
220
|
+
if (selectedItemCount === 0 && this.selectionBox) {
|
221
|
+
this.selectionBox.cancelSelection();
|
222
|
+
this.prevSelectionBox = this.selectionBox;
|
223
|
+
this.selectionBox = null;
|
224
|
+
}
|
225
|
+
}
|
226
|
+
|
227
|
+
private zoomToSelection() {
|
228
|
+
if (this.selectionBox) {
|
229
|
+
const selectionRect = this.selectionBox.region;
|
230
|
+
this.editor.dispatchNoAnnounce(this.editor.viewport.zoomTo(selectionRect, false), false);
|
231
|
+
}
|
232
|
+
}
|
233
|
+
|
234
|
+
private static handleableKeys = [
|
235
|
+
'a', 'h', 'ArrowLeft',
|
236
|
+
'd', 'l', 'ArrowRight',
|
237
|
+
'q', 'k', 'ArrowUp',
|
238
|
+
'e', 'j', 'ArrowDown',
|
239
|
+
'r', 'R',
|
240
|
+
'i', 'I', 'o', 'O',
|
241
|
+
'Control', 'Meta',
|
242
|
+
];
|
243
|
+
public override onKeyPress(event: KeyPressEvent): boolean {
|
244
|
+
const shortcucts = this.editor.shortcuts;
|
245
|
+
|
246
|
+
if (shortcucts.matchesShortcut(snapToGridKeyboardShortcutId, event)) {
|
247
|
+
this.snapToGrid = true;
|
248
|
+
return true;
|
249
|
+
}
|
250
|
+
|
251
|
+
if (this.selectionBox && shortcucts.matchesShortcut(duplicateSelectionShortcut, event)) {
|
252
|
+
// Handle duplication on key up — we don't want to accidentally duplicate
|
253
|
+
// many times.
|
254
|
+
return true;
|
255
|
+
}
|
256
|
+
else if (shortcucts.matchesShortcut(selectAllKeyboardShortcut, event)) {
|
257
|
+
this.setSelection(this.editor.image.getAllElements());
|
258
|
+
return true;
|
259
|
+
}
|
260
|
+
else if (event.ctrlKey) {
|
261
|
+
// Don't transform the selection with, for example, ctrl+i.
|
262
|
+
// Pass it to another tool, if apliccable.
|
263
|
+
return false;
|
264
|
+
}
|
265
|
+
else if (event.key === 'Shift') {
|
266
|
+
this.shiftKeyPressed = true;
|
267
|
+
return true;
|
268
|
+
}
|
269
|
+
|
270
|
+
let rotationSteps = 0;
|
271
|
+
let xTranslateSteps = 0;
|
272
|
+
let yTranslateSteps = 0;
|
273
|
+
let xScaleSteps = 0;
|
274
|
+
let yScaleSteps = 0;
|
275
|
+
|
276
|
+
switch (event.key) {
|
277
|
+
case 'a':
|
278
|
+
case 'h':
|
279
|
+
case 'ArrowLeft':
|
280
|
+
xTranslateSteps -= 1;
|
281
|
+
break;
|
282
|
+
case 'd':
|
283
|
+
case 'l':
|
284
|
+
case 'ArrowRight':
|
285
|
+
xTranslateSteps += 1;
|
286
|
+
break;
|
287
|
+
case 'q':
|
288
|
+
case 'k':
|
289
|
+
case 'ArrowUp':
|
290
|
+
yTranslateSteps -= 1;
|
291
|
+
break;
|
292
|
+
case 'e':
|
293
|
+
case 'j':
|
294
|
+
case 'ArrowDown':
|
295
|
+
yTranslateSteps += 1;
|
296
|
+
break;
|
297
|
+
case 'r':
|
298
|
+
rotationSteps += 1;
|
299
|
+
break;
|
300
|
+
case 'R':
|
301
|
+
rotationSteps -= 1;
|
302
|
+
break;
|
303
|
+
case 'i':
|
304
|
+
xScaleSteps -= 1;
|
305
|
+
break;
|
306
|
+
case 'I':
|
307
|
+
xScaleSteps += 1;
|
308
|
+
break;
|
309
|
+
case 'o':
|
310
|
+
yScaleSteps -= 1;
|
311
|
+
break;
|
312
|
+
case 'O':
|
313
|
+
yScaleSteps += 1;
|
314
|
+
break;
|
315
|
+
}
|
316
|
+
|
317
|
+
let handled = xTranslateSteps !== 0
|
318
|
+
|| yTranslateSteps !== 0
|
319
|
+
|| rotationSteps !== 0
|
320
|
+
|| xScaleSteps !== 0
|
321
|
+
|| yScaleSteps !== 0;
|
322
|
+
|
323
|
+
if (!this.selectionBox) {
|
324
|
+
handled = false;
|
325
|
+
} else if (handled) {
|
326
|
+
const translateStepSize = 10 * this.editor.viewport.getSizeOfPixelOnCanvas();
|
327
|
+
const rotateStepSize = Math.PI / 8;
|
328
|
+
const scaleStepSize = 5 / 4;
|
329
|
+
|
330
|
+
const region = this.selectionBox.region;
|
331
|
+
const scaleFactor = Vec2.of(scaleStepSize ** xScaleSteps, scaleStepSize ** yScaleSteps);
|
332
|
+
|
333
|
+
const rotationMat = Mat33.zRotation(
|
334
|
+
rotationSteps * rotateStepSize
|
335
|
+
);
|
336
|
+
const roundedRotationMatrix = rotationMat.mapEntries(component => Viewport.roundScaleRatio(component));
|
337
|
+
const regionCenter = this.editor.viewport.roundPoint(region.center);
|
338
|
+
|
339
|
+
const transform = Mat33.scaling2D(
|
340
|
+
scaleFactor,
|
341
|
+
this.editor.viewport.roundPoint(region.topLeft)
|
342
|
+
).rightMul(
|
343
|
+
Mat33.translation(regionCenter).rightMul(
|
344
|
+
roundedRotationMatrix
|
345
|
+
).rightMul(
|
346
|
+
Mat33.translation(regionCenter.times(-1))
|
347
|
+
)
|
348
|
+
).rightMul(Mat33.translation(
|
349
|
+
this.editor.viewport.roundPoint(Vec2.of(xTranslateSteps, yTranslateSteps).times(translateStepSize))
|
350
|
+
));
|
351
|
+
const oldTransform = this.selectionBox.getTransform();
|
352
|
+
this.selectionBox.setTransform(oldTransform.rightMul(transform));
|
353
|
+
}
|
354
|
+
|
355
|
+
if (this.selectionBox && !handled && (event.key === 'Delete' || event.key === 'Backspace')) {
|
356
|
+
this.editor.dispatch(this.selectionBox.deleteSelectedObjects());
|
357
|
+
this.clearSelection();
|
358
|
+
handled = true;
|
359
|
+
}
|
360
|
+
|
361
|
+
return handled;
|
362
|
+
}
|
363
|
+
|
364
|
+
public override onKeyUp(evt: KeyUpEvent) {
|
365
|
+
const shortcucts = this.editor.shortcuts;
|
366
|
+
if (shortcucts.matchesShortcut(snapToGridKeyboardShortcutId, evt)) {
|
367
|
+
this.snapToGrid = false;
|
368
|
+
return true;
|
369
|
+
}
|
370
|
+
|
371
|
+
if (shortcucts.matchesShortcut(selectAllKeyboardShortcut, evt)) {
|
372
|
+
// Selected all in onKeyDown. Don't finalizeTransform.
|
373
|
+
return true;
|
374
|
+
}
|
375
|
+
|
376
|
+
if (this.selectionBox && shortcucts.matchesShortcut(duplicateSelectionShortcut, evt)) {
|
377
|
+
// Finalize duplicating the selection
|
378
|
+
this.selectionBox.duplicateSelectedObjects().then(command => {
|
379
|
+
this.editor.dispatch(command);
|
380
|
+
});
|
381
|
+
return true;
|
382
|
+
}
|
383
|
+
|
384
|
+
if (evt.key === 'Shift') {
|
385
|
+
this.shiftKeyPressed = false;
|
386
|
+
return true;
|
387
|
+
}
|
388
|
+
|
389
|
+
if (this.selectionBox && SelectionTool.handleableKeys.some(key => key === evt.key)) {
|
390
|
+
this.selectionBox.finalizeTransform();
|
391
|
+
return true;
|
392
|
+
}
|
393
|
+
return false;
|
394
|
+
}
|
395
|
+
|
396
|
+
public override onCopy(event: CopyEvent): boolean {
|
397
|
+
if (!this.selectionBox) {
|
398
|
+
return false;
|
399
|
+
}
|
400
|
+
|
401
|
+
const selectedElems = this.selectionBox.getSelectedObjects();
|
402
|
+
const bbox = this.selectionBox.region;
|
403
|
+
if (selectedElems.length === 0) {
|
404
|
+
return false;
|
405
|
+
}
|
406
|
+
|
407
|
+
const exportViewport = new Viewport(() => { });
|
408
|
+
exportViewport.updateScreenSize(Vec2.of(bbox.w, bbox.h));
|
409
|
+
exportViewport.resetTransform(Mat33.translation(bbox.topLeft.times(-1)));
|
410
|
+
|
411
|
+
const sanitize = true;
|
412
|
+
const { element: svgExportElem, renderer: svgRenderer } = SVGRenderer.fromViewport(exportViewport, sanitize);
|
413
|
+
|
414
|
+
const text: string[] = [];
|
415
|
+
for (const elem of selectedElems) {
|
416
|
+
elem.render(svgRenderer);
|
417
|
+
|
418
|
+
if (elem instanceof TextComponent) {
|
419
|
+
text.push(elem.getText());
|
420
|
+
}
|
421
|
+
}
|
422
|
+
|
423
|
+
event.setData('image/svg+xml', svgExportElem.outerHTML);
|
424
|
+
event.setData('text/html', svgExportElem.outerHTML);
|
425
|
+
if (text.length > 0) {
|
426
|
+
event.setData('text/plain', text.join('\n'));
|
427
|
+
}
|
428
|
+
return true;
|
429
|
+
}
|
430
|
+
|
431
|
+
public override setEnabled(enabled: boolean) {
|
432
|
+
super.setEnabled(enabled);
|
433
|
+
|
434
|
+
// Clear the selection
|
435
|
+
this.selectionBox?.cancelSelection();
|
436
|
+
this.onSelectionUpdated();
|
437
|
+
|
438
|
+
this.handleOverlay.replaceChildren();
|
439
|
+
this.selectionBox = null;
|
440
|
+
|
441
|
+
this.shiftKeyPressed = false;
|
442
|
+
this.snapToGrid = false;
|
443
|
+
|
444
|
+
this.handleOverlay.style.display = enabled ? 'block' : 'none';
|
445
|
+
|
446
|
+
if (enabled) {
|
447
|
+
this.handleOverlay.tabIndex = 0;
|
448
|
+
this.handleOverlay.setAttribute('aria-label', this.editor.localization.selectionToolKeyboardShortcuts);
|
449
|
+
} else {
|
450
|
+
this.handleOverlay.tabIndex = -1;
|
451
|
+
}
|
452
|
+
}
|
453
|
+
|
454
|
+
// Get the object responsible for displaying this' selection.
|
455
|
+
// @internal
|
456
|
+
public getSelection(): Selection | null {
|
457
|
+
return this.selectionBox;
|
458
|
+
}
|
459
|
+
|
460
|
+
public getSelectedObjects(): AbstractComponent[] {
|
461
|
+
return this.selectionBox?.getSelectedObjects() ?? [];
|
462
|
+
}
|
463
|
+
|
464
|
+
// Select the given `objects`. Any non-selectable objects in `objects` are ignored.
|
465
|
+
public setSelection(objects: AbstractComponent[]) {
|
466
|
+
// Only select selectable objects.
|
467
|
+
objects = objects.filter(obj => obj.isSelectable());
|
468
|
+
|
469
|
+
// Sort by z-index
|
470
|
+
objects.sort((a, b) => a.getZIndex() - b.getZIndex());
|
471
|
+
|
472
|
+
// Remove duplicates
|
473
|
+
objects = objects.filter((current, idx) => {
|
474
|
+
if (idx > 0) {
|
475
|
+
return current !== objects[idx - 1];
|
476
|
+
}
|
477
|
+
|
478
|
+
return true;
|
479
|
+
});
|
480
|
+
|
481
|
+
let bbox: Rect2 | null = null;
|
482
|
+
for (const object of objects) {
|
483
|
+
if (bbox) {
|
484
|
+
bbox = bbox.union(object.getBBox());
|
485
|
+
} else {
|
486
|
+
bbox = object.getBBox();
|
487
|
+
}
|
488
|
+
}
|
489
|
+
|
490
|
+
if (!bbox) {
|
491
|
+
return;
|
492
|
+
}
|
493
|
+
|
494
|
+
this.clearSelection();
|
495
|
+
if (!this.selectionBox) {
|
496
|
+
this.makeSelectionBox(bbox.topLeft);
|
497
|
+
}
|
498
|
+
|
499
|
+
this.selectionBox!.setSelectedObjects(objects, bbox);
|
500
|
+
this.onSelectionUpdated();
|
501
|
+
}
|
502
|
+
|
503
|
+
public clearSelection() {
|
504
|
+
this.handleOverlay.replaceChildren();
|
505
|
+
this.prevSelectionBox = this.selectionBox;
|
506
|
+
this.selectionBox = null;
|
507
|
+
|
508
|
+
this.onSelectionUpdated();
|
509
|
+
}
|
510
|
+
}
|
@@ -0,0 +1,112 @@
|
|
1
|
+
import Editor from '../../Editor';
|
2
|
+
import { Vec3, Mat33, Vec2, Point2 } from '@js-draw/math';
|
3
|
+
import Viewport from '../../Viewport';
|
4
|
+
import Selection from './Selection';
|
5
|
+
import { ResizeMode } from './types';
|
6
|
+
|
7
|
+
export class DragTransformer {
|
8
|
+
private dragStartPoint: Point2;
|
9
|
+
public constructor(private readonly editor: Editor, private selection: Selection) { }
|
10
|
+
|
11
|
+
public onDragStart(startPoint: Vec3) {
|
12
|
+
this.selection.setTransform(Mat33.identity);
|
13
|
+
this.dragStartPoint = startPoint;
|
14
|
+
}
|
15
|
+
public onDragUpdate(canvasPos: Vec3) {
|
16
|
+
const delta = this.editor.viewport.roundPoint(canvasPos.minus(this.dragStartPoint));
|
17
|
+
this.selection.setTransform(Mat33.translation(
|
18
|
+
delta
|
19
|
+
));
|
20
|
+
}
|
21
|
+
public onDragEnd() {
|
22
|
+
this.selection.finalizeTransform();
|
23
|
+
}
|
24
|
+
}
|
25
|
+
|
26
|
+
export class ResizeTransformer {
|
27
|
+
private mode: ResizeMode = ResizeMode.Both;
|
28
|
+
private dragStartPoint: Point2;
|
29
|
+
public constructor(private readonly editor: Editor, private selection: Selection) { }
|
30
|
+
|
31
|
+
public onDragStart(startPoint: Vec3, mode: ResizeMode) {
|
32
|
+
this.selection.setTransform(Mat33.identity);
|
33
|
+
this.mode = mode;
|
34
|
+
this.dragStartPoint = startPoint;
|
35
|
+
}
|
36
|
+
public onDragUpdate(canvasPos: Vec3) {
|
37
|
+
const canvasDelta = canvasPos.minus(this.dragStartPoint);
|
38
|
+
|
39
|
+
const origWidth = this.selection.preTransformRegion.width;
|
40
|
+
const origHeight = this.selection.preTransformRegion.height;
|
41
|
+
|
42
|
+
let scale = Vec2.of(1, 1);
|
43
|
+
if (this.mode === ResizeMode.HorizontalOnly) {
|
44
|
+
const newWidth = origWidth + canvasDelta.x;
|
45
|
+
scale = Vec2.of(newWidth / origWidth, scale.y);
|
46
|
+
}
|
47
|
+
|
48
|
+
if (this.mode === ResizeMode.VerticalOnly) {
|
49
|
+
const newHeight = origHeight + canvasDelta.y;
|
50
|
+
scale = Vec2.of(scale.x, newHeight / origHeight);
|
51
|
+
}
|
52
|
+
|
53
|
+
if (this.mode === ResizeMode.Both) {
|
54
|
+
const delta = Math.abs(canvasDelta.x) > Math.abs(canvasDelta.y) ? canvasDelta.x : canvasDelta.y;
|
55
|
+
const newWidth = origWidth + delta;
|
56
|
+
scale = Vec2.of(newWidth / origWidth, newWidth / origWidth);
|
57
|
+
}
|
58
|
+
|
59
|
+
// Round: If this isn't done, scaling can create numbers with long decimal representations.
|
60
|
+
// long decimal representations => large file sizes.
|
61
|
+
scale = scale.map(component => Viewport.roundScaleRatio(component, 2));
|
62
|
+
|
63
|
+
if (scale.x !== 0 && scale.y !== 0) {
|
64
|
+
const origin = this.editor.viewport.roundPoint(this.selection.preTransformRegion.topLeft);
|
65
|
+
this.selection.setTransform(Mat33.scaling2D(scale, origin));
|
66
|
+
}
|
67
|
+
}
|
68
|
+
public onDragEnd() {
|
69
|
+
this.selection.finalizeTransform();
|
70
|
+
}
|
71
|
+
}
|
72
|
+
|
73
|
+
export class RotateTransformer {
|
74
|
+
private startAngle: number = 0;
|
75
|
+
public constructor(private readonly editor: Editor, private selection: Selection) { }
|
76
|
+
|
77
|
+
private getAngle(canvasPoint: Point2) {
|
78
|
+
const selectionCenter = this.selection.preTransformRegion.center;
|
79
|
+
const offset = canvasPoint.minus(selectionCenter);
|
80
|
+
return offset.angle();
|
81
|
+
}
|
82
|
+
|
83
|
+
private roundAngle(angle: number) {
|
84
|
+
// Round angles to the nearest 16th of a turn
|
85
|
+
const roundingFactor = 16 / 2 / Math.PI;
|
86
|
+
return Math.round(angle * roundingFactor) / roundingFactor;
|
87
|
+
}
|
88
|
+
|
89
|
+
public onDragStart(startPoint: Vec3) {
|
90
|
+
this.selection.setTransform(Mat33.identity);
|
91
|
+
this.startAngle = this.getAngle(startPoint);
|
92
|
+
}
|
93
|
+
|
94
|
+
public onDragUpdate(canvasPos: Vec3) {
|
95
|
+
const targetRotation = this.roundAngle(this.getAngle(canvasPos) - this.startAngle);
|
96
|
+
|
97
|
+
// Transform in canvas space
|
98
|
+
const canvasSelCenter = this.editor.viewport.roundPoint(this.selection.preTransformRegion.center);
|
99
|
+
const unrounded = Mat33.zRotation(targetRotation);
|
100
|
+
const roundedRotationTransform = unrounded.mapEntries(entry => Viewport.roundScaleRatio(entry));
|
101
|
+
|
102
|
+
const fullRoundedTransform = Mat33
|
103
|
+
.translation(canvasSelCenter)
|
104
|
+
.rightMul(roundedRotationTransform)
|
105
|
+
.rightMul(Mat33.translation(canvasSelCenter.times(-1)));
|
106
|
+
|
107
|
+
this.selection.setTransform(fullRoundedTransform);
|
108
|
+
}
|
109
|
+
public onDragEnd() {
|
110
|
+
this.selection.finalizeTransform();
|
111
|
+
}
|
112
|
+
}
|