@crux-diagram/react 0.0.0-stage → 0.1.0

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 (98) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/LICENSE +21 -0
  3. package/README.md +50 -2
  4. package/dist/cjs/DrawingEditor.d.ts +34 -0
  5. package/dist/cjs/DrawingEditor.js +289 -0
  6. package/dist/cjs/DrawingViewer.d.ts +14 -0
  7. package/dist/cjs/DrawingViewer.js +14 -0
  8. package/dist/cjs/JointDisplayBar.d.ts +8 -0
  9. package/dist/cjs/JointDisplayBar.js +22 -0
  10. package/dist/cjs/NameEditor.d.ts +15 -0
  11. package/dist/cjs/NameEditor.js +36 -0
  12. package/dist/cjs/Toolbar.d.ts +8 -0
  13. package/dist/cjs/Toolbar.js +34 -0
  14. package/dist/cjs/VersionHistory.d.ts +33 -0
  15. package/dist/cjs/VersionHistory.js +49 -0
  16. package/dist/cjs/a11y.d.ts +12 -0
  17. package/dist/cjs/a11y.js +53 -0
  18. package/dist/cjs/config.d.ts +50 -0
  19. package/dist/cjs/config.js +39 -0
  20. package/dist/cjs/controls.d.ts +12 -0
  21. package/dist/cjs/controls.js +15 -0
  22. package/dist/cjs/editor-context.d.ts +45 -0
  23. package/dist/cjs/editor-context.js +101 -0
  24. package/dist/cjs/export/export-image.d.ts +29 -0
  25. package/dist/cjs/export/export-image.js +94 -0
  26. package/dist/cjs/hooks.d.ts +6 -0
  27. package/dist/cjs/hooks.js +10 -0
  28. package/dist/cjs/index.d.ts +9 -0
  29. package/dist/cjs/index.js +21 -0
  30. package/dist/cjs/interaction/gestures.d.ts +133 -0
  31. package/dist/cjs/interaction/gestures.js +353 -0
  32. package/dist/cjs/interaction/joints.d.ts +41 -0
  33. package/dist/cjs/interaction/joints.js +104 -0
  34. package/dist/cjs/interaction/keyboard.d.ts +51 -0
  35. package/dist/cjs/interaction/keyboard.js +116 -0
  36. package/dist/cjs/interaction/palette.d.ts +19 -0
  37. package/dist/cjs/interaction/palette.js +64 -0
  38. package/dist/cjs/interaction/viewport.d.ts +14 -0
  39. package/dist/cjs/interaction/viewport.js +23 -0
  40. package/dist/cjs/labels.d.ts +39 -0
  41. package/dist/cjs/labels.js +49 -0
  42. package/dist/cjs/package.json +1 -0
  43. package/dist/cjs/render/DrawingStage.d.ts +27 -0
  44. package/dist/cjs/render/DrawingStage.js +77 -0
  45. package/dist/cjs/render/fonts.d.ts +7 -0
  46. package/dist/cjs/render/fonts.js +50 -0
  47. package/dist/cjs/render/scene.d.ts +75 -0
  48. package/dist/cjs/render/scene.js +92 -0
  49. package/dist/cjs/store.d.ts +90 -0
  50. package/dist/cjs/store.js +159 -0
  51. package/dist/esm/DrawingEditor.d.ts +34 -0
  52. package/dist/esm/DrawingEditor.js +285 -0
  53. package/dist/esm/DrawingViewer.d.ts +14 -0
  54. package/dist/esm/DrawingViewer.js +11 -0
  55. package/dist/esm/JointDisplayBar.d.ts +8 -0
  56. package/dist/esm/JointDisplayBar.js +19 -0
  57. package/dist/esm/NameEditor.d.ts +15 -0
  58. package/dist/esm/NameEditor.js +32 -0
  59. package/dist/esm/Toolbar.d.ts +8 -0
  60. package/dist/esm/Toolbar.js +31 -0
  61. package/dist/esm/VersionHistory.d.ts +33 -0
  62. package/dist/esm/VersionHistory.js +45 -0
  63. package/dist/esm/a11y.d.ts +12 -0
  64. package/dist/esm/a11y.js +47 -0
  65. package/dist/esm/config.d.ts +50 -0
  66. package/dist/esm/config.js +35 -0
  67. package/dist/esm/controls.d.ts +12 -0
  68. package/dist/esm/controls.js +11 -0
  69. package/dist/esm/editor-context.d.ts +45 -0
  70. package/dist/esm/editor-context.js +95 -0
  71. package/dist/esm/export/export-image.d.ts +29 -0
  72. package/dist/esm/export/export-image.js +86 -0
  73. package/dist/esm/hooks.d.ts +6 -0
  74. package/dist/esm/hooks.js +7 -0
  75. package/dist/esm/index.d.ts +9 -0
  76. package/dist/esm/index.js +9 -0
  77. package/dist/esm/interaction/gestures.d.ts +133 -0
  78. package/dist/esm/interaction/gestures.js +338 -0
  79. package/dist/esm/interaction/joints.d.ts +41 -0
  80. package/dist/esm/interaction/joints.js +95 -0
  81. package/dist/esm/interaction/keyboard.d.ts +51 -0
  82. package/dist/esm/interaction/keyboard.js +109 -0
  83. package/dist/esm/interaction/palette.d.ts +19 -0
  84. package/dist/esm/interaction/palette.js +59 -0
  85. package/dist/esm/interaction/viewport.d.ts +14 -0
  86. package/dist/esm/interaction/viewport.js +19 -0
  87. package/dist/esm/labels.d.ts +39 -0
  88. package/dist/esm/labels.js +45 -0
  89. package/dist/esm/package.json +1 -0
  90. package/dist/esm/render/DrawingStage.d.ts +27 -0
  91. package/dist/esm/render/DrawingStage.js +74 -0
  92. package/dist/esm/render/fonts.d.ts +7 -0
  93. package/dist/esm/render/fonts.js +45 -0
  94. package/dist/esm/render/scene.d.ts +75 -0
  95. package/dist/esm/render/scene.js +86 -0
  96. package/dist/esm/store.d.ts +90 -0
  97. package/dist/esm/store.js +156 -0
  98. package/package.json +80 -4
@@ -0,0 +1,133 @@
1
+ import { type DrawingDocument, type Point, type Rect, type ResizeHandle, type Shape, type Vector } from '@crux-diagram/core';
2
+ import type { EditorStore } from '../store.js';
3
+ /**
4
+ * Screen pixels a pointer may travel before release and still count as a tap (PN-4).
5
+ * Assumption: Flutter uses the platform touch slop and spec 05 gives no number.
6
+ */
7
+ export declare const TAP_SLOP = 4;
8
+ /** Grid settings read when a drag starts and kept until it ends (GA-8). */
9
+ export interface DragGrid {
10
+ /** `config.gridSize`: snapping step while the grid is hidden (GA-2); `null` disables snapping. */
11
+ readonly gridSize: number | null;
12
+ /** Finest drawn grid step while the grid is shown, else `null` (GA-1). */
13
+ readonly visibleStep: number | null;
14
+ /**
15
+ * How close, in canvas units, an anchor must come to another shape's anchor to snap onto it
16
+ * (`config.anchorSnapThreshold` over the view scale); `null` or absent disables it (REA-026).
17
+ */
18
+ readonly anchorSnapDistance?: number | null;
19
+ }
20
+ /** Touch radius of a handle, in screen pixels (spec 01 section 6). */
21
+ export declare const HANDLE_TOUCH_RADIUS = 24;
22
+ /** Visual radius of a handle, in screen pixels. */
23
+ export declare const HANDLE_VISUAL_RADIUS = 6;
24
+ /** Distance from the top edge to the rotation handle, in screen pixels. */
25
+ export declare const ROTATION_STEM_LENGTH = 32;
26
+ /** What `beginDrag` needs to offer the selected shape's handles. */
27
+ export interface HandleContext {
28
+ /** Screen pixels per canvas unit. */
29
+ readonly scale: number;
30
+ /** `config.rotationSnapStep` and `config.rotationSnapTolerance`, in radians (step 0 disables snapping). */
31
+ readonly rotationSnap: {
32
+ readonly step: number;
33
+ readonly tolerance: number;
34
+ };
35
+ /** The joint labels as drawn, back to front; they are hit before shapes (REA-022). */
36
+ readonly labels?: readonly LabelRect[];
37
+ }
38
+ /** The box of a joint's label on the canvas. */
39
+ export interface LabelRect {
40
+ readonly jointId: string;
41
+ readonly rect: Rect;
42
+ }
43
+ /** The id of the topmost joint whose label contains `point`, or `null`. */
44
+ export declare function labelAt(labels: readonly LabelRect[], point: Point): string | null;
45
+ /** Starts dragging the label under `point`, or returns `null` when there is none (AW-4). */
46
+ export declare function beginLabelDrag(editor: EditorStore, point: Point, labels: readonly LabelRect[]): Gesture | null;
47
+ /** A handle under a point: one of the resize handles or the rotation handle. */
48
+ export type HandleHit = {
49
+ readonly kind: 'resize';
50
+ readonly handle: ResizeHandle;
51
+ } | {
52
+ readonly kind: 'rotate';
53
+ };
54
+ export type Gesture = {
55
+ readonly kind: 'none';
56
+ } | {
57
+ readonly kind: 'move';
58
+ /** The committed document when the drag started; deltas are measured from it. */
59
+ readonly start: DrawingDocument;
60
+ readonly ids: readonly string[];
61
+ readonly origin: Point;
62
+ readonly grid: DragGrid;
63
+ /** The last previewed document, or `null` before the first update. */
64
+ readonly last: DrawingDocument | null;
65
+ } | {
66
+ readonly kind: 'marquee';
67
+ readonly origin: Point;
68
+ readonly rect: Rect;
69
+ } | {
70
+ readonly kind: 'label';
71
+ readonly start: DrawingDocument;
72
+ readonly id: string;
73
+ readonly origin: Point;
74
+ readonly startOffset: Vector;
75
+ readonly last: DrawingDocument | null;
76
+ } | {
77
+ readonly kind: 'resize';
78
+ readonly start: DrawingDocument;
79
+ readonly id: string;
80
+ readonly handle: ResizeHandle;
81
+ readonly grid: DragGrid;
82
+ readonly last: DrawingDocument | null;
83
+ } | {
84
+ readonly kind: 'rotate';
85
+ readonly start: DrawingDocument;
86
+ readonly id: string;
87
+ readonly snap: HandleContext['rotationSnap'];
88
+ readonly last: DrawingDocument | null;
89
+ };
90
+ /** Canvas position of `shape`'s rotation handle: above its top edge, turning with it (FR-7). */
91
+ export declare function rotationHandlePosition(shape: Shape, scale: number): Point;
92
+ /**
93
+ * The handle of `shape` under `point`: the rotation handle first, else the nearest resize
94
+ * handle within the touch radius. Squares and circles have corner handles only (FR-12).
95
+ */
96
+ export declare function handleAt(shape: Shape, point: Point, scale: number): HandleHit | null;
97
+ /**
98
+ * A tap at `point`: outside selection mode it selects the topmost shape (and its group)
99
+ * or clears the selection on empty canvas; in selection mode it toggles the shape and
100
+ * empty canvas keeps the selection (FR-6, MS-1, MS-6, PN-4).
101
+ */
102
+ export declare function tap(editor: EditorStore, point: Point, handles?: HandleContext): void;
103
+ /**
104
+ * Starts a drag at `point`. On a handle of the single selected shape (when `handles` is
105
+ * given) it resizes or rotates it (FR-11, FR-15). On a shape it moves the selection; an unselected shape is
106
+ * selected first (FR-8), or joins the selection in selection mode (MS-4). On empty
107
+ * canvas it draws a selection rectangle in selection mode (MS-3) and does nothing
108
+ * otherwise (PN-5; panning is a zoom feature).
109
+ */
110
+ export declare function beginDrag(editor: EditorStore, point: Point, grid: DragGrid, handles?: HandleContext): Gesture;
111
+ /** Follows the pointer to `point`: previews the moved document, or grows the rectangle. */
112
+ export declare function updateDrag(editor: EditorStore, gesture: Gesture, point: Point): Gesture;
113
+ /**
114
+ * Ends the drag: a move commits one history entry when a shape moved and clears the
115
+ * preview otherwise (FR-22); a selection rectangle adds every shape whose bounds lie
116
+ * fully inside it (MS-3).
117
+ */
118
+ export declare function endDrag(editor: EditorStore, gesture: Gesture): void;
119
+ /** Drops the preview of an unfinished move, as on `pointercancel` (Flutter `cancelGesture`). */
120
+ export declare function cancelDrag(editor: EditorStore, gesture: Gesture): void;
121
+ export declare function movedDocument(start: DrawingDocument, ids: readonly string[], delta: Vector, grid: DragGrid): DrawingDocument;
122
+ /**
123
+ * Like {@link movedDocument}, plus the anchor that was snapped onto, if any, for the
124
+ * highlight (SN-3). Anchor snapping comes first and replaces grid snapping when it applies
125
+ * (SN-1, SN-3, GA-7); only the nearest pair counts, and for several shapes any selected
126
+ * anchor may snap (MS-4).
127
+ */
128
+ export declare function movedWithSnap(start: DrawingDocument, ids: readonly string[], delta: Vector, grid: DragGrid): {
129
+ readonly document: DrawingDocument;
130
+ readonly highlight: readonly Point[];
131
+ };
132
+ /** The axis-aligned rectangle with corners `a` and `b`. */
133
+ export declare function rectBetween(a: Point, b: Point): Rect;
@@ -0,0 +1,353 @@
1
+ "use strict";
2
+ // Pointer gestures on shapes, free of Konva so they can be tested alone. Ported from
3
+ // Flutter's `DrawingEditor._onTap`, `_onScaleStart`/`_onScaleUpdate`/`_onScaleEnd` and
4
+ // the controller's `moveSelectedBy`. Points are in canvas units.
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.ROTATION_STEM_LENGTH = exports.HANDLE_VISUAL_RADIUS = exports.HANDLE_TOUCH_RADIUS = exports.TAP_SLOP = void 0;
7
+ exports.labelAt = labelAt;
8
+ exports.beginLabelDrag = beginLabelDrag;
9
+ exports.rotationHandlePosition = rotationHandlePosition;
10
+ exports.handleAt = handleAt;
11
+ exports.tap = tap;
12
+ exports.beginDrag = beginDrag;
13
+ exports.updateDrag = updateDrag;
14
+ exports.endDrag = endDrag;
15
+ exports.cancelDrag = cancelDrag;
16
+ exports.movedDocument = movedDocument;
17
+ exports.movedWithSnap = movedWithSnap;
18
+ exports.rectBetween = rectBetween;
19
+ const core_1 = require("@crux-diagram/core");
20
+ /**
21
+ * Screen pixels a pointer may travel before release and still count as a tap (PN-4).
22
+ * Assumption: Flutter uses the platform touch slop and spec 05 gives no number.
23
+ */
24
+ exports.TAP_SLOP = 4;
25
+ /** Touch radius of a handle, in screen pixels (spec 01 section 6). */
26
+ exports.HANDLE_TOUCH_RADIUS = 24;
27
+ /** Visual radius of a handle, in screen pixels. */
28
+ exports.HANDLE_VISUAL_RADIUS = 6;
29
+ /** Distance from the top edge to the rotation handle, in screen pixels. */
30
+ exports.ROTATION_STEM_LENGTH = 32;
31
+ /** The id of the topmost joint whose label contains `point`, or `null`. */
32
+ function labelAt(labels, point) {
33
+ for (let i = labels.length - 1; i >= 0; i--) {
34
+ if ((0, core_1.rectContains)(labels[i].rect, point))
35
+ return labels[i].jointId;
36
+ }
37
+ return null;
38
+ }
39
+ /** Starts dragging the label under `point`, or returns `null` when there is none (AW-4). */
40
+ function beginLabelDrag(editor, point, labels) {
41
+ const id = labelAt(labels, point);
42
+ const start = editor.getDocument();
43
+ const joint = id && start.joints.find((j) => j.id === id);
44
+ if (!joint)
45
+ return null;
46
+ return { kind: 'label', start, id: joint.id, origin: point, startOffset: joint.labelOffset, last: null };
47
+ }
48
+ /** Canvas position of `shape`'s rotation handle: above its top edge, turning with it (FR-7). */
49
+ function rotationHandlePosition(shape, scale) {
50
+ return (0, core_1.worldFromLocal)(shape.center, shape.rotation, {
51
+ x: 0,
52
+ y: -shape.size.height / 2 - exports.ROTATION_STEM_LENGTH / scale,
53
+ });
54
+ }
55
+ /**
56
+ * The handle of `shape` under `point`: the rotation handle first, else the nearest resize
57
+ * handle within the touch radius. Squares and circles have corner handles only (FR-12).
58
+ */
59
+ function handleAt(shape, point, scale) {
60
+ const radius = exports.HANDLE_TOUCH_RADIUS / scale;
61
+ const distance = (target) => Math.hypot(target.x - point.x, target.y - point.y);
62
+ if (distance(rotationHandlePosition(shape, scale)) <= radius)
63
+ return { kind: 'rotate' };
64
+ let nearest = null;
65
+ let nearestDistance = Infinity;
66
+ for (const handle of (0, core_1.handlesFor)(shape.type)) {
67
+ const d = distance((0, core_1.worldFromLocal)(shape.center, shape.rotation, (0, core_1.handleLocalPosition)(handle, shape.size)));
68
+ if (d <= radius && d < nearestDistance) {
69
+ nearest = { kind: 'resize', handle };
70
+ nearestDistance = d;
71
+ }
72
+ }
73
+ return nearest;
74
+ }
75
+ /**
76
+ * A tap at `point`: outside selection mode it selects the topmost shape (and its group)
77
+ * or clears the selection on empty canvas; in selection mode it toggles the shape and
78
+ * empty canvas keeps the selection (FR-6, MS-1, MS-6, PN-4).
79
+ */
80
+ function tap(editor, point, handles) {
81
+ const { document, selection, selectionMode } = editor.getState();
82
+ // A tap on a handle of the single selected shape is not a tap on the canvas (Flutter `_onTap`).
83
+ if (handles && selection.length === 1) {
84
+ const selected = document.shapes.find((s) => s.id === selection[0]);
85
+ if (selected && handleAt(selected, point, handles.scale))
86
+ return;
87
+ }
88
+ // A label is above the shapes: tapping it selects its joint (AW-7).
89
+ const labeled = handles?.labels && labelAt(handles.labels, point);
90
+ if (labeled)
91
+ return editor.selectJoint(labeled);
92
+ const shape = (0, core_1.topmostShapeAt)(document.shapes, point);
93
+ if (selectionMode) {
94
+ if (shape)
95
+ editor.toggleSelected(shape.id);
96
+ return;
97
+ }
98
+ editor.select(shape?.id ?? null);
99
+ }
100
+ /**
101
+ * Starts a drag at `point`. On a handle of the single selected shape (when `handles` is
102
+ * given) it resizes or rotates it (FR-11, FR-15). On a shape it moves the selection; an unselected shape is
103
+ * selected first (FR-8), or joins the selection in selection mode (MS-4). On empty
104
+ * canvas it draws a selection rectangle in selection mode (MS-3) and does nothing
105
+ * otherwise (PN-5; panning is a zoom feature).
106
+ */
107
+ function beginDrag(editor, point, grid, handles) {
108
+ const { document, selection, selectionMode } = editor.getState();
109
+ // Handles come before shapes, and only a single selected shape has them (FR-7, MS-5).
110
+ const selected = handles && selection.length === 1 ? document.shapes.find((s) => s.id === selection[0]) : undefined;
111
+ const hit = selected && handles ? handleAt(selected, point, handles.scale) : null;
112
+ if (selected && handles && hit) {
113
+ return hit.kind === 'rotate'
114
+ ? {
115
+ kind: 'rotate',
116
+ start: document,
117
+ id: selected.id,
118
+ snap: handles.rotationSnap,
119
+ last: null,
120
+ }
121
+ : {
122
+ kind: 'resize',
123
+ start: document,
124
+ id: selected.id,
125
+ handle: hit.handle,
126
+ grid: { ...grid },
127
+ last: null,
128
+ };
129
+ }
130
+ const label = handles?.labels && beginLabelDrag(editor, point, handles.labels);
131
+ if (label)
132
+ return label;
133
+ const shape = (0, core_1.topmostShapeAt)(document.shapes, point);
134
+ if (!shape) {
135
+ return selectionMode ? { kind: 'marquee', origin: point, rect: rectBetween(point, point) } : { kind: 'none' };
136
+ }
137
+ if (!selection.includes(shape.id)) {
138
+ if (selectionMode)
139
+ editor.toggleSelected(shape.id);
140
+ else
141
+ editor.select(shape.id);
142
+ }
143
+ const ids = editor.getState().selection;
144
+ return {
145
+ kind: 'move',
146
+ start: document,
147
+ ids,
148
+ origin: point,
149
+ grid: { ...grid },
150
+ last: null,
151
+ };
152
+ }
153
+ /** Follows the pointer to `point`: previews the moved document, or grows the rectangle. */
154
+ function updateDrag(editor, gesture, point) {
155
+ if (gesture.kind === 'marquee')
156
+ return { ...gesture, rect: rectBetween(gesture.origin, point) };
157
+ if (gesture.kind === 'none' || abandoned(editor, gesture))
158
+ return gesture;
159
+ if (gesture.kind === 'label') {
160
+ const offset = { dx: gesture.startOffset.dx + point.x - gesture.origin.x, dy: gesture.startOffset.dy + point.y - gesture.origin.y };
161
+ const next = (0, core_1.moveJointLabel)(gesture.start, gesture.id, offset);
162
+ editor.preview(next);
163
+ return { ...gesture, last: next };
164
+ }
165
+ if (gesture.kind === 'resize' || gesture.kind === 'rotate') {
166
+ const { document: next, highlight } = transformedWithSnap(gesture, point);
167
+ editor.preview(next, highlight);
168
+ return { ...gesture, last: next };
169
+ }
170
+ const delta = {
171
+ dx: point.x - gesture.origin.x,
172
+ dy: point.y - gesture.origin.y,
173
+ };
174
+ const { document: next, highlight } = movedWithSnap(gesture.start, gesture.ids, delta, gesture.grid);
175
+ editor.preview(next, highlight);
176
+ return { ...gesture, last: next };
177
+ }
178
+ /**
179
+ * Ends the drag: a move commits one history entry when a shape moved and clears the
180
+ * preview otherwise (FR-22); a selection rectangle adds every shape whose bounds lie
181
+ * fully inside it (MS-3).
182
+ */
183
+ function endDrag(editor, gesture) {
184
+ if (gesture.kind === 'marquee') {
185
+ const { rect } = gesture;
186
+ const inside = editor
187
+ .getState()
188
+ .document.shapes.filter((s) => containsRect(rect, (0, core_1.shapeBounds)(s)))
189
+ .map((s) => s.id);
190
+ if (inside.length > 0)
191
+ editor.selectAll(inside);
192
+ return;
193
+ }
194
+ if (gesture.kind === 'none' || abandoned(editor, gesture))
195
+ return;
196
+ const { last, start } = gesture;
197
+ const changed = gesture.kind === 'move'
198
+ ? last && movedAny(start, last, gesture.ids)
199
+ : gesture.kind === 'label'
200
+ ? last && labelMoved(start, last, gesture.id)
201
+ : last && transformedAny(start, last, gesture.id);
202
+ if (last && changed)
203
+ editor.commit(last);
204
+ else
205
+ editor.preview(null);
206
+ }
207
+ /** Drops the preview of an unfinished move, as on `pointercancel` (Flutter `cancelGesture`). */
208
+ function cancelDrag(editor, gesture) {
209
+ if (gesture.kind !== 'none' && gesture.kind !== 'marquee' && !abandoned(editor, gesture))
210
+ editor.preview(null);
211
+ }
212
+ /**
213
+ * The start document with the gesture's shape resized by dragging its handle to `point`
214
+ * (FR-11 to FR-14), with the dragged handle's anchor snapping onto another shape's anchor
215
+ * (SN-2), or rotated so its handle points at `point` with soft snapping (FR-15, FR-16).
216
+ */
217
+ function transformedWithSnap(gesture, point) {
218
+ const { start, id } = gesture;
219
+ const shape = start.shapes.find((s) => s.id === id);
220
+ if (!shape)
221
+ return { document: start, highlight: [] };
222
+ const replaced = (next) => ({ ...start, shapes: start.shapes.map((s) => (s === shape ? next : s)) });
223
+ if (gesture.kind === 'rotate') {
224
+ // No anchor snapping while rotating (SN-2).
225
+ const rotation = (0, core_1.softSnap)((0, core_1.rotationFromPointer)(shape.center, point), gesture.snap.step, gesture.snap.tolerance).angle;
226
+ return { document: replaced({ ...shape, rotation }), highlight: [] };
227
+ }
228
+ const gridSize = gesture.grid.gridSize ?? undefined;
229
+ const gridStep = gesture.grid.visibleStep ?? undefined;
230
+ const limits = (0, core_1.sizeLimitsForCanvas)(start.canvas, { gridSize, gridStep });
231
+ const snapped = snappedResize(gesture, shape, point, limits);
232
+ if (snapped)
233
+ return { document: replaced(snapped.shape), highlight: [snapped.to] };
234
+ return { document: replaced((0, core_1.resizeByHandle)(shape, gesture.handle, point, { limits, gridSize, gridStep })), highlight: [] };
235
+ }
236
+ /**
237
+ * The resize that puts the dragged handle's anchor exactly on a nearby anchor of another
238
+ * shape, or `null` when there is none or it cannot land exactly, for example at a size
239
+ * limit or under an aspect lock (SN-2, SN-3).
240
+ */
241
+ function snappedResize(gesture, shape, point, limits) {
242
+ const targets = snapTargets(gesture.start, (id) => id === shape.id, gesture.grid);
243
+ if (targets.length === 0)
244
+ return null;
245
+ const anchorId = (0, core_1.anchorIdForHandle)(gesture.handle);
246
+ const free = (0, core_1.resizeByHandle)(shape, gesture.handle, point, { limits });
247
+ const position = (0, core_1.anchorPositionOf)(free, anchorId);
248
+ if (!position)
249
+ return null;
250
+ const snap = (0, core_1.nearestAnchorSnap)([{ id: anchorId, kind: 'vertex', position }], targets, { threshold: gesture.grid.anchorSnapDistance ?? 0 });
251
+ if (!snap)
252
+ return null;
253
+ const next = (0, core_1.resizeByHandle)(shape, gesture.handle, snap.to, { limits });
254
+ const landed = (0, core_1.anchorPositionOf)(next, anchorId);
255
+ if (!landed || Math.hypot(landed.x - snap.to.x, landed.y - snap.to.y) > 1e-6)
256
+ return null;
257
+ return { shape: next, to: snap.to };
258
+ }
259
+ function transformedAny(start, next, id) {
260
+ const before = start.shapes.find((s) => s.id === id);
261
+ const after = next.shapes.find((s) => s.id === id);
262
+ if (!before || !after)
263
+ return false;
264
+ return (before.center.x !== after.center.x ||
265
+ before.center.y !== after.center.y ||
266
+ before.size.width !== after.size.width ||
267
+ before.size.height !== after.size.height ||
268
+ before.rotation !== after.rotation);
269
+ }
270
+ function movedDocument(start, ids, delta, grid) {
271
+ return movedWithSnap(start, ids, delta, grid).document;
272
+ }
273
+ /** Anchors of every shape of `document` not in `excluded`, when anchor snapping is on. */
274
+ function snapTargets(document, excluded, grid) {
275
+ if (grid.anchorSnapDistance == null)
276
+ return [];
277
+ return document.shapes.filter((s) => !excluded(s.id)).flatMap(core_1.anchorsOf);
278
+ }
279
+ /**
280
+ * Like {@link movedDocument}, plus the anchor that was snapped onto, if any, for the
281
+ * highlight (SN-3). Anchor snapping comes first and replaces grid snapping when it applies
282
+ * (SN-1, SN-3, GA-7); only the nearest pair counts, and for several shapes any selected
283
+ * anchor may snap (MS-4).
284
+ */
285
+ function movedWithSnap(start, ids, delta, grid) {
286
+ const moving = start.shapes.filter((s) => ids.includes(s.id));
287
+ if (moving.length === 0)
288
+ return { document: start, highlight: [] };
289
+ const targets = snapTargets(start, (id) => ids.includes(id), grid);
290
+ const threshold = grid.anchorSnapDistance ?? 0;
291
+ let shift = delta;
292
+ let highlight = [];
293
+ if (moving.length === 1) {
294
+ const shape = moving[0];
295
+ const raw = { x: shape.center.x + delta.dx, y: shape.center.y + delta.dy };
296
+ const snap = targets.length > 0 ? (0, core_1.nearestAnchorSnap)((0, core_1.anchorsOf)({ ...shape, center: raw }), targets, { threshold }) : null;
297
+ let center = raw;
298
+ if (snap) {
299
+ center = { x: raw.x + snap.delta.dx, y: raw.y + snap.delta.dy };
300
+ highlight = [snap.to];
301
+ }
302
+ else if (grid.visibleStep !== null) {
303
+ center = (0, core_1.snapMoveToGrid)(raw, { size: shape.size, rotation: shape.rotation, step: grid.visibleStep });
304
+ }
305
+ else if (grid.gridSize !== null) {
306
+ center = (0, core_1.snapPointToGrid)(raw, grid.gridSize);
307
+ }
308
+ shift = { dx: center.x - shape.center.x, dy: center.y - shape.center.y };
309
+ }
310
+ else {
311
+ const shifted = moving.flatMap((s) => (0, core_1.anchorsOf)({ ...s, center: { x: s.center.x + delta.dx, y: s.center.y + delta.dy } }));
312
+ const snap = targets.length > 0 ? (0, core_1.nearestAnchorSnap)(shifted, targets, { threshold }) : null;
313
+ const step = grid.visibleStep ?? grid.gridSize;
314
+ if (snap) {
315
+ shift = { dx: delta.dx + snap.delta.dx, dy: delta.dy + snap.delta.dy };
316
+ highlight = [snap.to];
317
+ }
318
+ else if (step !== null) {
319
+ shift = (0, core_1.snapBoxShiftToGrid)((0, core_1.boundsOfShapes)(moving), delta, {
320
+ step,
321
+ edges: moving.every((s) => (0, core_1.isQuarterTurn)(s.rotation)),
322
+ });
323
+ }
324
+ }
325
+ return { document: (0, core_1.moveShape)(start, ids, shift), highlight };
326
+ }
327
+ /** A gesture whose start is no longer the committed document (undo, redo, reset or an outside value). */
328
+ const abandoned = (editor, gesture) => editor.getDocument() !== gesture.start;
329
+ function labelMoved(start, next, id) {
330
+ const before = start.joints.find((j) => j.id === id)?.labelOffset;
331
+ const after = next.joints.find((j) => j.id === id)?.labelOffset;
332
+ return !!before && !!after && (before.dx !== after.dx || before.dy !== after.dy);
333
+ }
334
+ function movedAny(start, next, ids) {
335
+ if (start === next)
336
+ return false;
337
+ const before = new Map(start.shapes.map((s) => [s.id, s.center]));
338
+ return next.shapes.some((s) => {
339
+ const from = before.get(s.id);
340
+ return ids.includes(s.id) && from !== undefined && (from.x !== s.center.x || from.y !== s.center.y);
341
+ });
342
+ }
343
+ /** The axis-aligned rectangle with corners `a` and `b`. */
344
+ function rectBetween(a, b) {
345
+ return { left: Math.min(a.x, b.x), top: Math.min(a.y, b.y), width: Math.abs(a.x - b.x), height: Math.abs(a.y - b.y) };
346
+ }
347
+ /** Whether `inner` lies fully inside `outer`, edges included. */
348
+ function containsRect(outer, inner) {
349
+ return (inner.left >= outer.left &&
350
+ inner.top >= outer.top &&
351
+ inner.left + inner.width <= outer.left + outer.width &&
352
+ inner.top + inner.height <= outer.top + outer.height);
353
+ }
@@ -0,0 +1,41 @@
1
+ import { type JointLabelSize, type Point } from '@crux-diagram/core';
2
+ import type { EditorStore } from '../store.js';
3
+ import { type LabelRect } from './gestures.js';
4
+ /** Hit radius of an anchor, in screen pixels (JT-2). */
5
+ export declare const ANCHOR_HIT_RADIUS = 22;
6
+ /** Anchors whose distance differs by less than this, in screen pixels, are tied: the topmost shape wins (JT-4). */
7
+ export declare const ANCHOR_TIE_TOLERANCE = 2;
8
+ /** Radius of an anchor dot, in screen pixels (JT-1). */
9
+ export declare const ANCHOR_VISUAL_RADIUS = 5;
10
+ export interface JointToolOptions {
11
+ /** Screen pixels per canvas unit. */
12
+ readonly scale: number;
13
+ readonly namePrefix: string;
14
+ readonly nameMaxLength: number;
15
+ /** The joint labels as drawn: tapping one selects its joint before any anchor is tried. */
16
+ readonly labels?: readonly LabelRect[];
17
+ }
18
+ /**
19
+ * A tap with the joint tool at canvas `point`. A free anchor gets a new joint, shown as a
20
+ * preview until its name is confirmed so that creating and naming is one history entry
21
+ * (DL-3); an anchor with a joint selects it, showing hidden joints again (REA-025); empty
22
+ * canvas clears the joint selection.
23
+ */
24
+ export declare function jointToolTap(editor: EditorStore, point: Point, { scale, namePrefix, nameMaxLength, labels }: JointToolOptions): void;
25
+ /** Opens the name editor for an existing joint (NM-2). */
26
+ export declare function beginRename(editor: EditorStore, jointId: string): void;
27
+ /**
28
+ * Confirms the name editor with `raw`, trimmed and cut to `nameMaxLength` (NM-1). A new joint
29
+ * is committed with its name; an empty name cancels it. An existing joint is renamed, or
30
+ * keeps its name when `raw` is empty.
31
+ */
32
+ export declare function confirmNaming(editor: EditorStore, raw: string, nameMaxLength: number): void;
33
+ /** Closes the name editor without a change; a joint that was being created is dropped. */
34
+ export declare function cancelNaming(editor: EditorStore): void;
35
+ /** Sets the joint label size as one history entry; the size already active adds none (JS-4). */
36
+ export declare function setJointLabelSize(editor: EditorStore, labelSize: JointLabelSize): void;
37
+ /**
38
+ * Shows or hides every joint as one history entry (JV-6). Hiding clears the joint
39
+ * selection and keeps the shape selection (JV-3); the joints stay in the document (JV-5).
40
+ */
41
+ export declare function setJointsVisible(editor: EditorStore, visible: boolean): void;
@@ -0,0 +1,104 @@
1
+ "use strict";
2
+ // The joint tool and the name editor, as store operations (REA-020, REA-021, REA-025;
3
+ // Flutter JT-1 to JT-4, NM-1, NM-2, DL-3).
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.ANCHOR_VISUAL_RADIUS = exports.ANCHOR_TIE_TOLERANCE = exports.ANCHOR_HIT_RADIUS = void 0;
6
+ exports.jointToolTap = jointToolTap;
7
+ exports.beginRename = beginRename;
8
+ exports.confirmNaming = confirmNaming;
9
+ exports.cancelNaming = cancelNaming;
10
+ exports.setJointLabelSize = setJointLabelSize;
11
+ exports.setJointsVisible = setJointsVisible;
12
+ const core_1 = require("@crux-diagram/core");
13
+ const gestures_js_1 = require("./gestures.js");
14
+ /** Hit radius of an anchor, in screen pixels (JT-2). */
15
+ exports.ANCHOR_HIT_RADIUS = 22;
16
+ /** Anchors whose distance differs by less than this, in screen pixels, are tied: the topmost shape wins (JT-4). */
17
+ exports.ANCHOR_TIE_TOLERANCE = 2;
18
+ /** Radius of an anchor dot, in screen pixels (JT-1). */
19
+ exports.ANCHOR_VISUAL_RADIUS = 5;
20
+ /**
21
+ * A tap with the joint tool at canvas `point`. A free anchor gets a new joint, shown as a
22
+ * preview until its name is confirmed so that creating and naming is one history entry
23
+ * (DL-3); an anchor with a joint selects it, showing hidden joints again (REA-025); empty
24
+ * canvas clears the joint selection.
25
+ */
26
+ function jointToolTap(editor, point, { scale, namePrefix, nameMaxLength, labels }) {
27
+ const labeled = labels && (0, gestures_js_1.labelAt)(labels, point);
28
+ if (labeled)
29
+ return editor.selectJoint(labeled);
30
+ const document = editor.getDocument();
31
+ const hit = (0, core_1.pickAnchor)(document.shapes, point, {
32
+ radius: exports.ANCHOR_HIT_RADIUS / scale,
33
+ tieTolerance: exports.ANCHOR_TIE_TOLERANCE / scale,
34
+ });
35
+ if (!hit)
36
+ return editor.selectJoint(null);
37
+ const existing = document.joints.find((j) => j.shapeId === hit.shapeId && j.anchorId === hit.anchorId);
38
+ if (existing) {
39
+ if (!document.jointDisplay.visible)
40
+ editor.commit((0, core_1.setJointDisplay)(document, { visible: true }));
41
+ return editor.selectJoint(existing.id);
42
+ }
43
+ const next = (0, core_1.addJoint)(document, { ...hit, namePrefix, maxNameLength: nameMaxLength });
44
+ const created = next.joints[next.joints.length - 1];
45
+ editor.preview(next);
46
+ editor.selectJoint(created.id);
47
+ editor.setNaming({ jointId: created.id, isNew: true });
48
+ }
49
+ /** Opens the name editor for an existing joint (NM-2). */
50
+ function beginRename(editor, jointId) {
51
+ editor.setNaming({ jointId, isNew: false });
52
+ }
53
+ /**
54
+ * Confirms the name editor with `raw`, trimmed and cut to `nameMaxLength` (NM-1). A new joint
55
+ * is committed with its name; an empty name cancels it. An existing joint is renamed, or
56
+ * keeps its name when `raw` is empty.
57
+ */
58
+ function confirmNaming(editor, raw, nameMaxLength) {
59
+ const { naming, preview } = editor.getState();
60
+ if (!naming)
61
+ return;
62
+ const name = (0, core_1.normalizeJointName)(raw, nameMaxLength);
63
+ if (naming.isNew) {
64
+ if (!name || !preview)
65
+ return cancelNaming(editor);
66
+ editor.setNaming(null);
67
+ editor.commit((0, core_1.renameJoint)(preview, naming.jointId, name, nameMaxLength));
68
+ return;
69
+ }
70
+ editor.setNaming(null);
71
+ if (name)
72
+ editor.commit((0, core_1.renameJoint)(editor.getDocument(), naming.jointId, name, nameMaxLength));
73
+ }
74
+ /** Closes the name editor without a change; a joint that was being created is dropped. */
75
+ function cancelNaming(editor) {
76
+ const { naming } = editor.getState();
77
+ if (!naming)
78
+ return;
79
+ editor.setNaming(null);
80
+ if (naming.isNew) {
81
+ editor.preview(null);
82
+ editor.selectJoint(null);
83
+ }
84
+ }
85
+ /** Sets the joint label size as one history entry; the size already active adds none (JS-4). */
86
+ function setJointLabelSize(editor, labelSize) {
87
+ const document = editor.getDocument();
88
+ const next = (0, core_1.setJointDisplay)(document, { labelSize });
89
+ if (next !== document)
90
+ editor.commit(next);
91
+ }
92
+ /**
93
+ * Shows or hides every joint as one history entry (JV-6). Hiding clears the joint
94
+ * selection and keeps the shape selection (JV-3); the joints stay in the document (JV-5).
95
+ */
96
+ function setJointsVisible(editor, visible) {
97
+ const document = editor.getDocument();
98
+ const next = (0, core_1.setJointDisplay)(document, { visible });
99
+ if (next === document)
100
+ return;
101
+ if (!visible)
102
+ editor.selectJoint(null);
103
+ editor.commit(next);
104
+ }
@@ -0,0 +1,51 @@
1
+ import { type DrawingDocument, type Group } from '@crux-diagram/core';
2
+ import type { EditorStore } from '../store.js';
3
+ /** Arrow-key move, in canvas units; `Shift` uses the large step (REA-015). */
4
+ export declare const NUDGE_STEP = 1;
5
+ export declare const NUDGE_STEP_LARGE = 10;
6
+ export type EditorCommand = {
7
+ readonly type: 'delete';
8
+ } | {
9
+ readonly type: 'undo';
10
+ } | {
11
+ readonly type: 'redo';
12
+ } | {
13
+ readonly type: 'duplicate';
14
+ } | {
15
+ readonly type: 'rotate90';
16
+ } | {
17
+ readonly type: 'front';
18
+ } | {
19
+ readonly type: 'back';
20
+ } | {
21
+ readonly type: 'group';
22
+ } | {
23
+ readonly type: 'ungroup';
24
+ } | {
25
+ readonly type: 'nudge';
26
+ readonly dx: number;
27
+ readonly dy: number;
28
+ };
29
+ /** The modifier and key fields of a `KeyboardEvent`. */
30
+ export interface KeyInput {
31
+ readonly key: string;
32
+ readonly ctrlKey: boolean;
33
+ readonly metaKey: boolean;
34
+ readonly shiftKey: boolean;
35
+ readonly altKey: boolean;
36
+ }
37
+ /** The command bound to a key press, or `null` when the key is not a shortcut. */
38
+ export declare function commandForKey(event: KeyInput): EditorCommand | null;
39
+ /** Whether key presses on `target` belong to a text field rather than to the editor. */
40
+ export declare function isTextEntry(target: EventTarget | null): boolean;
41
+ /** The group whose shapes are exactly `selection`, if any (a selected group, MS-6). */
42
+ export declare function wholeGroup(document: DrawingDocument, selection: readonly string[]): Group | undefined;
43
+ /**
44
+ * Runs `command` on the committed document. Each change is one history entry: delete
45
+ * removes the selected shapes with their joints (REC-071) and clears the selection;
46
+ * duplicate copies them as FR-20 does and selects the copies; nudge moves them without
47
+ * grid snapping, every center kept inside the canvas (FR-10); rotate90 turns a single
48
+ * shape a quarter turn clockwise (FR-17, REA-013; it has no key and serves the toolbar).
49
+ * Without a selection, or when nothing would change, it does nothing.
50
+ */
51
+ export declare function runCommand(editor: EditorStore, command: EditorCommand): void;