@widgetic/canvas 0.5.40 → 0.5.41

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