js-draw 0.3.2 → 0.4.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/.github/pull_request_template.md +15 -0
- package/.github/workflows/firebase-hosting-merge.yml +7 -0
- package/.github/workflows/firebase-hosting-pull-request.yml +10 -0
- package/.github/workflows/github-pages.yml +2 -0
- package/CHANGELOG.md +16 -1
- package/README.md +1 -3
- package/dist/bundle.js +1 -1
- package/dist/src/Editor.d.ts +11 -0
- package/dist/src/Editor.js +107 -77
- package/dist/src/Pointer.d.ts +1 -1
- package/dist/src/Pointer.js +8 -3
- package/dist/src/Viewport.d.ts +1 -0
- package/dist/src/Viewport.js +14 -1
- package/dist/src/components/AbstractComponent.js +1 -0
- package/dist/src/components/ImageComponent.d.ts +2 -2
- package/dist/src/components/Stroke.js +15 -9
- package/dist/src/components/Text.d.ts +1 -1
- package/dist/src/components/Text.js +1 -1
- package/dist/src/components/builders/FreehandLineBuilder.d.ts +1 -0
- package/dist/src/components/builders/FreehandLineBuilder.js +34 -36
- package/dist/src/language/assertions.d.ts +1 -0
- package/dist/src/language/assertions.js +5 -0
- package/dist/src/math/Mat33.d.ts +38 -2
- package/dist/src/math/Mat33.js +30 -1
- package/dist/src/math/Path.d.ts +1 -1
- package/dist/src/math/Path.js +10 -8
- package/dist/src/math/Vec3.d.ts +12 -2
- package/dist/src/math/Vec3.js +16 -1
- package/dist/src/math/rounding.d.ts +1 -0
- package/dist/src/math/rounding.js +13 -6
- package/dist/src/rendering/renderers/AbstractRenderer.js +2 -1
- package/dist/src/testing/beforeEachFile.d.ts +1 -0
- package/dist/src/testing/beforeEachFile.js +3 -0
- package/dist/src/testing/createEditor.d.ts +1 -0
- package/dist/src/testing/createEditor.js +7 -1
- package/dist/src/testing/loadExpectExtensions.d.ts +0 -15
- package/dist/src/toolbar/HTMLToolbar.js +5 -4
- package/dist/src/toolbar/widgets/SelectionToolWidget.d.ts +1 -1
- package/dist/src/tools/PasteHandler.js +3 -1
- package/dist/src/tools/Pen.js +1 -1
- package/dist/src/tools/SelectionTool/Selection.d.ts +54 -0
- package/dist/src/tools/SelectionTool/Selection.js +337 -0
- package/dist/src/tools/SelectionTool/SelectionHandle.d.ts +35 -0
- package/dist/src/tools/SelectionTool/SelectionHandle.js +75 -0
- package/dist/src/tools/SelectionTool/SelectionTool.d.ts +31 -0
- package/dist/src/tools/SelectionTool/SelectionTool.js +284 -0
- package/dist/src/tools/SelectionTool/TransformMode.d.ts +34 -0
- package/dist/src/tools/SelectionTool/TransformMode.js +98 -0
- package/dist/src/tools/SelectionTool/types.d.ts +9 -0
- package/dist/src/tools/SelectionTool/types.js +11 -0
- package/dist/src/tools/ToolController.js +1 -1
- package/dist/src/tools/lib.d.ts +1 -1
- package/dist/src/tools/lib.js +1 -1
- package/dist/src/types.d.ts +1 -1
- package/jest.config.js +5 -0
- package/package.json +15 -14
- package/src/Editor.css +1 -0
- package/src/Editor.ts +147 -108
- package/src/Pointer.ts +8 -3
- package/src/Viewport.ts +17 -2
- package/src/components/AbstractComponent.ts +4 -6
- package/src/components/ImageComponent.ts +2 -6
- package/src/components/Stroke.test.ts +0 -3
- package/src/components/Stroke.ts +14 -7
- package/src/components/Text.test.ts +0 -3
- package/src/components/Text.ts +4 -8
- package/src/components/builders/FreehandLineBuilder.ts +37 -43
- package/src/language/assertions.ts +6 -0
- package/src/math/LineSegment2.test.ts +8 -10
- package/src/math/Mat33.test.ts +14 -2
- package/src/math/Mat33.ts +43 -2
- package/src/math/Path.toString.test.ts +12 -1
- package/src/math/Path.ts +11 -9
- package/src/math/Rect2.test.ts +0 -3
- package/src/math/Vec2.test.ts +0 -3
- package/src/math/Vec3.test.ts +0 -3
- package/src/math/Vec3.ts +23 -2
- package/src/math/rounding.test.ts +30 -5
- package/src/math/rounding.ts +16 -7
- package/src/rendering/renderers/AbstractRenderer.ts +3 -2
- package/src/testing/beforeEachFile.ts +3 -0
- package/src/testing/createEditor.ts +8 -1
- package/src/testing/global.d.ts +17 -0
- package/src/testing/loadExpectExtensions.ts +0 -15
- package/src/toolbar/HTMLToolbar.ts +5 -4
- package/src/toolbar/toolbar.css +3 -2
- package/src/toolbar/widgets/SelectionToolWidget.ts +1 -1
- package/src/tools/PasteHandler.ts +4 -1
- package/src/tools/Pen.test.ts +150 -0
- package/src/tools/Pen.ts +1 -1
- package/src/tools/SelectionTool/Selection.ts +455 -0
- package/src/tools/SelectionTool/SelectionHandle.ts +99 -0
- package/src/tools/SelectionTool/SelectionTool.css +22 -0
- package/src/tools/{SelectionTool.test.ts → SelectionTool/SelectionTool.test.ts} +21 -21
- package/src/tools/SelectionTool/SelectionTool.ts +344 -0
- package/src/tools/SelectionTool/TransformMode.ts +114 -0
- package/src/tools/SelectionTool/types.ts +11 -0
- package/src/tools/ToolController.ts +1 -1
- package/src/tools/lib.ts +1 -1
- package/src/types.ts +1 -1
- package/tsconfig.json +3 -1
- package/dist/src/tools/SelectionTool.d.ts +0 -65
- package/dist/src/tools/SelectionTool.js +0 -647
- package/src/tools/SelectionTool.ts +0 -797
@@ -1,647 +0,0 @@
|
|
1
|
-
// Allows users to select/transform portions of the `EditorImage`.
|
2
|
-
// With respect to `extend`ing, `SelectionTool` is not stable.
|
3
|
-
// @packageDocumentation
|
4
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
5
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
6
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
7
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
8
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
9
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
10
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
11
|
-
});
|
12
|
-
};
|
13
|
-
var _a;
|
14
|
-
import Duplicate from '../commands/Duplicate';
|
15
|
-
import Erase from '../commands/Erase';
|
16
|
-
import Mat33 from '../math/Mat33';
|
17
|
-
import Rect2 from '../math/Rect2';
|
18
|
-
import { Vec2 } from '../math/Vec2';
|
19
|
-
import { EditorEventType } from '../types';
|
20
|
-
import Viewport from '../Viewport';
|
21
|
-
import BaseTool from './BaseTool';
|
22
|
-
import SerializableCommand from '../commands/SerializableCommand';
|
23
|
-
import SVGRenderer from '../rendering/renderers/SVGRenderer';
|
24
|
-
const handleScreenSize = 30;
|
25
|
-
const styles = `
|
26
|
-
.handleOverlay {
|
27
|
-
}
|
28
|
-
|
29
|
-
.handleOverlay > .selectionBox {
|
30
|
-
position: absolute;
|
31
|
-
z-index: 0;
|
32
|
-
transform-origin: center;
|
33
|
-
}
|
34
|
-
|
35
|
-
.handleOverlay > .selectionBox .draggableBackground {
|
36
|
-
position: absolute;
|
37
|
-
top: 0;
|
38
|
-
left: 0;
|
39
|
-
right: 0;
|
40
|
-
bottom: 0;
|
41
|
-
|
42
|
-
background-color: var(--secondary-background-color);
|
43
|
-
opacity: 0.8;
|
44
|
-
border: 1px solid var(--primary-background-color);
|
45
|
-
}
|
46
|
-
|
47
|
-
.handleOverlay .resizeCorner {
|
48
|
-
width: ${handleScreenSize}px;
|
49
|
-
height: ${handleScreenSize}px;
|
50
|
-
margin-right: -${handleScreenSize / 2}px;
|
51
|
-
margin-bottom: -${handleScreenSize / 2}px;
|
52
|
-
|
53
|
-
position: absolute;
|
54
|
-
bottom: 0;
|
55
|
-
right: 0;
|
56
|
-
|
57
|
-
opacity: 0.8;
|
58
|
-
background-color: var(--primary-background-color);
|
59
|
-
border: 1px solid var(--primary-foreground-color);
|
60
|
-
}
|
61
|
-
|
62
|
-
.handleOverlay > .selectionBox .rotateCircleContainer {
|
63
|
-
position: absolute;
|
64
|
-
top: 50%;
|
65
|
-
bottom: 50%;
|
66
|
-
left: 50%;
|
67
|
-
right: 50%;
|
68
|
-
}
|
69
|
-
|
70
|
-
.handleOverlay .rotateCircle {
|
71
|
-
width: ${handleScreenSize}px;
|
72
|
-
height: ${handleScreenSize}px;
|
73
|
-
margin-left: -${handleScreenSize / 2}px;
|
74
|
-
margin-top: -${handleScreenSize / 2}px;
|
75
|
-
opacity: 0.8;
|
76
|
-
|
77
|
-
border: 1px solid var(--primary-foreground-color);
|
78
|
-
background-color: var(--primary-background-color);
|
79
|
-
border-radius: 100%;
|
80
|
-
}
|
81
|
-
`;
|
82
|
-
const makeDraggable = (element, onDrag, onDragEnd) => {
|
83
|
-
element.style.touchAction = 'none';
|
84
|
-
let down = false;
|
85
|
-
// Work around a Safari bug
|
86
|
-
element.addEventListener('touchstart', evt => evt.preventDefault());
|
87
|
-
let lastX;
|
88
|
-
let lastY;
|
89
|
-
element.addEventListener('pointerdown', event => {
|
90
|
-
if (event.isPrimary) {
|
91
|
-
down = true;
|
92
|
-
element.setPointerCapture(event.pointerId);
|
93
|
-
lastX = event.pageX;
|
94
|
-
lastY = event.pageY;
|
95
|
-
return true;
|
96
|
-
}
|
97
|
-
return false;
|
98
|
-
});
|
99
|
-
element.addEventListener('pointermove', event => {
|
100
|
-
if (event.isPrimary && down) {
|
101
|
-
// Safari/iOS doesn't seem to support movementX/movementY on pointer events.
|
102
|
-
// Calculate manually:
|
103
|
-
const delta = Vec2.of(event.pageX - lastX, event.pageY - lastY);
|
104
|
-
onDrag(delta, Vec2.of(event.offsetX, event.offsetY));
|
105
|
-
lastX = event.pageX;
|
106
|
-
lastY = event.pageY;
|
107
|
-
return true;
|
108
|
-
}
|
109
|
-
return false;
|
110
|
-
});
|
111
|
-
const onPointerEnd = (event) => {
|
112
|
-
if (event.isPrimary) {
|
113
|
-
down = false;
|
114
|
-
onDragEnd();
|
115
|
-
return true;
|
116
|
-
}
|
117
|
-
return false;
|
118
|
-
};
|
119
|
-
element.addEventListener('pointerup', onPointerEnd);
|
120
|
-
element.addEventListener('pointercancel', onPointerEnd);
|
121
|
-
};
|
122
|
-
// Maximum number of strokes to transform without a re-render.
|
123
|
-
const updateChunkSize = 100;
|
124
|
-
// @internal
|
125
|
-
class Selection {
|
126
|
-
constructor(startPoint, editor) {
|
127
|
-
this.startPoint = startPoint;
|
128
|
-
this.editor = editor;
|
129
|
-
this.boxRotation = this.editor.viewport.getRotationAngle();
|
130
|
-
this.selectedElems = [];
|
131
|
-
this.region = Rect2.bboxOf([startPoint]);
|
132
|
-
// Create draggable rectangles
|
133
|
-
this.backgroundBox = document.createElement('div');
|
134
|
-
const draggableBackground = document.createElement('div');
|
135
|
-
const resizeCorner = document.createElement('div');
|
136
|
-
this.rotateCircle = document.createElement('div');
|
137
|
-
const rotateCircleContainer = document.createElement('div');
|
138
|
-
this.backgroundBox.classList.add('selectionBox');
|
139
|
-
draggableBackground.classList.add('draggableBackground');
|
140
|
-
resizeCorner.classList.add('resizeCorner');
|
141
|
-
this.rotateCircle.classList.add('rotateCircle');
|
142
|
-
rotateCircleContainer.classList.add('rotateCircleContainer');
|
143
|
-
rotateCircleContainer.appendChild(this.rotateCircle);
|
144
|
-
this.backgroundBox.appendChild(draggableBackground);
|
145
|
-
this.backgroundBox.appendChild(rotateCircleContainer);
|
146
|
-
this.backgroundBox.appendChild(resizeCorner);
|
147
|
-
this.transformationCommands = [];
|
148
|
-
this.transform = Mat33.identity;
|
149
|
-
makeDraggable(draggableBackground, (deltaPosition) => {
|
150
|
-
this.handleBackgroundDrag(deltaPosition);
|
151
|
-
}, () => this.finalizeTransform());
|
152
|
-
makeDraggable(resizeCorner, (deltaPosition) => {
|
153
|
-
this.handleResizeCornerDrag(deltaPosition);
|
154
|
-
}, () => this.finalizeTransform());
|
155
|
-
makeDraggable(this.rotateCircle, (_deltaPosition, offset) => {
|
156
|
-
this.handleRotateCircleDrag(offset);
|
157
|
-
}, () => this.finalizeTransform());
|
158
|
-
}
|
159
|
-
// Note a small change in the position of this' background while dragging
|
160
|
-
// At the end of a drag, changes should be applied by calling this.finishDragging()
|
161
|
-
handleBackgroundDrag(deltaPosition) {
|
162
|
-
// Re-scale the change in position
|
163
|
-
// (use a Vec3 transform to avoid translating deltaPosition)
|
164
|
-
deltaPosition = this.editor.viewport.screenToCanvasTransform.transformVec3(deltaPosition);
|
165
|
-
// Snap position to a multiple of 10 (additional decimal points lead to larger files).
|
166
|
-
deltaPosition = this.editor.viewport.roundPoint(deltaPosition);
|
167
|
-
this.transformPreview(Mat33.translation(deltaPosition));
|
168
|
-
}
|
169
|
-
handleResizeCornerDrag(deltaPosition) {
|
170
|
-
deltaPosition = this.editor.viewport.screenToCanvasTransform.transformVec3(deltaPosition);
|
171
|
-
deltaPosition = this.editor.viewport.roundPoint(deltaPosition);
|
172
|
-
const oldWidth = this.region.w;
|
173
|
-
const oldHeight = this.region.h;
|
174
|
-
const newSize = this.region.size.plus(deltaPosition);
|
175
|
-
if (newSize.y > 0 && newSize.x > 0) {
|
176
|
-
const scaleFactor = Vec2.of(newSize.x / oldWidth, newSize.y / oldHeight);
|
177
|
-
this.transformPreview(Mat33.scaling2D(scaleFactor, this.region.topLeft));
|
178
|
-
}
|
179
|
-
}
|
180
|
-
handleRotateCircleDrag(offset) {
|
181
|
-
let targetRotation = offset.angle();
|
182
|
-
targetRotation = targetRotation % (2 * Math.PI);
|
183
|
-
if (targetRotation < 0) {
|
184
|
-
targetRotation += 2 * Math.PI;
|
185
|
-
}
|
186
|
-
let deltaRotation = (targetRotation - this.boxRotation);
|
187
|
-
const rotationStep = Math.PI / 12;
|
188
|
-
if (Math.abs(deltaRotation) < rotationStep || !isFinite(deltaRotation)) {
|
189
|
-
return;
|
190
|
-
}
|
191
|
-
else {
|
192
|
-
const rotationDirection = Math.sign(deltaRotation);
|
193
|
-
// Step exactly one rotationStep
|
194
|
-
deltaRotation = Math.floor(Math.abs(deltaRotation) / rotationStep) * rotationStep;
|
195
|
-
deltaRotation *= rotationDirection;
|
196
|
-
}
|
197
|
-
this.transformPreview(Mat33.zRotation(deltaRotation, this.region.center));
|
198
|
-
}
|
199
|
-
computeTransformCommands() {
|
200
|
-
return this.selectedElems.map(elem => {
|
201
|
-
return elem.transformBy(this.transform);
|
202
|
-
});
|
203
|
-
}
|
204
|
-
// Applies, previews, but doesn't finalize the given transformation.
|
205
|
-
transformPreview(transform) {
|
206
|
-
this.transform = this.transform.rightMul(transform);
|
207
|
-
const deltaRotation = transform.transformVec3(Vec2.unitX).angle();
|
208
|
-
transform = transform.rightMul(Mat33.zRotation(-deltaRotation, this.region.center));
|
209
|
-
this.boxRotation += deltaRotation;
|
210
|
-
this.boxRotation = this.boxRotation % (2 * Math.PI);
|
211
|
-
if (this.boxRotation < 0) {
|
212
|
-
this.boxRotation += 2 * Math.PI;
|
213
|
-
}
|
214
|
-
const newSize = transform.transformVec3(this.region.size);
|
215
|
-
const translation = transform.transformVec2(this.region.topLeft).minus(this.region.topLeft);
|
216
|
-
this.region = this.region.resizedTo(newSize);
|
217
|
-
this.region = this.region.translatedBy(translation);
|
218
|
-
this.previewTransformCmds();
|
219
|
-
this.scrollTo();
|
220
|
-
}
|
221
|
-
// Applies the current transformation to the selection
|
222
|
-
finalizeTransform() {
|
223
|
-
this.transformationCommands.forEach(cmd => {
|
224
|
-
cmd.unapply(this.editor);
|
225
|
-
});
|
226
|
-
const fullTransform = this.transform;
|
227
|
-
const inverseTransform = this.transform.inverse();
|
228
|
-
const deltaBoxRotation = this.boxRotation;
|
229
|
-
const currentTransfmCommands = this.computeTransformCommands();
|
230
|
-
// Reset for the next drag
|
231
|
-
this.transformationCommands = [];
|
232
|
-
this.transform = Mat33.identity;
|
233
|
-
this.region = this.region.transformedBoundingBox(inverseTransform);
|
234
|
-
// Make the commands undo-able
|
235
|
-
this.editor.dispatch(new Selection.ApplyTransformationCommand(this, currentTransfmCommands, fullTransform, deltaBoxRotation));
|
236
|
-
}
|
237
|
-
// Preview the effects of the current transformation on the selection
|
238
|
-
previewTransformCmds() {
|
239
|
-
// Don't render what we're moving if it's likely to be slow.
|
240
|
-
if (this.selectedElems.length > updateChunkSize) {
|
241
|
-
this.updateUI();
|
242
|
-
return;
|
243
|
-
}
|
244
|
-
this.transformationCommands.forEach(cmd => cmd.unapply(this.editor));
|
245
|
-
this.transformationCommands = this.computeTransformCommands();
|
246
|
-
this.transformationCommands.forEach(cmd => cmd.apply(this.editor));
|
247
|
-
this.updateUI();
|
248
|
-
}
|
249
|
-
appendBackgroundBoxTo(elem) {
|
250
|
-
if (this.backgroundBox.parentElement) {
|
251
|
-
this.backgroundBox.remove();
|
252
|
-
}
|
253
|
-
elem.appendChild(this.backgroundBox);
|
254
|
-
}
|
255
|
-
setToPoint(point) {
|
256
|
-
this.region = this.region.grownToPoint(point);
|
257
|
-
this.recomputeBoxRotation();
|
258
|
-
this.updateUI();
|
259
|
-
}
|
260
|
-
cancelSelection() {
|
261
|
-
if (this.backgroundBox.parentElement) {
|
262
|
-
this.backgroundBox.remove();
|
263
|
-
}
|
264
|
-
this.region = Rect2.empty;
|
265
|
-
}
|
266
|
-
setSelectedObjects(objects, bbox) {
|
267
|
-
this.region = bbox;
|
268
|
-
this.selectedElems = objects;
|
269
|
-
this.updateUI();
|
270
|
-
}
|
271
|
-
getSelectedObjects() {
|
272
|
-
return this.selectedElems;
|
273
|
-
}
|
274
|
-
// Find the objects corresponding to this in the document,
|
275
|
-
// select them.
|
276
|
-
// Returns false iff nothing was selected.
|
277
|
-
resolveToObjects() {
|
278
|
-
// Grow the rectangle, if necessary
|
279
|
-
if (this.region.w === 0 || this.region.h === 0) {
|
280
|
-
const padding = this.editor.viewport.visibleRect.maxDimension / 100;
|
281
|
-
this.region = Rect2.bboxOf(this.region.corners, padding);
|
282
|
-
}
|
283
|
-
this.selectedElems = this.editor.image.getElementsIntersectingRegion(this.region).filter(elem => {
|
284
|
-
if (this.region.containsRect(elem.getBBox())) {
|
285
|
-
return true;
|
286
|
-
}
|
287
|
-
// Calculated bounding boxes can be slightly larger than their actual contents' bounding box.
|
288
|
-
// As such, test with more lines than just this' edges.
|
289
|
-
const testLines = [];
|
290
|
-
for (const subregion of this.region.divideIntoGrid(2, 2)) {
|
291
|
-
testLines.push(...subregion.getEdges());
|
292
|
-
}
|
293
|
-
return testLines.some(edge => elem.intersects(edge));
|
294
|
-
});
|
295
|
-
// Find the bounding box of all selected elements.
|
296
|
-
if (!this.recomputeRegion()) {
|
297
|
-
return false;
|
298
|
-
}
|
299
|
-
this.updateUI();
|
300
|
-
return true;
|
301
|
-
}
|
302
|
-
// Recompute this' region from the selected elements. Resets rotation to zero.
|
303
|
-
// Returns false if the selection is empty.
|
304
|
-
recomputeRegion() {
|
305
|
-
const newRegion = this.selectedElems.reduce((accumulator, elem) => {
|
306
|
-
return (accumulator !== null && accumulator !== void 0 ? accumulator : elem.getBBox()).union(elem.getBBox());
|
307
|
-
}, null);
|
308
|
-
if (!newRegion) {
|
309
|
-
this.cancelSelection();
|
310
|
-
return false;
|
311
|
-
}
|
312
|
-
this.region = newRegion;
|
313
|
-
const minSize = this.getMinCanvasSize();
|
314
|
-
if (this.region.w < minSize || this.region.h < minSize) {
|
315
|
-
// Add padding
|
316
|
-
const padding = minSize / 2;
|
317
|
-
this.region = Rect2.bboxOf(this.region.corners, padding);
|
318
|
-
}
|
319
|
-
this.recomputeBoxRotation();
|
320
|
-
return true;
|
321
|
-
}
|
322
|
-
getMinCanvasSize() {
|
323
|
-
const canvasHandleSize = handleScreenSize / this.editor.viewport.getScaleFactor();
|
324
|
-
return canvasHandleSize * 2;
|
325
|
-
}
|
326
|
-
recomputeBoxRotation() {
|
327
|
-
this.boxRotation = this.editor.viewport.getRotationAngle();
|
328
|
-
}
|
329
|
-
getSelectedItemCount() {
|
330
|
-
return this.selectedElems.length;
|
331
|
-
}
|
332
|
-
updateUI() {
|
333
|
-
if (!this.backgroundBox) {
|
334
|
-
return;
|
335
|
-
}
|
336
|
-
const rightSideDirection = this.region.topRight.minus(this.region.bottomRight);
|
337
|
-
const topSideDirection = this.region.topLeft.minus(this.region.topRight);
|
338
|
-
const toScreen = this.editor.viewport.canvasToScreenTransform;
|
339
|
-
const centerOnScreen = toScreen.transformVec2(this.region.center);
|
340
|
-
const heightOnScreen = toScreen.transformVec3(rightSideDirection).magnitude();
|
341
|
-
const widthOnScreen = toScreen.transformVec3(topSideDirection).magnitude();
|
342
|
-
this.backgroundBox.style.marginLeft = `${centerOnScreen.x - widthOnScreen / 2}px`;
|
343
|
-
this.backgroundBox.style.marginTop = `${centerOnScreen.y - heightOnScreen / 2}px`;
|
344
|
-
this.backgroundBox.style.width = `${widthOnScreen}px`;
|
345
|
-
this.backgroundBox.style.height = `${heightOnScreen}px`;
|
346
|
-
const rotationDeg = this.boxRotation * 180 / Math.PI;
|
347
|
-
this.backgroundBox.style.transform = `rotate(${rotationDeg}deg)`;
|
348
|
-
this.rotateCircle.style.transform = `rotate(${-rotationDeg}deg)`;
|
349
|
-
}
|
350
|
-
// Scroll the viewport to this. Does not zoom
|
351
|
-
scrollTo() {
|
352
|
-
const viewport = this.editor.viewport;
|
353
|
-
const visibleRect = viewport.visibleRect;
|
354
|
-
if (!visibleRect.containsPoint(this.region.center)) {
|
355
|
-
const closestPoint = visibleRect.getClosestPointOnBoundaryTo(this.region.center);
|
356
|
-
const delta = this.region.center.minus(closestPoint);
|
357
|
-
this.editor.dispatchNoAnnounce(Viewport.transformBy(Mat33.translation(delta.times(-1))), false);
|
358
|
-
}
|
359
|
-
}
|
360
|
-
deleteSelectedObjects() {
|
361
|
-
return new Erase(this.selectedElems);
|
362
|
-
}
|
363
|
-
duplicateSelectedObjects() {
|
364
|
-
return new Duplicate(this.selectedElems);
|
365
|
-
}
|
366
|
-
}
|
367
|
-
_a = Selection;
|
368
|
-
(() => {
|
369
|
-
SerializableCommand.register('selection-tool-transform', (json, editor) => {
|
370
|
-
// The selection box is lost when serializing/deserializing. No need to store box rotation
|
371
|
-
const guiBoxRotation = 0;
|
372
|
-
const fullTransform = new Mat33(...json.transform);
|
373
|
-
const commands = json.commands.map(data => SerializableCommand.deserialize(data, editor));
|
374
|
-
return new _a.ApplyTransformationCommand(null, commands, fullTransform, guiBoxRotation);
|
375
|
-
});
|
376
|
-
})();
|
377
|
-
Selection.ApplyTransformationCommand = class extends SerializableCommand {
|
378
|
-
constructor(selection, currentTransfmCommands, fullTransform, deltaBoxRotation) {
|
379
|
-
super('selection-tool-transform');
|
380
|
-
this.selection = selection;
|
381
|
-
this.currentTransfmCommands = currentTransfmCommands;
|
382
|
-
this.fullTransform = fullTransform;
|
383
|
-
this.deltaBoxRotation = deltaBoxRotation;
|
384
|
-
}
|
385
|
-
apply(editor) {
|
386
|
-
var _b, _c;
|
387
|
-
return __awaiter(this, void 0, void 0, function* () {
|
388
|
-
// Approximate the new selection
|
389
|
-
if (this.selection) {
|
390
|
-
this.selection.region = this.selection.region.transformedBoundingBox(this.fullTransform);
|
391
|
-
this.selection.boxRotation += this.deltaBoxRotation;
|
392
|
-
this.selection.updateUI();
|
393
|
-
}
|
394
|
-
yield editor.asyncApplyCommands(this.currentTransfmCommands, updateChunkSize);
|
395
|
-
(_b = this.selection) === null || _b === void 0 ? void 0 : _b.recomputeRegion();
|
396
|
-
(_c = this.selection) === null || _c === void 0 ? void 0 : _c.updateUI();
|
397
|
-
});
|
398
|
-
}
|
399
|
-
unapply(editor) {
|
400
|
-
var _b, _c;
|
401
|
-
return __awaiter(this, void 0, void 0, function* () {
|
402
|
-
if (this.selection) {
|
403
|
-
this.selection.region = this.selection.region.transformedBoundingBox(this.fullTransform.inverse());
|
404
|
-
this.selection.boxRotation -= this.deltaBoxRotation;
|
405
|
-
this.selection.updateUI();
|
406
|
-
}
|
407
|
-
yield editor.asyncUnapplyCommands(this.currentTransfmCommands, updateChunkSize);
|
408
|
-
(_b = this.selection) === null || _b === void 0 ? void 0 : _b.recomputeRegion();
|
409
|
-
(_c = this.selection) === null || _c === void 0 ? void 0 : _c.updateUI();
|
410
|
-
});
|
411
|
-
}
|
412
|
-
serializeToJSON() {
|
413
|
-
return {
|
414
|
-
commands: this.currentTransfmCommands.map(command => command.serialize()),
|
415
|
-
transform: this.fullTransform.toArray(),
|
416
|
-
};
|
417
|
-
}
|
418
|
-
description(_editor, localizationTable) {
|
419
|
-
return localizationTable.transformedElements(this.currentTransfmCommands.length);
|
420
|
-
}
|
421
|
-
};
|
422
|
-
// {@inheritDoc SelectionTool!}
|
423
|
-
export default class SelectionTool extends BaseTool {
|
424
|
-
constructor(editor, description) {
|
425
|
-
super(editor.notifier, description);
|
426
|
-
this.editor = editor;
|
427
|
-
this.handleOverlay = document.createElement('div');
|
428
|
-
editor.createHTMLOverlay(this.handleOverlay);
|
429
|
-
editor.addStyleSheet(styles);
|
430
|
-
this.handleOverlay.style.display = 'none';
|
431
|
-
this.handleOverlay.classList.add('handleOverlay');
|
432
|
-
editor.notifier.on(EditorEventType.ViewportChanged, _data => {
|
433
|
-
var _b, _c;
|
434
|
-
(_b = this.selectionBox) === null || _b === void 0 ? void 0 : _b.recomputeRegion();
|
435
|
-
(_c = this.selectionBox) === null || _c === void 0 ? void 0 : _c.updateUI();
|
436
|
-
});
|
437
|
-
this.editor.handleKeyEventsFrom(this.handleOverlay);
|
438
|
-
}
|
439
|
-
makeSelectionBox(selectionStartPos) {
|
440
|
-
this.prevSelectionBox = this.selectionBox;
|
441
|
-
this.selectionBox = new Selection(selectionStartPos, this.editor);
|
442
|
-
// Remove any previous selection rects
|
443
|
-
this.handleOverlay.replaceChildren();
|
444
|
-
this.selectionBox.appendBackgroundBoxTo(this.handleOverlay);
|
445
|
-
}
|
446
|
-
onPointerDown(event) {
|
447
|
-
if (event.allPointers.length === 1 && event.current.isPrimary) {
|
448
|
-
this.makeSelectionBox(event.current.canvasPos);
|
449
|
-
return true;
|
450
|
-
}
|
451
|
-
return false;
|
452
|
-
}
|
453
|
-
onPointerMove(event) {
|
454
|
-
if (!this.selectionBox)
|
455
|
-
return;
|
456
|
-
this.selectionBox.setToPoint(event.current.canvasPos);
|
457
|
-
}
|
458
|
-
onGestureEnd() {
|
459
|
-
if (!this.selectionBox)
|
460
|
-
return;
|
461
|
-
// Expand/shrink the selection rectangle, if applicable
|
462
|
-
const hasSelection = this.selectionBox.resolveToObjects();
|
463
|
-
// Note that the selection has changed
|
464
|
-
this.editor.notifier.dispatch(EditorEventType.ToolUpdated, {
|
465
|
-
kind: EditorEventType.ToolUpdated,
|
466
|
-
tool: this,
|
467
|
-
});
|
468
|
-
if (hasSelection) {
|
469
|
-
this.editor.announceForAccessibility(this.editor.localization.selectedElements(this.selectionBox.getSelectedItemCount()));
|
470
|
-
this.zoomToSelection();
|
471
|
-
}
|
472
|
-
}
|
473
|
-
zoomToSelection() {
|
474
|
-
if (this.selectionBox) {
|
475
|
-
const selectionRect = this.selectionBox.region;
|
476
|
-
this.editor.dispatchNoAnnounce(this.editor.viewport.zoomTo(selectionRect, false), false);
|
477
|
-
}
|
478
|
-
}
|
479
|
-
onPointerUp(event) {
|
480
|
-
if (!this.selectionBox)
|
481
|
-
return;
|
482
|
-
this.selectionBox.setToPoint(event.current.canvasPos);
|
483
|
-
this.onGestureEnd();
|
484
|
-
}
|
485
|
-
onGestureCancel() {
|
486
|
-
var _b, _c;
|
487
|
-
// Revert to the previous selection, if any.
|
488
|
-
(_b = this.selectionBox) === null || _b === void 0 ? void 0 : _b.cancelSelection();
|
489
|
-
this.selectionBox = this.prevSelectionBox;
|
490
|
-
(_c = this.selectionBox) === null || _c === void 0 ? void 0 : _c.appendBackgroundBoxTo(this.handleOverlay);
|
491
|
-
}
|
492
|
-
onKeyPress(event) {
|
493
|
-
let rotationSteps = 0;
|
494
|
-
let xTranslateSteps = 0;
|
495
|
-
let yTranslateSteps = 0;
|
496
|
-
let xScaleSteps = 0;
|
497
|
-
let yScaleSteps = 0;
|
498
|
-
switch (event.key) {
|
499
|
-
case 'a':
|
500
|
-
case 'h':
|
501
|
-
case 'ArrowLeft':
|
502
|
-
xTranslateSteps -= 1;
|
503
|
-
break;
|
504
|
-
case 'd':
|
505
|
-
case 'l':
|
506
|
-
case 'ArrowRight':
|
507
|
-
xTranslateSteps += 1;
|
508
|
-
break;
|
509
|
-
case 'q':
|
510
|
-
case 'k':
|
511
|
-
case 'ArrowUp':
|
512
|
-
yTranslateSteps -= 1;
|
513
|
-
break;
|
514
|
-
case 'e':
|
515
|
-
case 'j':
|
516
|
-
case 'ArrowDown':
|
517
|
-
yTranslateSteps += 1;
|
518
|
-
break;
|
519
|
-
case 'r':
|
520
|
-
rotationSteps += 1;
|
521
|
-
break;
|
522
|
-
case 'R':
|
523
|
-
rotationSteps -= 1;
|
524
|
-
break;
|
525
|
-
case 'i':
|
526
|
-
xScaleSteps -= 1;
|
527
|
-
break;
|
528
|
-
case 'I':
|
529
|
-
xScaleSteps += 1;
|
530
|
-
break;
|
531
|
-
case 'o':
|
532
|
-
yScaleSteps -= 1;
|
533
|
-
break;
|
534
|
-
case 'O':
|
535
|
-
yScaleSteps += 1;
|
536
|
-
break;
|
537
|
-
}
|
538
|
-
let handled = xTranslateSteps !== 0
|
539
|
-
|| yTranslateSteps !== 0
|
540
|
-
|| rotationSteps !== 0
|
541
|
-
|| xScaleSteps !== 0
|
542
|
-
|| yScaleSteps !== 0;
|
543
|
-
if (!this.selectionBox) {
|
544
|
-
handled = false;
|
545
|
-
}
|
546
|
-
else if (handled) {
|
547
|
-
const translateStepSize = 10 * this.editor.viewport.getSizeOfPixelOnCanvas();
|
548
|
-
const rotateStepSize = Math.PI / 8;
|
549
|
-
const scaleStepSize = translateStepSize / 2;
|
550
|
-
const region = this.selectionBox.region;
|
551
|
-
const scaledSize = this.selectionBox.region.size.plus(Vec2.of(xScaleSteps, yScaleSteps).times(scaleStepSize));
|
552
|
-
const transform = Mat33.scaling2D(Vec2.of(
|
553
|
-
// Don't more-than-half the size of the selection
|
554
|
-
Math.max(0.5, scaledSize.x / region.size.x), Math.max(0.5, scaledSize.y / region.size.y)), region.topLeft).rightMul(Mat33.zRotation(rotationSteps * rotateStepSize, region.center)).rightMul(Mat33.translation(Vec2.of(xTranslateSteps, yTranslateSteps).times(translateStepSize)));
|
555
|
-
this.selectionBox.transformPreview(transform);
|
556
|
-
}
|
557
|
-
if (this.selectionBox && !handled && (event.key === 'Delete' || event.key === 'Backspace')) {
|
558
|
-
this.editor.dispatch(this.selectionBox.deleteSelectedObjects());
|
559
|
-
this.clearSelection();
|
560
|
-
handled = true;
|
561
|
-
}
|
562
|
-
return handled;
|
563
|
-
}
|
564
|
-
onKeyUp(evt) {
|
565
|
-
if (this.selectionBox && SelectionTool.handleableKeys.some(key => key === evt.key)) {
|
566
|
-
this.selectionBox.finalizeTransform();
|
567
|
-
return true;
|
568
|
-
}
|
569
|
-
return false;
|
570
|
-
}
|
571
|
-
onCopy(event) {
|
572
|
-
if (!this.selectionBox) {
|
573
|
-
return false;
|
574
|
-
}
|
575
|
-
const selectedElems = this.selectionBox.getSelectedObjects();
|
576
|
-
const bbox = this.selectionBox.region;
|
577
|
-
if (selectedElems.length === 0) {
|
578
|
-
return false;
|
579
|
-
}
|
580
|
-
const exportViewport = new Viewport(this.editor.notifier);
|
581
|
-
exportViewport.updateScreenSize(Vec2.of(bbox.w, bbox.h));
|
582
|
-
exportViewport.resetTransform(Mat33.translation(bbox.topLeft));
|
583
|
-
const svgNameSpace = 'http://www.w3.org/2000/svg';
|
584
|
-
const exportElem = document.createElementNS(svgNameSpace, 'svg');
|
585
|
-
const sanitize = true;
|
586
|
-
const renderer = new SVGRenderer(exportElem, exportViewport, sanitize);
|
587
|
-
for (const elem of selectedElems) {
|
588
|
-
elem.render(renderer);
|
589
|
-
}
|
590
|
-
event.setData('image/svg+xml', exportElem.outerHTML);
|
591
|
-
return true;
|
592
|
-
}
|
593
|
-
setEnabled(enabled) {
|
594
|
-
super.setEnabled(enabled);
|
595
|
-
// Clear the selection
|
596
|
-
this.handleOverlay.replaceChildren();
|
597
|
-
this.selectionBox = null;
|
598
|
-
this.handleOverlay.style.display = enabled ? 'block' : 'none';
|
599
|
-
if (enabled) {
|
600
|
-
this.handleOverlay.tabIndex = 0;
|
601
|
-
this.handleOverlay.setAttribute('aria-label', this.editor.localization.selectionToolKeyboardShortcuts);
|
602
|
-
}
|
603
|
-
else {
|
604
|
-
this.handleOverlay.tabIndex = -1;
|
605
|
-
}
|
606
|
-
}
|
607
|
-
// Get the object responsible for displaying this' selection.
|
608
|
-
getSelection() {
|
609
|
-
return this.selectionBox;
|
610
|
-
}
|
611
|
-
setSelection(objects) {
|
612
|
-
let bbox = null;
|
613
|
-
for (const object of objects) {
|
614
|
-
if (bbox) {
|
615
|
-
bbox = bbox.union(object.getBBox());
|
616
|
-
}
|
617
|
-
else {
|
618
|
-
bbox = object.getBBox();
|
619
|
-
}
|
620
|
-
}
|
621
|
-
if (!bbox) {
|
622
|
-
return;
|
623
|
-
}
|
624
|
-
this.clearSelection();
|
625
|
-
if (!this.selectionBox) {
|
626
|
-
this.makeSelectionBox(bbox.topLeft);
|
627
|
-
}
|
628
|
-
this.selectionBox.setSelectedObjects(objects, bbox);
|
629
|
-
}
|
630
|
-
clearSelection() {
|
631
|
-
this.handleOverlay.replaceChildren();
|
632
|
-
this.prevSelectionBox = this.selectionBox;
|
633
|
-
this.selectionBox = null;
|
634
|
-
this.editor.notifier.dispatch(EditorEventType.ToolUpdated, {
|
635
|
-
kind: EditorEventType.ToolUpdated,
|
636
|
-
tool: this,
|
637
|
-
});
|
638
|
-
}
|
639
|
-
}
|
640
|
-
SelectionTool.handleableKeys = [
|
641
|
-
'a', 'h', 'ArrowLeft',
|
642
|
-
'd', 'l', 'ArrowRight',
|
643
|
-
'q', 'k', 'ArrowUp',
|
644
|
-
'e', 'j', 'ArrowDown',
|
645
|
-
'r', 'R',
|
646
|
-
'i', 'I', 'o', 'O',
|
647
|
-
];
|