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