@widgetic/canvas 0.5.40 → 0.5.42

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) 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/host/CanvasHost.d.ts +6 -0
  5. package/dist/canvas/host/CanvasHost.js +39 -0
  6. package/dist/canvas/host/CanvasHostApi.d.ts +2 -2
  7. package/dist/canvas/host/CanvasHostApi.js +7 -1
  8. package/dist/canvas/ui/CanvasHint.svelte +16 -3
  9. package/dist/canvas/ui/CanvasHint.svelte.d.ts +2 -17
  10. package/dist/canvas/ui/CanvasToolbar.svelte +7 -7
  11. package/dist/canvas/ui/ToolButton.svelte +11 -3
  12. package/dist/canvas/ui/ToolButton.svelte.d.ts +2 -0
  13. package/dist/canvas/ui/sizeLabelVisibility.d.ts +1 -1
  14. package/dist/canvas/ui/sizeLabelVisibility.js +1 -1
  15. package/dist/canvas/view/SelectionOverlay.svelte +33 -14
  16. package/dist/canvas/view/geometry.d.ts +14 -4
  17. package/dist/canvas/view/geometry.js +14 -7
  18. package/dist/engine/EngineCore.d.ts +4 -1
  19. package/dist/engine/EngineCore.js +11 -4
  20. package/dist/engine/codec/canvasDocument.js +42 -6
  21. package/dist/engine/core/arrow.d.ts +10 -3
  22. package/dist/engine/core/arrow.js +10 -2
  23. package/dist/engine/core/arrowBinding.d.ts +69 -0
  24. package/dist/engine/core/arrowBinding.js +326 -0
  25. package/dist/engine/core/arrowBindingRecords.d.ts +43 -0
  26. package/dist/engine/core/arrowBindingRecords.js +126 -0
  27. package/dist/engine/core/hitTest.d.ts +3 -0
  28. package/dist/engine/core/hitTest.js +2 -1
  29. package/dist/engine/core/scene.d.ts +3 -0
  30. package/dist/engine/core/scene.js +7 -2
  31. package/dist/engine/core/types.d.ts +29 -0
  32. package/dist/engine/input/PointerController.js +23 -1
  33. package/dist/engine/input/gestures.d.ts +12 -1
  34. package/dist/engine/input/gestures.js +56 -11
  35. package/dist/engine/input/keyboard.d.ts +9 -1
  36. package/dist/engine/input/keyboard.js +18 -2
  37. package/dist/engine/state.d.ts +6 -1
  38. package/dist/engine/state.js +2 -1
  39. package/dist/i18n/shortcuts.d.ts +12 -0
  40. package/dist/i18n/shortcuts.js +19 -5
  41. package/dist/tools/components/catalog.d.ts +11 -0
  42. package/dist/tools/components/catalog.js +260 -0
  43. package/dist/tools/components/grouping.d.ts +9 -0
  44. package/dist/tools/components/grouping.js +11 -0
  45. package/dist/tools/definitions.d.ts +19 -0
  46. package/dist/tools/definitions.js +147 -0
  47. package/dist/tools/executors.d.ts +7 -0
  48. package/dist/tools/executors.js +288 -0
  49. package/dist/tools/hostAdapter.d.ts +14 -0
  50. package/dist/tools/hostAdapter.js +38 -0
  51. package/dist/tools/index.d.ts +6 -0
  52. package/dist/tools/index.js +6 -0
  53. package/dist/tools/sceneSummary.d.ts +24 -0
  54. package/dist/tools/sceneSummary.js +156 -0
  55. package/dist/tools/sketchIntent.d.ts +17 -0
  56. package/dist/tools/sketchIntent.js +158 -0
  57. package/dist/tools/types.d.ts +54 -0
  58. package/dist/tools/types.js +11 -0
  59. package/package.json +7 -2
@@ -0,0 +1,126 @@
1
+ /**
2
+ * The ties of arrows as plain records: reading one, setting or clearing one, cutting the ones that must not survive an edit,
3
+ * and pointing the ones of a copy at the copies. No geometry here and no import of anything but types, so `scene.ts` may use it
4
+ * (the geometry of ties, which reads hit tests and outlines, is in `arrowBinding.ts`).
5
+ *
6
+ * A tie is `ArrowBinding` (see `types.ts`): an end of an arrow that follows a shape. A free end has no tie, and no key either:
7
+ * every function that makes a spec writes `startBinding` and `endBinding` only while they are set, so an arrow that is tied to
8
+ * nothing is the very same record it was before ties existed.
9
+ */
10
+ /** The two ends of an arrow, the tail first. */
11
+ export const ARROW_ENDS = Object.freeze(['start', 'end']);
12
+ /** The tie of one end of an arrow, or null while that end is free. */
13
+ export function bindingOfEnd(spec, end) {
14
+ return (end === 'start' ? spec.startBinding : spec.endBinding) ?? null;
15
+ }
16
+ /** Whether either end of an arrow is tied. */
17
+ export function hasBindings(spec) {
18
+ return !!spec.startBinding || !!spec.endBinding;
19
+ }
20
+ /** Whether two ties say the same thing (an anchor is the same when it is the same to a billionth of the box). */
21
+ export function sameBinding(first, second) {
22
+ if (!first || !second)
23
+ return !first && !second;
24
+ if (first.targetId !== second.targetId || first.kind !== second.kind)
25
+ return false;
26
+ if (first.kind === 'edge' && second.kind === 'edge') {
27
+ return Math.abs(first.anchor.x - second.anchor.x) < 1e-9 && Math.abs(first.anchor.y - second.anchor.y) < 1e-9;
28
+ }
29
+ return true;
30
+ }
31
+ /** The spec with its ties changed. The same spec when the change says what it says already. */
32
+ export function withBindings(spec, change) {
33
+ const start = 'startBinding' in change ? (change.startBinding ?? null) : (spec.startBinding ?? null);
34
+ const end = 'endBinding' in change ? (change.endBinding ?? null) : (spec.endBinding ?? null);
35
+ if (sameBinding(start, spec.startBinding) && sameBinding(end, spec.endBinding))
36
+ return spec;
37
+ return { start: spec.start, end: spec.end, bend: spec.bend, ...(start ? { startBinding: start } : {}), ...(end ? { endBinding: end } : {}) };
38
+ }
39
+ /** Whether an arrow can be tied to this object: a shape that has an outline, and is not itself a line (an arrow, a brush stroke) or a group. */
40
+ export function canBeTiedTo(object) {
41
+ switch (object.type) {
42
+ case 'rect':
43
+ case 'ellipse':
44
+ case 'text':
45
+ case 'image':
46
+ case 'widget':
47
+ return true;
48
+ case 'path':
49
+ return object.arrow === null && object.ink === null;
50
+ default:
51
+ return false;
52
+ }
53
+ }
54
+ /** An arrow, as far as these records are concerned: a path with the points it is drawn from. */
55
+ function arrowSpecOf(object) {
56
+ return object.type === 'path' && object.arrow ? object.arrow : null;
57
+ }
58
+ /** The same arrow with its spec replaced, or the very same object when the spec did not change. */
59
+ function withSpec(object, spec) {
60
+ return spec === object.arrow ? object : { ...object, arrow: spec };
61
+ }
62
+ /**
63
+ * An arrow that is carried, stretched or turned together with some shapes: the ties to shapes that are not among them are cut (an
64
+ * arrow that is moved by itself comes off the shapes it was tied to, the way it does in tldraw). The same object when nothing is cut.
65
+ */
66
+ export function releaseUnmovedEnds(object, moving) {
67
+ const spec = arrowSpecOf(object);
68
+ if (!spec || !hasBindings(spec))
69
+ return object;
70
+ const change = {};
71
+ if (spec.startBinding && !moving.has(spec.startBinding.targetId))
72
+ change.startBinding = null;
73
+ if (spec.endBinding && !moving.has(spec.endBinding.targetId))
74
+ change.endBinding = null;
75
+ return withSpec(object, withBindings(spec, change));
76
+ }
77
+ /**
78
+ * The ties of a copy of an arrow: a tie to a shape that was copied too points at its copy, any other is cut (a copy of an arrow
79
+ * alone is a free arrow). `idMap` says which id was copied to which.
80
+ */
81
+ export function remapEnds(object, idMap) {
82
+ const spec = arrowSpecOf(object);
83
+ if (!spec || !hasBindings(spec))
84
+ return object;
85
+ const remap = (binding) => {
86
+ const copyId = binding ? idMap.get(binding.targetId) : undefined;
87
+ return binding && copyId ? { ...binding, targetId: copyId } : null;
88
+ };
89
+ return withSpec(object, withBindings(spec, { startBinding: remap(spec.startBinding), endBinding: remap(spec.endBinding) }));
90
+ }
91
+ /**
92
+ * A scene in which every tie points at something an arrow can be tied to: a tie whose shape is not on the board (a board that was
93
+ * edited by hand, a shape deleted by another tool), is not a shape with an outline, or is the arrow itself is cut. The very same
94
+ * array when nothing is cut.
95
+ */
96
+ export function dropDanglingBindings(scene) {
97
+ if (!scene.some((object) => {
98
+ const spec = arrowSpecOf(object);
99
+ return !!spec && hasBindings(spec);
100
+ })) {
101
+ return scene;
102
+ }
103
+ const index = new Map();
104
+ for (const object of scene)
105
+ index.set(object.id, object);
106
+ let changed = false;
107
+ const next = scene.map((object) => {
108
+ const spec = arrowSpecOf(object);
109
+ if (!spec || !hasBindings(spec))
110
+ return object;
111
+ const holds = (binding) => {
112
+ const target = binding ? index.get(binding.targetId) : undefined;
113
+ return !!binding && !!target && target.id !== object.id && canBeTiedTo(target);
114
+ };
115
+ const change = {};
116
+ if (spec.startBinding && !holds(spec.startBinding))
117
+ change.startBinding = null;
118
+ if (spec.endBinding && !holds(spec.endBinding))
119
+ change.endBinding = null;
120
+ const cut = withSpec(object, withBindings(spec, change));
121
+ if (cut !== object)
122
+ changed = true;
123
+ return cut;
124
+ });
125
+ return changed ? next : scene;
126
+ }
@@ -10,6 +10,7 @@
10
10
  * - A group answers as a whole: the hit is the group nearest to the board, unless the person has entered a
11
11
  * group, whose members are then picked one by one.
12
12
  */
13
+ import type { FlatSubpath } from './pathData.js';
13
14
  import type { Scene } from './scene.js';
14
15
  import type { ObjectId, Point, Rect, SceneObject, ShapeObject } from './types.js';
15
16
  export interface HitTestOptions {
@@ -28,6 +29,8 @@ export interface MarqueeOptions extends HitTestOptions {
28
29
  /** `intersect` takes what the marquee touches, `contain` only what it holds entirely. */
29
30
  mode: 'intersect' | 'contain';
30
31
  }
32
+ /** The outline of a path flattened to straight pieces, in the coordinates of its box (the same pieces for as long as the same data is asked for). */
33
+ export declare function flattenedOutline(data: string): FlatSubpath[];
31
34
  /** Whether a world point is on a shape (inside it, or near its outline, depending on the kind of shape). */
32
35
  export declare function shapeContainsPoint(shape: ShapeObject, worldPoint: Point, toleranceWorld: number): boolean;
33
36
  /** The topmost selectable object under a world point, or null. */
@@ -20,7 +20,8 @@ const DEFAULT_TOLERANCE_PIXELS = 5;
20
20
  const FLATTENED_PATH_CACHE_LIMIT = 400;
21
21
  /** Flattened outlines by path data, so a hover over a busy board does not parse the same strokes again. */
22
22
  const flattenedPathCache = new Map();
23
- function flattenedOutline(data) {
23
+ /** The outline of a path flattened to straight pieces, in the coordinates of its box (the same pieces for as long as the same data is asked for). */
24
+ export function flattenedOutline(data) {
24
25
  const cached = flattenedPathCache.get(data);
25
26
  if (cached)
26
27
  return cached;
@@ -62,6 +62,9 @@ export declare function movingShapeIds(scene: Scene, ids: Iterable<ObjectId>): S
62
62
  /**
63
63
  * Moves the shapes with the given ids by a distance. A drag carries the same shapes at every step: it asks `movingShapeIds`
64
64
  * once and calls this at every move, so no step walks the groups of the board again to find out who is carried.
65
+ *
66
+ * An arrow that is carried comes off the shapes it is tied to, unless they are carried with it (an arrow moved by itself is let go,
67
+ * the way it is in tldraw); the arrows of the shapes that are carried while they stay follow them, in `reconcileArrowBindings`.
65
68
  */
66
69
  export declare function translateShapes(scene: Scene, moving: ReadonlySet<ObjectId>, deltaX: number, deltaY: number): Scene;
67
70
  /** Moves the shapes drawn by the given objects by a distance. */
@@ -9,6 +9,7 @@
9
9
  * which keeps one rule true everywhere: a group comes first, directly followed by its members, and the
10
10
  * paint order of the board is the order of the list.
11
11
  */
12
+ import { releaseUnmovedEnds, remapEnds } from './arrowBindingRecords.js';
12
13
  import { brushOverflow, isBrushStroke } from './brush.js';
13
14
  import { rotatedBoxBounds, unionRects, inflateRect } from './geometry.js';
14
15
  import { createGroupObject, createObjectId } from './defaults.js';
@@ -290,12 +291,15 @@ export function movingShapeIds(scene, ids) {
290
291
  /**
291
292
  * Moves the shapes with the given ids by a distance. A drag carries the same shapes at every step: it asks `movingShapeIds`
292
293
  * once and calls this at every move, so no step walks the groups of the board again to find out who is carried.
294
+ *
295
+ * An arrow that is carried comes off the shapes it is tied to, unless they are carried with it (an arrow moved by itself is let go,
296
+ * the way it is in tldraw); the arrows of the shapes that are carried while they stay follow them, in `reconcileArrowBindings`.
293
297
  */
294
298
  export function translateShapes(scene, moving, deltaX, deltaY) {
295
299
  if ((deltaX === 0 && deltaY === 0) || moving.size === 0)
296
300
  return scene;
297
301
  return scene.map((object) => moving.has(object.id) && isBoxedObject(object)
298
- ? { ...object, x: object.x + deltaX, y: object.y + deltaY }
302
+ ? releaseUnmovedEnds({ ...object, x: object.x + deltaX, y: object.y + deltaY }, moving)
299
303
  : object);
300
304
  }
301
305
  /** Moves the shapes drawn by the given objects by a distance. */
@@ -446,5 +450,6 @@ export function duplicateObjects(scene, ids, offset = { x: 0, y: 0 }, makeId = (
446
450
  }
447
451
  objects.push(copy);
448
452
  }
449
- return { objects, idMap };
453
+ // An arrow that was tied to a shape that is copied with it is tied to the copy; one whose shape stays behind is let go.
454
+ return { objects: objects.map((copy) => remapEnds(copy, idMap)), idMap };
450
455
  }
@@ -99,6 +99,35 @@ export interface ArrowSpec {
99
99
  readonly start: Point;
100
100
  readonly end: Point;
101
101
  readonly bend: number;
102
+ /** What the start is tied to: set only while it is tied, so a free arrow has neither key. */
103
+ readonly startBinding?: ArrowBinding;
104
+ /** What the end (the head) is tied to: set only while it is tied. */
105
+ readonly endBinding?: ArrowBinding;
106
+ }
107
+ /**
108
+ * What one end of an arrow is tied to (the way tldraw ties its arrows): the end follows the shape when it is moved, stretched, turned
109
+ * or, for a text, when it grows. Either the end aims at the middle of the shape and rests where the line to the other end meets
110
+ * its outline (`center`), or it rests on one point of the outline (`edge`), given as a fraction of the shape's own box (0 to 1 across,
111
+ * 0 to 1 down, before the shape is turned) so that the point goes where the shape goes.
112
+ */
113
+ export type ArrowBinding = {
114
+ readonly targetId: ObjectId;
115
+ readonly kind: 'center';
116
+ } | {
117
+ readonly targetId: ObjectId;
118
+ readonly kind: 'edge';
119
+ readonly anchor: Point;
120
+ };
121
+ /** Which end of an arrow: the tail it starts from, or the end with the head. */
122
+ export type ArrowEnd = 'start' | 'end';
123
+ /**
124
+ * What the board shows while an end of an arrow is dragged over a shape it would be tied to if it were let go now: the shape, and
125
+ * the point (in the world) where the end would rest: a point of its outline for `edge`, its middle for `center`.
126
+ */
127
+ export interface ArrowBindHint {
128
+ readonly targetId: ObjectId;
129
+ readonly kind: ArrowBinding['kind'];
130
+ readonly point: Point;
102
131
  }
103
132
  /**
104
133
  * Anything drawn as an SVG path: freehand ink, arrows, triangles and the shapes of the library.
@@ -6,6 +6,7 @@
6
6
  * the release. Two fingers make a pinch, the wheel pans and zooms, and a double click enters a group or edits a
7
7
  * text. Nothing here touches the DOM, so every rule is tested with simulated pointers.
8
8
  */
9
+ import { pickArrowBinding } from '../core/arrowBinding.js';
9
10
  import { hitTestLeaf } from '../core/hitTest.js';
10
11
  import { clampNumber, distanceBetween } from '../core/geometry.js';
11
12
  import { leafShapesOf } from '../core/scene.js';
@@ -374,11 +375,15 @@ export class PointerController {
374
375
  #hover(input) {
375
376
  const engine = this.#engine;
376
377
  const state = engine.state;
377
- if (this.#spaceHeld || state.tool !== 'select')
378
+ if (this.#spaceHeld || (state.tool !== 'select' && state.tool !== 'arrow'))
378
379
  return;
379
380
  if (this.#lastHover && Math.abs(this.#lastHover.x - input.x) < 1 && Math.abs(this.#lastHover.y - input.y) < 1)
380
381
  return;
381
382
  this.#lastHover = { x: input.x, y: input.y };
383
+ if (state.tool === 'arrow') {
384
+ this.#hoverArrowTarget(input);
385
+ return;
386
+ }
382
387
  const screenPoint = { x: input.x, y: input.y };
383
388
  let overHandle = false;
384
389
  let overDot = false;
@@ -428,6 +433,21 @@ export class PointerController {
428
433
  engine.setCursor(cursor);
429
434
  this.#hoverWidgetCard(overHandle ? null : hitTestLeaf(state.scene, world, { zoom: state.viewport.zoom }));
430
435
  }
436
+ /**
437
+ * The Arrow tool, nothing pressed: over a shape, the board outlines it and puts a dot where the tail of an arrow pressed here would
438
+ * rest (Alt held: the arrow would be tied to nothing, so nothing is shown).
439
+ */
440
+ #hoverArrowTarget(input) {
441
+ const engine = this.#engine;
442
+ const world = engine.toWorld({ x: input.x, y: input.y });
443
+ const hint = input.altKey ? null : (pickArrowBinding(engine.state.scene, world, { zoom: engine.state.viewport.zoom })?.hint ?? null);
444
+ const shown = engine.state.overlay.arrowBind;
445
+ const same = hint === null
446
+ ? shown === null
447
+ : shown !== null && shown.targetId === hint.targetId && shown.kind === hint.kind && shown.point.x === hint.point.x && shown.point.y === hint.point.y;
448
+ if (!same)
449
+ engine.setOverlay({ arrowBind: hint });
450
+ }
431
451
  /**
432
452
  * The host draws the bar of a live widget over its card while the pointer is on it, and the card is picked
433
453
  * just by pointing at it (the bar and the widget inside take the pointer from there).
@@ -450,6 +470,8 @@ export class PointerController {
450
470
  this.#engine.setHover(null);
451
471
  this.#engine.setRotationDotHover(false);
452
472
  this.#engine.setArrowHandleHover(null);
473
+ if (this.#engine.state.overlay.arrowBind)
474
+ this.#engine.setOverlay({ arrowBind: null });
453
475
  }
454
476
  }
455
477
  // ──────────────────────────────────────────────────────── release
@@ -45,6 +45,10 @@ export declare function createRotateGesture(context: GestureContext, start: Poin
45
45
  * end. The middle bends the arrow: its handle is moved only across the arrow (the pointer's distance from the line between the
46
46
  * 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
47
47
  * changes until the pointer has gone `DRAG_THRESHOLD` pixels, the whole drag is one undo step, and Escape puts the arrow back.
48
+ *
49
+ * 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
50
+ * 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),
51
+ * 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.
48
52
  */
49
53
  export declare function createArrowHandleGesture(context: GestureContext, start: PointerInput, arrow: ArrowObject, handle: ArrowHandleId): Gesture;
50
54
  /**
@@ -62,7 +66,14 @@ export declare function createPanGesture(context: GestureContext, start: Pointer
62
66
  * hand spends (a slow stroke swells, a quick one thins). Shift draws a straight line from where the pointer went down.
63
67
  */
64
68
  export declare function createDrawGesture(context: GestureContext, start: PointerInput): Gesture;
65
- /** Drags a shape out. A click places it at its default size. Shift constrains it; Alt draws it from the middle. */
69
+ /**
70
+ * Drags a shape out. A click places it at its default size. Shift constrains it; Alt draws it from the middle.
71
+ *
72
+ * 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
73
+ * 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
74
+ * 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
75
+ * default arrow, tied to nothing; the two ends are never tied to the same shape.
76
+ */
66
77
  export declare function createShapeGesture(context: GestureContext, start: PointerInput, kind: DragShapeKind, libraryKey: string | null): Gesture;
67
78
  /** A click makes a label that grows with its text, a drag makes a box of that width. Then the text is edited. */
68
79
  export declare function createTextGesture(context: GestureContext, start: PointerInput): Gesture;
@@ -4,7 +4,9 @@
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
+ import { arrowPointsInWorld, bendTowards, isArrow, reshapeArrow } from '../core/arrow.js';
8
+ import { pickArrowBinding, positionBoundArrow } from '../core/arrowBinding.js';
9
+ import { bindingOfEnd, releaseUnmovedEnds } from '../core/arrowBindingRecords.js';
8
10
  import { buildBrushStroke } from '../core/brush.js';
9
11
  import { ARROW_SNAP_DEGREES, MIN_DRAW_DISTANCE, buildDraggedShape, snapPointAround } from '../core/shapeTools.js';
10
12
  import { distanceBetween, inflateRect, rectContainsPoint, rectFromPoints, rectsIntersect, unionRects } from '../core/geometry.js';
@@ -200,6 +202,8 @@ export function createResizeGesture(context, start, handle, frame, shapes) {
200
202
  const startScene = engine.state.scene;
201
203
  const corner = isCornerHandle(handle);
202
204
  const single = shapes.length === 1 ? shapes[0] : null;
205
+ // An arrow stretched with these shapes comes off the shapes it is tied to that are not stretched with it.
206
+ const stretchedIds = new Set(shapes.map((shape) => shape.id));
203
207
  // A circle, an icon, a picture and a text keep their proportions when a corner is dragged.
204
208
  const keepsAspect = !!single &&
205
209
  (((single.type === 'path' || single.type === 'ellipse') && single.aspectLocked) ||
@@ -225,7 +229,7 @@ export function createResizeGesture(context, start, handle, frame, shapes) {
225
229
  keepAspect: input.shiftKey || keepsAspect,
226
230
  allowFlip: true
227
231
  });
228
- const next = fitAll(context, applyFrameResize(shapes, frame, resized, corner));
232
+ const next = fitAll(context, applyFrameResize(shapes, frame, resized, corner)).map((shape) => releaseUnmovedEnds(shape, stretchedIds));
229
233
  engine.applyScene(withShapes(startScene, next), 'transient');
230
234
  const after = selectionFrameOf(next);
231
235
  engine.setOverlay({
@@ -281,6 +285,8 @@ export function createRotateGesture(context, start, frame, shapes, source = 'cor
281
285
  const center = frameCenter(frame);
282
286
  const startAngle = angleFromCenter(center, worldOf(context, start));
283
287
  const single = shapes.length === 1 ? shapes[0] : null;
288
+ // An arrow turned with these shapes comes off the shapes it is tied to that are not turned with it.
289
+ const turnedIds = new Set(shapes.map((shape) => shape.id));
284
290
  engine.setCursor(source === 'dot' ? 'grabbing' : ROTATE_CURSOR);
285
291
  let started = false;
286
292
  let last = start;
@@ -291,7 +297,7 @@ export function createRotateGesture(context, start, frame, shapes, source = 'cor
291
297
  ? snapAngle(single.rotation + delta, ROTATION_SNAP_DEGREES) - single.rotation
292
298
  : snapAngle(delta, ROTATION_SNAP_DEGREES);
293
299
  }
294
- const turned = rotateShapesAbout(shapes, center, delta);
300
+ const turned = rotateShapesAbout(shapes, center, delta).map((shape) => releaseUnmovedEnds(shape, turnedIds));
295
301
  engine.applyScene(withShapes(startScene, turned), 'transient');
296
302
  const shown = single ? normalizeDegrees(single.rotation + delta) : normalizeDegrees(delta);
297
303
  engine.setOverlay({ dragging: 'rotate', readout: { kind: 'angle', degrees: Math.round(shown) % 360 } });
@@ -335,11 +341,17 @@ export function createRotateGesture(context, start, frame, shapes, source = 'cor
335
341
  * end. The middle bends the arrow: its handle is moved only across the arrow (the pointer's distance from the line between the
336
342
  * 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
343
  * changes until the pointer has gone `DRAG_THRESHOLD` pixels, the whole drag is one undo step, and Escape puts the arrow back.
344
+ *
345
+ * 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
346
+ * 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),
347
+ * ties it to nothing. The shape the other end is tied to is passed over: the two ends of an arrow are never tied to the same shape.
338
348
  */
339
349
  export function createArrowHandleGesture(context, start, arrow, handle) {
340
350
  const { engine } = context;
341
351
  const startScene = engine.state.scene;
342
352
  const startPoints = arrowPointsInWorld(arrow);
353
+ const otherEndBinding = handle === 'start' ? bindingOfEnd(arrow.arrow, 'end') : handle === 'end' ? bindingOfEnd(arrow.arrow, 'start') : null;
354
+ const passedOver = otherEndBinding ? new Set([otherEndBinding.targetId]) : undefined;
343
355
  const grabbed = handle === 'start' ? startPoints.start : handle === 'end' ? startPoints.end : startPoints.middle;
344
356
  const pressWorld = worldOf(context, start);
345
357
  // Where the handle is, from where the pointer is: the pointer carries this much of an offset the whole way.
@@ -352,17 +364,25 @@ export function createArrowHandleGesture(context, start, arrow, handle) {
352
364
  const pointer = worldOf(context, input);
353
365
  const target = { x: pointer.x + offset.x, y: pointer.y + offset.y };
354
366
  let changed;
367
+ let pick = null;
355
368
  if (handle === 'bend') {
356
369
  changed = reshapeArrow(arrow, { bend: bendTowards(startPoints.start, startPoints.end, target) });
357
370
  }
358
371
  else {
359
372
  const fixed = handle === 'start' ? startPoints.end : startPoints.start;
360
373
  const moved = input.shiftKey ? snapPointAround(fixed, target, ARROW_SNAP_DEGREES) : target;
361
- changed = reshapeArrow(arrow, handle === 'start' ? { start: moved } : { end: moved });
374
+ if (!input.altKey && !input.shiftKey) {
375
+ pick = pickArrowBinding(startScene, target, { zoom: engine.state.viewport.zoom, ignore: passedOver });
376
+ }
377
+ // The end is where the hand puts it; the engine draws a tied end from its shape (`reconcileArrowBindings`) in the same step.
378
+ const change = handle === 'start' ? { start: moved, startBinding: pick?.binding ?? null } : { end: moved, endBinding: pick?.binding ?? null };
379
+ changed = reshapeArrow(arrow, change);
362
380
  }
363
381
  engine.applyScene(withShapes(startScene, [changed]), 'transient');
382
+ engine.setOverlay({ arrowBind: pick?.hint ?? null });
364
383
  };
365
384
  const finish = () => {
385
+ engine.setOverlay({ arrowBind: null });
366
386
  engine.setArrowHandleHover(null);
367
387
  engine.setCursor(cursorForTool(engine.state.tool));
368
388
  };
@@ -552,7 +572,14 @@ export function createDrawGesture(context, start) {
552
572
  };
553
573
  }
554
574
  // ──────────────────────────────────────────────────────────── shapes
555
- /** Drags a shape out. A click places it at its default size. Shift constrains it; Alt draws it from the middle. */
575
+ /**
576
+ * Drags a shape out. A click places it at its default size. Shift constrains it; Alt draws it from the middle.
577
+ *
578
+ * 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
579
+ * 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
580
+ * 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
581
+ * default arrow, tied to nothing; the two ends are never tied to the same shape.
582
+ */
556
583
  export function createShapeGesture(context, start, kind, libraryKey) {
557
584
  const { engine } = context;
558
585
  const id = engine.services.createId('obj');
@@ -561,10 +588,15 @@ export function createShapeGesture(context, start, kind, libraryKey) {
561
588
  const startWorld = worldOf(context, start);
562
589
  let dragged = false;
563
590
  let last = start;
591
+ const pickAt = (point, ignore) => pickArrowBinding(engine.state.scene, point, { zoom: engine.state.viewport.zoom, ignore });
592
+ // The shape the tail of an arrow would be tied to, from the moment of the press.
593
+ const tailPick = kind === 'arrow' && !start.altKey ? pickAt(startWorld) : null;
594
+ if (tailPick)
595
+ engine.setOverlay({ arrowBind: tailPick.hint });
564
596
  const build = (input) => {
565
597
  const end = worldOf(context, input);
566
598
  const fromCenter = input.altKey && kind !== 'arrow' && dragged;
567
- return buildDraggedShape({
599
+ const shape = buildDraggedShape({
568
600
  kind,
569
601
  start: fromCenter ? { x: 2 * startWorld.x - end.x, y: 2 * startWorld.y - end.y } : startWorld,
570
602
  end,
@@ -576,6 +608,15 @@ export function createShapeGesture(context, start, kind, libraryKey) {
576
608
  random: () => colorSeed,
577
609
  workspacePaint: engine.workspacePaintForNewShape(engine.wireframeMode)
578
610
  });
611
+ if (kind !== 'arrow' || !dragged || input.altKey || shape.type !== 'path' || !isArrow(shape))
612
+ return { shape, hint: null };
613
+ const tail = tailPick?.binding;
614
+ const headPick = input.shiftKey ? null : pickAt(end, tail ? new Set([tail.targetId]) : undefined);
615
+ if (!tail && !headPick)
616
+ return { shape, hint: null };
617
+ const index = engine.sceneIndex();
618
+ const tied = positionBoundArrow(reshapeArrow(shape, { startBinding: tail ?? null, endBinding: headPick?.binding ?? null }), (targetId) => index.get(targetId));
619
+ return { shape: tied, hint: headPick?.hint ?? null };
579
620
  };
580
621
  return {
581
622
  kind: 'shape',
@@ -584,11 +625,13 @@ export function createShapeGesture(context, start, kind, libraryKey) {
584
625
  last = input;
585
626
  if (!dragged && distanceBetween({ x: start.x, y: start.y }, { x: input.x, y: input.y }) >= MIN_DRAW_DISTANCE)
586
627
  dragged = true;
587
- if (dragged)
588
- engine.setOverlay({ preview: build(input) });
628
+ if (dragged) {
629
+ const { shape, hint } = build(input);
630
+ engine.setOverlay({ preview: shape, arrowBind: hint });
631
+ }
589
632
  },
590
633
  end(input) {
591
- const shape = build(input);
634
+ const { shape } = build(input);
592
635
  engine.clearOverlay();
593
636
  engine.addObjects([shape]);
594
637
  if (engine.state.stickyTool) {
@@ -603,8 +646,10 @@ export function createShapeGesture(context, start, kind, libraryKey) {
603
646
  engine.clearOverlay();
604
647
  },
605
648
  modifiersChanged(modifiers) {
606
- if (dragged)
607
- engine.setOverlay({ preview: build({ ...last, ...modifiers }) });
649
+ if (!dragged)
650
+ return;
651
+ const { shape, hint } = build({ ...last, ...modifiers });
652
+ engine.setOverlay({ preview: shape, arrowBind: hint });
608
653
  }
609
654
  };
610
655
  }
@@ -5,7 +5,8 @@
5
5
  * in a form field, is a text being typed on the board, which tools the host allows), and this module answers
6
6
  * whether the board used the key. When it did, the shell stops the browser from also acting on it. The rules are
7
7
  * the ones the board has always had (delete, arrows, copy and paste, group, undo, layers, save, zoom, the letter
8
- * of each tool); they live here, apart from the DOM, so each one is tested with a plain record.
8
+ * of each tool, and the number of each tool of the drawing bar); they live here, apart from the DOM, so each one is
9
+ * tested with a plain record.
9
10
  */
10
11
  import type { EngineCore } from '../EngineCore.js';
11
12
  import type { PointerController } from './PointerController.js';
@@ -25,6 +26,13 @@ export interface KeyInput {
25
26
  export type ToolShortcut = 'select' | 'erase' | 'draw' | 'pan' | 'arrow' | 'rect' | 'triangle' | 'ellipse' | 'text' | 'image';
26
27
  /** The letter of each tool (the same letters the hints and the list of shortcuts show). */
27
28
  export declare const TOOL_LETTERS: Readonly<Record<string, ToolShortcut>>;
29
+ /**
30
+ * The number of each tool of the drawing bar, in the order the bar lists them (select, draw, erase, arrow, text): the
31
+ * digits 1 to 5 choose them, besides their letters. A digit is the one the person types (`key`), on the number row or on the
32
+ * numpad, so a layout whose number row needs Shift to type a digit reaches the tools as it types the digit. The hints and the
33
+ * list of shortcuts show these same numbers (`TOOL_DIGIT_KEYS` in `i18n/shortcuts.ts`; a test holds the two together).
34
+ */
35
+ export declare const TOOL_DIGITS: Readonly<Record<string, ToolShortcut>>;
28
36
  /** What the shell does that the engine does not: the things outside the board. */
29
37
  export interface KeyboardActions {
30
38
  /** Chooses a tool. The image tool opens the file picker, so the shell does it, not the engine. */
@@ -5,7 +5,8 @@
5
5
  * in a form field, is a text being typed on the board, which tools the host allows), and this module answers
6
6
  * whether the board used the key. When it did, the shell stops the browser from also acting on it. The rules are
7
7
  * the ones the board has always had (delete, arrows, copy and paste, group, undo, layers, save, zoom, the letter
8
- * of each tool); they live here, apart from the DOM, so each one is tested with a plain record.
8
+ * of each tool, and the number of each tool of the drawing bar); they live here, apart from the DOM, so each one is
9
+ * tested with a plain record.
9
10
  */
10
11
  /** The letter of each tool (the same letters the hints and the list of shortcuts show). */
11
12
  export const TOOL_LETTERS = Object.freeze({
@@ -20,6 +21,19 @@ export const TOOL_LETTERS = Object.freeze({
20
21
  T: 'text',
21
22
  I: 'image'
22
23
  });
24
+ /**
25
+ * The number of each tool of the drawing bar, in the order the bar lists them (select, draw, erase, arrow, text): the
26
+ * digits 1 to 5 choose them, besides their letters. A digit is the one the person types (`key`), on the number row or on the
27
+ * numpad, so a layout whose number row needs Shift to type a digit reaches the tools as it types the digit. The hints and the
28
+ * list of shortcuts show these same numbers (`TOOL_DIGIT_KEYS` in `i18n/shortcuts.ts`; a test holds the two together).
29
+ */
30
+ export const TOOL_DIGITS = Object.freeze({
31
+ '1': 'select',
32
+ '2': 'draw',
33
+ '3': 'erase',
34
+ '4': 'arrow',
35
+ '5': 'text'
36
+ });
23
37
  /** The tools that belong to the drawing bar: they are hidden, and so are their letters, when the host turns that bar off. */
24
38
  const DRAWING_TOOLS = new Set(['select', 'erase', 'draw', 'arrow', 'text']);
25
39
  const ARROW_STEPS = Object.freeze({
@@ -241,7 +255,9 @@ export function routeKeyDown(input, host) {
241
255
  }
242
256
  if (input.altKey)
243
257
  return false;
244
- const tool = key.length === 1 ? TOOL_LETTERS[key.toUpperCase()] : undefined;
258
+ // A letter chooses its tool, and so does the number of a tool of the drawing bar. This point is only reached when the person is
259
+ // not typing (a field of the page, or a text on the board: both returned above), so a digit written into a text stays a digit.
260
+ const tool = key.length === 1 ? (TOOL_LETTERS[key.toUpperCase()] ?? TOOL_DIGITS[key]) : undefined;
245
261
  if (tool && toolIsAvailable(tool, host.tools)) {
246
262
  actions.chooseTool(tool);
247
263
  return true;
@@ -7,7 +7,7 @@ import type { ArrowHandleId } from './core/arrow.js';
7
7
  import type { Scene } from './core/scene.js';
8
8
  import type { ScribbleView } from './core/scribble.js';
9
9
  import type { SnapGuide } from './core/snapping.js';
10
- import type { ObjectId, Rect, SceneObject, Size, Viewport } from './core/types.js';
10
+ import type { ArrowBindHint, ObjectId, Rect, SceneObject, Size, Viewport } from './core/types.js';
11
11
  export type ToolId = 'select' | 'draw' | 'rect' | 'rounded-rect' | 'ellipse' | 'circle' | 'triangle' | 'arrow' | 'library-shape' | 'text' | 'erase' | 'pan';
12
12
  /** The tools that drag a shape out. */
13
13
  export type ShapeToolId = 'rect' | 'rounded-rect' | 'ellipse' | 'circle' | 'triangle' | 'arrow' | 'library-shape';
@@ -76,6 +76,11 @@ export interface EngineOverlay {
76
76
  * one undo step) or Escape leaves them as they were. Ids of shapes, never of groups: a group is marked by its members.
77
77
  */
78
78
  erasing: readonly ObjectId[];
79
+ /**
80
+ * While an end of an arrow is dragged (the Arrow tool, or a handle of a selected arrow) over a shape it would be tied to: that shape
81
+ * and the point where the end would rest. The board outlines the shape and puts a dot on the point.
82
+ */
83
+ arrowBind: ArrowBindHint | null;
79
84
  }
80
85
  export declare const EMPTY_OVERLAY: EngineOverlay;
81
86
  export interface EngineState {
@@ -15,7 +15,8 @@ export const EMPTY_OVERLAY = Object.freeze({
15
15
  dragging: null,
16
16
  readout: null,
17
17
  virtualCursor: null,
18
- erasing: Object.freeze([])
18
+ erasing: Object.freeze([]),
19
+ arrowBind: null
19
20
  });
20
21
  export function createInitialState() {
21
22
  return {
@@ -17,6 +17,18 @@ export declare const TOOL_KEYS: {
17
17
  readonly text: "T";
18
18
  readonly image: "I";
19
19
  };
20
+ /**
21
+ * The number that also chooses each tool of the drawing bar: the place of its button on the bar, counted from the start of it.
22
+ * The hints and the list of keyboard shortcuts show it beside the letter. A digit is the same on every keyboard, so it is not a
23
+ * message either. The engine's table (`TOOL_DIGITS`) is the one that answers the key; a test holds the two together.
24
+ */
25
+ export declare const TOOL_DIGIT_KEYS: {
26
+ readonly select: "1";
27
+ readonly draw: "2";
28
+ readonly erase: "3";
29
+ readonly arrow: "4";
30
+ readonly text: "5";
31
+ };
20
32
  /**
21
33
  * One key of a shortcut: a key that is a word (a message, so a language can write "Strg" for "Ctrl"), a key cap that
22
34
  * reads the same everywhere ("V", "↑", "]"), or `null` for the "+" that is drawn between two keys (a `null` rather than