@widgetic/canvas 0.5.41 → 0.5.42
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/dist/canvas/autosaveScheduler.js +37 -89
- package/dist/canvas/boardFrame.js +1 -1
- package/dist/canvas/boardSurface.js +1 -1
- package/dist/canvas/canvasLogger.js +7 -19
- package/dist/canvas/chromeLayout.js +17 -18
- package/dist/canvas/eventScope.js +10 -14
- package/dist/canvas/host/CanvasHost.js +1286 -1536
- package/dist/canvas/host/CanvasHostApi.js +4 -19
- package/dist/canvas/host/browserServices.js +65 -116
- package/dist/canvas/host/conversion.js +82 -170
- package/dist/canvas/host/hostObject.js +7 -7
- package/dist/canvas/host/pageEvents.js +4 -4
- package/dist/canvas/host/screenRects.js +28 -31
- package/dist/canvas/host/strokeSnap.js +88 -143
- package/dist/canvas/openMarks.js +2 -2
- package/dist/canvas/outsideClickDeselect.js +6 -7
- package/dist/canvas/panZoomPosition.js +2 -2
- package/dist/canvas/preferredTextFont.js +13 -25
- package/dist/canvas/shapes/ShapeLibrary.js +138 -148
- package/dist/canvas/shapes/shapeImageStore.js +3 -3
- package/dist/canvas/shapes/strokeShapeSnap.js +188 -233
- package/dist/canvas/toolbarPosition.js +2 -2
- package/dist/canvas/ui/colorModel.js +21 -32
- package/dist/canvas/ui/colorPickerMessages.js +5 -5
- package/dist/canvas/ui/floatingCard.js +18 -18
- package/dist/canvas/ui/floatingLayer.js +5 -6
- package/dist/canvas/ui/keyHints.js +9 -11
- package/dist/canvas/ui/popoverSide.js +2 -3
- package/dist/canvas/ui/selectionTraits.js +34 -40
- package/dist/canvas/view/erasingMarks.js +7 -7
- package/dist/canvas/view/geometry.js +71 -90
- package/dist/components/preferenceStorage.js +2 -2
- package/dist/engine/EngineCore.js +696 -795
- package/dist/engine/codec/canvasDocument.js +183 -147
- package/dist/engine/codec/hostReadableKeys.js +1 -1
- package/dist/engine/core/arrange.js +26 -38
- package/dist/engine/core/arrow.js +76 -89
- package/dist/engine/core/arrowBinding.js +98 -116
- package/dist/engine/core/arrowBindingRecords.js +23 -38
- package/dist/engine/core/brush.js +67 -94
- package/dist/engine/core/defaults.js +90 -51
- package/dist/engine/core/freehand.js +147 -163
- package/dist/engine/core/geometry.js +47 -54
- package/dist/engine/core/history.js +33 -50
- package/dist/engine/core/hitTest.js +68 -81
- package/dist/engine/core/matrix.js +18 -20
- package/dist/engine/core/pathData.js +183 -210
- package/dist/engine/core/rotationReset.js +5 -6
- package/dist/engine/core/scene.js +129 -187
- package/dist/engine/core/scribble.js +59 -84
- package/dist/engine/core/shapePaths.js +12 -15
- package/dist/engine/core/shapeTools.js +72 -63
- package/dist/engine/core/snapping.js +65 -117
- package/dist/engine/core/style.js +31 -64
- package/dist/engine/core/transform.js +83 -94
- package/dist/engine/core/types.js +4 -4
- package/dist/engine/core/viewport.js +23 -36
- package/dist/engine/input/PointerController.js +467 -474
- package/dist/engine/input/arrowHandles.js +12 -12
- package/dist/engine/input/gestures.js +262 -308
- package/dist/engine/input/hoverCursor.js +1 -1
- package/dist/engine/input/keyboard.js +27 -30
- package/dist/engine/input/selectionHandles.js +45 -50
- package/dist/engine/input/types.js +1 -1
- package/dist/engine/render/rasterize.js +86 -158
- package/dist/engine/state.js +4 -15
- package/dist/engine/text/layoutText.js +43 -62
- package/dist/engine/text/textMetrics.js +8 -10
- package/dist/engine/widgets.js +101 -137
- package/dist/fonts/interFont.js +17 -68
- package/dist/fonts/workspaceFonts.js +32 -90
- package/dist/i18n/labels.js +4 -4
- package/dist/i18n/language.js +4 -4
- package/dist/i18n/messages.js +1 -1
- package/dist/i18n/shortcuts.js +14 -14
- package/dist/settings/workspaceToolSettings.js +32 -48
- package/dist/tools/components/catalog.js +64 -74
- package/dist/tools/components/grouping.js +6 -28
- package/dist/tools/definitions.js +5 -5
- package/dist/tools/executors.js +105 -141
- package/dist/tools/hostAdapter.js +11 -13
- package/dist/tools/sceneSummary.js +43 -57
- package/dist/tools/sketchIntent.js +13 -21
- package/dist/tools/types.js +1 -1
- package/package.json +1 -1
|
@@ -4,26 +4,6 @@
|
|
|
4
4
|
* A gesture changes the scene *transiently* on every move and the engine records one undo step when it ends,
|
|
5
5
|
* so a drag of a hundred frames is one step. `cancel` puts the board back as it was before the press.
|
|
6
6
|
*/
|
|
7
|
-
var __assign = (this && this.__assign) || function () {
|
|
8
|
-
__assign = Object.assign || function(t) {
|
|
9
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
10
|
-
s = arguments[i];
|
|
11
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
12
|
-
t[p] = s[p];
|
|
13
|
-
}
|
|
14
|
-
return t;
|
|
15
|
-
};
|
|
16
|
-
return __assign.apply(this, arguments);
|
|
17
|
-
};
|
|
18
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
19
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
20
|
-
if (ar || !(i in from)) {
|
|
21
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
22
|
-
ar[i] = from[i];
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
26
|
-
};
|
|
27
7
|
import { arrowPointsInWorld, bendTowards, isArrow, reshapeArrow } from '../core/arrow.js';
|
|
28
8
|
import { pickArrowBinding, positionBoundArrow } from '../core/arrowBinding.js';
|
|
29
9
|
import { bindingOfEnd, releaseUnmovedEnds } from '../core/arrowBindingRecords.js';
|
|
@@ -40,122 +20,115 @@ import { CORNER_HANDLES, ROTATE_CURSOR, cursorForHandle, isCornerHandle } from '
|
|
|
40
20
|
import { fitTextObject } from '../text/layoutText.js';
|
|
41
21
|
import { cursorForTool } from '../EngineCore.js';
|
|
42
22
|
/** A press that moves less than this (screen pixels) is still a click. */
|
|
43
|
-
export
|
|
23
|
+
export const DRAG_THRESHOLD = 3;
|
|
44
24
|
/** How far the eraser reaches, in screen pixels. */
|
|
45
|
-
export
|
|
25
|
+
export const ERASER_RADIUS = 8;
|
|
46
26
|
/** The step a rotation snaps to while Shift (or Ctrl, or Meta) is held, in degrees. */
|
|
47
|
-
export
|
|
27
|
+
export const ROTATION_SNAP_DEGREES = 5;
|
|
48
28
|
/** When true, a text corner resize hides the pointer and draws an arrow on the corner. */
|
|
49
|
-
export
|
|
29
|
+
export const ENABLE_TEXT_VIRTUAL_CURSOR = false;
|
|
50
30
|
/** Objects farther than this from the screen (in screen pixels) are not snapped to. */
|
|
51
|
-
|
|
31
|
+
const SNAP_TARGET_MARGIN = 400;
|
|
52
32
|
function worldOf(context, input) {
|
|
53
33
|
return context.engine.toWorld({ x: input.x, y: input.y });
|
|
54
34
|
}
|
|
55
35
|
/** Puts changed shapes into the scene the gesture started from. */
|
|
56
36
|
function withShapes(scene, shapes) {
|
|
57
|
-
|
|
58
|
-
for (
|
|
59
|
-
var shape = shapes_1[_i];
|
|
37
|
+
const byId = new Map();
|
|
38
|
+
for (const shape of shapes)
|
|
60
39
|
byId.set(shape.id, shape);
|
|
61
|
-
|
|
62
|
-
return scene.map(function (object) { var _a; return (_a = byId.get(object.id)) !== null && _a !== void 0 ? _a : object; });
|
|
40
|
+
return scene.map((object) => byId.get(object.id) ?? object);
|
|
63
41
|
}
|
|
64
42
|
function fitAll(context, shapes) {
|
|
65
|
-
|
|
66
|
-
return shapes.map(
|
|
43
|
+
const measure = context.engine.services.measureText;
|
|
44
|
+
return shapes.map((shape) => (shape.type === 'text' ? fitTextObject(shape, measure) : shape));
|
|
67
45
|
}
|
|
68
46
|
/** The unit (top-level object, or member of the entered group) that holds an object. */
|
|
69
47
|
function unitIdOf(index, id, enteredGroupId) {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
var seen = new Set();
|
|
48
|
+
let current = index.get(id);
|
|
49
|
+
const seen = new Set();
|
|
73
50
|
while (current && current.parentId !== null && current.parentId !== enteredGroupId && !seen.has(current.id)) {
|
|
74
51
|
seen.add(current.id);
|
|
75
52
|
current = index.get(current.parentId);
|
|
76
53
|
}
|
|
77
|
-
return
|
|
54
|
+
return current?.id ?? id;
|
|
78
55
|
}
|
|
79
56
|
/** The boxes of the other units near the screen: what a moving selection snaps to. */
|
|
80
57
|
function collectSnapTargets(context, movingIds) {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
for (
|
|
87
|
-
var object = _a[_i];
|
|
58
|
+
const { engine } = context;
|
|
59
|
+
const state = engine.state;
|
|
60
|
+
const index = engine.sceneIndex();
|
|
61
|
+
const reach = inflateRect(engine.visibleRect(), SNAP_TARGET_MARGIN / state.viewport.zoom);
|
|
62
|
+
const rectsByUnit = new Map();
|
|
63
|
+
for (const object of state.scene) {
|
|
88
64
|
if (object.type === 'group' || state.pending[object.id])
|
|
89
65
|
continue;
|
|
90
|
-
|
|
66
|
+
const unitId = unitIdOf(index, object.id, state.enteredGroupId);
|
|
91
67
|
if (movingIds.has(unitId))
|
|
92
68
|
continue;
|
|
93
|
-
|
|
94
|
-
|
|
69
|
+
const bounds = shapeBounds(object);
|
|
70
|
+
const list = rectsByUnit.get(unitId);
|
|
95
71
|
if (list)
|
|
96
72
|
list.push(bounds);
|
|
97
73
|
else
|
|
98
74
|
rectsByUnit.set(unitId, [bounds]);
|
|
99
75
|
}
|
|
100
|
-
|
|
101
|
-
for (
|
|
102
|
-
|
|
103
|
-
var union = unionRects(rects);
|
|
76
|
+
const targets = [];
|
|
77
|
+
for (const rects of rectsByUnit.values()) {
|
|
78
|
+
const union = unionRects(rects);
|
|
104
79
|
if (union && rectsIntersect(union, reach))
|
|
105
80
|
targets.push(union);
|
|
106
81
|
}
|
|
107
82
|
return targets;
|
|
108
83
|
}
|
|
109
84
|
/** Drags the selected units. Nothing moves until the pointer has gone `DRAG_THRESHOLD` pixels. */
|
|
110
|
-
export function createMoveGesture(context, start, unitIds, options) {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
var targets = null;
|
|
85
|
+
export function createMoveGesture(context, start, unitIds, options = {}) {
|
|
86
|
+
const { engine } = context;
|
|
87
|
+
let startScene = engine.state.scene;
|
|
88
|
+
const startWorld = worldOf(context, start);
|
|
89
|
+
let moving = new Set(unitIds);
|
|
90
|
+
const startBounds = boundsOfObjects(startScene, unitIds);
|
|
91
|
+
const speed = new PointerSpeedTracker();
|
|
92
|
+
let started = false;
|
|
93
|
+
let duplicated = false;
|
|
94
|
+
let targets = null;
|
|
121
95
|
// The shapes the drag carries, asked once at the first step: the same shapes move at every step, and a step must not
|
|
122
96
|
// walk the board's groups again to find them (a press that never becomes a drag never asks).
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
var lockY = input.shiftKey && !lockX;
|
|
97
|
+
let carried = null;
|
|
98
|
+
let last = start;
|
|
99
|
+
const apply = (input) => {
|
|
100
|
+
const world = worldOf(context, input);
|
|
101
|
+
let deltaX = world.x - startWorld.x;
|
|
102
|
+
let deltaY = world.y - startWorld.y;
|
|
103
|
+
const lockX = input.shiftKey && Math.abs(deltaX) < Math.abs(deltaY);
|
|
104
|
+
const lockY = input.shiftKey && !lockX;
|
|
132
105
|
if (lockX)
|
|
133
106
|
deltaX = 0;
|
|
134
107
|
if (lockY)
|
|
135
108
|
deltaY = 0;
|
|
136
|
-
|
|
137
|
-
|
|
109
|
+
let guides = [];
|
|
110
|
+
const snapping = (context.options.snapEnabled?.() ?? true) && !input.metaKey && !input.ctrlKey;
|
|
138
111
|
speed.update(input.x, input.y, input.time);
|
|
139
112
|
if (snapping && startBounds) {
|
|
140
113
|
if (!targets)
|
|
141
114
|
targets = collectSnapTargets(context, moving);
|
|
142
|
-
|
|
143
|
-
|
|
115
|
+
const reach = snapReachInWorld(engine.state.viewport.zoom, speed.speed);
|
|
116
|
+
const snap = computeMoveSnap({ ...startBounds, x: startBounds.x + deltaX, y: startBounds.y + deltaY }, targets, reach);
|
|
144
117
|
if (!lockX)
|
|
145
118
|
deltaX += snap.deltaX;
|
|
146
119
|
if (!lockY)
|
|
147
120
|
deltaY += snap.deltaY;
|
|
148
|
-
guides = snap.guides.filter(
|
|
121
|
+
guides = snap.guides.filter((guide) => (guide.orientation === 'vertical' ? !lockX : !lockY));
|
|
149
122
|
}
|
|
150
|
-
carried
|
|
123
|
+
carried ??= movingShapeIds(startScene, [...moving]);
|
|
151
124
|
engine.applyScene(translateShapes(startScene, carried, deltaX, deltaY), 'transient');
|
|
152
125
|
// The handles step aside while the selection is carried: what is carried is all that is to be seen.
|
|
153
|
-
engine.setOverlay({ guides
|
|
126
|
+
engine.setOverlay({ guides, dragging: 'move' });
|
|
154
127
|
};
|
|
155
128
|
return {
|
|
156
129
|
kind: 'move',
|
|
157
130
|
pointerId: start.pointerId,
|
|
158
|
-
move
|
|
131
|
+
move(input) {
|
|
159
132
|
last = input;
|
|
160
133
|
if (options.immovable)
|
|
161
134
|
return;
|
|
@@ -168,7 +141,7 @@ export function createMoveGesture(context, start, unitIds, options) {
|
|
|
168
141
|
duplicated = true;
|
|
169
142
|
if (engine.state.selectedIds.length === 0 && unitIds.length > 0)
|
|
170
143
|
engine.select(unitIds);
|
|
171
|
-
|
|
144
|
+
const copyIds = engine.duplicateSelectionForDrag();
|
|
172
145
|
if (copyIds.length > 0) {
|
|
173
146
|
moving = new Set(copyIds);
|
|
174
147
|
startScene = engine.state.scene;
|
|
@@ -178,10 +151,9 @@ export function createMoveGesture(context, start, unitIds, options) {
|
|
|
178
151
|
}
|
|
179
152
|
apply(input);
|
|
180
153
|
},
|
|
181
|
-
end
|
|
182
|
-
var _a;
|
|
154
|
+
end(input) {
|
|
183
155
|
if (!started) {
|
|
184
|
-
|
|
156
|
+
options.onClick?.();
|
|
185
157
|
return;
|
|
186
158
|
}
|
|
187
159
|
last = input;
|
|
@@ -190,21 +162,21 @@ export function createMoveGesture(context, start, unitIds, options) {
|
|
|
190
162
|
engine.commit();
|
|
191
163
|
engine.setCursor(cursorForTool(engine.state.tool));
|
|
192
164
|
},
|
|
193
|
-
cancel
|
|
165
|
+
cancel() {
|
|
194
166
|
if (started)
|
|
195
167
|
engine.revertToCommitted();
|
|
196
168
|
engine.setCursor(cursorForTool(engine.state.tool));
|
|
197
169
|
},
|
|
198
|
-
modifiersChanged
|
|
170
|
+
modifiersChanged(modifiers) {
|
|
199
171
|
if (started)
|
|
200
|
-
apply(
|
|
172
|
+
apply({ ...last, ...modifiers });
|
|
201
173
|
}
|
|
202
174
|
};
|
|
203
175
|
}
|
|
204
176
|
// ──────────────────────────────────────────────────────────── resize
|
|
205
177
|
/** Where a handle of a frame is in the world. */
|
|
206
178
|
function handlePointOf(frame, handle) {
|
|
207
|
-
|
|
179
|
+
const direction = handleDirection(handle);
|
|
208
180
|
return applyMatrixToPoint(boxToWorldMatrix(frame), {
|
|
209
181
|
x: direction.x === 0 ? frame.width / 2 : direction.x > 0 ? frame.width : 0,
|
|
210
182
|
y: direction.y === 0 ? frame.height / 2 : direction.y > 0 ? frame.height : 0
|
|
@@ -212,12 +184,11 @@ function handlePointOf(frame, handle) {
|
|
|
212
184
|
}
|
|
213
185
|
/** The corner of a frame that is nearest to a point of the world: the one a corner handle dragged to that point is on. */
|
|
214
186
|
function cornerNearestTo(frame, target) {
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
for (
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
var distance = distanceBetween(point, target);
|
|
187
|
+
let nearest = handlePointOf(frame, CORNER_HANDLES[0]);
|
|
188
|
+
let nearestDistance = distanceBetween(nearest, target);
|
|
189
|
+
for (const handle of CORNER_HANDLES.slice(1)) {
|
|
190
|
+
const point = handlePointOf(frame, handle);
|
|
191
|
+
const distance = distanceBetween(point, target);
|
|
221
192
|
if (distance < nearestDistance) {
|
|
222
193
|
nearest = point;
|
|
223
194
|
nearestDistance = distance;
|
|
@@ -227,51 +198,52 @@ function cornerNearestTo(frame, target) {
|
|
|
227
198
|
}
|
|
228
199
|
/** Drags a handle of the selection. Nothing changes until the pointer has gone `DRAG_THRESHOLD` pixels: a press and a double press are not a stretch. */
|
|
229
200
|
export function createResizeGesture(context, start, handle, frame, shapes) {
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
201
|
+
const { engine } = context;
|
|
202
|
+
const startScene = engine.state.scene;
|
|
203
|
+
const corner = isCornerHandle(handle);
|
|
204
|
+
const single = shapes.length === 1 ? shapes[0] : null;
|
|
234
205
|
// An arrow stretched with these shapes comes off the shapes it is tied to that are not stretched with it.
|
|
235
|
-
|
|
206
|
+
const stretchedIds = new Set(shapes.map((shape) => shape.id));
|
|
236
207
|
// A circle, an icon, a picture and a text keep their proportions when a corner is dragged.
|
|
237
|
-
|
|
208
|
+
const keepsAspect = !!single &&
|
|
238
209
|
(((single.type === 'path' || single.type === 'ellipse') && single.aspectLocked) ||
|
|
239
210
|
(corner && (single.type === 'image' || single.type === 'text')));
|
|
240
211
|
// A text's corner scales its letters, and the box the letters make is not where the hand is. The hand is hidden for the
|
|
241
212
|
// drag and an arrow is drawn on the corner instead, so the person sees what is being held.
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
213
|
+
const drawsOwnPointer = ENABLE_TEXT_VIRTUAL_CURSOR && corner && single?.type === 'text';
|
|
214
|
+
const direction = handleDirection(handle);
|
|
215
|
+
const arrowAngle = (Math.atan2(direction.y, direction.x) * 180) / Math.PI + frame.rotation;
|
|
245
216
|
engine.setCursor(cursorForHandle(handle, frame.rotation));
|
|
246
|
-
|
|
247
|
-
|
|
217
|
+
let started = false;
|
|
218
|
+
let last = start;
|
|
248
219
|
// The handle follows the pointer by the distance the press was from it. A handle's target reaches well past the 8 px
|
|
249
220
|
// square that is drawn (more for a finger), and the frame must not jump that far to the pointer on the first move.
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
221
|
+
const handleInWorld = handlePointOf(frame, handle);
|
|
222
|
+
const pressInWorld = worldOf(context, start);
|
|
223
|
+
const grabOffset = { x: handleInWorld.x - pressInWorld.x, y: handleInWorld.y - pressInWorld.y };
|
|
224
|
+
const apply = (input) => {
|
|
225
|
+
const pointerInWorld = worldOf(context, input);
|
|
226
|
+
const handleTarget = { x: pointerInWorld.x + grabOffset.x, y: pointerInWorld.y + grabOffset.y };
|
|
227
|
+
const resized = computeResizedFrame(frame, handle, handleTarget, {
|
|
257
228
|
fromCenter: input.altKey,
|
|
258
229
|
keepAspect: input.shiftKey || keepsAspect,
|
|
259
230
|
allowFlip: true
|
|
260
231
|
});
|
|
261
|
-
|
|
232
|
+
const next = fitAll(context, applyFrameResize(shapes, frame, resized, corner)).map((shape) => releaseUnmovedEnds(shape, stretchedIds));
|
|
262
233
|
engine.applyScene(withShapes(startScene, next), 'transient');
|
|
263
|
-
|
|
234
|
+
const after = selectionFrameOf(next);
|
|
264
235
|
engine.setOverlay({
|
|
265
236
|
dragging: 'resize',
|
|
266
237
|
readout: after ? { kind: 'size', width: Math.round(after.width), height: Math.round(after.height) } : null,
|
|
267
238
|
virtualCursor: drawsOwnPointer && after
|
|
268
|
-
?
|
|
239
|
+
? { ...worldToScreen(engine.state.viewport, cornerNearestTo(after, handleTarget)), angle: arrowAngle }
|
|
240
|
+
: null
|
|
269
241
|
});
|
|
270
242
|
};
|
|
271
243
|
return {
|
|
272
244
|
kind: 'resize',
|
|
273
245
|
pointerId: start.pointerId,
|
|
274
|
-
move
|
|
246
|
+
move(input) {
|
|
275
247
|
last = input;
|
|
276
248
|
if (!started) {
|
|
277
249
|
if (distanceBetween({ x: start.x, y: start.y }, { x: input.x, y: input.y }) < DRAG_THRESHOLD)
|
|
@@ -282,21 +254,21 @@ export function createResizeGesture(context, start, handle, frame, shapes) {
|
|
|
282
254
|
}
|
|
283
255
|
apply(input);
|
|
284
256
|
},
|
|
285
|
-
end
|
|
257
|
+
end() {
|
|
286
258
|
if (started) {
|
|
287
259
|
engine.clearOverlay();
|
|
288
260
|
engine.commit();
|
|
289
261
|
}
|
|
290
262
|
engine.setCursor(cursorForTool(engine.state.tool));
|
|
291
263
|
},
|
|
292
|
-
cancel
|
|
264
|
+
cancel() {
|
|
293
265
|
if (started)
|
|
294
266
|
engine.revertToCommitted();
|
|
295
267
|
engine.setCursor(cursorForTool(engine.state.tool));
|
|
296
268
|
},
|
|
297
|
-
modifiersChanged
|
|
269
|
+
modifiersChanged(modifiers) {
|
|
298
270
|
if (started)
|
|
299
|
-
apply(
|
|
271
|
+
apply({ ...last, ...modifiers });
|
|
300
272
|
}
|
|
301
273
|
};
|
|
302
274
|
}
|
|
@@ -307,34 +279,33 @@ export function createResizeGesture(context, start, handle, frame, shapes) {
|
|
|
307
279
|
* the pointer has gone `DRAG_THRESHOLD` pixels. `source` is where the press landed: the dot (the pointer is a hand that
|
|
308
280
|
* holds it) or the zone outside a corner (the turning arrow).
|
|
309
281
|
*/
|
|
310
|
-
export function createRotateGesture(context, start, frame, shapes, source) {
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
var single = shapes.length === 1 ? shapes[0] : null;
|
|
282
|
+
export function createRotateGesture(context, start, frame, shapes, source = 'corner') {
|
|
283
|
+
const { engine } = context;
|
|
284
|
+
const startScene = engine.state.scene;
|
|
285
|
+
const center = frameCenter(frame);
|
|
286
|
+
const startAngle = angleFromCenter(center, worldOf(context, start));
|
|
287
|
+
const single = shapes.length === 1 ? shapes[0] : null;
|
|
317
288
|
// An arrow turned with these shapes comes off the shapes it is tied to that are not turned with it.
|
|
318
|
-
|
|
289
|
+
const turnedIds = new Set(shapes.map((shape) => shape.id));
|
|
319
290
|
engine.setCursor(source === 'dot' ? 'grabbing' : ROTATE_CURSOR);
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
291
|
+
let started = false;
|
|
292
|
+
let last = start;
|
|
293
|
+
const apply = (input) => {
|
|
294
|
+
let delta = angleFromCenter(center, worldOf(context, input)) - startAngle;
|
|
324
295
|
if (input.shiftKey || input.ctrlKey || input.metaKey) {
|
|
325
296
|
delta = single
|
|
326
297
|
? snapAngle(single.rotation + delta, ROTATION_SNAP_DEGREES) - single.rotation
|
|
327
298
|
: snapAngle(delta, ROTATION_SNAP_DEGREES);
|
|
328
299
|
}
|
|
329
|
-
|
|
300
|
+
const turned = rotateShapesAbout(shapes, center, delta).map((shape) => releaseUnmovedEnds(shape, turnedIds));
|
|
330
301
|
engine.applyScene(withShapes(startScene, turned), 'transient');
|
|
331
|
-
|
|
302
|
+
const shown = single ? normalizeDegrees(single.rotation + delta) : normalizeDegrees(delta);
|
|
332
303
|
engine.setOverlay({ dragging: 'rotate', readout: { kind: 'angle', degrees: Math.round(shown) % 360 } });
|
|
333
304
|
};
|
|
334
305
|
return {
|
|
335
306
|
kind: 'rotate',
|
|
336
307
|
pointerId: start.pointerId,
|
|
337
|
-
move
|
|
308
|
+
move(input) {
|
|
338
309
|
last = input;
|
|
339
310
|
if (!started) {
|
|
340
311
|
if (distanceBetween({ x: start.x, y: start.y }, { x: input.x, y: input.y }) < DRAG_THRESHOLD)
|
|
@@ -343,21 +314,21 @@ export function createRotateGesture(context, start, frame, shapes, source) {
|
|
|
343
314
|
}
|
|
344
315
|
apply(input);
|
|
345
316
|
},
|
|
346
|
-
end
|
|
317
|
+
end() {
|
|
347
318
|
if (started) {
|
|
348
319
|
engine.clearOverlay();
|
|
349
320
|
engine.commit();
|
|
350
321
|
}
|
|
351
322
|
engine.setCursor(cursorForTool(engine.state.tool));
|
|
352
323
|
},
|
|
353
|
-
cancel
|
|
324
|
+
cancel() {
|
|
354
325
|
if (started)
|
|
355
326
|
engine.revertToCommitted();
|
|
356
327
|
engine.setCursor(cursorForTool(engine.state.tool));
|
|
357
328
|
},
|
|
358
|
-
modifiersChanged
|
|
329
|
+
modifiersChanged(modifiers) {
|
|
359
330
|
if (started)
|
|
360
|
-
apply(
|
|
331
|
+
apply({ ...last, ...modifiers });
|
|
361
332
|
}
|
|
362
333
|
};
|
|
363
334
|
}
|
|
@@ -376,42 +347,41 @@ export function createRotateGesture(context, start, frame, shapes, source) {
|
|
|
376
347
|
* ties it to nothing. The shape the other end is tied to is passed over: the two ends of an arrow are never tied to the same shape.
|
|
377
348
|
*/
|
|
378
349
|
export function createArrowHandleGesture(context, start, arrow, handle) {
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
350
|
+
const { engine } = context;
|
|
351
|
+
const startScene = engine.state.scene;
|
|
352
|
+
const startPoints = arrowPointsInWorld(arrow);
|
|
353
|
+
const otherEndBinding = handle === 'start' ? bindingOfEnd(arrow.arrow, 'end') : handle === 'end' ? bindingOfEnd(arrow.arrow, 'start') : null;
|
|
354
|
+
const passedOver = otherEndBinding ? new Set([otherEndBinding.targetId]) : undefined;
|
|
355
|
+
const grabbed = handle === 'start' ? startPoints.start : handle === 'end' ? startPoints.end : startPoints.middle;
|
|
356
|
+
const pressWorld = worldOf(context, start);
|
|
386
357
|
// Where the handle is, from where the pointer is: the pointer carries this much of an offset the whole way.
|
|
387
|
-
|
|
358
|
+
const offset = { x: grabbed.x - pressWorld.x, y: grabbed.y - pressWorld.y };
|
|
388
359
|
engine.setCursor('grabbing');
|
|
389
360
|
engine.setArrowHandleHover(handle);
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
var pick = null;
|
|
361
|
+
let started = false;
|
|
362
|
+
let last = start;
|
|
363
|
+
const apply = (input) => {
|
|
364
|
+
const pointer = worldOf(context, input);
|
|
365
|
+
const target = { x: pointer.x + offset.x, y: pointer.y + offset.y };
|
|
366
|
+
let changed;
|
|
367
|
+
let pick = null;
|
|
398
368
|
if (handle === 'bend') {
|
|
399
369
|
changed = reshapeArrow(arrow, { bend: bendTowards(startPoints.start, startPoints.end, target) });
|
|
400
370
|
}
|
|
401
371
|
else {
|
|
402
|
-
|
|
403
|
-
|
|
372
|
+
const fixed = handle === 'start' ? startPoints.end : startPoints.start;
|
|
373
|
+
const moved = input.shiftKey ? snapPointAround(fixed, target, ARROW_SNAP_DEGREES) : target;
|
|
404
374
|
if (!input.altKey && !input.shiftKey) {
|
|
405
375
|
pick = pickArrowBinding(startScene, target, { zoom: engine.state.viewport.zoom, ignore: passedOver });
|
|
406
376
|
}
|
|
407
377
|
// The end is where the hand puts it; the engine draws a tied end from its shape (`reconcileArrowBindings`) in the same step.
|
|
408
|
-
|
|
378
|
+
const change = handle === 'start' ? { start: moved, startBinding: pick?.binding ?? null } : { end: moved, endBinding: pick?.binding ?? null };
|
|
409
379
|
changed = reshapeArrow(arrow, change);
|
|
410
380
|
}
|
|
411
381
|
engine.applyScene(withShapes(startScene, [changed]), 'transient');
|
|
412
|
-
engine.setOverlay({ arrowBind:
|
|
382
|
+
engine.setOverlay({ arrowBind: pick?.hint ?? null });
|
|
413
383
|
};
|
|
414
|
-
|
|
384
|
+
const finish = () => {
|
|
415
385
|
engine.setOverlay({ arrowBind: null });
|
|
416
386
|
engine.setArrowHandleHover(null);
|
|
417
387
|
engine.setCursor(cursorForTool(engine.state.tool));
|
|
@@ -419,7 +389,7 @@ export function createArrowHandleGesture(context, start, arrow, handle) {
|
|
|
419
389
|
return {
|
|
420
390
|
kind: 'arrow-handle',
|
|
421
391
|
pointerId: start.pointerId,
|
|
422
|
-
move
|
|
392
|
+
move(input) {
|
|
423
393
|
last = input;
|
|
424
394
|
if (!started) {
|
|
425
395
|
if (distanceBetween({ x: start.x, y: start.y }, { x: input.x, y: input.y }) < DRAG_THRESHOLD)
|
|
@@ -428,41 +398,41 @@ export function createArrowHandleGesture(context, start, arrow, handle) {
|
|
|
428
398
|
}
|
|
429
399
|
apply(input);
|
|
430
400
|
},
|
|
431
|
-
end
|
|
401
|
+
end() {
|
|
432
402
|
if (started)
|
|
433
403
|
engine.commit();
|
|
434
404
|
finish();
|
|
435
405
|
},
|
|
436
|
-
cancel
|
|
406
|
+
cancel() {
|
|
437
407
|
if (started)
|
|
438
408
|
engine.revertToCommitted();
|
|
439
409
|
finish();
|
|
440
410
|
},
|
|
441
|
-
modifiersChanged
|
|
411
|
+
modifiersChanged(modifiers) {
|
|
442
412
|
if (started)
|
|
443
|
-
apply(
|
|
413
|
+
apply({ ...last, ...modifiers });
|
|
444
414
|
}
|
|
445
415
|
};
|
|
446
416
|
}
|
|
447
417
|
// ──────────────────────────────────────────────────────────── marquee
|
|
448
418
|
/** What a marquee that touches nothing hands to the overlay: the one shared empty list. */
|
|
449
|
-
|
|
419
|
+
const NO_UNITS = Object.freeze([]);
|
|
450
420
|
/**
|
|
451
421
|
* Drags a rectangle over the board. While it is dragged, what it touches is outlined the way the unit under the pointer is
|
|
452
422
|
* (the overlay's `marqueeHover`), so the person sees what the release will take; the selection is made at the release, from
|
|
453
423
|
* the same units (added to the ones that were selected when Shift was held at the press).
|
|
454
424
|
*/
|
|
455
425
|
export function createMarqueeGesture(context, start) {
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
426
|
+
const { engine } = context;
|
|
427
|
+
const startWorld = worldOf(context, start);
|
|
428
|
+
const base = start.shiftKey ? [...engine.state.selectedIds] : [];
|
|
429
|
+
let started = false;
|
|
460
430
|
// The units the marquee takes now. The board is handed the very same list for as long as they are the same units, so a
|
|
461
431
|
// marquee that grows over nothing new wakes nothing (see `erasingMarks.ts`).
|
|
462
|
-
|
|
432
|
+
let wouldSelect = NO_UNITS;
|
|
463
433
|
/** The selection a release at this rectangle would make: made as `select` makes it (a group once, no repeats). */
|
|
464
|
-
|
|
465
|
-
|
|
434
|
+
const selectionFor = (rect) => {
|
|
435
|
+
const units = normalizeSelection(engine.state.scene, [...base, ...engine.hitTestRect(rect, 'intersect')]);
|
|
466
436
|
if (units.length === 0)
|
|
467
437
|
return NO_UNITS;
|
|
468
438
|
return sameIds(units, wouldSelect) ? wouldSelect : units;
|
|
@@ -470,23 +440,23 @@ export function createMarqueeGesture(context, start) {
|
|
|
470
440
|
return {
|
|
471
441
|
kind: 'marquee',
|
|
472
442
|
pointerId: start.pointerId,
|
|
473
|
-
move
|
|
443
|
+
move(input) {
|
|
474
444
|
if (!started) {
|
|
475
445
|
if (distanceBetween({ x: start.x, y: start.y }, { x: input.x, y: input.y }) < DRAG_THRESHOLD)
|
|
476
446
|
return;
|
|
477
447
|
started = true;
|
|
478
448
|
}
|
|
479
|
-
|
|
449
|
+
const rect = rectFromPoints(startWorld, worldOf(context, input));
|
|
480
450
|
wouldSelect = selectionFor(rect);
|
|
481
451
|
engine.setOverlay({ marquee: rect, marqueeHover: wouldSelect });
|
|
482
452
|
},
|
|
483
|
-
end
|
|
453
|
+
end(input) {
|
|
484
454
|
// A press that never became a drag selects nothing (it cleared the selection when it began).
|
|
485
455
|
if (started)
|
|
486
456
|
engine.select(selectionFor(rectFromPoints(startWorld, worldOf(context, input))));
|
|
487
457
|
engine.setOverlay({ marquee: null, marqueeHover: NO_UNITS });
|
|
488
458
|
},
|
|
489
|
-
cancel
|
|
459
|
+
cancel() {
|
|
490
460
|
// Nothing was selected while it was dragged, so there is nothing to put back.
|
|
491
461
|
engine.setOverlay({ marquee: null, marqueeHover: NO_UNITS });
|
|
492
462
|
}
|
|
@@ -494,18 +464,17 @@ export function createMarqueeGesture(context, start) {
|
|
|
494
464
|
}
|
|
495
465
|
// ──────────────────────────────────────────────────────────── pan
|
|
496
466
|
/** Drags the view. `onTap` is what a press that never moved does (a finger on empty board clears the selection). */
|
|
497
|
-
export function createPanGesture(context, start, options) {
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
var moved = false;
|
|
467
|
+
export function createPanGesture(context, start, options = {}) {
|
|
468
|
+
const { engine } = context;
|
|
469
|
+
let lastX = start.x;
|
|
470
|
+
let lastY = start.y;
|
|
471
|
+
let moved = false;
|
|
503
472
|
engine.setCursor('grabbing');
|
|
504
|
-
|
|
473
|
+
const restore = () => engine.setCursor(cursorForTool(engine.state.tool));
|
|
505
474
|
return {
|
|
506
475
|
kind: 'pan',
|
|
507
476
|
pointerId: start.pointerId,
|
|
508
|
-
move
|
|
477
|
+
move(input) {
|
|
509
478
|
if (!moved && distanceBetween({ x: start.x, y: start.y }, { x: input.x, y: input.y }) >= DRAG_THRESHOLD)
|
|
510
479
|
moved = true;
|
|
511
480
|
if (!moved)
|
|
@@ -514,72 +483,67 @@ export function createPanGesture(context, start, options) {
|
|
|
514
483
|
lastX = input.x;
|
|
515
484
|
lastY = input.y;
|
|
516
485
|
},
|
|
517
|
-
end
|
|
518
|
-
var _a;
|
|
486
|
+
end() {
|
|
519
487
|
restore();
|
|
520
488
|
if (!moved)
|
|
521
|
-
|
|
489
|
+
options.onTap?.();
|
|
522
490
|
},
|
|
523
491
|
cancel: restore
|
|
524
492
|
};
|
|
525
493
|
}
|
|
526
494
|
// ──────────────────────────────────────────────────────────── ink
|
|
527
495
|
/** A pen whose pressure never moves by more than this has none to report, and draws as a mouse does. */
|
|
528
|
-
|
|
496
|
+
const PEN_PRESSURE_SPREAD = 0.02;
|
|
529
497
|
/** The pressure of a straight line (Shift): even, so that it is as wide at one end as at the other. */
|
|
530
|
-
|
|
498
|
+
const STRAIGHT_LINE_PRESSURE = 0.5;
|
|
531
499
|
/**
|
|
532
500
|
* Draws with the brush. The line is as thick as the hand presses: a pen's own pressure, or, for a mouse or a finger, the time the
|
|
533
501
|
* hand spends (a slow stroke swells, a quick one thins). Shift draws a straight line from where the pointer went down.
|
|
534
502
|
*/
|
|
535
503
|
export function createDrawGesture(context, start) {
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
504
|
+
const { engine } = context;
|
|
505
|
+
const strokeId = engine.services.createId('obj');
|
|
506
|
+
const isPen = start.pointerType === 'pen';
|
|
507
|
+
const samples = [];
|
|
508
|
+
let leastPressure = Infinity;
|
|
509
|
+
let mostPressure = -Infinity;
|
|
510
|
+
let straight = start.shiftKey;
|
|
511
|
+
let lastScreen = { x: start.x, y: start.y };
|
|
512
|
+
let lastInput = start;
|
|
513
|
+
const takeSample = (input) => {
|
|
514
|
+
const world = worldOf(context, input);
|
|
547
515
|
// Only a pen's pressure means anything: a mouse and a finger report one fixed value while they are down.
|
|
548
516
|
if (isPen && typeof input.pressure === 'number') {
|
|
549
517
|
leastPressure = Math.min(leastPressure, input.pressure);
|
|
550
518
|
mostPressure = Math.max(mostPressure, input.pressure);
|
|
551
|
-
samples.push(
|
|
519
|
+
samples.push({ ...world, pressure: input.pressure });
|
|
552
520
|
}
|
|
553
521
|
else {
|
|
554
522
|
samples.push(world);
|
|
555
523
|
}
|
|
556
524
|
};
|
|
557
525
|
takeSample(start);
|
|
558
|
-
|
|
559
|
-
|
|
526
|
+
const readsPressure = () => isPen && mostPressure - leastPressure >= PEN_PRESSURE_SPREAD;
|
|
527
|
+
const isStraight = () => straight && samples.length > 1;
|
|
560
528
|
/** The line the hand drew: the samples, or just their two ends when Shift holds it straight. */
|
|
561
|
-
|
|
562
|
-
|
|
529
|
+
const lineDrawn = () => (isStraight() ? [samples[0], samples[samples.length - 1]] : samples);
|
|
530
|
+
const build = (finished) => {
|
|
563
531
|
// A straight line has two ends and an even width, which is what a pen's reading of a steady pressure gives.
|
|
564
|
-
|
|
565
|
-
? lineDrawn().map(
|
|
566
|
-
var x = _a.x, y = _a.y;
|
|
567
|
-
return ({ x: x, y: y, pressure: STRAIGHT_LINE_PRESSURE });
|
|
568
|
-
})
|
|
532
|
+
const line = isStraight()
|
|
533
|
+
? lineDrawn().map(({ x, y }) => ({ x, y, pressure: STRAIGHT_LINE_PRESSURE }))
|
|
569
534
|
: samples;
|
|
570
|
-
return buildBrushStroke(line, { createId:
|
|
535
|
+
return buildBrushStroke(line, { createId: () => strokeId, pressure: isStraight() || readsPressure() ? 'pen' : 'speed', finished });
|
|
571
536
|
};
|
|
572
|
-
|
|
537
|
+
const showPreview = () => engine.setOverlay({ preview: build(false) });
|
|
573
538
|
showPreview();
|
|
574
539
|
return {
|
|
575
540
|
kind: 'draw',
|
|
576
541
|
pointerId: start.pointerId,
|
|
577
|
-
move
|
|
542
|
+
move(input) {
|
|
578
543
|
lastInput = input;
|
|
579
544
|
straight = input.shiftKey;
|
|
580
|
-
|
|
581
|
-
for (
|
|
582
|
-
var sample = moved_1[_i];
|
|
545
|
+
const moved = input.coalesced && input.coalesced.length > 0 ? input.coalesced : [input];
|
|
546
|
+
for (const sample of moved) {
|
|
583
547
|
// A new sample closer than a pixel to the last one adds nothing the eye can see.
|
|
584
548
|
if (Math.hypot(sample.x - lastScreen.x, sample.y - lastScreen.y) < 1)
|
|
585
549
|
continue;
|
|
@@ -588,22 +552,21 @@ export function createDrawGesture(context, start) {
|
|
|
588
552
|
}
|
|
589
553
|
showPreview();
|
|
590
554
|
},
|
|
591
|
-
end
|
|
592
|
-
var _a, _b;
|
|
555
|
+
end(input) {
|
|
593
556
|
straight = input.shiftKey;
|
|
594
|
-
|
|
557
|
+
const stroke = build(true);
|
|
595
558
|
engine.clearOverlay();
|
|
596
559
|
if (!stroke)
|
|
597
560
|
return;
|
|
598
561
|
engine.addObjects([stroke]);
|
|
599
|
-
|
|
562
|
+
context.options.onStrokeFinished?.(stroke, lineDrawn());
|
|
600
563
|
},
|
|
601
|
-
cancel
|
|
564
|
+
cancel() {
|
|
602
565
|
engine.clearOverlay();
|
|
603
566
|
},
|
|
604
|
-
modifiersChanged
|
|
567
|
+
modifiersChanged(modifiers) {
|
|
605
568
|
straight = modifiers.shiftKey;
|
|
606
|
-
lastInput =
|
|
569
|
+
lastInput = { ...lastInput, ...modifiers };
|
|
607
570
|
showPreview();
|
|
608
571
|
}
|
|
609
572
|
};
|
|
@@ -618,60 +581,57 @@ export function createDrawGesture(context, start) {
|
|
|
618
581
|
* default arrow, tied to nothing; the two ends are never tied to the same shape.
|
|
619
582
|
*/
|
|
620
583
|
export function createShapeGesture(context, start, kind, libraryKey) {
|
|
621
|
-
|
|
622
|
-
|
|
584
|
+
const { engine } = context;
|
|
585
|
+
const id = engine.services.createId('obj');
|
|
623
586
|
// One colour for the whole drag: a random fill must not change on every frame.
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
return pickArrowBinding(engine.state.scene, point, { zoom: engine.state.viewport.zoom, ignore: ignore });
|
|
630
|
-
};
|
|
587
|
+
const colorSeed = engine.services.random();
|
|
588
|
+
const startWorld = worldOf(context, start);
|
|
589
|
+
let dragged = false;
|
|
590
|
+
let last = start;
|
|
591
|
+
const pickAt = (point, ignore) => pickArrowBinding(engine.state.scene, point, { zoom: engine.state.viewport.zoom, ignore });
|
|
631
592
|
// The shape the tail of an arrow would be tied to, from the moment of the press.
|
|
632
|
-
|
|
593
|
+
const tailPick = kind === 'arrow' && !start.altKey ? pickAt(startWorld) : null;
|
|
633
594
|
if (tailPick)
|
|
634
595
|
engine.setOverlay({ arrowBind: tailPick.hint });
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
kind: kind,
|
|
596
|
+
const build = (input) => {
|
|
597
|
+
const end = worldOf(context, input);
|
|
598
|
+
const fromCenter = input.altKey && kind !== 'arrow' && dragged;
|
|
599
|
+
const shape = buildDraggedShape({
|
|
600
|
+
kind,
|
|
641
601
|
start: fromCenter ? { x: 2 * startWorld.x - end.x, y: 2 * startWorld.y - end.y } : startWorld,
|
|
642
|
-
end
|
|
602
|
+
end,
|
|
643
603
|
constrain: input.shiftKey,
|
|
644
|
-
dragged
|
|
645
|
-
libraryKey
|
|
604
|
+
dragged,
|
|
605
|
+
libraryKey,
|
|
646
606
|
wireframe: engine.wireframeMode,
|
|
647
|
-
createId:
|
|
648
|
-
random:
|
|
607
|
+
createId: () => id,
|
|
608
|
+
random: () => colorSeed,
|
|
649
609
|
workspacePaint: engine.workspacePaintForNewShape(engine.wireframeMode)
|
|
650
610
|
});
|
|
651
611
|
if (kind !== 'arrow' || !dragged || input.altKey || shape.type !== 'path' || !isArrow(shape))
|
|
652
|
-
return { shape
|
|
653
|
-
|
|
654
|
-
|
|
612
|
+
return { shape, hint: null };
|
|
613
|
+
const tail = tailPick?.binding;
|
|
614
|
+
const headPick = input.shiftKey ? null : pickAt(end, tail ? new Set([tail.targetId]) : undefined);
|
|
655
615
|
if (!tail && !headPick)
|
|
656
|
-
return { shape
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
return { shape: tied, hint:
|
|
616
|
+
return { shape, hint: null };
|
|
617
|
+
const index = engine.sceneIndex();
|
|
618
|
+
const tied = positionBoundArrow(reshapeArrow(shape, { startBinding: tail ?? null, endBinding: headPick?.binding ?? null }), (targetId) => index.get(targetId));
|
|
619
|
+
return { shape: tied, hint: headPick?.hint ?? null };
|
|
660
620
|
};
|
|
661
621
|
return {
|
|
662
622
|
kind: 'shape',
|
|
663
623
|
pointerId: start.pointerId,
|
|
664
|
-
move
|
|
624
|
+
move(input) {
|
|
665
625
|
last = input;
|
|
666
626
|
if (!dragged && distanceBetween({ x: start.x, y: start.y }, { x: input.x, y: input.y }) >= MIN_DRAW_DISTANCE)
|
|
667
627
|
dragged = true;
|
|
668
628
|
if (dragged) {
|
|
669
|
-
|
|
629
|
+
const { shape, hint } = build(input);
|
|
670
630
|
engine.setOverlay({ preview: shape, arrowBind: hint });
|
|
671
631
|
}
|
|
672
632
|
},
|
|
673
|
-
end
|
|
674
|
-
|
|
633
|
+
end(input) {
|
|
634
|
+
const { shape } = build(input);
|
|
675
635
|
engine.clearOverlay();
|
|
676
636
|
engine.addObjects([shape]);
|
|
677
637
|
if (engine.state.stickyTool) {
|
|
@@ -682,13 +642,13 @@ export function createShapeGesture(context, start, kind, libraryKey) {
|
|
|
682
642
|
engine.select([shape.id]);
|
|
683
643
|
}
|
|
684
644
|
},
|
|
685
|
-
cancel
|
|
645
|
+
cancel() {
|
|
686
646
|
engine.clearOverlay();
|
|
687
647
|
},
|
|
688
|
-
modifiersChanged
|
|
648
|
+
modifiersChanged(modifiers) {
|
|
689
649
|
if (!dragged)
|
|
690
650
|
return;
|
|
691
|
-
|
|
651
|
+
const { shape, hint } = build({ ...last, ...modifiers });
|
|
692
652
|
engine.setOverlay({ preview: shape, arrowBind: hint });
|
|
693
653
|
}
|
|
694
654
|
};
|
|
@@ -696,35 +656,35 @@ export function createShapeGesture(context, start, kind, libraryKey) {
|
|
|
696
656
|
// ──────────────────────────────────────────────────────────── text
|
|
697
657
|
/** A click makes a label that grows with its text, a drag makes a box of that width. Then the text is edited. */
|
|
698
658
|
export function createTextGesture(context, start) {
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
659
|
+
const { engine } = context;
|
|
660
|
+
const startWorld = worldOf(context, start);
|
|
661
|
+
let dragged = false;
|
|
702
662
|
return {
|
|
703
663
|
kind: 'text',
|
|
704
664
|
pointerId: start.pointerId,
|
|
705
|
-
move
|
|
665
|
+
move(input) {
|
|
706
666
|
if (!dragged && distanceBetween({ x: start.x, y: start.y }, { x: input.x, y: input.y }) >= MIN_DRAW_DISTANCE)
|
|
707
667
|
dragged = true;
|
|
708
668
|
if (dragged)
|
|
709
669
|
engine.setOverlay({ textDraft: rectFromPoints(startWorld, worldOf(context, input)) });
|
|
710
670
|
},
|
|
711
|
-
end
|
|
671
|
+
end(input) {
|
|
712
672
|
engine.setOverlay({ textDraft: null });
|
|
713
673
|
if (!dragged) {
|
|
714
674
|
engine.createText(startWorld);
|
|
715
675
|
return;
|
|
716
676
|
}
|
|
717
|
-
|
|
677
|
+
const rect = rectFromPoints(startWorld, worldOf(context, input));
|
|
718
678
|
engine.createText({ x: rect.x, y: rect.y }, { width: rect.width });
|
|
719
679
|
},
|
|
720
|
-
cancel
|
|
680
|
+
cancel() {
|
|
721
681
|
engine.setOverlay({ textDraft: null });
|
|
722
682
|
}
|
|
723
683
|
};
|
|
724
684
|
}
|
|
725
685
|
// ──────────────────────────────────────────────────────────── eraser
|
|
726
686
|
/** The most spots along one sweep of the eraser that are tested for shapes (a flick across a huge board stays affordable). */
|
|
727
|
-
|
|
687
|
+
const ERASER_MAX_STEPS_PER_SWEEP = 400;
|
|
728
688
|
/**
|
|
729
689
|
* Marks what the pointer passes over and removes it when the pointer is released.
|
|
730
690
|
*
|
|
@@ -736,48 +696,44 @@ var ERASER_MAX_STEPS_PER_SWEEP = 400;
|
|
|
736
696
|
* one by one. A press that does not move marks what lies under it and removes that on release (with Ctrl or Cmd only the top shape).
|
|
737
697
|
*/
|
|
738
698
|
export function createEraseGesture(context, start) {
|
|
739
|
-
|
|
699
|
+
const { engine } = context;
|
|
740
700
|
// The scene stays as it was at the press: nothing is removed until the release.
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
701
|
+
const scene = engine.state.scene;
|
|
702
|
+
const index = engine.sceneIndex(scene);
|
|
703
|
+
const startWorld = worldOf(context, start);
|
|
704
|
+
const zoom = () => engine.state.viewport.zoom;
|
|
705
|
+
const marked = [];
|
|
746
706
|
// What the hit tests look through: the cards that are never erased, and what is marked already.
|
|
747
|
-
|
|
748
|
-
for (
|
|
749
|
-
var object = scene_1[_i];
|
|
707
|
+
const skipped = new Set();
|
|
708
|
+
for (const object of scene) {
|
|
750
709
|
if (object.type === 'widget' || engine.state.pending[object.id])
|
|
751
710
|
skipped.add(object.id);
|
|
752
711
|
}
|
|
753
712
|
// The groups the press began inside of: their members are taken one by one instead of the group as a whole.
|
|
754
|
-
|
|
755
|
-
for (
|
|
756
|
-
var object = scene_2[_a];
|
|
713
|
+
const enteredGroups = new Set();
|
|
714
|
+
for (const object of scene) {
|
|
757
715
|
if (object.type !== 'group')
|
|
758
716
|
continue;
|
|
759
|
-
|
|
717
|
+
const box = boundsOfObjects(scene, [object.id]);
|
|
760
718
|
if (box && rectContainsPoint(box, startWorld))
|
|
761
719
|
enteredGroups.add(object.id);
|
|
762
720
|
}
|
|
763
721
|
/** What goes with a shape: the outermost group above it that the press did not begin inside of, else the shape itself. */
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
var visited = new Set();
|
|
722
|
+
const unitOf = (shapeId) => {
|
|
723
|
+
let unit = index.get(shapeId);
|
|
724
|
+
const visited = new Set();
|
|
768
725
|
while (unit && unit.parentId !== null && !visited.has(unit.id)) {
|
|
769
726
|
visited.add(unit.id);
|
|
770
|
-
|
|
727
|
+
const parent = index.get(unit.parentId);
|
|
771
728
|
if (!parent || enteredGroups.has(parent.id))
|
|
772
729
|
break;
|
|
773
730
|
unit = parent;
|
|
774
731
|
}
|
|
775
|
-
return
|
|
732
|
+
return unit?.id ?? shapeId;
|
|
776
733
|
};
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
for (
|
|
780
|
-
var shape = _a[_i];
|
|
734
|
+
let marksChanged = false;
|
|
735
|
+
const markUnitOf = (shapeId) => {
|
|
736
|
+
for (const shape of leafShapesOf(scene, [unitOf(shapeId)])) {
|
|
781
737
|
if (skipped.has(shape.id))
|
|
782
738
|
continue;
|
|
783
739
|
skipped.add(shape.id);
|
|
@@ -786,10 +742,9 @@ export function createEraseGesture(context, start) {
|
|
|
786
742
|
}
|
|
787
743
|
};
|
|
788
744
|
/** Marks the shapes under a spot (all of them, or only the top one). */
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
var hit = hitTestLeaf(scene, world, { zoom: zoom(), tolerancePixels: ERASER_RADIUS, ignore: skipped });
|
|
745
|
+
const markAt = (world, onlyTop = false) => {
|
|
746
|
+
for (let guard = 0; guard < 64; guard += 1) {
|
|
747
|
+
const hit = hitTestLeaf(scene, world, { zoom: zoom(), tolerancePixels: ERASER_RADIUS, ignore: skipped });
|
|
793
748
|
if (!hit)
|
|
794
749
|
break;
|
|
795
750
|
markUnitOf(hit.id);
|
|
@@ -799,18 +754,18 @@ export function createEraseGesture(context, start) {
|
|
|
799
754
|
break;
|
|
800
755
|
}
|
|
801
756
|
};
|
|
802
|
-
|
|
757
|
+
let lastWorld = startWorld;
|
|
803
758
|
/** Marks what lies along the way from where the eraser was to where it is. */
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
for (
|
|
808
|
-
|
|
759
|
+
const sweepTo = (to) => {
|
|
760
|
+
const screenLength = distanceBetween(lastWorld, to) * zoom();
|
|
761
|
+
const steps = Math.min(ERASER_MAX_STEPS_PER_SWEEP, Math.max(1, Math.ceil(screenLength / (ERASER_RADIUS / 2))));
|
|
762
|
+
for (let step = 1; step <= steps; step += 1) {
|
|
763
|
+
const progress = step / steps;
|
|
809
764
|
markAt({ x: lastWorld.x + (to.x - lastWorld.x) * progress, y: lastWorld.y + (to.y - lastWorld.y) * progress });
|
|
810
765
|
}
|
|
811
766
|
lastWorld = to;
|
|
812
767
|
};
|
|
813
|
-
|
|
768
|
+
const showMarks = () => {
|
|
814
769
|
if (!marksChanged)
|
|
815
770
|
return;
|
|
816
771
|
marksChanged = false;
|
|
@@ -819,8 +774,8 @@ export function createEraseGesture(context, start) {
|
|
|
819
774
|
// The press itself marks what is under it, so a click shows what it is about to remove.
|
|
820
775
|
markAt(startWorld, start.ctrlKey || start.metaKey);
|
|
821
776
|
showMarks();
|
|
822
|
-
|
|
823
|
-
|
|
777
|
+
let dragging = false;
|
|
778
|
+
const beginDragging = () => {
|
|
824
779
|
dragging = true;
|
|
825
780
|
engine.beginScribble();
|
|
826
781
|
// A drag takes everything under the spot where it began, also after a click that took only the top shape.
|
|
@@ -829,22 +784,21 @@ export function createEraseGesture(context, start) {
|
|
|
829
784
|
return {
|
|
830
785
|
kind: 'erase',
|
|
831
786
|
pointerId: start.pointerId,
|
|
832
|
-
move
|
|
787
|
+
move(input) {
|
|
833
788
|
if (!dragging) {
|
|
834
789
|
if (distanceBetween({ x: start.x, y: start.y }, { x: input.x, y: input.y }) < DRAG_THRESHOLD)
|
|
835
790
|
return;
|
|
836
791
|
beginDragging();
|
|
837
792
|
}
|
|
838
|
-
|
|
839
|
-
for (
|
|
840
|
-
|
|
841
|
-
var world = worldOf(context, sample);
|
|
793
|
+
const samples = input.coalesced && input.coalesced.length > 0 ? input.coalesced : [input];
|
|
794
|
+
for (const sample of samples) {
|
|
795
|
+
const world = worldOf(context, sample);
|
|
842
796
|
sweepTo(world);
|
|
843
797
|
engine.extendScribble(world);
|
|
844
798
|
}
|
|
845
799
|
showMarks();
|
|
846
800
|
},
|
|
847
|
-
end
|
|
801
|
+
end(input) {
|
|
848
802
|
// The release can come with a last position no move reported.
|
|
849
803
|
if (dragging)
|
|
850
804
|
sweepTo(worldOf(context, input));
|
|
@@ -853,7 +807,7 @@ export function createEraseGesture(context, start) {
|
|
|
853
807
|
if (marked.length > 0)
|
|
854
808
|
engine.removeObjects(marked);
|
|
855
809
|
},
|
|
856
|
-
cancel
|
|
810
|
+
cancel() {
|
|
857
811
|
engine.stopScribble();
|
|
858
812
|
engine.clearOverlay();
|
|
859
813
|
}
|