@daasboard/editor 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 (83) hide show
  1. package/LICENSE +5 -0
  2. package/README.md +6 -2
  3. package/dist/camera/CameraInput.d.ts +106 -0
  4. package/dist/camera/CameraInput.js +398 -0
  5. package/dist/clipboard/ObjectClipboard.d.ts +40 -0
  6. package/dist/clipboard/ObjectClipboard.js +243 -0
  7. package/dist/clipboard/PasteCommitError.d.ts +4 -0
  8. package/dist/clipboard/PasteCommitError.js +7 -0
  9. package/dist/clipboard/carrier.d.ts +5 -0
  10. package/dist/clipboard/carrier.js +59 -0
  11. package/dist/clipboard/objects.d.ts +21 -0
  12. package/dist/clipboard/objects.js +362 -0
  13. package/dist/clipboard/placement.d.ts +11 -0
  14. package/dist/clipboard/placement.js +21 -0
  15. package/dist/clipboard/systemImages.d.ts +1 -0
  16. package/dist/clipboard/systemImages.js +24 -0
  17. package/dist/index.d.ts +23 -0
  18. package/dist/index.js +22 -0
  19. package/dist/input/TouchInput.d.ts +159 -0
  20. package/dist/input/TouchInput.js +656 -0
  21. package/dist/input/visiblePart.d.ts +8 -0
  22. package/dist/input/visiblePart.js +15 -0
  23. package/dist/interaction/InteractionLayer.d.ts +83 -0
  24. package/dist/interaction/InteractionLayer.js +520 -0
  25. package/dist/props/PropertyController.d.ts +130 -0
  26. package/dist/props/PropertyController.js +1448 -0
  27. package/dist/props/PropertyRegistry.d.ts +38 -0
  28. package/dist/props/PropertyRegistry.js +21 -0
  29. package/dist/props/builtinProperties.d.ts +23 -0
  30. package/dist/props/builtinProperties.js +350 -0
  31. package/dist/props/defaults.d.ts +6 -0
  32. package/dist/props/defaults.js +29 -0
  33. package/dist/selection/SelectionModel.d.ts +25 -0
  34. package/dist/selection/SelectionModel.js +79 -0
  35. package/dist/text/CanvasEditorView.d.ts +99 -0
  36. package/dist/text/CanvasEditorView.js +308 -0
  37. package/dist/text/InputSink.d.ts +157 -0
  38. package/dist/text/InputSink.js +841 -0
  39. package/dist/text/TextEditSession.d.ts +173 -0
  40. package/dist/text/TextEditSession.js +1328 -0
  41. package/dist/text/clipboard.d.ts +11 -0
  42. package/dist/text/clipboard.js +489 -0
  43. package/dist/text/commands.d.ts +48 -0
  44. package/dist/text/commands.js +329 -0
  45. package/dist/text/contextMirror.d.ts +51 -0
  46. package/dist/text/contextMirror.js +292 -0
  47. package/dist/text/flat.d.ts +35 -0
  48. package/dist/text/flat.js +556 -0
  49. package/dist/text/graphemes.d.ts +16 -0
  50. package/dist/text/graphemes.js +219 -0
  51. package/dist/text/index.d.ts +13 -0
  52. package/dist/text/index.js +13 -0
  53. package/dist/text/inputReducer.d.ts +24 -0
  54. package/dist/text/inputReducer.js +44 -0
  55. package/dist/text/keymap.d.ts +17 -0
  56. package/dist/text/keymap.js +148 -0
  57. package/dist/text/schema.d.ts +12 -0
  58. package/dist/text/schema.js +44 -0
  59. package/dist/text/sessionFocus.d.ts +20 -0
  60. package/dist/text/sessionFocus.js +51 -0
  61. package/dist/text/types.d.ts +114 -0
  62. package/dist/text/types.js +1 -0
  63. package/dist/tools/CreateTool.d.ts +78 -0
  64. package/dist/tools/CreateTool.js +258 -0
  65. package/dist/tools/SelectTool.d.ts +155 -0
  66. package/dist/tools/SelectTool.js +808 -0
  67. package/dist/tools/ToolManager.d.ts +63 -0
  68. package/dist/tools/ToolManager.js +146 -0
  69. package/dist/tools/connectorEndpoints.d.ts +17 -0
  70. package/dist/tools/connectorEndpoints.js +53 -0
  71. package/dist/tools/frameRotation.d.ts +8 -0
  72. package/dist/tools/frameRotation.js +66 -0
  73. package/dist/tools/gradientHandles.d.ts +67 -0
  74. package/dist/tools/gradientHandles.js +110 -0
  75. package/dist/tools/presetPaths.d.ts +5 -0
  76. package/dist/tools/presetPaths.js +49 -0
  77. package/dist/tools/snapResize.d.ts +7 -0
  78. package/dist/tools/snapResize.js +65 -0
  79. package/dist/tools/snapping.d.ts +30 -0
  80. package/dist/tools/snapping.js +280 -0
  81. package/dist/tools/transform.d.ts +52 -0
  82. package/dist/tools/transform.js +277 -0
  83. package/package.json +36 -3
@@ -0,0 +1,808 @@
1
+ import { hitTest, marquee } from "@daasboard/render-core";
2
+ import { MAX_PRESENCE_DRAG_OBJECTS } from "@daasboard/doc";
3
+ import { canHaveText } from "../props/builtinProperties.js";
4
+ import { frameOf, gradientAt, gradientHandleAt, gradientHandleGeometry, GRADIENT_HANDLE_PX, linearPoints, MIN_GRADIENT_RADIUS, radialParams, snapLinear, worldToBox } from "./gradientHandles.js";
5
+ import { endpointAt, endpointMarkers, endpointPointKey, endpointsDraggable } from "./connectorEndpoints.js";
6
+ import { snapBox } from "./snapping.js";
7
+ import { snapResize } from "./snapResize.js";
8
+ import { cursorFor, handleAt, handleFrame, handlePositions, handlesSuppressed, isLocked, isTransformable, mapBoxInto, MIN_SIZE, nearestSide, pointerHit, pointerProfile, normalizeAngle, resizeBounds, rotatePoint, snapAngle, transformBounds, whole } from "./transform.js";
9
+ const DROP_CONTAINER_KINDS = new Set(["frame", "section"]);
10
+ const ENDPOINT_LABEL = "Move connector end";
11
+ export class SelectTool {
12
+ store;
13
+ selection;
14
+ ctrl;
15
+ mode = "idle";
16
+ handleProfile_;
17
+ cancelledPress = false;
18
+ pressWorld = { x: 0, y: 0 };
19
+ pressedId = null;
20
+ pressedWasSelected = false;
21
+ starts = new Map();
22
+ moveObjects = [];
23
+ snapDisabled = false;
24
+ guides = [];
25
+ sizeLabel = null;
26
+ marqueeRect = null;
27
+ hoverId = null;
28
+ hoverHandle = null;
29
+ hoverRot = 0;
30
+ handle = null;
31
+ transform = null;
32
+ editingId = null;
33
+ gradient = null;
34
+ gradientHover = null;
35
+ gradientDrag = null;
36
+ hoverEndpoint = null;
37
+ endpointDrag = null;
38
+ threshold = 3;
39
+ preciseGrab = true;
40
+ onEditText;
41
+ reparentOnDrop;
42
+ onPresenceDrag;
43
+ constructor(store, selection, ctrl, opts = {}) {
44
+ this.store = store;
45
+ this.selection = selection;
46
+ this.ctrl = ctrl;
47
+ this.reparentOnDrop = opts.reparentOnDrop ?? true;
48
+ this.onPresenceDrag = opts.onPresenceDrag;
49
+ }
50
+ state() {
51
+ const geo = this.gradientGeometry();
52
+ const gradient = geo && { ...geo, active: this.gradientDrag?.kind ?? this.gradientHover };
53
+ const endpoint = this.endpointDrag ? { id: this.endpointDrag.id, end: this.endpointDrag.end } : this.hoveredEndpoint();
54
+ return { ...(this.handleProfile_ ? { handleProfile: this.handleProfile_ } : {}), mode: this.mode, marquee: this.marqueeRect, hover: this.hoverId, handle: this.handle ?? this.hoverHandle, guides: this.guides, sizeLabel: this.sizeLabel, gradient, endpoint };
55
+ }
56
+ setPointerProfile(p) {
57
+ this.handleProfile_ = pointerProfile(p).handles;
58
+ }
59
+ cursor() {
60
+ switch (this.mode) {
61
+ case "moving":
62
+ return "move";
63
+ case "marquee":
64
+ return "crosshair";
65
+ case "rotating":
66
+ return "grabbing";
67
+ case "handle":
68
+ case "resizing":
69
+ return this.handle ? cursorFor(this.handle, this.transform?.frame.rot ?? 0) : "default";
70
+ case "gradient":
71
+ return this.gradientDrag?.kind === "stop" ? "default" : "grabbing";
72
+ case "endpoint":
73
+ return "grabbing";
74
+ default:
75
+ if (this.gradientHover)
76
+ return this.gradientHover === "stop" ? "default" : "grab";
77
+ if (this.hoverHandle)
78
+ return cursorFor(this.hoverHandle, this.hoverRot);
79
+ if (this.hoveredEndpoint())
80
+ return "grab";
81
+ return this.hoverId ? "move" : "default";
82
+ }
83
+ }
84
+ showGradientHandles(target, events, opts = {}) {
85
+ if (this.gradient)
86
+ this.hideGradientHandles(true);
87
+ const session = { target: { ...target }, events, phi: 0, presses: opts.presses ?? true };
88
+ this.gradient = session;
89
+ this.gradientHover = null;
90
+ return {
91
+ cancelDrag: () => {
92
+ if (this.gradient !== session || !this.gradientDrag)
93
+ return;
94
+ this.endGradientDrag(false);
95
+ this.cancelledPress = true;
96
+ },
97
+ hide: () => {
98
+ if (this.gradient === session)
99
+ this.hideGradientHandles(false);
100
+ },
101
+ };
102
+ }
103
+ gradientHandleAt(p) {
104
+ const geo = this.gradient?.presses ? this.gradientGeometry() : null;
105
+ return geo ? gradientHandleAt(geo, p, p.zoom, gradientTolerance(geo.frame, p)) : null;
106
+ }
107
+ pressTarget(p) {
108
+ const press = this.capturePress(p);
109
+ return press.kind === "object" ? (press.selected ? "selected" : "object") : press.kind === "endpoint" ? "handle" : press.kind;
110
+ }
111
+ capturePress(p) {
112
+ const geo = this.gradient?.presses ? this.gradientGeometry() : null;
113
+ const grad = geo && gradientHandleAt(geo, p, p.zoom, gradientTolerance(geo.frame, p));
114
+ if (geo && grad)
115
+ return { kind: "gradient", handle: grad, type: geo.type, target: { ...geo.target } };
116
+ const { all, written } = this.transformTargets();
117
+ const frame = handleFrame(all);
118
+ const profile = this.hitProfile(p);
119
+ const handle = frame && written.length ? handleAt(frame.box, p, p.zoom, p.tolerance, frame.rot, profile) : null;
120
+ if (handle)
121
+ return { kind: "handle", handle, ids: all.map((o) => o.id), profile };
122
+ const marker = this.endpointMarkerAt(p, profile);
123
+ if (marker)
124
+ return { kind: "endpoint", id: marker.id, end: marker.end, grab: { x: marker.at.x - p.x, y: marker.at.y - p.y } };
125
+ const hit = pointerHit(this.store, p);
126
+ return hit ? { kind: "object", id: hit.id, selected: this.selection.has(hit.id) } : { kind: "canvas" };
127
+ }
128
+ pressable(press) {
129
+ switch (press.kind) {
130
+ case "gradient": {
131
+ const geo = this.gradient?.presses ? this.gradientGeometry() : null;
132
+ return !!geo && geo.target.id === press.target.id && geo.target.layer === press.target.layer && geo.type === press.type
133
+ && (press.handle === "stop" ? geo.stops.length > 0 : !!geo.handles[press.handle]);
134
+ }
135
+ case "handle": {
136
+ const { all, written } = this.transformTargets();
137
+ return written.length > 0 && all.length === press.ids.length && all.every((o, i) => o.id === press.ids[i]);
138
+ }
139
+ case "endpoint":
140
+ return this.endpointConnector(press.id) !== null;
141
+ case "object": {
142
+ const o = this.store.scene.objects.get(press.id);
143
+ return !!o && !o.hidden;
144
+ }
145
+ case "canvas":
146
+ return true;
147
+ }
148
+ }
149
+ hitProfile(p) {
150
+ const drawn = this.handleProfile_;
151
+ const own = pointerProfile(p).handles;
152
+ const mouse = p.pointer !== "touch" && p.pointer !== "pen";
153
+ const sameGeometry = drawn
154
+ ? drawn.sizePx === own.sizePx && drawn.shape === own.shape && drawn.minSidePx === own.minSidePx && drawn.rotateGapPx === own.rotateGapPx
155
+ : mouse;
156
+ return sameGeometry ? (mouse ? undefined : own) : drawn;
157
+ }
158
+ viewScene() {
159
+ const base = this.store.scene;
160
+ if (this.ctrl.previewOverlay().size === 0)
161
+ return base;
162
+ const ctrl = this.ctrl;
163
+ const objects = {
164
+ get: (id) => {
165
+ const o = base.objects.get(id);
166
+ return o ? ctrl.effective(o) : undefined;
167
+ },
168
+ };
169
+ return { objects, order: base.order };
170
+ }
171
+ endpointConnector(id) {
172
+ if (this.selection.size !== 1 || !this.selection.has(id))
173
+ return null;
174
+ const o = this.store.scene.objects.get(id);
175
+ return endpointsDraggable(o, this.store.scene) ? o : null;
176
+ }
177
+ hoveredEndpoint() {
178
+ return this.hoverEndpoint && this.endpointConnector(this.hoverEndpoint.id) ? this.hoverEndpoint : null;
179
+ }
180
+ endpointHandles() {
181
+ if (this.selection.size !== 1)
182
+ return [];
183
+ const [id] = this.selection.ids;
184
+ const o = this.endpointConnector(id);
185
+ return o ? endpointMarkers(this.ctrl.effective(o), this.viewScene()) ?? [] : [];
186
+ }
187
+ endpointMarkerAt(p, profile) {
188
+ return endpointAt(this.endpointHandles(), p, p.zoom, p.tolerance, profile);
189
+ }
190
+ previewEndpoint(p) {
191
+ const drag = this.endpointDrag;
192
+ const o = this.endpointConnector(drag.id);
193
+ if (!o) {
194
+ this.endEndpointDrag();
195
+ return false;
196
+ }
197
+ drag.moved = true;
198
+ const at = { x: p.x + drag.grab.x, y: p.y + drag.grab.y };
199
+ const other = o.conn[drag.end === "start" ? "end" : "start"]?.ref;
200
+ const hit = pointerHit(this.store, { ...p, x: at.x, y: at.y }, isTransformable);
201
+ const key = `conn.${drag.end}`;
202
+ let patch;
203
+ this.guides = [];
204
+ if (hit && hit.id !== other)
205
+ patch = { [key]: { ref: hit.id, side: nearestSide(hit, at) } };
206
+ else {
207
+ const snap = this.snapDisabled ? null : snapBox(this.store, { x: at.x, y: at.y, w: 0, h: 0 }, p.zoom, undefined, { spacing: false });
208
+ if (snap)
209
+ this.guides = snap.guides;
210
+ patch = { [key]: {}, [`conn.${endpointPointKey(drag.end)}`]: [at.x + (snap?.dx ?? 0), at.y + (snap?.dy ?? 0)] };
211
+ }
212
+ this.ctrl.preview((t) => (t.id === drag.id ? patch : {}), ENDPOINT_LABEL);
213
+ return true;
214
+ }
215
+ endEndpointDrag() {
216
+ const drag = this.endpointDrag;
217
+ if (!drag)
218
+ return;
219
+ this.endpointDrag = null;
220
+ if (drag.moved)
221
+ this.ctrl.cancelPreview();
222
+ if (this.mode === "endpoint")
223
+ this.mode = "idle";
224
+ this.guides = [];
225
+ }
226
+ gradientHandleTarget() {
227
+ return this.gradient ? { ...this.gradient.target } : null;
228
+ }
229
+ gradientGeometry() {
230
+ const now = this.gradientNow();
231
+ return now && gradientHandleGeometry(now.o, this.gradient.target, this.gradient.phi);
232
+ }
233
+ gradientNow() {
234
+ const s = this.gradient;
235
+ if (!s || this.selection.size !== 1 || !this.selection.has(s.target.id) || s.target.id === this.editingId)
236
+ return null;
237
+ const base = this.store.scene.objects.get(s.target.id);
238
+ if (!base || base.hidden)
239
+ return null;
240
+ const o = this.ctrl.effective(base);
241
+ const g = gradientAt(base, s.target.layer);
242
+ const shown = !g || !(o.w > 0 && o.h > 0) ? null : o === base ? g : gradientAt(o, s.target.layer);
243
+ const type = this.gradientDrag?.type;
244
+ return g && shown && (!type || (g.type === type && shown.type === type)) ? { o, g } : null;
245
+ }
246
+ hideGradientHandles(notify) {
247
+ this.endGradientDrag(notify);
248
+ this.gradient = null;
249
+ this.gradientHover = null;
250
+ }
251
+ endGradientDrag(notify) {
252
+ const drag = this.gradientDrag;
253
+ if (!drag)
254
+ return;
255
+ this.gradientDrag = null;
256
+ if (this.mode === "gradient")
257
+ this.mode = "idle";
258
+ if (this.gradient)
259
+ this.gradient.phi = drag.phi;
260
+ if (notify && drag.last)
261
+ this.gradient?.events.cancel();
262
+ }
263
+ beginGradientDrag(geo, kind, p) {
264
+ const center = kind === "stop" ? null : geo.handles[kind];
265
+ this.gradientDrag = { kind, type: geo.type, grab: center ? { x: center.x - p.x, y: center.y - p.y } : { x: 0, y: 0 }, phi: this.gradient.phi, last: null, at: null };
266
+ this.gradientHover = null;
267
+ this.hoverHandle = null;
268
+ this.hoverEndpoint = null;
269
+ this.hoverId = null;
270
+ this.mode = "gradient";
271
+ }
272
+ gradientEdit(p, now = this.gradientNow()) {
273
+ if (!now)
274
+ return "gone";
275
+ const { o, g } = now;
276
+ const drag = this.gradientDrag;
277
+ const { w, h } = o;
278
+ const q = worldToBox(frameOf(o), { x: p.x + drag.grab.x, y: p.y + drag.grab.y });
279
+ if (g.type === "linear") {
280
+ const { p0, p1 } = linearPoints(g, w, h);
281
+ const fixed = drag.kind === "p0" ? p1 : p0;
282
+ const moving = p.shift ? snapLinear(fixed, q) : q;
283
+ if (Math.hypot(moving[0] - fixed[0], moving[1] - fixed[1]) <= 1e-6 * Math.max(w, h))
284
+ return "hold";
285
+ return drag.kind === "p0" ? { op: "axis", p0: moving, p1: fixed } : { op: "axis", p0: fixed, p1: moving };
286
+ }
287
+ if (drag.kind === "center")
288
+ return { op: "radial", cx: q[0] / w, cy: q[1] / h };
289
+ const { cx, cy } = radialParams(g);
290
+ const ux = q[0] / w - cx;
291
+ const uy = q[1] / h - cy;
292
+ if (ux || uy)
293
+ this.gradient.phi = Math.atan2(uy, ux);
294
+ return { op: "radial", r: Math.max(MIN_GRADIENT_RADIUS, Math.hypot(ux, uy)) };
295
+ }
296
+ setTextEditing(id) {
297
+ this.editingId = id;
298
+ }
299
+ transformTargets() {
300
+ const gradient = this.gradientGeometry()?.target.id;
301
+ const all = this.ctrl.targetsFor("x").filter((o) => isTransformable(o) && !isLocked(o, this.store.scene) && o.id !== this.editingId && o.id !== gradient);
302
+ const ids = new Set(all.map((o) => o.id));
303
+ const written = all.filter((o) => !this.hasAncestorIn(o, ids));
304
+ return { all, written };
305
+ }
306
+ hasAncestorIn(o, ids) {
307
+ for (let p = o.parent; p; p = this.store.scene.objects.get(p)?.parent ?? null)
308
+ if (ids.has(p))
309
+ return true;
310
+ return false;
311
+ }
312
+ transformBounds() {
313
+ return transformBounds(this.transformTargets().all.map((o) => this.ctrl.effective(o)));
314
+ }
315
+ handleFrame() {
316
+ return handleFrame(this.transformTargets().all.map((o) => this.ctrl.effective(o)));
317
+ }
318
+ handlePositions(zoom, p) {
319
+ const f = this.handleFrame();
320
+ const profile = p ? pointerProfile(p).handles : this.handleProfile_;
321
+ return f && !handlesSuppressed(f.box, zoom, profile) ? handlePositions(f.box, zoom, f.rot, profile) : null;
322
+ }
323
+ hover(p) {
324
+ this.setPointerProfile(p);
325
+ if (this.mode !== "idle")
326
+ return;
327
+ this.gradientHover = this.gradientHandleAt(p);
328
+ if (this.gradientHover) {
329
+ this.hoverHandle = null;
330
+ this.hoverEndpoint = null;
331
+ this.hoverId = null;
332
+ return;
333
+ }
334
+ const f = this.handleFrame();
335
+ const profile = p.pointer === "touch" || p.pointer === "pen" ? this.handleProfile_ : undefined;
336
+ this.hoverHandle = f ? handleAt(f.box, p, p.zoom, p.tolerance, f.rot, profile) : null;
337
+ this.hoverRot = f?.rot ?? 0;
338
+ if (this.hoverHandle) {
339
+ this.hoverEndpoint = null;
340
+ this.hoverId = null;
341
+ return;
342
+ }
343
+ const marker = this.endpointMarkerAt(p, profile);
344
+ this.hoverEndpoint = marker && { id: marker.id, end: marker.end };
345
+ if (marker) {
346
+ this.hoverId = null;
347
+ return;
348
+ }
349
+ const hit = pointerHit(this.store, p);
350
+ this.hoverId = hit?.id ?? null;
351
+ }
352
+ pointerDown(p, press) {
353
+ this.endEndpointDrag();
354
+ if (press && !this.pressable(press))
355
+ return false;
356
+ const hitProfile = press?.kind === "handle" ? press.profile : this.hitProfile(p);
357
+ this.setPointerProfile(p);
358
+ this.cancelledPress = false;
359
+ this.snapDisabled = !!p.snapOff;
360
+ this.guides = [];
361
+ this.sizeLabel = null;
362
+ this.pressWorld = { x: p.x, y: p.y };
363
+ this.threshold = pointerProfile(p).dragThreshold;
364
+ this.preciseGrab = p.pointer !== "touch" && p.pointer !== "pen";
365
+ const grad = press ? (press.kind === "gradient" ? press.handle : null) : this.gradientHandleAt(p);
366
+ if (grad) {
367
+ this.beginGradientDrag(this.gradientGeometry(), grad, p);
368
+ return true;
369
+ }
370
+ const { all, written } = this.transformTargets();
371
+ const frame = handleFrame(all);
372
+ const handle = press ? (press.kind === "handle" ? press.handle : null) : frame ? handleAt(frame.box, p, p.zoom, p.tolerance, frame.rot, hitProfile) : null;
373
+ if (frame && handle && written.length) {
374
+ const boxes = new Map();
375
+ for (const o of written)
376
+ boxes.set(o.id, { x: o.x, y: o.y, w: o.w, h: o.h, rot: o.rot });
377
+ const centre = { x: frame.box.x + frame.box.w / 2, y: frame.box.y + frame.box.h / 2 };
378
+ const hc = handlePositions(frame.box, p.zoom, 0, hitProfile)[handle];
379
+ const lp = frame.rot ? rotatePoint(p, centre, -frame.rot) : p;
380
+ this.transform = { frame, boxes, angle0: Math.atan2(p.y - centre.y, p.x - centre.x), grab: { x: hc.x - lp.x, y: hc.y - lp.y } };
381
+ if (handle === "rotate")
382
+ this.ctrl.beginRotation(this.editingId ?? undefined);
383
+ this.handle = handle;
384
+ this.hoverHandle = null;
385
+ this.mode = "handle";
386
+ return true;
387
+ }
388
+ const marker = press ? null : this.endpointMarkerAt(p, hitProfile);
389
+ const endpoint = press?.kind === "endpoint" ? press : marker && { id: marker.id, end: marker.end, grab: { x: marker.at.x - p.x, y: marker.at.y - p.y } };
390
+ if (endpoint) {
391
+ this.endpointDrag = { id: endpoint.id, end: endpoint.end, grab: { ...endpoint.grab }, moved: false };
392
+ this.hoverEndpoint = null;
393
+ this.hoverHandle = null;
394
+ this.hoverId = null;
395
+ this.mode = "endpoint";
396
+ return true;
397
+ }
398
+ const hit = press ? (press.kind === "object" ? this.store.scene.objects.get(press.id) : null) : pointerHit(this.store, p);
399
+ if (hit) {
400
+ this.pressedId = hit.id;
401
+ this.pressedWasSelected = this.selection.has(hit.id);
402
+ if (p.shift)
403
+ this.selection.toggle(hit.id);
404
+ else if (!this.pressedWasSelected)
405
+ this.selection.set([hit.id]);
406
+ this.mode = "pressed";
407
+ }
408
+ else {
409
+ this.pressedId = null;
410
+ if (!p.shift)
411
+ this.selection.clear();
412
+ this.mode = "marquee";
413
+ this.marqueeRect = { x: p.x, y: p.y, w: 0, h: 0 };
414
+ }
415
+ return true;
416
+ }
417
+ pointerMove(p) {
418
+ this.setPointerProfile(p);
419
+ this.snapDisabled ||= !!p.snapOff;
420
+ const dx = p.x - this.pressWorld.x;
421
+ const dy = p.y - this.pressWorld.y;
422
+ switch (this.mode) {
423
+ case "idle":
424
+ this.hover(p);
425
+ return;
426
+ case "pressed": {
427
+ if (Math.abs(dx) < this.threshold && Math.abs(dy) < this.threshold)
428
+ return;
429
+ const movable = this.ctrl.targetsFor("x").filter((o) => !isLocked(o, this.store.scene));
430
+ if (!movable.length)
431
+ return;
432
+ this.mode = "moving";
433
+ this.starts.clear();
434
+ this.moveObjects = movable;
435
+ for (const o of movable)
436
+ this.starts.set(o.id, { x: o.x, y: o.y });
437
+ }
438
+ case "moving": {
439
+ const starts = this.starts;
440
+ const initial = transformBounds(this.moveObjects);
441
+ const box = initial && { ...initial, x: initial.x + dx, y: initial.y + dy };
442
+ const snap = box && !this.snapDisabled ? snapBox(this.store, box, p.zoom, new Set(starts.keys())) : { dx: 0, dy: 0, guides: [] };
443
+ this.guides = snap.guides;
444
+ const snapX = this.guides.some((g) => g.axis === "x");
445
+ const snapY = this.guides.some((g) => g.axis === "y");
446
+ const moveX = snapX && box && initial ? stableCoordinate(box.x + snap.dx - initial.x) : dx;
447
+ const moveY = snapY && box && initial ? stableCoordinate(box.y + snap.dy - initial.y) : dy;
448
+ this.ctrl.preview((o) => {
449
+ const s = starts.get(o.id);
450
+ if (!s)
451
+ return {};
452
+ return { x: snapX ? stableCoordinate(s.x + moveX) : whole(s.x + moveX), y: snapY ? stableCoordinate(s.y + moveY) : whole(s.y + moveY) };
453
+ }, "Move");
454
+ this.publishPresenceDrag();
455
+ return;
456
+ }
457
+ case "marquee": {
458
+ this.marqueeRect = { x: Math.min(p.x, this.pressWorld.x), y: Math.min(p.y, this.pressWorld.y), w: Math.abs(dx), h: Math.abs(dy) };
459
+ return;
460
+ }
461
+ case "handle":
462
+ if (this.stillPressed(dx, dy))
463
+ return;
464
+ this.mode = this.handle === "rotate" ? "rotating" : "resizing";
465
+ case "resizing":
466
+ case "rotating":
467
+ if (this.mode === "rotating")
468
+ this.previewRotate(p);
469
+ else
470
+ this.previewResize(p);
471
+ this.publishPresenceDrag();
472
+ return;
473
+ case "gradient": {
474
+ const drag = this.gradientDrag;
475
+ if (drag.kind === "stop" || (!drag.last && this.stillPressed(dx, dy)))
476
+ return;
477
+ const edit = this.gradientEdit(p);
478
+ if (edit === "gone") {
479
+ this.endGradientDrag(true);
480
+ this.cancelledPress = true;
481
+ return;
482
+ }
483
+ if (edit === "hold")
484
+ return;
485
+ drag.last = edit;
486
+ drag.at = p;
487
+ this.gradient.events.preview(edit);
488
+ return;
489
+ }
490
+ case "endpoint":
491
+ if (!this.endpointDrag.moved && Math.abs(dx) < this.threshold && Math.abs(dy) < this.threshold)
492
+ return;
493
+ if (!this.previewEndpoint(p))
494
+ this.cancelledPress = true;
495
+ return;
496
+ }
497
+ }
498
+ stillPressed(dx, dy) {
499
+ return this.preciseGrab ? dx === 0 && dy === 0 : Math.abs(dx) < this.threshold && Math.abs(dy) < this.threshold;
500
+ }
501
+ publishPresenceDrag() {
502
+ if (!this.onPresenceDrag)
503
+ return;
504
+ const objects = [];
505
+ const seen = new Set();
506
+ const append = (base) => {
507
+ if (base.hidden || seen.has(base.id))
508
+ return;
509
+ seen.add(base.id);
510
+ const { id, x, y, w, h, rot } = this.ctrl.effective(base);
511
+ objects.push({ id, x, y, w, h, rot });
512
+ };
513
+ for (const id of this.ctrl.previewOverlay().keys()) {
514
+ const base = this.store.scene.objects.get(id);
515
+ if (!base)
516
+ continue;
517
+ append(base);
518
+ if (objects.length === MAX_PRESENCE_DRAG_OBJECTS)
519
+ break;
520
+ if (this.ctrl.subtreeDelta(base)) {
521
+ const ancestors = new Set([id]);
522
+ for (let i = this.store.rankOf(id) + 1; i < this.store.scene.order.length; i++) {
523
+ const child = this.store.scene.objects.get(this.store.scene.order[i]);
524
+ if (!child || !this.hasAncestorIn(child, ancestors))
525
+ break;
526
+ append(child);
527
+ if (objects.length === MAX_PRESENCE_DRAG_OBJECTS)
528
+ break;
529
+ }
530
+ }
531
+ if (objects.length === MAX_PRESENCE_DRAG_OBJECTS)
532
+ break;
533
+ }
534
+ this.onPresenceDrag(objects.length ? { objects } : null);
535
+ }
536
+ previewResize(p) {
537
+ const t = this.transform;
538
+ const { box, rot } = t.frame;
539
+ const centre = { x: box.x + box.w / 2, y: box.y + box.h / 2 };
540
+ const lp = rot ? rotatePoint(p, centre, -rot) : p;
541
+ const pointer = { x: lp.x + t.grab.x, y: lp.y + t.grab.y };
542
+ const image = t.boxes.size === 1 && this.store.scene.objects.get(t.boxes.keys().next().value)?.kind === "image";
543
+ const mods = { shift: image ? !p.shift : p.shift, alt: p.alt, aspectEdges: image };
544
+ const snapped = this.snapDisabled
545
+ ? { box: resizeBounds(box, this.handle, pointer, mods), guides: [] }
546
+ : snapResize(this.store, t.frame, this.handle, pointer, mods, p.zoom, new Set(t.boxes.keys()));
547
+ const nb = snapped.box;
548
+ this.guides = snapped.guides;
549
+ const boxes = t.boxes;
550
+ const applyPreview = () => {
551
+ const round = { x: !this.guides.some(g => g.axis === "x"), y: !this.guides.some(g => g.axis === "y") };
552
+ const finishX = round.x ? whole : (v) => v;
553
+ const finishY = round.y ? whole : (v) => v;
554
+ if (rot || image) {
555
+ const exactAspect = image && mods.shift;
556
+ const finish = !exactAspect && round.x && round.y ? whole : (v) => v;
557
+ const w = Math.max(MIN_SIZE, finish(nb.w));
558
+ const h = Math.max(MIN_SIZE, finish(nb.h));
559
+ const c = rotatePoint({ x: nb.x + nb.w / 2, y: nb.y + nb.h / 2 }, centre, rot);
560
+ const next = { x: exactAspect ? c.x - w / 2 : finishX(c.x - w / 2), y: exactAspect ? c.y - h / 2 : finishY(c.y - h / 2), w, h };
561
+ this.ctrl.preview((o) => (boxes.has(o.id) ? changedKeys(o, next) : {}), "Resize");
562
+ }
563
+ else {
564
+ this.ctrl.preview((o) => {
565
+ const s = boxes.get(o.id);
566
+ if (!s)
567
+ return {};
568
+ return changedKeys(o, mapBoxInto(s, box, nb, s.rot, round));
569
+ }, "Resize");
570
+ }
571
+ };
572
+ applyPreview();
573
+ let effective = this.handleFrame();
574
+ if (effective && this.handle && this.handle !== "rotate") {
575
+ const active = handlePositions(effective.box, p.zoom, effective.rot, this.handleProfile_)[this.handle];
576
+ const count = this.guides.length;
577
+ this.guides = this.guides.filter((g) => Math.abs(active[g.axis] - g.at) < 1e-7);
578
+ if (this.guides.length !== count) {
579
+ applyPreview();
580
+ effective = this.handleFrame();
581
+ }
582
+ }
583
+ const extent = this.transformBounds();
584
+ this.sizeLabel = effective && extent ? { width: effective.box.w, height: effective.box.h, at: { x: extent.x + extent.w, y: extent.y + extent.h / 2 } } : null;
585
+ }
586
+ previewRotate(p) {
587
+ const t = this.transform;
588
+ const { box } = t.frame;
589
+ const centre = { x: box.x + box.w / 2, y: box.y + box.h / 2 };
590
+ let delta = Math.atan2(p.y - centre.y, p.x - centre.x) - t.angle0;
591
+ const single = t.boxes.size === 1;
592
+ if (p.shift && !single)
593
+ delta = snapAngle(delta);
594
+ if (p.shift && single) {
595
+ const initial = t.boxes.values().next().value.rot;
596
+ delta = normalizeAngle(snapAngle(initial + delta)) - initial;
597
+ }
598
+ this.ctrl.previewRotation(delta, single ? undefined : centre);
599
+ }
600
+ pointerUp(p) {
601
+ this.setPointerProfile(p);
602
+ this.cancelledPress = false;
603
+ switch (this.mode) {
604
+ case "handle":
605
+ if (this.handle === "rotate")
606
+ this.ctrl.cancelPreview();
607
+ break;
608
+ case "pressed":
609
+ if (this.pressedId && this.pressedWasSelected && !p.shift)
610
+ this.selection.set([this.pressedId]);
611
+ break;
612
+ case "moving":
613
+ this.ctrl.commitPreview({ dropTarget: this.reparentOnDrop ? this.dropContainerAt(p) : undefined });
614
+ break;
615
+ case "resizing":
616
+ case "rotating":
617
+ this.ctrl.commitPreview();
618
+ break;
619
+ case "gradient": {
620
+ const drag = this.gradientDrag;
621
+ const now = drag?.at ? this.gradientNow() : null;
622
+ const edit = drag?.at && now ? this.gradientEdit(drag.at, now) : "gone";
623
+ if (drag && now && typeof edit === "object" && !sameEdit(edit, unchangedEdit(drag.kind, now.g, now.o.w, now.o.h))) {
624
+ this.gradientDrag = null;
625
+ this.gradient?.events.commit(edit);
626
+ }
627
+ else
628
+ this.endGradientDrag(true);
629
+ break;
630
+ }
631
+ case "endpoint": {
632
+ const drag = this.endpointDrag;
633
+ this.snapDisabled ||= !!p.snapOff;
634
+ if (drag?.moved && this.previewEndpoint(p))
635
+ this.ctrl.commitPreview();
636
+ this.endpointDrag = null;
637
+ break;
638
+ }
639
+ case "marquee": {
640
+ const r = this.marqueeRect;
641
+ if (r && (r.w > this.threshold || r.h > this.threshold)) {
642
+ const hits = marquee(this.store, r, !p.alt)
643
+ .filter((o) => !isLocked(o, this.store.scene))
644
+ .map((o) => o.id);
645
+ if (p.shift)
646
+ this.selection.add(hits);
647
+ else
648
+ this.selection.set(hits);
649
+ }
650
+ this.marqueeRect = null;
651
+ break;
652
+ }
653
+ }
654
+ this.mode = "idle";
655
+ this.guides = [];
656
+ this.sizeLabel = null;
657
+ this.pressedId = null;
658
+ this.handle = null;
659
+ this.transform = null;
660
+ this.onPresenceDrag?.(null);
661
+ }
662
+ dropContainerAt(p) {
663
+ const dragged = this.ctrl.previewOverlay();
664
+ for (const hit of hitTest(this.store, p.x, p.y)) {
665
+ if (!DROP_CONTAINER_KINDS.has(hit.kind) || dragged.has(hit.id))
666
+ continue;
667
+ let inDragged = false;
668
+ for (let a = hit.parent; a && !inDragged; a = this.store.scene.objects.get(a)?.parent ?? null)
669
+ inDragged = dragged.has(a);
670
+ if (!inDragged)
671
+ return hit.id;
672
+ }
673
+ return null;
674
+ }
675
+ doubleClick(p) {
676
+ this.setPointerProfile(p);
677
+ if (this.gradientHandleAt(p))
678
+ return;
679
+ const hit = pointerHit(this.store, p);
680
+ if (!hit || !canHaveText(hit))
681
+ return;
682
+ this.onEditText?.(hit.id, { x: p.x, y: p.y });
683
+ }
684
+ editableSelected() {
685
+ if (this.selection.size !== 1)
686
+ return null;
687
+ const o = this.store.scene.objects.get(this.selection.toArray()[0]);
688
+ return o && !o.hidden && canHaveText(o) ? o : null;
689
+ }
690
+ cancel() {
691
+ this.cancelledPress = false;
692
+ if (this.mode === "moving" || this.mode === "resizing" || this.mode === "rotating" || (this.mode === "handle" && this.handle === "rotate"))
693
+ this.ctrl.cancelPreview();
694
+ this.endGradientDrag(true);
695
+ this.endEndpointDrag();
696
+ this.hoverEndpoint = null;
697
+ this.gradientHover = null;
698
+ this.marqueeRect = null;
699
+ this.guides = [];
700
+ this.sizeLabel = null;
701
+ this.mode = "idle";
702
+ this.pressedId = null;
703
+ this.handle = null;
704
+ this.transform = null;
705
+ this.hoverId = null;
706
+ this.hoverHandle = null;
707
+ this.hoverRot = 0;
708
+ this.onPresenceDrag?.(null);
709
+ }
710
+ key(key, mods) {
711
+ if (this.cancelledPress || (this.mode !== "idle" && key !== "Escape"))
712
+ return true;
713
+ switch (key) {
714
+ case "Escape":
715
+ if (this.mode !== "idle") {
716
+ this.cancel();
717
+ this.cancelledPress = true;
718
+ }
719
+ else
720
+ this.selection.clear();
721
+ return true;
722
+ case "Delete":
723
+ case "Backspace":
724
+ if (this.ctrl.commands.delete.canRun())
725
+ this.ctrl.commands.delete.run();
726
+ return true;
727
+ case "Enter":
728
+ case "F2": {
729
+ if (mods.ctrl || !this.onEditText || this.mode !== "idle")
730
+ return false;
731
+ const o = this.editableSelected();
732
+ if (!o)
733
+ return false;
734
+ this.onEditText(o.id);
735
+ return true;
736
+ }
737
+ case "z":
738
+ case "Z":
739
+ if (!mods.ctrl)
740
+ return false;
741
+ if (mods.shift)
742
+ this.ctrl.commands.redo.run();
743
+ else
744
+ this.ctrl.commands.undo.run();
745
+ return true;
746
+ case "y":
747
+ case "Y":
748
+ if (!mods.ctrl)
749
+ return false;
750
+ this.ctrl.commands.redo.run();
751
+ return true;
752
+ case "ArrowLeft":
753
+ case "ArrowRight":
754
+ case "ArrowUp":
755
+ case "ArrowDown": {
756
+ if (!this.selection.size)
757
+ return false;
758
+ const step = mods.shift ? 10 : 1;
759
+ const dx = key === "ArrowLeft" ? -step : key === "ArrowRight" ? step : 0;
760
+ const dy = key === "ArrowUp" ? -step : key === "ArrowDown" ? step : 0;
761
+ const scene = this.store.scene;
762
+ this.ctrl.set((o) => (isLocked(o, scene) ? {} : { x: o.x + dx, y: o.y + dy }), { label: "Nudge" });
763
+ return true;
764
+ }
765
+ case "]":
766
+ case "}":
767
+ if (mods.ctrl)
768
+ (mods.shift || key === "}" ? this.ctrl.commands.bringToFront : this.ctrl.commands.bringForward).run();
769
+ return mods.ctrl;
770
+ case "[":
771
+ case "{":
772
+ if (mods.ctrl)
773
+ (mods.shift || key === "{" ? this.ctrl.commands.sendToBack : this.ctrl.commands.sendBackward).run();
774
+ return mods.ctrl;
775
+ }
776
+ return false;
777
+ }
778
+ }
779
+ const stableCoordinate = (value) => Math.round(value * 1e9) / 1e9 || 0;
780
+ function gradientTolerance(f, p) {
781
+ if (p.pointer === "pen")
782
+ return p.tolerance * 1.5;
783
+ if (p.pointer !== "touch")
784
+ return p.tolerance;
785
+ const [x, y] = worldToBox(f, p);
786
+ const roundoff = f.rot ? 8 * Number.EPSILON * Math.max(1, Math.abs(f.x), Math.abs(f.y), f.w, f.h, Math.abs(p.x), Math.abs(p.y)) : 0;
787
+ const inBox = x >= -roundoff && x <= f.w + roundoff && y >= -roundoff && y <= f.h + roundoff;
788
+ return ((inBox ? 10 : 22) * p.tolerance) / 4 - GRADIENT_HANDLE_PX / 2 / p.zoom;
789
+ }
790
+ function unchangedEdit(kind, g, w, h) {
791
+ if (g.type === "linear")
792
+ return { op: "axis", ...linearPoints(g, w, h) };
793
+ const { cx, cy, r } = radialParams(g);
794
+ return kind === "center" ? { op: "radial", cx, cy } : { op: "radial", r };
795
+ }
796
+ function sameEdit(a, b) {
797
+ const near = (x, y) => x === y || (x !== undefined && y !== undefined && Math.abs(x - y) <= 1e-9 * Math.max(1, Math.abs(x), Math.abs(y)));
798
+ if (a.op === "axis" && b.op === "axis")
799
+ return [0, 1].every((i) => near(a.p0[i], b.p0[i]) && near(a.p1[i], b.p1[i]));
800
+ return a.op === "radial" && b.op === "radial" && near(a.cx, b.cx) && near(a.cy, b.cy) && near(a.r, b.r);
801
+ }
802
+ function changedKeys(o, next) {
803
+ const out = {};
804
+ for (const [k, v] of Object.entries(next))
805
+ if (v !== undefined && Number.isFinite(v) && o[k] !== v)
806
+ out[k] = v;
807
+ return out;
808
+ }