js-draw 0.18.2 → 0.20.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/.eslintrc.js +1 -0
- package/CHANGELOG.md +10 -0
- package/dist/bundle.js +2 -2
- package/dist/bundledStyles.js +1 -0
- package/dist/cjs/src/Color4.d.ts +8 -0
- package/dist/cjs/src/Color4.js +67 -0
- package/dist/cjs/src/Editor.d.ts +2 -2
- package/dist/cjs/src/Editor.js +7 -7
- package/dist/cjs/src/SVGLoader.js +77 -12
- package/dist/cjs/src/Viewport.d.ts +2 -0
- package/dist/cjs/src/Viewport.js +6 -2
- package/dist/cjs/src/components/AbstractComponent.d.ts +2 -2
- package/dist/cjs/src/components/AbstractComponent.js +3 -3
- package/dist/cjs/src/components/{ImageBackground.d.ts → BackgroundComponent.d.ts} +23 -3
- package/dist/cjs/src/components/BackgroundComponent.js +309 -0
- package/dist/cjs/src/components/RestylableComponent.d.ts +21 -2
- package/dist/cjs/src/components/Stroke.d.ts +35 -0
- package/dist/cjs/src/components/Stroke.js +37 -3
- package/dist/cjs/src/components/TextComponent.d.ts +27 -17
- package/dist/cjs/src/components/TextComponent.js +23 -1
- package/dist/cjs/src/components/lib.d.ts +4 -3
- package/dist/cjs/src/components/lib.js +2 -2
- package/dist/cjs/src/components/util/StrokeSmoother.js +25 -15
- package/dist/cjs/src/lib.d.ts +30 -0
- package/dist/cjs/src/lib.js +30 -0
- package/dist/cjs/src/localizations/de.js +1 -1
- package/dist/cjs/src/localizations/es.js +1 -1
- package/dist/cjs/src/math/Path.js +1 -1
- package/dist/cjs/src/math/polynomial/QuadraticBezier.d.ts +28 -0
- package/dist/cjs/src/math/polynomial/QuadraticBezier.js +115 -0
- package/dist/cjs/src/math/polynomial/solveQuadratic.d.ts +6 -0
- package/dist/cjs/src/math/polynomial/solveQuadratic.js +36 -0
- package/dist/cjs/src/rendering/RenderingStyle.d.ts +4 -4
- package/dist/cjs/src/rendering/TextRenderingStyle.d.ts +10 -10
- package/dist/cjs/src/rendering/lib.d.ts +2 -0
- package/dist/cjs/src/rendering/renderers/AbstractRenderer.d.ts +2 -2
- package/dist/cjs/src/rendering/renderers/CanvasRenderer.d.ts +2 -2
- package/dist/cjs/src/rendering/renderers/CanvasRenderer.js +5 -3
- package/dist/cjs/src/rendering/renderers/DummyRenderer.d.ts +2 -2
- package/dist/cjs/src/rendering/renderers/SVGRenderer.d.ts +2 -2
- package/dist/cjs/src/rendering/renderers/SVGRenderer.js +15 -6
- package/dist/cjs/src/rendering/renderers/TextOnlyRenderer.d.ts +2 -2
- package/dist/cjs/src/toolbar/IconProvider.d.ts +2 -2
- package/dist/cjs/src/toolbar/localization.d.ts +2 -1
- package/dist/cjs/src/toolbar/localization.js +3 -2
- package/dist/cjs/src/toolbar/widgets/BaseWidget.js +1 -1
- package/dist/cjs/src/toolbar/widgets/DocumentPropertiesWidget.d.ts +5 -0
- package/dist/cjs/src/toolbar/widgets/DocumentPropertiesWidget.js +77 -2
- package/dist/cjs/src/toolbar/widgets/PenToolWidget.js +1 -1
- package/dist/cjs/src/tools/FindTool.js +1 -1
- package/dist/cjs/src/tools/SoundUITool.d.ts +24 -0
- package/dist/cjs/src/tools/SoundUITool.js +164 -0
- package/dist/cjs/src/tools/TextTool.d.ts +2 -2
- package/dist/cjs/src/tools/ToolController.js +6 -1
- package/dist/cjs/src/tools/lib.d.ts +1 -0
- package/dist/cjs/src/tools/lib.js +3 -1
- package/dist/cjs/src/tools/localization.d.ts +3 -0
- package/dist/cjs/src/tools/localization.js +3 -0
- package/dist/mjs/src/Color4.d.ts +8 -0
- package/dist/mjs/src/Color4.mjs +64 -0
- package/dist/mjs/src/Editor.d.ts +2 -2
- package/dist/mjs/src/Editor.mjs +6 -6
- package/dist/mjs/src/SVGLoader.mjs +76 -11
- package/dist/mjs/src/Viewport.d.ts +2 -0
- package/dist/mjs/src/Viewport.mjs +6 -2
- package/dist/mjs/src/components/AbstractComponent.d.ts +2 -2
- package/dist/mjs/src/components/AbstractComponent.mjs +3 -3
- package/dist/mjs/src/components/{ImageBackground.d.ts → BackgroundComponent.d.ts} +23 -3
- package/dist/mjs/src/components/BackgroundComponent.mjs +279 -0
- package/dist/mjs/src/components/RestylableComponent.d.ts +21 -2
- package/dist/mjs/src/components/Stroke.d.ts +35 -0
- package/dist/mjs/src/components/Stroke.mjs +37 -3
- package/dist/mjs/src/components/TextComponent.d.ts +27 -17
- package/dist/mjs/src/components/TextComponent.mjs +23 -1
- package/dist/mjs/src/components/lib.d.ts +4 -3
- package/dist/mjs/src/components/lib.mjs +2 -2
- package/dist/mjs/src/components/util/StrokeSmoother.mjs +25 -15
- package/dist/mjs/src/lib.d.ts +30 -0
- package/dist/mjs/src/lib.mjs +30 -0
- package/dist/mjs/src/localizations/de.mjs +1 -1
- package/dist/mjs/src/localizations/es.mjs +1 -1
- package/dist/mjs/src/math/Path.mjs +1 -1
- package/dist/mjs/src/math/polynomial/QuadraticBezier.d.ts +28 -0
- package/dist/mjs/src/math/polynomial/QuadraticBezier.mjs +109 -0
- package/dist/mjs/src/math/polynomial/solveQuadratic.d.ts +6 -0
- package/dist/mjs/src/math/polynomial/solveQuadratic.mjs +34 -0
- package/dist/mjs/src/rendering/RenderingStyle.d.ts +4 -4
- package/dist/mjs/src/rendering/TextRenderingStyle.d.ts +10 -10
- package/dist/mjs/src/rendering/lib.d.ts +2 -0
- package/dist/mjs/src/rendering/renderers/AbstractRenderer.d.ts +2 -2
- package/dist/mjs/src/rendering/renderers/CanvasRenderer.d.ts +2 -2
- package/dist/mjs/src/rendering/renderers/CanvasRenderer.mjs +5 -3
- package/dist/mjs/src/rendering/renderers/DummyRenderer.d.ts +2 -2
- package/dist/mjs/src/rendering/renderers/SVGRenderer.d.ts +2 -2
- package/dist/mjs/src/rendering/renderers/SVGRenderer.mjs +15 -6
- package/dist/mjs/src/rendering/renderers/TextOnlyRenderer.d.ts +2 -2
- package/dist/mjs/src/toolbar/IconProvider.d.ts +2 -2
- package/dist/mjs/src/toolbar/localization.d.ts +2 -1
- package/dist/mjs/src/toolbar/localization.mjs +3 -2
- package/dist/mjs/src/toolbar/widgets/BaseWidget.mjs +1 -1
- package/dist/mjs/src/toolbar/widgets/DocumentPropertiesWidget.d.ts +5 -0
- package/dist/mjs/src/toolbar/widgets/DocumentPropertiesWidget.mjs +54 -2
- package/dist/mjs/src/toolbar/widgets/PenToolWidget.mjs +1 -1
- package/dist/mjs/src/tools/FindTool.mjs +1 -1
- package/dist/mjs/src/tools/SoundUITool.d.ts +24 -0
- package/dist/mjs/src/tools/SoundUITool.mjs +158 -0
- package/dist/mjs/src/tools/TextTool.d.ts +2 -2
- package/dist/mjs/src/tools/ToolController.mjs +6 -1
- package/dist/mjs/src/tools/lib.d.ts +1 -0
- package/dist/mjs/src/tools/lib.mjs +1 -0
- package/dist/mjs/src/tools/localization.d.ts +3 -0
- package/dist/mjs/src/tools/localization.mjs +3 -0
- package/jest.config.js +1 -1
- package/package.json +19 -17
- package/src/Editor.css +2 -2
- package/src/tools/SoundUITool.css +15 -0
- package/src/tools/tools.css +4 -0
- package/dist/cjs/src/components/ImageBackground.js +0 -146
- package/dist/mjs/src/components/ImageBackground.mjs +0 -139
- package/src/Color4.test.ts +0 -40
- package/src/Color4.ts +0 -236
- package/src/Editor.loadFrom.test.ts +0 -24
- package/src/Editor.toSVG.test.ts +0 -111
- package/src/Editor.ts +0 -1122
- package/src/EditorImage.test.ts +0 -120
- package/src/EditorImage.ts +0 -603
- package/src/EventDispatcher.test.ts +0 -123
- package/src/EventDispatcher.ts +0 -71
- package/src/Pointer.ts +0 -127
- package/src/SVGLoader.test.ts +0 -114
- package/src/SVGLoader.ts +0 -511
- package/src/UndoRedoHistory.test.ts +0 -33
- package/src/UndoRedoHistory.ts +0 -102
- package/src/Viewport.ts +0 -319
- package/src/bundle/bundled.ts +0 -7
- package/src/commands/Command.ts +0 -45
- package/src/commands/Duplicate.ts +0 -48
- package/src/commands/Erase.ts +0 -74
- package/src/commands/SerializableCommand.ts +0 -49
- package/src/commands/UnresolvedCommand.ts +0 -37
- package/src/commands/invertCommand.ts +0 -51
- 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 -135
- package/src/components/AbstractComponent.transformBy.test.ts +0 -22
- package/src/components/AbstractComponent.ts +0 -364
- package/src/components/ImageBackground.test.ts +0 -35
- package/src/components/ImageBackground.ts +0 -176
- package/src/components/ImageComponent.ts +0 -171
- package/src/components/RestylableComponent.ts +0 -142
- package/src/components/SVGGlobalAttributesObject.ts +0 -81
- package/src/components/Stroke.test.ts +0 -139
- package/src/components/Stroke.ts +0 -245
- package/src/components/TextComponent.test.ts +0 -99
- package/src/components/TextComponent.ts +0 -315
- package/src/components/UnknownSVGObject.test.ts +0 -10
- package/src/components/UnknownSVGObject.ts +0 -60
- package/src/components/builders/ArrowBuilder.ts +0 -107
- package/src/components/builders/FreehandLineBuilder.ts +0 -212
- package/src/components/builders/LineBuilder.ts +0 -77
- package/src/components/builders/PressureSensitiveFreehandLineBuilder.ts +0 -454
- package/src/components/builders/RectangleBuilder.ts +0 -74
- package/src/components/builders/types.ts +0 -15
- package/src/components/lib.ts +0 -25
- package/src/components/localization.ts +0 -22
- package/src/components/util/StrokeSmoother.ts +0 -293
- package/src/components/util/describeComponentList.ts +0 -18
- package/src/lib.ts +0 -37
- package/src/localization.ts +0 -34
- package/src/localizations/de.ts +0 -98
- 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 -55
- package/src/math/LineSegment2.test.ts +0 -99
- package/src/math/LineSegment2.ts +0 -160
- package/src/math/Mat33.test.ts +0 -244
- package/src/math/Mat33.ts +0 -437
- package/src/math/Path.fromString.test.ts +0 -223
- package/src/math/Path.test.ts +0 -198
- package/src/math/Path.toString.test.ts +0 -77
- package/src/math/Path.ts +0 -790
- package/src/math/Rect2.test.ts +0 -204
- package/src/math/Rect2.ts +0 -315
- package/src/math/Triangle.ts +0 -29
- package/src/math/Vec2.test.ts +0 -30
- package/src/math/Vec2.ts +0 -18
- package/src/math/Vec3.test.ts +0 -44
- package/src/math/Vec3.ts +0 -218
- package/src/math/lib.ts +0 -15
- package/src/math/rounding.test.ts +0 -65
- package/src/math/rounding.ts +0 -156
- package/src/rendering/Display.ts +0 -249
- package/src/rendering/RenderingStyle.test.ts +0 -68
- package/src/rendering/RenderingStyle.ts +0 -55
- package/src/rendering/TextRenderingStyle.ts +0 -45
- package/src/rendering/caching/CacheRecord.test.ts +0 -49
- package/src/rendering/caching/CacheRecord.ts +0 -77
- package/src/rendering/caching/CacheRecordManager.ts +0 -71
- package/src/rendering/caching/RenderingCache.test.ts +0 -44
- package/src/rendering/caching/RenderingCache.ts +0 -66
- package/src/rendering/caching/RenderingCacheNode.ts +0 -405
- package/src/rendering/caching/testUtils.ts +0 -35
- package/src/rendering/caching/types.ts +0 -34
- package/src/rendering/lib.ts +0 -6
- package/src/rendering/localization.ts +0 -20
- package/src/rendering/renderers/AbstractRenderer.ts +0 -222
- package/src/rendering/renderers/CanvasRenderer.ts +0 -296
- package/src/rendering/renderers/DummyRenderer.test.ts +0 -42
- package/src/rendering/renderers/DummyRenderer.ts +0 -136
- package/src/rendering/renderers/SVGRenderer.ts +0 -354
- package/src/rendering/renderers/TextOnlyRenderer.ts +0 -70
- package/src/testing/beforeEachFile.ts +0 -8
- package/src/testing/createEditor.ts +0 -11
- package/src/testing/global.d.ts +0 -17
- package/src/testing/lib.ts +0 -3
- package/src/testing/loadExpectExtensions.ts +0 -25
- package/src/testing/sendPenEvent.ts +0 -31
- package/src/testing/sendTouchEvent.ts +0 -78
- package/src/toolbar/HTMLToolbar.ts +0 -492
- package/src/toolbar/IconProvider.ts +0 -736
- package/src/toolbar/lib.ts +0 -4
- package/src/toolbar/localization.ts +0 -106
- package/src/toolbar/makeColorInput.ts +0 -145
- package/src/toolbar/types.ts +0 -5
- package/src/toolbar/widgets/ActionButtonWidget.ts +0 -39
- package/src/toolbar/widgets/BaseToolWidget.ts +0 -56
- package/src/toolbar/widgets/BaseWidget.ts +0 -377
- package/src/toolbar/widgets/DocumentPropertiesWidget.ts +0 -167
- package/src/toolbar/widgets/EraserToolWidget.ts +0 -85
- package/src/toolbar/widgets/HandToolWidget.ts +0 -250
- package/src/toolbar/widgets/InsertImageWidget.ts +0 -223
- package/src/toolbar/widgets/OverflowWidget.ts +0 -92
- package/src/toolbar/widgets/PenToolWidget.ts +0 -288
- package/src/toolbar/widgets/SelectionToolWidget.ts +0 -190
- package/src/toolbar/widgets/TextToolWidget.ts +0 -145
- package/src/toolbar/widgets/lib.ts +0 -13
- package/src/tools/BaseTool.ts +0 -76
- package/src/tools/Eraser.test.ts +0 -103
- package/src/tools/Eraser.ts +0 -139
- package/src/tools/FindTool.ts +0 -152
- package/src/tools/PanZoom.test.ts +0 -310
- package/src/tools/PanZoom.ts +0 -520
- package/src/tools/PasteHandler.ts +0 -95
- package/src/tools/Pen.test.ts +0 -194
- package/src/tools/Pen.ts +0 -226
- package/src/tools/PipetteTool.ts +0 -55
- package/src/tools/SelectionTool/SelectAllShortcutHandler.ts +0 -28
- package/src/tools/SelectionTool/Selection.ts +0 -607
- package/src/tools/SelectionTool/SelectionHandle.ts +0 -108
- package/src/tools/SelectionTool/SelectionTool.test.ts +0 -261
- package/src/tools/SelectionTool/SelectionTool.ts +0 -480
- package/src/tools/SelectionTool/TransformMode.ts +0 -114
- package/src/tools/SelectionTool/types.ts +0 -11
- package/src/tools/TextTool.ts +0 -326
- package/src/tools/ToolController.ts +0 -178
- package/src/tools/ToolEnabledGroup.ts +0 -14
- package/src/tools/ToolSwitcherShortcut.ts +0 -39
- package/src/tools/ToolbarShortcutHandler.ts +0 -34
- package/src/tools/UndoRedoShortcut.test.ts +0 -56
- package/src/tools/UndoRedoShortcut.ts +0 -25
- package/src/tools/lib.ts +0 -21
- package/src/tools/localization.ts +0 -66
- package/src/types.ts +0 -234
- package/src/util/assertions.ts +0 -55
- package/src/util/fileToBase64.ts +0 -18
- package/src/util/untilNextAnimationFrame.ts +0 -9
- package/src/util/waitForTimeout.ts +0 -9
@@ -1,480 +0,0 @@
|
|
1
|
-
import AbstractComponent from '../../components/AbstractComponent';
|
2
|
-
import Editor from '../../Editor';
|
3
|
-
import Mat33 from '../../math/Mat33';
|
4
|
-
import Rect2 from '../../math/Rect2';
|
5
|
-
import { Point2, Vec2 } from '../../math/Vec2';
|
6
|
-
import { CopyEvent, EditorEventType, KeyPressEvent, KeyUpEvent, PointerEvt } from '../../types';
|
7
|
-
import Viewport from '../../Viewport';
|
8
|
-
import BaseTool from '../BaseTool';
|
9
|
-
import SVGRenderer from '../../rendering/renderers/SVGRenderer';
|
10
|
-
import Selection from './Selection';
|
11
|
-
import TextComponent from '../../components/TextComponent';
|
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
|
-
private lastEvtTarget: EventTarget|null = null;
|
22
|
-
|
23
|
-
private expandingSelectionBox: boolean = false;
|
24
|
-
private shiftKeyPressed: boolean = false;
|
25
|
-
private ctrlKeyPressed: 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
|
-
this.selectionBox?.updateUI();
|
38
|
-
});
|
39
|
-
|
40
|
-
this.editor.handleKeyEventsFrom(this.handleOverlay);
|
41
|
-
this.editor.handlePointerEventsFrom(this.handleOverlay, (eventName, htmlEvent: PointerEvent) => {
|
42
|
-
if (eventName === 'pointerdown') {
|
43
|
-
this.lastEvtTarget = htmlEvent.target;
|
44
|
-
}
|
45
|
-
return true;
|
46
|
-
});
|
47
|
-
}
|
48
|
-
|
49
|
-
private makeSelectionBox(selectionStartPos: Point2) {
|
50
|
-
this.prevSelectionBox = this.selectionBox;
|
51
|
-
this.selectionBox = new Selection(
|
52
|
-
selectionStartPos, this.editor
|
53
|
-
);
|
54
|
-
|
55
|
-
if (!this.expandingSelectionBox) {
|
56
|
-
// Remove any previous selection rects
|
57
|
-
this.prevSelectionBox?.cancelSelection();
|
58
|
-
}
|
59
|
-
this.selectionBox.addTo(this.handleOverlay);
|
60
|
-
}
|
61
|
-
|
62
|
-
private snapSelectionToGrid() {
|
63
|
-
if (!this.selectionBox) throw new Error('No selection to snap!');
|
64
|
-
|
65
|
-
// Snap the top left corner of what we have selected.
|
66
|
-
const topLeftOfBBox = this.selectionBox.computeTightBoundingBox().topLeft;
|
67
|
-
const snappedTopLeft = this.editor.viewport.snapToGrid(topLeftOfBBox);
|
68
|
-
const snapDelta = snappedTopLeft.minus(topLeftOfBBox);
|
69
|
-
|
70
|
-
const oldTransform = this.selectionBox.getTransform();
|
71
|
-
this.selectionBox.setTransform(oldTransform.rightMul(Mat33.translation(snapDelta)));
|
72
|
-
this.selectionBox.finalizeTransform();
|
73
|
-
}
|
74
|
-
|
75
|
-
private selectionBoxHandlingEvt: boolean = false;
|
76
|
-
public onPointerDown({ allPointers, current }: PointerEvt): boolean {
|
77
|
-
const snapToGrid = this.ctrlKeyPressed;
|
78
|
-
if (snapToGrid) {
|
79
|
-
current = current.snappedToGrid(this.editor.viewport);
|
80
|
-
}
|
81
|
-
|
82
|
-
if (allPointers.length === 1 && current.isPrimary) {
|
83
|
-
let transforming = false;
|
84
|
-
|
85
|
-
if (this.lastEvtTarget && this.selectionBox) {
|
86
|
-
if (snapToGrid) {
|
87
|
-
this.snapSelectionToGrid();
|
88
|
-
}
|
89
|
-
|
90
|
-
const dragStartResult = this.selectionBox.onDragStart(current, this.lastEvtTarget);
|
91
|
-
|
92
|
-
if (dragStartResult) {
|
93
|
-
transforming = true;
|
94
|
-
|
95
|
-
this.selectionBoxHandlingEvt = true;
|
96
|
-
this.expandingSelectionBox = false;
|
97
|
-
}
|
98
|
-
}
|
99
|
-
|
100
|
-
if (!transforming) {
|
101
|
-
// Shift key: Combine the new and old selection boxes at the end of the gesture.
|
102
|
-
this.expandingSelectionBox = this.shiftKeyPressed;
|
103
|
-
this.makeSelectionBox(current.canvasPos);
|
104
|
-
}
|
105
|
-
|
106
|
-
return true;
|
107
|
-
}
|
108
|
-
return false;
|
109
|
-
}
|
110
|
-
|
111
|
-
public onPointerMove(event: PointerEvt): void {
|
112
|
-
if (!this.selectionBox) return;
|
113
|
-
|
114
|
-
let currentPointer = event.current;
|
115
|
-
if (this.ctrlKeyPressed) {
|
116
|
-
currentPointer = currentPointer.snappedToGrid(this.editor.viewport);
|
117
|
-
}
|
118
|
-
|
119
|
-
if (this.selectionBoxHandlingEvt) {
|
120
|
-
this.selectionBox.onDragUpdate(currentPointer);
|
121
|
-
} else {
|
122
|
-
this.selectionBox!.setToPoint(currentPointer.canvasPos);
|
123
|
-
}
|
124
|
-
}
|
125
|
-
|
126
|
-
private onSelectionUpdated() {
|
127
|
-
// Note that the selection has changed
|
128
|
-
this.editor.notifier.dispatch(EditorEventType.ToolUpdated, {
|
129
|
-
kind: EditorEventType.ToolUpdated,
|
130
|
-
tool: this,
|
131
|
-
});
|
132
|
-
|
133
|
-
const selectedItemCount = this.selectionBox?.getSelectedItemCount() ?? 0;
|
134
|
-
if (selectedItemCount > 0) {
|
135
|
-
this.editor.announceForAccessibility(
|
136
|
-
this.editor.localization.selectedElements(selectedItemCount)
|
137
|
-
);
|
138
|
-
this.zoomToSelection();
|
139
|
-
} else if (this.selectionBox) {
|
140
|
-
this.selectionBox.cancelSelection();
|
141
|
-
this.prevSelectionBox = this.selectionBox;
|
142
|
-
this.selectionBox = null;
|
143
|
-
}
|
144
|
-
}
|
145
|
-
|
146
|
-
// Called after a gestureCancel and a pointerUp
|
147
|
-
private onGestureEnd() {
|
148
|
-
this.lastEvtTarget = null;
|
149
|
-
|
150
|
-
if (!this.selectionBox) return;
|
151
|
-
|
152
|
-
if (!this.selectionBoxHandlingEvt) {
|
153
|
-
// Expand/shrink the selection rectangle, if applicable
|
154
|
-
this.selectionBox.resolveToObjects();
|
155
|
-
this.onSelectionUpdated();
|
156
|
-
} else {
|
157
|
-
this.selectionBox.onDragEnd();
|
158
|
-
}
|
159
|
-
|
160
|
-
|
161
|
-
this.selectionBoxHandlingEvt = false;
|
162
|
-
}
|
163
|
-
|
164
|
-
private zoomToSelection() {
|
165
|
-
if (this.selectionBox) {
|
166
|
-
const selectionRect = this.selectionBox.region;
|
167
|
-
this.editor.dispatchNoAnnounce(this.editor.viewport.zoomTo(selectionRect, false), false);
|
168
|
-
}
|
169
|
-
}
|
170
|
-
|
171
|
-
public onPointerUp(event: PointerEvt): void {
|
172
|
-
if (!this.selectionBox) return;
|
173
|
-
|
174
|
-
let currentPointer = event.current;
|
175
|
-
if (this.ctrlKeyPressed) {
|
176
|
-
currentPointer = currentPointer.snappedToGrid(this.editor.viewport);
|
177
|
-
}
|
178
|
-
|
179
|
-
this.selectionBox.setToPoint(currentPointer.canvasPos);
|
180
|
-
|
181
|
-
// Were we expanding the previous selection?
|
182
|
-
if (this.expandingSelectionBox && this.prevSelectionBox) {
|
183
|
-
// If so, finish expanding.
|
184
|
-
this.expandingSelectionBox = false;
|
185
|
-
this.selectionBox.resolveToObjects();
|
186
|
-
this.setSelection([
|
187
|
-
...this.selectionBox.getSelectedObjects(),
|
188
|
-
...this.prevSelectionBox.getSelectedObjects(),
|
189
|
-
]);
|
190
|
-
} else {
|
191
|
-
this.onGestureEnd();
|
192
|
-
}
|
193
|
-
}
|
194
|
-
|
195
|
-
public onGestureCancel(): void {
|
196
|
-
if (this.selectionBoxHandlingEvt) {
|
197
|
-
this.selectionBox?.onDragCancel();
|
198
|
-
} else {
|
199
|
-
// Revert to the previous selection, if any.
|
200
|
-
this.selectionBox?.cancelSelection();
|
201
|
-
this.selectionBox = this.prevSelectionBox;
|
202
|
-
this.selectionBox?.addTo(this.handleOverlay);
|
203
|
-
this.selectionBox?.recomputeRegion();
|
204
|
-
this.prevSelectionBox = null;
|
205
|
-
}
|
206
|
-
|
207
|
-
this.expandingSelectionBox = false;
|
208
|
-
}
|
209
|
-
|
210
|
-
private static handleableKeys = [
|
211
|
-
'a', 'h', 'ArrowLeft',
|
212
|
-
'd', 'l', 'ArrowRight',
|
213
|
-
'q', 'k', 'ArrowUp',
|
214
|
-
'e', 'j', 'ArrowDown',
|
215
|
-
'r', 'R',
|
216
|
-
'i', 'I', 'o', 'O',
|
217
|
-
'Control', 'Meta',
|
218
|
-
];
|
219
|
-
public onKeyPress(event: KeyPressEvent): boolean {
|
220
|
-
if (event.key === 'Control' || event.key === 'Meta') {
|
221
|
-
this.ctrlKeyPressed = true;
|
222
|
-
return true;
|
223
|
-
}
|
224
|
-
|
225
|
-
if (this.selectionBox && event.ctrlKey && event.key === 'd') {
|
226
|
-
// Handle duplication on key up — we don't want to accidentally duplicate
|
227
|
-
// many times.
|
228
|
-
return true;
|
229
|
-
}
|
230
|
-
else if (event.key === 'a' && event.ctrlKey) {
|
231
|
-
this.setSelection(this.editor.image.getAllElements());
|
232
|
-
return true;
|
233
|
-
}
|
234
|
-
else if (event.ctrlKey) {
|
235
|
-
// Don't transform the selection with, for example, ctrl+i.
|
236
|
-
// Pass it to another tool, if apliccable.
|
237
|
-
return false;
|
238
|
-
}
|
239
|
-
else if (event.key === 'Shift') {
|
240
|
-
this.shiftKeyPressed = true;
|
241
|
-
return true;
|
242
|
-
}
|
243
|
-
|
244
|
-
let rotationSteps = 0;
|
245
|
-
let xTranslateSteps = 0;
|
246
|
-
let yTranslateSteps = 0;
|
247
|
-
let xScaleSteps = 0;
|
248
|
-
let yScaleSteps = 0;
|
249
|
-
|
250
|
-
switch (event.key) {
|
251
|
-
case 'a':
|
252
|
-
case 'h':
|
253
|
-
case 'ArrowLeft':
|
254
|
-
xTranslateSteps -= 1;
|
255
|
-
break;
|
256
|
-
case 'd':
|
257
|
-
case 'l':
|
258
|
-
case 'ArrowRight':
|
259
|
-
xTranslateSteps += 1;
|
260
|
-
break;
|
261
|
-
case 'q':
|
262
|
-
case 'k':
|
263
|
-
case 'ArrowUp':
|
264
|
-
yTranslateSteps -= 1;
|
265
|
-
break;
|
266
|
-
case 'e':
|
267
|
-
case 'j':
|
268
|
-
case 'ArrowDown':
|
269
|
-
yTranslateSteps += 1;
|
270
|
-
break;
|
271
|
-
case 'r':
|
272
|
-
rotationSteps += 1;
|
273
|
-
break;
|
274
|
-
case 'R':
|
275
|
-
rotationSteps -= 1;
|
276
|
-
break;
|
277
|
-
case 'i':
|
278
|
-
xScaleSteps -= 1;
|
279
|
-
break;
|
280
|
-
case 'I':
|
281
|
-
xScaleSteps += 1;
|
282
|
-
break;
|
283
|
-
case 'o':
|
284
|
-
yScaleSteps -= 1;
|
285
|
-
break;
|
286
|
-
case 'O':
|
287
|
-
yScaleSteps += 1;
|
288
|
-
break;
|
289
|
-
}
|
290
|
-
|
291
|
-
let handled = xTranslateSteps !== 0
|
292
|
-
|| yTranslateSteps !== 0
|
293
|
-
|| rotationSteps !== 0
|
294
|
-
|| xScaleSteps !== 0
|
295
|
-
|| yScaleSteps !== 0;
|
296
|
-
|
297
|
-
if (!this.selectionBox) {
|
298
|
-
handled = false;
|
299
|
-
} else if (handled) {
|
300
|
-
const translateStepSize = 10 * this.editor.viewport.getSizeOfPixelOnCanvas();
|
301
|
-
const rotateStepSize = Math.PI / 8;
|
302
|
-
const scaleStepSize = 5 / 4;
|
303
|
-
|
304
|
-
const region = this.selectionBox.region;
|
305
|
-
const scaleFactor = Vec2.of(scaleStepSize ** xScaleSteps, scaleStepSize ** yScaleSteps);
|
306
|
-
|
307
|
-
const rotationMat = Mat33.zRotation(
|
308
|
-
rotationSteps * rotateStepSize
|
309
|
-
);
|
310
|
-
const roundedRotationMatrix = rotationMat.mapEntries(component => Viewport.roundScaleRatio(component));
|
311
|
-
const regionCenter = this.editor.viewport.roundPoint(region.center);
|
312
|
-
|
313
|
-
const transform = Mat33.scaling2D(
|
314
|
-
scaleFactor,
|
315
|
-
this.editor.viewport.roundPoint(region.topLeft)
|
316
|
-
).rightMul(
|
317
|
-
Mat33.translation(regionCenter).rightMul(
|
318
|
-
roundedRotationMatrix
|
319
|
-
).rightMul(
|
320
|
-
Mat33.translation(regionCenter.times(-1))
|
321
|
-
)
|
322
|
-
).rightMul(Mat33.translation(
|
323
|
-
this.editor.viewport.roundPoint(Vec2.of(xTranslateSteps, yTranslateSteps).times(translateStepSize))
|
324
|
-
));
|
325
|
-
const oldTransform = this.selectionBox.getTransform();
|
326
|
-
this.selectionBox.setTransform(oldTransform.rightMul(transform));
|
327
|
-
}
|
328
|
-
|
329
|
-
if (this.selectionBox && !handled && (event.key === 'Delete' || event.key === 'Backspace')) {
|
330
|
-
this.editor.dispatch(this.selectionBox.deleteSelectedObjects());
|
331
|
-
this.clearSelection();
|
332
|
-
handled = true;
|
333
|
-
}
|
334
|
-
|
335
|
-
return handled;
|
336
|
-
}
|
337
|
-
|
338
|
-
public onKeyUp(evt: KeyUpEvent) {
|
339
|
-
if (evt.key === 'Control' || evt.key === 'Meta') {
|
340
|
-
this.ctrlKeyPressed = false;
|
341
|
-
return true;
|
342
|
-
}
|
343
|
-
|
344
|
-
if (evt.key === 'Shift') {
|
345
|
-
this.shiftKeyPressed = false;
|
346
|
-
return true;
|
347
|
-
}
|
348
|
-
else if (evt.ctrlKey) {
|
349
|
-
if (this.selectionBox && evt.key === 'd') {
|
350
|
-
this.selectionBox.duplicateSelectedObjects().then(command => {
|
351
|
-
this.editor.dispatch(command);
|
352
|
-
});
|
353
|
-
return true;
|
354
|
-
}
|
355
|
-
|
356
|
-
if (evt.key === 'a' || evt.key === 'r') {
|
357
|
-
// Selected all in onKeyDown. Don't finalizeTransform.
|
358
|
-
return true;
|
359
|
-
}
|
360
|
-
}
|
361
|
-
|
362
|
-
if (this.selectionBox && SelectionTool.handleableKeys.some(key => key === evt.key)) {
|
363
|
-
this.selectionBox.finalizeTransform();
|
364
|
-
return true;
|
365
|
-
}
|
366
|
-
return false;
|
367
|
-
}
|
368
|
-
|
369
|
-
public onCopy(event: CopyEvent): boolean {
|
370
|
-
if (!this.selectionBox) {
|
371
|
-
return false;
|
372
|
-
}
|
373
|
-
|
374
|
-
const selectedElems = this.selectionBox.getSelectedObjects();
|
375
|
-
const bbox = this.selectionBox.region;
|
376
|
-
if (selectedElems.length === 0) {
|
377
|
-
return false;
|
378
|
-
}
|
379
|
-
|
380
|
-
const exportViewport = new Viewport(() => {});
|
381
|
-
exportViewport.updateScreenSize(Vec2.of(bbox.w, bbox.h));
|
382
|
-
exportViewport.resetTransform(Mat33.translation(bbox.topLeft.times(-1)));
|
383
|
-
|
384
|
-
const sanitize = true;
|
385
|
-
const { element: svgExportElem, renderer: svgRenderer } = SVGRenderer.fromViewport(exportViewport, sanitize);
|
386
|
-
|
387
|
-
const text: string[] = [];
|
388
|
-
for (const elem of selectedElems) {
|
389
|
-
elem.render(svgRenderer);
|
390
|
-
|
391
|
-
if (elem instanceof TextComponent) {
|
392
|
-
text.push(elem.getText());
|
393
|
-
}
|
394
|
-
}
|
395
|
-
|
396
|
-
event.setData('image/svg+xml', svgExportElem.outerHTML);
|
397
|
-
event.setData('text/html', svgExportElem.outerHTML);
|
398
|
-
if (text.length > 0) {
|
399
|
-
event.setData('text/plain', text.join('\n'));
|
400
|
-
}
|
401
|
-
return true;
|
402
|
-
}
|
403
|
-
|
404
|
-
public setEnabled(enabled: boolean) {
|
405
|
-
super.setEnabled(enabled);
|
406
|
-
|
407
|
-
// Clear the selection
|
408
|
-
this.handleOverlay.replaceChildren();
|
409
|
-
this.selectionBox = null;
|
410
|
-
|
411
|
-
this.shiftKeyPressed = false;
|
412
|
-
this.ctrlKeyPressed = false;
|
413
|
-
|
414
|
-
this.handleOverlay.style.display = enabled ? 'block' : 'none';
|
415
|
-
|
416
|
-
if (enabled) {
|
417
|
-
this.handleOverlay.tabIndex = 0;
|
418
|
-
this.handleOverlay.setAttribute('aria-label', this.editor.localization.selectionToolKeyboardShortcuts);
|
419
|
-
} else {
|
420
|
-
this.handleOverlay.tabIndex = -1;
|
421
|
-
}
|
422
|
-
}
|
423
|
-
|
424
|
-
// Get the object responsible for displaying this' selection.
|
425
|
-
// @internal
|
426
|
-
public getSelection(): Selection|null {
|
427
|
-
return this.selectionBox;
|
428
|
-
}
|
429
|
-
|
430
|
-
public getSelectedObjects(): AbstractComponent[] {
|
431
|
-
return this.selectionBox?.getSelectedObjects() ?? [];
|
432
|
-
}
|
433
|
-
|
434
|
-
// Select the given `objects`. Any non-selectable objects in `objects` are ignored.
|
435
|
-
public setSelection(objects: AbstractComponent[]) {
|
436
|
-
// Only select selectable objects.
|
437
|
-
objects = objects.filter(obj => obj.isSelectable());
|
438
|
-
|
439
|
-
// Sort by z-index
|
440
|
-
objects.sort((a, b) => a.getZIndex() - b.getZIndex());
|
441
|
-
|
442
|
-
// Remove duplicates
|
443
|
-
objects = objects.filter((current, idx) => {
|
444
|
-
if (idx > 0) {
|
445
|
-
return current !== objects[idx - 1];
|
446
|
-
}
|
447
|
-
|
448
|
-
return true;
|
449
|
-
});
|
450
|
-
|
451
|
-
let bbox: Rect2|null = null;
|
452
|
-
for (const object of objects) {
|
453
|
-
if (bbox) {
|
454
|
-
bbox = bbox.union(object.getBBox());
|
455
|
-
} else {
|
456
|
-
bbox = object.getBBox();
|
457
|
-
}
|
458
|
-
}
|
459
|
-
|
460
|
-
if (!bbox) {
|
461
|
-
return;
|
462
|
-
}
|
463
|
-
|
464
|
-
this.clearSelection();
|
465
|
-
if (!this.selectionBox) {
|
466
|
-
this.makeSelectionBox(bbox.topLeft);
|
467
|
-
}
|
468
|
-
|
469
|
-
this.selectionBox!.setSelectedObjects(objects, bbox);
|
470
|
-
this.onSelectionUpdated();
|
471
|
-
}
|
472
|
-
|
473
|
-
public clearSelection() {
|
474
|
-
this.handleOverlay.replaceChildren();
|
475
|
-
this.prevSelectionBox = this.selectionBox;
|
476
|
-
this.selectionBox = null;
|
477
|
-
|
478
|
-
this.onSelectionUpdated();
|
479
|
-
}
|
480
|
-
}
|
@@ -1,114 +0,0 @@
|
|
1
|
-
import Editor from '../../Editor';
|
2
|
-
import Mat33 from '../../math/Mat33';
|
3
|
-
import { Point2, Vec2 } from '../../math/Vec2';
|
4
|
-
import Vec3 from '../../math/Vec3';
|
5
|
-
import Viewport from '../../Viewport';
|
6
|
-
import Selection from './Selection';
|
7
|
-
import { ResizeMode } from './types';
|
8
|
-
|
9
|
-
export class DragTransformer {
|
10
|
-
private dragStartPoint: Point2;
|
11
|
-
public constructor(private readonly editor: Editor, private selection: Selection) { }
|
12
|
-
|
13
|
-
public onDragStart(startPoint: Vec3) {
|
14
|
-
this.selection.setTransform(Mat33.identity);
|
15
|
-
this.dragStartPoint = startPoint;
|
16
|
-
}
|
17
|
-
public onDragUpdate(canvasPos: Vec3) {
|
18
|
-
const delta = this.editor.viewport.roundPoint(canvasPos.minus(this.dragStartPoint));
|
19
|
-
this.selection.setTransform(Mat33.translation(
|
20
|
-
delta
|
21
|
-
));
|
22
|
-
}
|
23
|
-
public onDragEnd() {
|
24
|
-
this.selection.finalizeTransform();
|
25
|
-
}
|
26
|
-
}
|
27
|
-
|
28
|
-
export class ResizeTransformer {
|
29
|
-
private mode: ResizeMode = ResizeMode.Both;
|
30
|
-
private dragStartPoint: Point2;
|
31
|
-
public constructor(private readonly editor: Editor, private selection: Selection) { }
|
32
|
-
|
33
|
-
public onDragStart(startPoint: Vec3, mode: ResizeMode) {
|
34
|
-
this.selection.setTransform(Mat33.identity);
|
35
|
-
this.mode = mode;
|
36
|
-
this.dragStartPoint = startPoint;
|
37
|
-
}
|
38
|
-
public onDragUpdate(canvasPos: Vec3) {
|
39
|
-
const canvasDelta = canvasPos.minus(this.dragStartPoint);
|
40
|
-
|
41
|
-
const origWidth = this.selection.preTransformRegion.width;
|
42
|
-
const origHeight = this.selection.preTransformRegion.height;
|
43
|
-
|
44
|
-
let scale = Vec2.of(1, 1);
|
45
|
-
if (this.mode === ResizeMode.HorizontalOnly) {
|
46
|
-
const newWidth = origWidth + canvasDelta.x;
|
47
|
-
scale = Vec2.of(newWidth / origWidth, scale.y);
|
48
|
-
}
|
49
|
-
|
50
|
-
if (this.mode === ResizeMode.VerticalOnly) {
|
51
|
-
const newHeight = origHeight + canvasDelta.y;
|
52
|
-
scale = Vec2.of(scale.x, newHeight / origHeight);
|
53
|
-
}
|
54
|
-
|
55
|
-
if (this.mode === ResizeMode.Both) {
|
56
|
-
const delta = Math.abs(canvasDelta.x) > Math.abs(canvasDelta.y) ? canvasDelta.x : canvasDelta.y;
|
57
|
-
const newWidth = origWidth + delta;
|
58
|
-
scale = Vec2.of(newWidth / origWidth, newWidth / origWidth);
|
59
|
-
}
|
60
|
-
|
61
|
-
// Round: If this isn't done, scaling can create numbers with long decimal representations.
|
62
|
-
// long decimal representations => large file sizes.
|
63
|
-
scale = scale.map(component => Viewport.roundScaleRatio(component, 2));
|
64
|
-
|
65
|
-
if (scale.x !== 0 && scale.y !== 0) {
|
66
|
-
const origin = this.editor.viewport.roundPoint(this.selection.preTransformRegion.topLeft);
|
67
|
-
this.selection.setTransform(Mat33.scaling2D(scale, origin));
|
68
|
-
}
|
69
|
-
}
|
70
|
-
public onDragEnd() {
|
71
|
-
this.selection.finalizeTransform();
|
72
|
-
}
|
73
|
-
}
|
74
|
-
|
75
|
-
export class RotateTransformer {
|
76
|
-
private startAngle: number = 0;
|
77
|
-
public constructor(private readonly editor: Editor, private selection: Selection) { }
|
78
|
-
|
79
|
-
private getAngle(canvasPoint: Point2) {
|
80
|
-
const selectionCenter = this.selection.preTransformRegion.center;
|
81
|
-
const offset = canvasPoint.minus(selectionCenter);
|
82
|
-
return offset.angle();
|
83
|
-
}
|
84
|
-
|
85
|
-
private roundAngle(angle: number) {
|
86
|
-
// Round angles to the nearest 16th of a turn
|
87
|
-
const roundingFactor = 16 / 2 / Math.PI;
|
88
|
-
return Math.round(angle * roundingFactor) / roundingFactor;
|
89
|
-
}
|
90
|
-
|
91
|
-
public onDragStart(startPoint: Vec3) {
|
92
|
-
this.selection.setTransform(Mat33.identity);
|
93
|
-
this.startAngle = this.getAngle(startPoint);
|
94
|
-
}
|
95
|
-
|
96
|
-
public onDragUpdate(canvasPos: Vec3) {
|
97
|
-
const targetRotation = this.roundAngle(this.getAngle(canvasPos) - this.startAngle);
|
98
|
-
|
99
|
-
// Transform in canvas space
|
100
|
-
const canvasSelCenter = this.editor.viewport.roundPoint(this.selection.preTransformRegion.center);
|
101
|
-
const unrounded = Mat33.zRotation(targetRotation);
|
102
|
-
const roundedRotationTransform = unrounded.mapEntries(entry => Viewport.roundScaleRatio(entry));
|
103
|
-
|
104
|
-
const fullRoundedTransform = Mat33
|
105
|
-
.translation(canvasSelCenter)
|
106
|
-
.rightMul(roundedRotationTransform)
|
107
|
-
.rightMul(Mat33.translation(canvasSelCenter.times(-1)));
|
108
|
-
|
109
|
-
this.selection.setTransform(fullRoundedTransform);
|
110
|
-
}
|
111
|
-
public onDragEnd() {
|
112
|
-
this.selection.finalizeTransform();
|
113
|
-
}
|
114
|
-
}
|