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