@widgetic/canvas 0.5.41 → 0.5.43
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/dist/canvas/DomCanvas.svelte +49 -20
- package/dist/canvas/DomCanvas.svelte.d.ts +13 -7
- 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.d.ts +14 -3
- package/dist/canvas/chromeLayout.js +30 -20
- package/dist/canvas/eventScope.js +10 -14
- package/dist/canvas/host/CanvasHost.d.ts +47 -5
- package/dist/canvas/host/CanvasHost.js +1481 -1566
- 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/nearCards.d.ts +33 -0
- package/dist/canvas/host/nearCards.js +78 -0
- package/dist/canvas/host/pageEvents.d.ts +16 -4
- package/dist/canvas/host/pageEvents.js +17 -8
- package/dist/canvas/host/pictureSaves.d.ts +33 -0
- package/dist/canvas/host/pictureSaves.js +42 -0
- package/dist/canvas/host/screenRects.d.ts +5 -0
- package/dist/canvas/host/screenRects.js +28 -31
- package/dist/canvas/host/storedPictures.d.ts +88 -0
- package/dist/canvas/host/storedPictures.js +288 -0
- package/dist/canvas/host/strokeSnap.js +88 -143
- package/dist/canvas/index.d.ts +1 -1
- package/dist/canvas/index.js +1 -1
- 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.d.ts +45 -2
- package/dist/canvas/shapes/shapeImageStore.js +10 -3
- package/dist/canvas/shapes/strokeShapeSnap.js +188 -233
- package/dist/canvas/toolbarPosition.d.ts +2 -2
- package/dist/canvas/toolbarPosition.js +4 -4
- package/dist/canvas/ui/SelectionPanel.svelte +28 -5
- package/dist/canvas/ui/SelectionPanel.svelte.d.ts +7 -0
- package/dist/canvas/ui/SelectorIndicator.svelte +81 -0
- package/dist/canvas/ui/SelectorIndicator.svelte.d.ts +11 -0
- package/dist/canvas/ui/ToolButton.svelte +8 -2
- package/dist/canvas/ui/colorModel.js +21 -32
- package/dist/canvas/ui/colorPickerMessages.js +5 -5
- package/dist/canvas/ui/floatingCard.d.ts +2 -2
- package/dist/canvas/ui/floatingCard.js +20 -20
- package/dist/canvas/ui/floatingLayer.d.ts +13 -2
- package/dist/canvas/ui/floatingLayer.js +18 -7
- 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/CanvasStage.svelte +101 -67
- package/dist/canvas/view/LeftToRightScope.svelte +21 -0
- package/dist/canvas/view/LeftToRightScope.svelte.d.ts +7 -0
- package/dist/canvas/view/ObjectView.svelte +15 -10
- package/dist/canvas/view/ObjectView.svelte.d.ts +1 -1
- package/dist/canvas/view/erasingMarks.js +7 -7
- package/dist/canvas/view/geometry.js +71 -90
- package/dist/canvas/view/liftedScene.d.ts +31 -0
- package/dist/canvas/view/liftedScene.js +146 -0
- package/dist/canvas/view/mountedScene.d.ts +71 -0
- package/dist/canvas/view/mountedScene.js +363 -0
- package/dist/components/preferenceStorage.js +2 -2
- package/dist/engine/EngineCore.d.ts +15 -1
- package/dist/engine/EngineCore.js +724 -797
- package/dist/engine/codec/canvasDocument.d.ts +5 -0
- package/dist/engine/codec/canvasDocument.js +212 -149
- 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.d.ts +6 -0
- package/dist/engine/core/history.js +49 -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.d.ts +10 -0
- package/dist/engine/core/scene.js +153 -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.d.ts +8 -2
- 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 +265 -309
- 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.d.ts +20 -0
- package/dist/engine/state.js +5 -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.d.ts +10 -1
- package/dist/fonts/interFont.js +47 -75
- package/dist/fonts/workspaceFonts.js +32 -90
- package/dist/i18n/labels.js +4 -4
- package/dist/i18n/language.d.ts +3 -3
- package/dist/i18n/language.js +4 -4
- package/dist/i18n/messages.d.ts +8 -1
- package/dist/i18n/messages.js +10 -2
- package/dist/i18n/shortcuts.js +14 -14
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- 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
- package/dist/canvas/ui/SelectorBar.svelte +0 -84
- package/dist/canvas/ui/SelectorBar.svelte.d.ts +0 -12
|
@@ -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,117 @@ 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
|
-
// The handles step aside while the selection is carried: what is carried is all that is to be seen.
|
|
153
|
-
|
|
125
|
+
// The handles step aside while the selection is carried: what is carried is all that is to be seen. The board is also told
|
|
126
|
+
// what is carried, from which records and how far (`CarriedShapes`), so that it can draw a big selection as one layer and
|
|
127
|
+
// not as a new record for every shape at every step; the records written above are the same, and the engine reads only them.
|
|
128
|
+
engine.setOverlay({ guides, dragging: 'move', carry: { startRecords: engine.sceneIndex(startScene), shapeIds: carried, deltaX, deltaY } });
|
|
154
129
|
};
|
|
155
130
|
return {
|
|
156
131
|
kind: 'move',
|
|
157
132
|
pointerId: start.pointerId,
|
|
158
|
-
move
|
|
133
|
+
move(input) {
|
|
159
134
|
last = input;
|
|
160
135
|
if (options.immovable)
|
|
161
136
|
return;
|
|
@@ -168,7 +143,7 @@ export function createMoveGesture(context, start, unitIds, options) {
|
|
|
168
143
|
duplicated = true;
|
|
169
144
|
if (engine.state.selectedIds.length === 0 && unitIds.length > 0)
|
|
170
145
|
engine.select(unitIds);
|
|
171
|
-
|
|
146
|
+
const copyIds = engine.duplicateSelectionForDrag();
|
|
172
147
|
if (copyIds.length > 0) {
|
|
173
148
|
moving = new Set(copyIds);
|
|
174
149
|
startScene = engine.state.scene;
|
|
@@ -178,10 +153,9 @@ export function createMoveGesture(context, start, unitIds, options) {
|
|
|
178
153
|
}
|
|
179
154
|
apply(input);
|
|
180
155
|
},
|
|
181
|
-
end
|
|
182
|
-
var _a;
|
|
156
|
+
end(input) {
|
|
183
157
|
if (!started) {
|
|
184
|
-
|
|
158
|
+
options.onClick?.();
|
|
185
159
|
return;
|
|
186
160
|
}
|
|
187
161
|
last = input;
|
|
@@ -190,21 +164,21 @@ export function createMoveGesture(context, start, unitIds, options) {
|
|
|
190
164
|
engine.commit();
|
|
191
165
|
engine.setCursor(cursorForTool(engine.state.tool));
|
|
192
166
|
},
|
|
193
|
-
cancel
|
|
167
|
+
cancel() {
|
|
194
168
|
if (started)
|
|
195
169
|
engine.revertToCommitted();
|
|
196
170
|
engine.setCursor(cursorForTool(engine.state.tool));
|
|
197
171
|
},
|
|
198
|
-
modifiersChanged
|
|
172
|
+
modifiersChanged(modifiers) {
|
|
199
173
|
if (started)
|
|
200
|
-
apply(
|
|
174
|
+
apply({ ...last, ...modifiers });
|
|
201
175
|
}
|
|
202
176
|
};
|
|
203
177
|
}
|
|
204
178
|
// ──────────────────────────────────────────────────────────── resize
|
|
205
179
|
/** Where a handle of a frame is in the world. */
|
|
206
180
|
function handlePointOf(frame, handle) {
|
|
207
|
-
|
|
181
|
+
const direction = handleDirection(handle);
|
|
208
182
|
return applyMatrixToPoint(boxToWorldMatrix(frame), {
|
|
209
183
|
x: direction.x === 0 ? frame.width / 2 : direction.x > 0 ? frame.width : 0,
|
|
210
184
|
y: direction.y === 0 ? frame.height / 2 : direction.y > 0 ? frame.height : 0
|
|
@@ -212,12 +186,11 @@ function handlePointOf(frame, handle) {
|
|
|
212
186
|
}
|
|
213
187
|
/** 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
188
|
function cornerNearestTo(frame, target) {
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
for (
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
var distance = distanceBetween(point, target);
|
|
189
|
+
let nearest = handlePointOf(frame, CORNER_HANDLES[0]);
|
|
190
|
+
let nearestDistance = distanceBetween(nearest, target);
|
|
191
|
+
for (const handle of CORNER_HANDLES.slice(1)) {
|
|
192
|
+
const point = handlePointOf(frame, handle);
|
|
193
|
+
const distance = distanceBetween(point, target);
|
|
221
194
|
if (distance < nearestDistance) {
|
|
222
195
|
nearest = point;
|
|
223
196
|
nearestDistance = distance;
|
|
@@ -227,51 +200,52 @@ function cornerNearestTo(frame, target) {
|
|
|
227
200
|
}
|
|
228
201
|
/** 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
202
|
export function createResizeGesture(context, start, handle, frame, shapes) {
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
203
|
+
const { engine } = context;
|
|
204
|
+
const startScene = engine.state.scene;
|
|
205
|
+
const corner = isCornerHandle(handle);
|
|
206
|
+
const single = shapes.length === 1 ? shapes[0] : null;
|
|
234
207
|
// An arrow stretched with these shapes comes off the shapes it is tied to that are not stretched with it.
|
|
235
|
-
|
|
208
|
+
const stretchedIds = new Set(shapes.map((shape) => shape.id));
|
|
236
209
|
// A circle, an icon, a picture and a text keep their proportions when a corner is dragged.
|
|
237
|
-
|
|
210
|
+
const keepsAspect = !!single &&
|
|
238
211
|
(((single.type === 'path' || single.type === 'ellipse') && single.aspectLocked) ||
|
|
239
212
|
(corner && (single.type === 'image' || single.type === 'text')));
|
|
240
213
|
// 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
214
|
// drag and an arrow is drawn on the corner instead, so the person sees what is being held.
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
215
|
+
const drawsOwnPointer = ENABLE_TEXT_VIRTUAL_CURSOR && corner && single?.type === 'text';
|
|
216
|
+
const direction = handleDirection(handle);
|
|
217
|
+
const arrowAngle = (Math.atan2(direction.y, direction.x) * 180) / Math.PI + frame.rotation;
|
|
245
218
|
engine.setCursor(cursorForHandle(handle, frame.rotation));
|
|
246
|
-
|
|
247
|
-
|
|
219
|
+
let started = false;
|
|
220
|
+
let last = start;
|
|
248
221
|
// The handle follows the pointer by the distance the press was from it. A handle's target reaches well past the 8 px
|
|
249
222
|
// 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
|
-
|
|
223
|
+
const handleInWorld = handlePointOf(frame, handle);
|
|
224
|
+
const pressInWorld = worldOf(context, start);
|
|
225
|
+
const grabOffset = { x: handleInWorld.x - pressInWorld.x, y: handleInWorld.y - pressInWorld.y };
|
|
226
|
+
const apply = (input) => {
|
|
227
|
+
const pointerInWorld = worldOf(context, input);
|
|
228
|
+
const handleTarget = { x: pointerInWorld.x + grabOffset.x, y: pointerInWorld.y + grabOffset.y };
|
|
229
|
+
const resized = computeResizedFrame(frame, handle, handleTarget, {
|
|
257
230
|
fromCenter: input.altKey,
|
|
258
231
|
keepAspect: input.shiftKey || keepsAspect,
|
|
259
232
|
allowFlip: true
|
|
260
233
|
});
|
|
261
|
-
|
|
234
|
+
const next = fitAll(context, applyFrameResize(shapes, frame, resized, corner)).map((shape) => releaseUnmovedEnds(shape, stretchedIds));
|
|
262
235
|
engine.applyScene(withShapes(startScene, next), 'transient');
|
|
263
|
-
|
|
236
|
+
const after = selectionFrameOf(next);
|
|
264
237
|
engine.setOverlay({
|
|
265
238
|
dragging: 'resize',
|
|
266
239
|
readout: after ? { kind: 'size', width: Math.round(after.width), height: Math.round(after.height) } : null,
|
|
267
240
|
virtualCursor: drawsOwnPointer && after
|
|
268
|
-
?
|
|
241
|
+
? { ...worldToScreen(engine.state.viewport, cornerNearestTo(after, handleTarget)), angle: arrowAngle }
|
|
242
|
+
: null
|
|
269
243
|
});
|
|
270
244
|
};
|
|
271
245
|
return {
|
|
272
246
|
kind: 'resize',
|
|
273
247
|
pointerId: start.pointerId,
|
|
274
|
-
move
|
|
248
|
+
move(input) {
|
|
275
249
|
last = input;
|
|
276
250
|
if (!started) {
|
|
277
251
|
if (distanceBetween({ x: start.x, y: start.y }, { x: input.x, y: input.y }) < DRAG_THRESHOLD)
|
|
@@ -282,21 +256,21 @@ export function createResizeGesture(context, start, handle, frame, shapes) {
|
|
|
282
256
|
}
|
|
283
257
|
apply(input);
|
|
284
258
|
},
|
|
285
|
-
end
|
|
259
|
+
end() {
|
|
286
260
|
if (started) {
|
|
287
261
|
engine.clearOverlay();
|
|
288
262
|
engine.commit();
|
|
289
263
|
}
|
|
290
264
|
engine.setCursor(cursorForTool(engine.state.tool));
|
|
291
265
|
},
|
|
292
|
-
cancel
|
|
266
|
+
cancel() {
|
|
293
267
|
if (started)
|
|
294
268
|
engine.revertToCommitted();
|
|
295
269
|
engine.setCursor(cursorForTool(engine.state.tool));
|
|
296
270
|
},
|
|
297
|
-
modifiersChanged
|
|
271
|
+
modifiersChanged(modifiers) {
|
|
298
272
|
if (started)
|
|
299
|
-
apply(
|
|
273
|
+
apply({ ...last, ...modifiers });
|
|
300
274
|
}
|
|
301
275
|
};
|
|
302
276
|
}
|
|
@@ -307,34 +281,33 @@ export function createResizeGesture(context, start, handle, frame, shapes) {
|
|
|
307
281
|
* the pointer has gone `DRAG_THRESHOLD` pixels. `source` is where the press landed: the dot (the pointer is a hand that
|
|
308
282
|
* holds it) or the zone outside a corner (the turning arrow).
|
|
309
283
|
*/
|
|
310
|
-
export function createRotateGesture(context, start, frame, shapes, source) {
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
var single = shapes.length === 1 ? shapes[0] : null;
|
|
284
|
+
export function createRotateGesture(context, start, frame, shapes, source = 'corner') {
|
|
285
|
+
const { engine } = context;
|
|
286
|
+
const startScene = engine.state.scene;
|
|
287
|
+
const center = frameCenter(frame);
|
|
288
|
+
const startAngle = angleFromCenter(center, worldOf(context, start));
|
|
289
|
+
const single = shapes.length === 1 ? shapes[0] : null;
|
|
317
290
|
// An arrow turned with these shapes comes off the shapes it is tied to that are not turned with it.
|
|
318
|
-
|
|
291
|
+
const turnedIds = new Set(shapes.map((shape) => shape.id));
|
|
319
292
|
engine.setCursor(source === 'dot' ? 'grabbing' : ROTATE_CURSOR);
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
293
|
+
let started = false;
|
|
294
|
+
let last = start;
|
|
295
|
+
const apply = (input) => {
|
|
296
|
+
let delta = angleFromCenter(center, worldOf(context, input)) - startAngle;
|
|
324
297
|
if (input.shiftKey || input.ctrlKey || input.metaKey) {
|
|
325
298
|
delta = single
|
|
326
299
|
? snapAngle(single.rotation + delta, ROTATION_SNAP_DEGREES) - single.rotation
|
|
327
300
|
: snapAngle(delta, ROTATION_SNAP_DEGREES);
|
|
328
301
|
}
|
|
329
|
-
|
|
302
|
+
const turned = rotateShapesAbout(shapes, center, delta).map((shape) => releaseUnmovedEnds(shape, turnedIds));
|
|
330
303
|
engine.applyScene(withShapes(startScene, turned), 'transient');
|
|
331
|
-
|
|
304
|
+
const shown = single ? normalizeDegrees(single.rotation + delta) : normalizeDegrees(delta);
|
|
332
305
|
engine.setOverlay({ dragging: 'rotate', readout: { kind: 'angle', degrees: Math.round(shown) % 360 } });
|
|
333
306
|
};
|
|
334
307
|
return {
|
|
335
308
|
kind: 'rotate',
|
|
336
309
|
pointerId: start.pointerId,
|
|
337
|
-
move
|
|
310
|
+
move(input) {
|
|
338
311
|
last = input;
|
|
339
312
|
if (!started) {
|
|
340
313
|
if (distanceBetween({ x: start.x, y: start.y }, { x: input.x, y: input.y }) < DRAG_THRESHOLD)
|
|
@@ -343,21 +316,21 @@ export function createRotateGesture(context, start, frame, shapes, source) {
|
|
|
343
316
|
}
|
|
344
317
|
apply(input);
|
|
345
318
|
},
|
|
346
|
-
end
|
|
319
|
+
end() {
|
|
347
320
|
if (started) {
|
|
348
321
|
engine.clearOverlay();
|
|
349
322
|
engine.commit();
|
|
350
323
|
}
|
|
351
324
|
engine.setCursor(cursorForTool(engine.state.tool));
|
|
352
325
|
},
|
|
353
|
-
cancel
|
|
326
|
+
cancel() {
|
|
354
327
|
if (started)
|
|
355
328
|
engine.revertToCommitted();
|
|
356
329
|
engine.setCursor(cursorForTool(engine.state.tool));
|
|
357
330
|
},
|
|
358
|
-
modifiersChanged
|
|
331
|
+
modifiersChanged(modifiers) {
|
|
359
332
|
if (started)
|
|
360
|
-
apply(
|
|
333
|
+
apply({ ...last, ...modifiers });
|
|
361
334
|
}
|
|
362
335
|
};
|
|
363
336
|
}
|
|
@@ -376,42 +349,41 @@ export function createRotateGesture(context, start, frame, shapes, source) {
|
|
|
376
349
|
* 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
350
|
*/
|
|
378
351
|
export function createArrowHandleGesture(context, start, arrow, handle) {
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
352
|
+
const { engine } = context;
|
|
353
|
+
const startScene = engine.state.scene;
|
|
354
|
+
const startPoints = arrowPointsInWorld(arrow);
|
|
355
|
+
const otherEndBinding = handle === 'start' ? bindingOfEnd(arrow.arrow, 'end') : handle === 'end' ? bindingOfEnd(arrow.arrow, 'start') : null;
|
|
356
|
+
const passedOver = otherEndBinding ? new Set([otherEndBinding.targetId]) : undefined;
|
|
357
|
+
const grabbed = handle === 'start' ? startPoints.start : handle === 'end' ? startPoints.end : startPoints.middle;
|
|
358
|
+
const pressWorld = worldOf(context, start);
|
|
386
359
|
// Where the handle is, from where the pointer is: the pointer carries this much of an offset the whole way.
|
|
387
|
-
|
|
360
|
+
const offset = { x: grabbed.x - pressWorld.x, y: grabbed.y - pressWorld.y };
|
|
388
361
|
engine.setCursor('grabbing');
|
|
389
362
|
engine.setArrowHandleHover(handle);
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
var pick = null;
|
|
363
|
+
let started = false;
|
|
364
|
+
let last = start;
|
|
365
|
+
const apply = (input) => {
|
|
366
|
+
const pointer = worldOf(context, input);
|
|
367
|
+
const target = { x: pointer.x + offset.x, y: pointer.y + offset.y };
|
|
368
|
+
let changed;
|
|
369
|
+
let pick = null;
|
|
398
370
|
if (handle === 'bend') {
|
|
399
371
|
changed = reshapeArrow(arrow, { bend: bendTowards(startPoints.start, startPoints.end, target) });
|
|
400
372
|
}
|
|
401
373
|
else {
|
|
402
|
-
|
|
403
|
-
|
|
374
|
+
const fixed = handle === 'start' ? startPoints.end : startPoints.start;
|
|
375
|
+
const moved = input.shiftKey ? snapPointAround(fixed, target, ARROW_SNAP_DEGREES) : target;
|
|
404
376
|
if (!input.altKey && !input.shiftKey) {
|
|
405
377
|
pick = pickArrowBinding(startScene, target, { zoom: engine.state.viewport.zoom, ignore: passedOver });
|
|
406
378
|
}
|
|
407
379
|
// The end is where the hand puts it; the engine draws a tied end from its shape (`reconcileArrowBindings`) in the same step.
|
|
408
|
-
|
|
380
|
+
const change = handle === 'start' ? { start: moved, startBinding: pick?.binding ?? null } : { end: moved, endBinding: pick?.binding ?? null };
|
|
409
381
|
changed = reshapeArrow(arrow, change);
|
|
410
382
|
}
|
|
411
383
|
engine.applyScene(withShapes(startScene, [changed]), 'transient');
|
|
412
|
-
engine.setOverlay({ arrowBind:
|
|
384
|
+
engine.setOverlay({ arrowBind: pick?.hint ?? null });
|
|
413
385
|
};
|
|
414
|
-
|
|
386
|
+
const finish = () => {
|
|
415
387
|
engine.setOverlay({ arrowBind: null });
|
|
416
388
|
engine.setArrowHandleHover(null);
|
|
417
389
|
engine.setCursor(cursorForTool(engine.state.tool));
|
|
@@ -419,7 +391,7 @@ export function createArrowHandleGesture(context, start, arrow, handle) {
|
|
|
419
391
|
return {
|
|
420
392
|
kind: 'arrow-handle',
|
|
421
393
|
pointerId: start.pointerId,
|
|
422
|
-
move
|
|
394
|
+
move(input) {
|
|
423
395
|
last = input;
|
|
424
396
|
if (!started) {
|
|
425
397
|
if (distanceBetween({ x: start.x, y: start.y }, { x: input.x, y: input.y }) < DRAG_THRESHOLD)
|
|
@@ -428,41 +400,41 @@ export function createArrowHandleGesture(context, start, arrow, handle) {
|
|
|
428
400
|
}
|
|
429
401
|
apply(input);
|
|
430
402
|
},
|
|
431
|
-
end
|
|
403
|
+
end() {
|
|
432
404
|
if (started)
|
|
433
405
|
engine.commit();
|
|
434
406
|
finish();
|
|
435
407
|
},
|
|
436
|
-
cancel
|
|
408
|
+
cancel() {
|
|
437
409
|
if (started)
|
|
438
410
|
engine.revertToCommitted();
|
|
439
411
|
finish();
|
|
440
412
|
},
|
|
441
|
-
modifiersChanged
|
|
413
|
+
modifiersChanged(modifiers) {
|
|
442
414
|
if (started)
|
|
443
|
-
apply(
|
|
415
|
+
apply({ ...last, ...modifiers });
|
|
444
416
|
}
|
|
445
417
|
};
|
|
446
418
|
}
|
|
447
419
|
// ──────────────────────────────────────────────────────────── marquee
|
|
448
420
|
/** What a marquee that touches nothing hands to the overlay: the one shared empty list. */
|
|
449
|
-
|
|
421
|
+
const NO_UNITS = Object.freeze([]);
|
|
450
422
|
/**
|
|
451
423
|
* Drags a rectangle over the board. While it is dragged, what it touches is outlined the way the unit under the pointer is
|
|
452
424
|
* (the overlay's `marqueeHover`), so the person sees what the release will take; the selection is made at the release, from
|
|
453
425
|
* the same units (added to the ones that were selected when Shift was held at the press).
|
|
454
426
|
*/
|
|
455
427
|
export function createMarqueeGesture(context, start) {
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
428
|
+
const { engine } = context;
|
|
429
|
+
const startWorld = worldOf(context, start);
|
|
430
|
+
const base = start.shiftKey ? [...engine.state.selectedIds] : [];
|
|
431
|
+
let started = false;
|
|
460
432
|
// 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
433
|
// marquee that grows over nothing new wakes nothing (see `erasingMarks.ts`).
|
|
462
|
-
|
|
434
|
+
let wouldSelect = NO_UNITS;
|
|
463
435
|
/** The selection a release at this rectangle would make: made as `select` makes it (a group once, no repeats). */
|
|
464
|
-
|
|
465
|
-
|
|
436
|
+
const selectionFor = (rect) => {
|
|
437
|
+
const units = normalizeSelection(engine.state.scene, [...base, ...engine.hitTestRect(rect, 'intersect')]);
|
|
466
438
|
if (units.length === 0)
|
|
467
439
|
return NO_UNITS;
|
|
468
440
|
return sameIds(units, wouldSelect) ? wouldSelect : units;
|
|
@@ -470,23 +442,23 @@ export function createMarqueeGesture(context, start) {
|
|
|
470
442
|
return {
|
|
471
443
|
kind: 'marquee',
|
|
472
444
|
pointerId: start.pointerId,
|
|
473
|
-
move
|
|
445
|
+
move(input) {
|
|
474
446
|
if (!started) {
|
|
475
447
|
if (distanceBetween({ x: start.x, y: start.y }, { x: input.x, y: input.y }) < DRAG_THRESHOLD)
|
|
476
448
|
return;
|
|
477
449
|
started = true;
|
|
478
450
|
}
|
|
479
|
-
|
|
451
|
+
const rect = rectFromPoints(startWorld, worldOf(context, input));
|
|
480
452
|
wouldSelect = selectionFor(rect);
|
|
481
453
|
engine.setOverlay({ marquee: rect, marqueeHover: wouldSelect });
|
|
482
454
|
},
|
|
483
|
-
end
|
|
455
|
+
end(input) {
|
|
484
456
|
// A press that never became a drag selects nothing (it cleared the selection when it began).
|
|
485
457
|
if (started)
|
|
486
458
|
engine.select(selectionFor(rectFromPoints(startWorld, worldOf(context, input))));
|
|
487
459
|
engine.setOverlay({ marquee: null, marqueeHover: NO_UNITS });
|
|
488
460
|
},
|
|
489
|
-
cancel
|
|
461
|
+
cancel() {
|
|
490
462
|
// Nothing was selected while it was dragged, so there is nothing to put back.
|
|
491
463
|
engine.setOverlay({ marquee: null, marqueeHover: NO_UNITS });
|
|
492
464
|
}
|
|
@@ -494,18 +466,17 @@ export function createMarqueeGesture(context, start) {
|
|
|
494
466
|
}
|
|
495
467
|
// ──────────────────────────────────────────────────────────── pan
|
|
496
468
|
/** 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;
|
|
469
|
+
export function createPanGesture(context, start, options = {}) {
|
|
470
|
+
const { engine } = context;
|
|
471
|
+
let lastX = start.x;
|
|
472
|
+
let lastY = start.y;
|
|
473
|
+
let moved = false;
|
|
503
474
|
engine.setCursor('grabbing');
|
|
504
|
-
|
|
475
|
+
const restore = () => engine.setCursor(cursorForTool(engine.state.tool));
|
|
505
476
|
return {
|
|
506
477
|
kind: 'pan',
|
|
507
478
|
pointerId: start.pointerId,
|
|
508
|
-
move
|
|
479
|
+
move(input) {
|
|
509
480
|
if (!moved && distanceBetween({ x: start.x, y: start.y }, { x: input.x, y: input.y }) >= DRAG_THRESHOLD)
|
|
510
481
|
moved = true;
|
|
511
482
|
if (!moved)
|
|
@@ -514,72 +485,67 @@ export function createPanGesture(context, start, options) {
|
|
|
514
485
|
lastX = input.x;
|
|
515
486
|
lastY = input.y;
|
|
516
487
|
},
|
|
517
|
-
end
|
|
518
|
-
var _a;
|
|
488
|
+
end() {
|
|
519
489
|
restore();
|
|
520
490
|
if (!moved)
|
|
521
|
-
|
|
491
|
+
options.onTap?.();
|
|
522
492
|
},
|
|
523
493
|
cancel: restore
|
|
524
494
|
};
|
|
525
495
|
}
|
|
526
496
|
// ──────────────────────────────────────────────────────────── ink
|
|
527
497
|
/** A pen whose pressure never moves by more than this has none to report, and draws as a mouse does. */
|
|
528
|
-
|
|
498
|
+
const PEN_PRESSURE_SPREAD = 0.02;
|
|
529
499
|
/** The pressure of a straight line (Shift): even, so that it is as wide at one end as at the other. */
|
|
530
|
-
|
|
500
|
+
const STRAIGHT_LINE_PRESSURE = 0.5;
|
|
531
501
|
/**
|
|
532
502
|
* 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
503
|
* hand spends (a slow stroke swells, a quick one thins). Shift draws a straight line from where the pointer went down.
|
|
534
504
|
*/
|
|
535
505
|
export function createDrawGesture(context, start) {
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
506
|
+
const { engine } = context;
|
|
507
|
+
const strokeId = engine.services.createId('obj');
|
|
508
|
+
const isPen = start.pointerType === 'pen';
|
|
509
|
+
const samples = [];
|
|
510
|
+
let leastPressure = Infinity;
|
|
511
|
+
let mostPressure = -Infinity;
|
|
512
|
+
let straight = start.shiftKey;
|
|
513
|
+
let lastScreen = { x: start.x, y: start.y };
|
|
514
|
+
let lastInput = start;
|
|
515
|
+
const takeSample = (input) => {
|
|
516
|
+
const world = worldOf(context, input);
|
|
547
517
|
// Only a pen's pressure means anything: a mouse and a finger report one fixed value while they are down.
|
|
548
518
|
if (isPen && typeof input.pressure === 'number') {
|
|
549
519
|
leastPressure = Math.min(leastPressure, input.pressure);
|
|
550
520
|
mostPressure = Math.max(mostPressure, input.pressure);
|
|
551
|
-
samples.push(
|
|
521
|
+
samples.push({ ...world, pressure: input.pressure });
|
|
552
522
|
}
|
|
553
523
|
else {
|
|
554
524
|
samples.push(world);
|
|
555
525
|
}
|
|
556
526
|
};
|
|
557
527
|
takeSample(start);
|
|
558
|
-
|
|
559
|
-
|
|
528
|
+
const readsPressure = () => isPen && mostPressure - leastPressure >= PEN_PRESSURE_SPREAD;
|
|
529
|
+
const isStraight = () => straight && samples.length > 1;
|
|
560
530
|
/** The line the hand drew: the samples, or just their two ends when Shift holds it straight. */
|
|
561
|
-
|
|
562
|
-
|
|
531
|
+
const lineDrawn = () => (isStraight() ? [samples[0], samples[samples.length - 1]] : samples);
|
|
532
|
+
const build = (finished) => {
|
|
563
533
|
// 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
|
-
})
|
|
534
|
+
const line = isStraight()
|
|
535
|
+
? lineDrawn().map(({ x, y }) => ({ x, y, pressure: STRAIGHT_LINE_PRESSURE }))
|
|
569
536
|
: samples;
|
|
570
|
-
return buildBrushStroke(line, { createId:
|
|
537
|
+
return buildBrushStroke(line, { createId: () => strokeId, pressure: isStraight() || readsPressure() ? 'pen' : 'speed', finished });
|
|
571
538
|
};
|
|
572
|
-
|
|
539
|
+
const showPreview = () => engine.setOverlay({ preview: build(false) });
|
|
573
540
|
showPreview();
|
|
574
541
|
return {
|
|
575
542
|
kind: 'draw',
|
|
576
543
|
pointerId: start.pointerId,
|
|
577
|
-
move
|
|
544
|
+
move(input) {
|
|
578
545
|
lastInput = input;
|
|
579
546
|
straight = input.shiftKey;
|
|
580
|
-
|
|
581
|
-
for (
|
|
582
|
-
var sample = moved_1[_i];
|
|
547
|
+
const moved = input.coalesced && input.coalesced.length > 0 ? input.coalesced : [input];
|
|
548
|
+
for (const sample of moved) {
|
|
583
549
|
// A new sample closer than a pixel to the last one adds nothing the eye can see.
|
|
584
550
|
if (Math.hypot(sample.x - lastScreen.x, sample.y - lastScreen.y) < 1)
|
|
585
551
|
continue;
|
|
@@ -588,22 +554,21 @@ export function createDrawGesture(context, start) {
|
|
|
588
554
|
}
|
|
589
555
|
showPreview();
|
|
590
556
|
},
|
|
591
|
-
end
|
|
592
|
-
var _a, _b;
|
|
557
|
+
end(input) {
|
|
593
558
|
straight = input.shiftKey;
|
|
594
|
-
|
|
559
|
+
const stroke = build(true);
|
|
595
560
|
engine.clearOverlay();
|
|
596
561
|
if (!stroke)
|
|
597
562
|
return;
|
|
598
563
|
engine.addObjects([stroke]);
|
|
599
|
-
|
|
564
|
+
context.options.onStrokeFinished?.(stroke, lineDrawn());
|
|
600
565
|
},
|
|
601
|
-
cancel
|
|
566
|
+
cancel() {
|
|
602
567
|
engine.clearOverlay();
|
|
603
568
|
},
|
|
604
|
-
modifiersChanged
|
|
569
|
+
modifiersChanged(modifiers) {
|
|
605
570
|
straight = modifiers.shiftKey;
|
|
606
|
-
lastInput =
|
|
571
|
+
lastInput = { ...lastInput, ...modifiers };
|
|
607
572
|
showPreview();
|
|
608
573
|
}
|
|
609
574
|
};
|
|
@@ -618,60 +583,57 @@ export function createDrawGesture(context, start) {
|
|
|
618
583
|
* default arrow, tied to nothing; the two ends are never tied to the same shape.
|
|
619
584
|
*/
|
|
620
585
|
export function createShapeGesture(context, start, kind, libraryKey) {
|
|
621
|
-
|
|
622
|
-
|
|
586
|
+
const { engine } = context;
|
|
587
|
+
const id = engine.services.createId('obj');
|
|
623
588
|
// 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
|
-
};
|
|
589
|
+
const colorSeed = engine.services.random();
|
|
590
|
+
const startWorld = worldOf(context, start);
|
|
591
|
+
let dragged = false;
|
|
592
|
+
let last = start;
|
|
593
|
+
const pickAt = (point, ignore) => pickArrowBinding(engine.state.scene, point, { zoom: engine.state.viewport.zoom, ignore });
|
|
631
594
|
// The shape the tail of an arrow would be tied to, from the moment of the press.
|
|
632
|
-
|
|
595
|
+
const tailPick = kind === 'arrow' && !start.altKey ? pickAt(startWorld) : null;
|
|
633
596
|
if (tailPick)
|
|
634
597
|
engine.setOverlay({ arrowBind: tailPick.hint });
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
kind: kind,
|
|
598
|
+
const build = (input) => {
|
|
599
|
+
const end = worldOf(context, input);
|
|
600
|
+
const fromCenter = input.altKey && kind !== 'arrow' && dragged;
|
|
601
|
+
const shape = buildDraggedShape({
|
|
602
|
+
kind,
|
|
641
603
|
start: fromCenter ? { x: 2 * startWorld.x - end.x, y: 2 * startWorld.y - end.y } : startWorld,
|
|
642
|
-
end
|
|
604
|
+
end,
|
|
643
605
|
constrain: input.shiftKey,
|
|
644
|
-
dragged
|
|
645
|
-
libraryKey
|
|
606
|
+
dragged,
|
|
607
|
+
libraryKey,
|
|
646
608
|
wireframe: engine.wireframeMode,
|
|
647
|
-
createId:
|
|
648
|
-
random:
|
|
609
|
+
createId: () => id,
|
|
610
|
+
random: () => colorSeed,
|
|
649
611
|
workspacePaint: engine.workspacePaintForNewShape(engine.wireframeMode)
|
|
650
612
|
});
|
|
651
613
|
if (kind !== 'arrow' || !dragged || input.altKey || shape.type !== 'path' || !isArrow(shape))
|
|
652
|
-
return { shape
|
|
653
|
-
|
|
654
|
-
|
|
614
|
+
return { shape, hint: null };
|
|
615
|
+
const tail = tailPick?.binding;
|
|
616
|
+
const headPick = input.shiftKey ? null : pickAt(end, tail ? new Set([tail.targetId]) : undefined);
|
|
655
617
|
if (!tail && !headPick)
|
|
656
|
-
return { shape
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
return { shape: tied, hint:
|
|
618
|
+
return { shape, hint: null };
|
|
619
|
+
const index = engine.sceneIndex();
|
|
620
|
+
const tied = positionBoundArrow(reshapeArrow(shape, { startBinding: tail ?? null, endBinding: headPick?.binding ?? null }), (targetId) => index.get(targetId));
|
|
621
|
+
return { shape: tied, hint: headPick?.hint ?? null };
|
|
660
622
|
};
|
|
661
623
|
return {
|
|
662
624
|
kind: 'shape',
|
|
663
625
|
pointerId: start.pointerId,
|
|
664
|
-
move
|
|
626
|
+
move(input) {
|
|
665
627
|
last = input;
|
|
666
628
|
if (!dragged && distanceBetween({ x: start.x, y: start.y }, { x: input.x, y: input.y }) >= MIN_DRAW_DISTANCE)
|
|
667
629
|
dragged = true;
|
|
668
630
|
if (dragged) {
|
|
669
|
-
|
|
631
|
+
const { shape, hint } = build(input);
|
|
670
632
|
engine.setOverlay({ preview: shape, arrowBind: hint });
|
|
671
633
|
}
|
|
672
634
|
},
|
|
673
|
-
end
|
|
674
|
-
|
|
635
|
+
end(input) {
|
|
636
|
+
const { shape } = build(input);
|
|
675
637
|
engine.clearOverlay();
|
|
676
638
|
engine.addObjects([shape]);
|
|
677
639
|
if (engine.state.stickyTool) {
|
|
@@ -682,13 +644,13 @@ export function createShapeGesture(context, start, kind, libraryKey) {
|
|
|
682
644
|
engine.select([shape.id]);
|
|
683
645
|
}
|
|
684
646
|
},
|
|
685
|
-
cancel
|
|
647
|
+
cancel() {
|
|
686
648
|
engine.clearOverlay();
|
|
687
649
|
},
|
|
688
|
-
modifiersChanged
|
|
650
|
+
modifiersChanged(modifiers) {
|
|
689
651
|
if (!dragged)
|
|
690
652
|
return;
|
|
691
|
-
|
|
653
|
+
const { shape, hint } = build({ ...last, ...modifiers });
|
|
692
654
|
engine.setOverlay({ preview: shape, arrowBind: hint });
|
|
693
655
|
}
|
|
694
656
|
};
|
|
@@ -696,35 +658,35 @@ export function createShapeGesture(context, start, kind, libraryKey) {
|
|
|
696
658
|
// ──────────────────────────────────────────────────────────── text
|
|
697
659
|
/** A click makes a label that grows with its text, a drag makes a box of that width. Then the text is edited. */
|
|
698
660
|
export function createTextGesture(context, start) {
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
661
|
+
const { engine } = context;
|
|
662
|
+
const startWorld = worldOf(context, start);
|
|
663
|
+
let dragged = false;
|
|
702
664
|
return {
|
|
703
665
|
kind: 'text',
|
|
704
666
|
pointerId: start.pointerId,
|
|
705
|
-
move
|
|
667
|
+
move(input) {
|
|
706
668
|
if (!dragged && distanceBetween({ x: start.x, y: start.y }, { x: input.x, y: input.y }) >= MIN_DRAW_DISTANCE)
|
|
707
669
|
dragged = true;
|
|
708
670
|
if (dragged)
|
|
709
671
|
engine.setOverlay({ textDraft: rectFromPoints(startWorld, worldOf(context, input)) });
|
|
710
672
|
},
|
|
711
|
-
end
|
|
673
|
+
end(input) {
|
|
712
674
|
engine.setOverlay({ textDraft: null });
|
|
713
675
|
if (!dragged) {
|
|
714
676
|
engine.createText(startWorld);
|
|
715
677
|
return;
|
|
716
678
|
}
|
|
717
|
-
|
|
679
|
+
const rect = rectFromPoints(startWorld, worldOf(context, input));
|
|
718
680
|
engine.createText({ x: rect.x, y: rect.y }, { width: rect.width });
|
|
719
681
|
},
|
|
720
|
-
cancel
|
|
682
|
+
cancel() {
|
|
721
683
|
engine.setOverlay({ textDraft: null });
|
|
722
684
|
}
|
|
723
685
|
};
|
|
724
686
|
}
|
|
725
687
|
// ──────────────────────────────────────────────────────────── eraser
|
|
726
688
|
/** The most spots along one sweep of the eraser that are tested for shapes (a flick across a huge board stays affordable). */
|
|
727
|
-
|
|
689
|
+
const ERASER_MAX_STEPS_PER_SWEEP = 400;
|
|
728
690
|
/**
|
|
729
691
|
* Marks what the pointer passes over and removes it when the pointer is released.
|
|
730
692
|
*
|
|
@@ -736,48 +698,44 @@ var ERASER_MAX_STEPS_PER_SWEEP = 400;
|
|
|
736
698
|
* 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
699
|
*/
|
|
738
700
|
export function createEraseGesture(context, start) {
|
|
739
|
-
|
|
701
|
+
const { engine } = context;
|
|
740
702
|
// The scene stays as it was at the press: nothing is removed until the release.
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
703
|
+
const scene = engine.state.scene;
|
|
704
|
+
const index = engine.sceneIndex(scene);
|
|
705
|
+
const startWorld = worldOf(context, start);
|
|
706
|
+
const zoom = () => engine.state.viewport.zoom;
|
|
707
|
+
const marked = [];
|
|
746
708
|
// 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];
|
|
709
|
+
const skipped = new Set();
|
|
710
|
+
for (const object of scene) {
|
|
750
711
|
if (object.type === 'widget' || engine.state.pending[object.id])
|
|
751
712
|
skipped.add(object.id);
|
|
752
713
|
}
|
|
753
714
|
// 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];
|
|
715
|
+
const enteredGroups = new Set();
|
|
716
|
+
for (const object of scene) {
|
|
757
717
|
if (object.type !== 'group')
|
|
758
718
|
continue;
|
|
759
|
-
|
|
719
|
+
const box = boundsOfObjects(scene, [object.id]);
|
|
760
720
|
if (box && rectContainsPoint(box, startWorld))
|
|
761
721
|
enteredGroups.add(object.id);
|
|
762
722
|
}
|
|
763
723
|
/** 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();
|
|
724
|
+
const unitOf = (shapeId) => {
|
|
725
|
+
let unit = index.get(shapeId);
|
|
726
|
+
const visited = new Set();
|
|
768
727
|
while (unit && unit.parentId !== null && !visited.has(unit.id)) {
|
|
769
728
|
visited.add(unit.id);
|
|
770
|
-
|
|
729
|
+
const parent = index.get(unit.parentId);
|
|
771
730
|
if (!parent || enteredGroups.has(parent.id))
|
|
772
731
|
break;
|
|
773
732
|
unit = parent;
|
|
774
733
|
}
|
|
775
|
-
return
|
|
734
|
+
return unit?.id ?? shapeId;
|
|
776
735
|
};
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
for (
|
|
780
|
-
var shape = _a[_i];
|
|
736
|
+
let marksChanged = false;
|
|
737
|
+
const markUnitOf = (shapeId) => {
|
|
738
|
+
for (const shape of leafShapesOf(scene, [unitOf(shapeId)])) {
|
|
781
739
|
if (skipped.has(shape.id))
|
|
782
740
|
continue;
|
|
783
741
|
skipped.add(shape.id);
|
|
@@ -786,10 +744,9 @@ export function createEraseGesture(context, start) {
|
|
|
786
744
|
}
|
|
787
745
|
};
|
|
788
746
|
/** 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 });
|
|
747
|
+
const markAt = (world, onlyTop = false) => {
|
|
748
|
+
for (let guard = 0; guard < 64; guard += 1) {
|
|
749
|
+
const hit = hitTestLeaf(scene, world, { zoom: zoom(), tolerancePixels: ERASER_RADIUS, ignore: skipped });
|
|
793
750
|
if (!hit)
|
|
794
751
|
break;
|
|
795
752
|
markUnitOf(hit.id);
|
|
@@ -799,18 +756,18 @@ export function createEraseGesture(context, start) {
|
|
|
799
756
|
break;
|
|
800
757
|
}
|
|
801
758
|
};
|
|
802
|
-
|
|
759
|
+
let lastWorld = startWorld;
|
|
803
760
|
/** Marks what lies along the way from where the eraser was to where it is. */
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
for (
|
|
808
|
-
|
|
761
|
+
const sweepTo = (to) => {
|
|
762
|
+
const screenLength = distanceBetween(lastWorld, to) * zoom();
|
|
763
|
+
const steps = Math.min(ERASER_MAX_STEPS_PER_SWEEP, Math.max(1, Math.ceil(screenLength / (ERASER_RADIUS / 2))));
|
|
764
|
+
for (let step = 1; step <= steps; step += 1) {
|
|
765
|
+
const progress = step / steps;
|
|
809
766
|
markAt({ x: lastWorld.x + (to.x - lastWorld.x) * progress, y: lastWorld.y + (to.y - lastWorld.y) * progress });
|
|
810
767
|
}
|
|
811
768
|
lastWorld = to;
|
|
812
769
|
};
|
|
813
|
-
|
|
770
|
+
const showMarks = () => {
|
|
814
771
|
if (!marksChanged)
|
|
815
772
|
return;
|
|
816
773
|
marksChanged = false;
|
|
@@ -819,8 +776,8 @@ export function createEraseGesture(context, start) {
|
|
|
819
776
|
// The press itself marks what is under it, so a click shows what it is about to remove.
|
|
820
777
|
markAt(startWorld, start.ctrlKey || start.metaKey);
|
|
821
778
|
showMarks();
|
|
822
|
-
|
|
823
|
-
|
|
779
|
+
let dragging = false;
|
|
780
|
+
const beginDragging = () => {
|
|
824
781
|
dragging = true;
|
|
825
782
|
engine.beginScribble();
|
|
826
783
|
// A drag takes everything under the spot where it began, also after a click that took only the top shape.
|
|
@@ -829,22 +786,21 @@ export function createEraseGesture(context, start) {
|
|
|
829
786
|
return {
|
|
830
787
|
kind: 'erase',
|
|
831
788
|
pointerId: start.pointerId,
|
|
832
|
-
move
|
|
789
|
+
move(input) {
|
|
833
790
|
if (!dragging) {
|
|
834
791
|
if (distanceBetween({ x: start.x, y: start.y }, { x: input.x, y: input.y }) < DRAG_THRESHOLD)
|
|
835
792
|
return;
|
|
836
793
|
beginDragging();
|
|
837
794
|
}
|
|
838
|
-
|
|
839
|
-
for (
|
|
840
|
-
|
|
841
|
-
var world = worldOf(context, sample);
|
|
795
|
+
const samples = input.coalesced && input.coalesced.length > 0 ? input.coalesced : [input];
|
|
796
|
+
for (const sample of samples) {
|
|
797
|
+
const world = worldOf(context, sample);
|
|
842
798
|
sweepTo(world);
|
|
843
799
|
engine.extendScribble(world);
|
|
844
800
|
}
|
|
845
801
|
showMarks();
|
|
846
802
|
},
|
|
847
|
-
end
|
|
803
|
+
end(input) {
|
|
848
804
|
// The release can come with a last position no move reported.
|
|
849
805
|
if (dragging)
|
|
850
806
|
sweepTo(worldOf(context, input));
|
|
@@ -853,7 +809,7 @@ export function createEraseGesture(context, start) {
|
|
|
853
809
|
if (marked.length > 0)
|
|
854
810
|
engine.removeObjects(marked);
|
|
855
811
|
},
|
|
856
|
-
cancel
|
|
812
|
+
cancel() {
|
|
857
813
|
engine.stopScribble();
|
|
858
814
|
engine.clearOverlay();
|
|
859
815
|
}
|