@salmonui/editors 0.0.0-stage → 0.3.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 (111) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +39 -2
  3. package/dist/image-editor/ImageEditor.d.ts +59 -0
  4. package/dist/image-editor/ImageEditor.d.ts.map +1 -0
  5. package/dist/image-editor/ImageEditor.js +493 -0
  6. package/dist/image-editor/ImageEditor.js.map +1 -0
  7. package/dist/image-editor/adjust.d.ts +17 -0
  8. package/dist/image-editor/adjust.d.ts.map +1 -0
  9. package/dist/image-editor/adjust.js +53 -0
  10. package/dist/image-editor/adjust.js.map +1 -0
  11. package/dist/image-editor/annotations.d.ts +40 -0
  12. package/dist/image-editor/annotations.d.ts.map +1 -0
  13. package/dist/image-editor/annotations.js +184 -0
  14. package/dist/image-editor/annotations.js.map +1 -0
  15. package/dist/image-editor/draw.d.ts +25 -0
  16. package/dist/image-editor/draw.d.ts.map +1 -0
  17. package/dist/image-editor/draw.js +248 -0
  18. package/dist/image-editor/draw.js.map +1 -0
  19. package/dist/image-editor/engine.d.ts +116 -0
  20. package/dist/image-editor/engine.d.ts.map +1 -0
  21. package/dist/image-editor/engine.js +367 -0
  22. package/dist/image-editor/engine.js.map +1 -0
  23. package/dist/image-editor/geometry.d.ts +41 -0
  24. package/dist/image-editor/geometry.d.ts.map +1 -0
  25. package/dist/image-editor/geometry.js +62 -0
  26. package/dist/image-editor/geometry.js.map +1 -0
  27. package/dist/image-editor/history.d.ts +24 -0
  28. package/dist/image-editor/history.d.ts.map +1 -0
  29. package/dist/image-editor/history.js +31 -0
  30. package/dist/image-editor/history.js.map +1 -0
  31. package/dist/image-editor/interact.d.ts +73 -0
  32. package/dist/image-editor/interact.d.ts.map +1 -0
  33. package/dist/image-editor/interact.js +686 -0
  34. package/dist/image-editor/interact.js.map +1 -0
  35. package/dist/image-editor/items.d.ts +9 -0
  36. package/dist/image-editor/items.d.ts.map +1 -0
  37. package/dist/image-editor/items.js +179 -0
  38. package/dist/image-editor/items.js.map +1 -0
  39. package/dist/image-editor/ops.d.ts +35 -0
  40. package/dist/image-editor/ops.d.ts.map +1 -0
  41. package/dist/image-editor/ops.js +332 -0
  42. package/dist/image-editor/ops.js.map +1 -0
  43. package/dist/image-editor/panels.d.ts +24 -0
  44. package/dist/image-editor/panels.d.ts.map +1 -0
  45. package/dist/image-editor/panels.js +185 -0
  46. package/dist/image-editor/panels.js.map +1 -0
  47. package/dist/image-editor/pixels.d.ts +10 -0
  48. package/dist/image-editor/pixels.d.ts.map +1 -0
  49. package/dist/image-editor/pixels.js +172 -0
  50. package/dist/image-editor/pixels.js.map +1 -0
  51. package/dist/image-editor/transform.d.ts +33 -0
  52. package/dist/image-editor/transform.d.ts.map +1 -0
  53. package/dist/image-editor/transform.js +142 -0
  54. package/dist/image-editor/transform.js.map +1 -0
  55. package/dist/image-editor/types.d.ts +450 -0
  56. package/dist/image-editor/types.d.ts.map +1 -0
  57. package/dist/image-editor/types.js +6 -0
  58. package/dist/image-editor/types.js.map +1 -0
  59. package/dist/image-editor.css +1228 -0
  60. package/dist/image-editor.d.ts +5 -0
  61. package/dist/image-editor.d.ts.map +1 -0
  62. package/dist/image-editor.js +13 -0
  63. package/dist/image-editor.js.map +1 -0
  64. package/dist/inline-edit/InlineEdit.d.ts +6 -0
  65. package/dist/inline-edit/InlineEdit.d.ts.map +1 -0
  66. package/dist/inline-edit/InlineEdit.js +340 -0
  67. package/dist/inline-edit/InlineEdit.js.map +1 -0
  68. package/dist/inline-edit/types.d.ts +177 -0
  69. package/dist/inline-edit/types.d.ts.map +1 -0
  70. package/dist/inline-edit/types.js +2 -0
  71. package/dist/inline-edit/types.js.map +1 -0
  72. package/dist/inline-edit/validate.d.ts +17 -0
  73. package/dist/inline-edit/validate.d.ts.map +1 -0
  74. package/dist/inline-edit/validate.js +34 -0
  75. package/dist/inline-edit/validate.js.map +1 -0
  76. package/dist/inline-edit.css +621 -0
  77. package/dist/inline-edit.d.ts +3 -0
  78. package/dist/inline-edit.d.ts.map +1 -0
  79. package/dist/inline-edit.js +9 -0
  80. package/dist/inline-edit.js.map +1 -0
  81. package/dist/markdown-editor/MarkdownEditor.d.ts +6 -0
  82. package/dist/markdown-editor/MarkdownEditor.d.ts.map +1 -0
  83. package/dist/markdown-editor/MarkdownEditor.js +423 -0
  84. package/dist/markdown-editor/MarkdownEditor.js.map +1 -0
  85. package/dist/markdown-editor/commands.d.ts +69 -0
  86. package/dist/markdown-editor/commands.d.ts.map +1 -0
  87. package/dist/markdown-editor/commands.js +207 -0
  88. package/dist/markdown-editor/commands.js.map +1 -0
  89. package/dist/markdown-editor/dialog.d.ts +19 -0
  90. package/dist/markdown-editor/dialog.d.ts.map +1 -0
  91. package/dist/markdown-editor/dialog.js +88 -0
  92. package/dist/markdown-editor/dialog.js.map +1 -0
  93. package/dist/markdown-editor/items.d.ts +12 -0
  94. package/dist/markdown-editor/items.d.ts.map +1 -0
  95. package/dist/markdown-editor/items.js +110 -0
  96. package/dist/markdown-editor/items.js.map +1 -0
  97. package/dist/markdown-editor/menu-popup.d.ts +14 -0
  98. package/dist/markdown-editor/menu-popup.d.ts.map +1 -0
  99. package/dist/markdown-editor/menu-popup.js +13 -0
  100. package/dist/markdown-editor/menu-popup.js.map +1 -0
  101. package/dist/markdown-editor/types.d.ts +158 -0
  102. package/dist/markdown-editor/types.d.ts.map +1 -0
  103. package/dist/markdown-editor/types.js +2 -0
  104. package/dist/markdown-editor/types.js.map +1 -0
  105. package/dist/markdown-editor.css +1020 -0
  106. package/dist/markdown-editor.d.ts +6 -0
  107. package/dist/markdown-editor.d.ts.map +1 -0
  108. package/dist/markdown-editor.js +12 -0
  109. package/dist/markdown-editor.js.map +1 -0
  110. package/dist/styles.css +1699 -0
  111. package/package.json +53 -4
@@ -0,0 +1,686 @@
1
+ // Interaction layer of the ImageEditor engine, loaded after mount: pointer, pinch, two-finger pan, wheel zoom,
2
+ // crop handles, annotation select / move / resize / rotate / draw, and the
3
+ // overlays (crop frame, selection handles, quick toolbar and text editor
4
+ // placement). Every transient lives in this object; only committed
5
+ // operations reach React through `engine.host`.
6
+ import { centerOf, clamp, clampPan, clampRect, cropOf, orientedSize, outputSize } from './geometry.js';
7
+ import { fitRatio, HANDLES, panForZoom, resizeFromHandle, rotateAround, roundRect } from './transform.js';
8
+ const CURSOR = { n: 'ns-resize', s: 'ns-resize', e: 'ew-resize', w: 'ew-resize', ne: 'nesw-resize', sw: 'nesw-resize', nw: 'nwse-resize', se: 'nwse-resize' };
9
+ const DRAG_KINDS = new Set(['rectangle', 'ellipse', 'line', 'arrow', 'freehand', 'redact']);
10
+ const r10 = (v) => Math.round(v * 10) / 10;
11
+ /** Committed annotations use tenths of a pixel. */
12
+ function roundAnnotation(a) {
13
+ if ('points' in a)
14
+ return { ...a, points: a.points.map(r10) };
15
+ if (a.kind === 'text')
16
+ return { ...a, x: r10(a.x), y: r10(a.y) };
17
+ return { ...a, x: r10(a.x), y: r10(a.y), width: r10(a.width), height: r10(a.height) };
18
+ }
19
+ export class EngineUI {
20
+ e;
21
+ /** Crop being edited in the crop tool (oriented pixels). */
22
+ cropRect = null;
23
+ cropShape = 'rect';
24
+ cropRatio = null;
25
+ /** Points of a `path` being placed. */
26
+ pathPoints = null;
27
+ coarse = false;
28
+ gesture = null;
29
+ pointers = new Map();
30
+ zoomTimer = 0;
31
+ constructor(engine) {
32
+ this.e = engine;
33
+ this.coarse = !!engine.canvas?.ownerDocument.defaultView?.matchMedia?.('(pointer: coarse)').matches;
34
+ }
35
+ busy() {
36
+ return this.gesture?.type === 'pinch' || this.gesture?.type === 'pan';
37
+ }
38
+ detach() {
39
+ if (this.zoomTimer)
40
+ clearTimeout(this.zoomTimer);
41
+ this.zoomTimer = 0;
42
+ }
43
+ sync(prev, s) {
44
+ if (s.tool === 'crop' && (!prev || prev.tool !== 'crop' || !this.cropRect))
45
+ this.beginCrop();
46
+ if (s.tool !== 'crop')
47
+ this.cropRect = null;
48
+ if (s.tool !== 'path' && this.pathPoints)
49
+ this.setPath(null);
50
+ }
51
+ // ---------- coordinates ----------
52
+ outputMap() {
53
+ const s = this.e.state;
54
+ const size = this.e.size;
55
+ if (!s || !size)
56
+ return null;
57
+ const crop = cropOf(s.transform, orientedSize(size, s.transform.rotation));
58
+ const out = outputSize(s.transform, size);
59
+ return { ox: crop.x, oy: crop.y, kx: out.width / crop.width, ky: out.height / crop.height };
60
+ }
61
+ /** Oriented-image point of a CSS point on the canvas. */
62
+ toImage(p) {
63
+ const v = this.e.view();
64
+ if (!v)
65
+ return null;
66
+ const sp = { x: (p.x - v.x) / v.scale, y: (p.y - v.y) / v.scale };
67
+ if (v.space === 'oriented')
68
+ return sp;
69
+ const m = this.outputMap();
70
+ return m ? { x: sp.x / m.kx + m.ox, y: sp.y / m.ky + m.oy } : null;
71
+ }
72
+ /** CSS point of an oriented-image point. */
73
+ toView(p) {
74
+ const v = this.e.view();
75
+ if (!v)
76
+ return null;
77
+ let sp = p;
78
+ if (v.space === 'output') {
79
+ const m = this.outputMap();
80
+ if (!m)
81
+ return null;
82
+ sp = { x: (p.x - m.ox) * m.kx, y: (p.y - m.oy) * m.ky };
83
+ }
84
+ return { x: v.x + sp.x * v.scale, y: v.y + sp.y * v.scale };
85
+ }
86
+ /** CSS pixels per oriented pixel. */
87
+ viewScale() {
88
+ const v = this.e.view();
89
+ if (!v)
90
+ return 1;
91
+ const m = v.space === 'output' ? this.outputMap() : null;
92
+ return m ? v.scale * m.kx : v.scale;
93
+ }
94
+ // ---------- overlays ----------
95
+ handleSize() {
96
+ return this.coarse ? 22 : 10;
97
+ }
98
+ boxHandles(b, rotation) {
99
+ const c = centerOf(b);
100
+ const x = { w: b.x, n: c.x, e: b.x + b.width };
101
+ const y = { n: b.y, w: c.y, s: b.y + b.height };
102
+ return HANDLES.map((h) => {
103
+ const hx = h.includes('w') ? x.w : h.includes('e') ? x.e : x.n;
104
+ const hy = h.includes('n') ? y.n : h.includes('s') ? y.s : y.w;
105
+ return [h, rotateAround({ x: hx, y: hy }, c, rotation)];
106
+ });
107
+ }
108
+ cropBox(v) {
109
+ const r = this.cropRect;
110
+ return { x: v.x + r.x * v.scale, y: v.y + r.y * v.scale, width: r.width * v.scale, height: r.height * v.scale };
111
+ }
112
+ /** The selected annotation's frame in CSS pixels: corners, handles, rotate knob. */
113
+ selectionFrame() {
114
+ const s = this.e.state;
115
+ const model = this.e.mods.annotations;
116
+ if (!s || !model || !s.selected || s.tool === 'crop')
117
+ return null;
118
+ const a = model.findAnnotation(this.e.draft ?? s.annotations, s.selected);
119
+ if (!a)
120
+ return null;
121
+ const b = model.boundsOf(a, this.e.measure);
122
+ const k = this.viewScale();
123
+ const tl = this.toView(b);
124
+ if (!tl)
125
+ return null;
126
+ const box = { x: tl.x, y: tl.y, width: Math.max(1, b.width * k), height: Math.max(1, b.height * k) };
127
+ const rotation = a.rotation ?? 0;
128
+ const c = centerOf(box);
129
+ const corners = [box, { x: box.x + box.width, y: box.y }, { x: box.x + box.width, y: box.y + box.height }, { x: box.x, y: box.y + box.height }].map((p) => rotateAround(p, c, rotation));
130
+ const knob = rotateAround({ x: c.x, y: box.y - (this.coarse ? 36 : 24) }, c, rotation);
131
+ return { box, handles: this.boxHandles(box, rotation), knob, corners };
132
+ }
133
+ handles(ctx, pts, accent) {
134
+ const hs = this.handleSize();
135
+ ctx.fillStyle = '#fff';
136
+ ctx.strokeStyle = accent;
137
+ ctx.lineWidth = 1.5;
138
+ for (const [, p] of pts) {
139
+ ctx.beginPath();
140
+ ctx.rect(p.x - hs / 2, p.y - hs / 2, hs, hs);
141
+ ctx.fill();
142
+ ctx.stroke();
143
+ }
144
+ }
145
+ /** Draws the crop frame or the selection handles, then places the quick toolbar and text editor. */
146
+ overlay(ctx, v) {
147
+ const canvas = this.e.canvas;
148
+ const accent = canvas.ownerDocument.defaultView?.getComputedStyle(canvas).getPropertyValue('--salmon-image-editor-accent').trim() || '#2f6fed';
149
+ let anchor = null;
150
+ if (this.e.state?.tool === 'crop' && this.cropRect) {
151
+ const b = this.cropBox(v);
152
+ anchor = b;
153
+ ctx.fillStyle = 'rgba(0,0,0,0.5)';
154
+ ctx.beginPath();
155
+ ctx.rect(v.x, v.y, v.scene.width * v.scale, v.scene.height * v.scale);
156
+ if (this.cropShape === 'circle')
157
+ ctx.ellipse(b.x + b.width / 2, b.y + b.height / 2, b.width / 2, b.height / 2, 0, 0, Math.PI * 2);
158
+ else
159
+ ctx.rect(b.x, b.y, b.width, b.height);
160
+ ctx.fill('evenodd');
161
+ ctx.strokeStyle = 'rgba(255,255,255,0.6)';
162
+ ctx.lineWidth = 1;
163
+ ctx.beginPath();
164
+ for (let i = 1; i < 3; i++) {
165
+ ctx.moveTo(b.x + (b.width * i) / 3, b.y);
166
+ ctx.lineTo(b.x + (b.width * i) / 3, b.y + b.height);
167
+ ctx.moveTo(b.x, b.y + (b.height * i) / 3);
168
+ ctx.lineTo(b.x + b.width, b.y + (b.height * i) / 3);
169
+ }
170
+ ctx.stroke();
171
+ ctx.strokeStyle = accent;
172
+ ctx.lineWidth = 2;
173
+ ctx.strokeRect(b.x, b.y, b.width, b.height);
174
+ this.handles(ctx, this.boxHandles(b, 0), accent);
175
+ }
176
+ else {
177
+ const f = this.selectionFrame();
178
+ if (f) {
179
+ ctx.strokeStyle = accent;
180
+ ctx.lineWidth = 1.5;
181
+ ctx.setLineDash([5, 4]);
182
+ ctx.beginPath();
183
+ f.corners.forEach((p, i) => (i ? ctx.lineTo(p.x, p.y) : ctx.moveTo(p.x, p.y)));
184
+ ctx.closePath();
185
+ ctx.stroke();
186
+ ctx.setLineDash([]);
187
+ const top = f.handles[1][1];
188
+ ctx.beginPath();
189
+ ctx.moveTo(top.x, top.y);
190
+ ctx.lineTo(f.knob.x, f.knob.y);
191
+ ctx.stroke();
192
+ ctx.beginPath();
193
+ ctx.arc(f.knob.x, f.knob.y, this.handleSize() / 2 + 1, 0, Math.PI * 2);
194
+ ctx.fillStyle = '#fff';
195
+ ctx.fill();
196
+ ctx.stroke();
197
+ this.handles(ctx, f.handles, accent);
198
+ const xs = f.corners.map((p) => p.x);
199
+ const ys = [...f.corners.map((p) => p.y), f.knob.y];
200
+ anchor = { x: Math.min(...xs), y: Math.min(...ys), width: Math.max(...xs) - Math.min(...xs), height: Math.max(...ys) - Math.min(...ys) };
201
+ }
202
+ }
203
+ this.place(anchor);
204
+ }
205
+ rtl(el) {
206
+ return el.ownerDocument.defaultView?.getComputedStyle(el).direction === 'rtl';
207
+ }
208
+ /** Positions the quick-access toolbar and the text editor (DOM writes only, no React state). */
209
+ place(anchor) {
210
+ const vp = this.e.viewport;
211
+ const q = this.e.quick;
212
+ if (q) {
213
+ if (anchor) {
214
+ const qh = q.offsetHeight || 36;
215
+ const qw = q.offsetWidth || 200;
216
+ const above = anchor.y - qh - 8;
217
+ // Below the anchor only when it clears the tool panel docked at the
218
+ // stage bottom; otherwise inside the anchor's top edge, so the two
219
+ // bars never overlap (a crop frame that fills the stage).
220
+ const panel = q.parentElement?.querySelector('.salmon-image-editor-panel');
221
+ const floor = vp.height - qh - (panel ? panel.offsetHeight + 16 : 0);
222
+ const below = anchor.y + anchor.height + 8;
223
+ const y = above >= 0 ? above : below <= floor ? below : Math.min(Math.max(0, floor), anchor.y + 8);
224
+ const x = clamp(anchor.x + anchor.width / 2 - qw / 2, 0, Math.max(0, vp.width - qw));
225
+ // The element sits at the inline start (the right edge in RTL); the translate is physical.
226
+ const tx = this.rtl(q) ? x + qw - vp.width : x;
227
+ q.style.transform = `translate(${Math.round(tx)}px, ${Math.round(Math.max(0, y))}px)`;
228
+ q.style.visibility = 'visible';
229
+ }
230
+ else
231
+ q.style.visibility = 'hidden';
232
+ }
233
+ const t = this.e.textBox;
234
+ const s = this.e.state;
235
+ const model = this.e.mods.annotations;
236
+ if (t && s?.editing && model) {
237
+ const a = model.findAnnotation(s.annotations, s.editing);
238
+ const p = a?.kind === 'text' ? this.toView(a) : null;
239
+ if (a?.kind === 'text' && p) {
240
+ const tx = this.rtl(t) ? p.x + t.offsetWidth - vp.width : p.x;
241
+ t.style.transform = `translate(${Math.round(tx)}px, ${Math.round(p.y)}px)${a.rotation ? ` rotate(${a.rotation}deg)` : ''}`;
242
+ Object.assign(t.style, { fontSize: `${Math.max(8, (a.fontSize ?? 32) * this.viewScale())}px`, fontFamily: a.fontFamily ?? 'sans-serif', fontWeight: a.bold ? 'bold' : '', fontStyle: a.italic ? 'italic' : '', color: a.color ?? '#e5484d' });
243
+ }
244
+ }
245
+ }
246
+ // ---------- crop ----------
247
+ beginCrop() {
248
+ const s = this.e.state;
249
+ const size = this.e.size;
250
+ if (!s || !size)
251
+ return;
252
+ const c = cropOf(s.transform, orientedSize(size, s.transform.rotation));
253
+ this.cropRect = { x: c.x, y: c.y, width: c.width, height: c.height };
254
+ this.cropShape = c.shape ?? 'rect';
255
+ this.e.invalidate();
256
+ }
257
+ /** Applies a ratio preset to the crop draft (the largest centered rect of that ratio). */
258
+ setCropRatio(ratio, shape) {
259
+ const s = this.e.state;
260
+ const size = this.e.size;
261
+ if (!s || !size)
262
+ return;
263
+ this.cropRatio = ratio;
264
+ this.cropShape = shape;
265
+ const o = orientedSize(size, s.transform.rotation);
266
+ const whole = { x: 0, y: 0, width: o.width, height: o.height };
267
+ this.cropRect = fitRatio(ratio ? whole : (this.cropRect ?? whole), ratio, o);
268
+ this.e.host.cropDraft();
269
+ this.e.invalidate();
270
+ }
271
+ /** The committed form of the crop draft (whole pixels), or `null` for the whole image. */
272
+ cropValue() {
273
+ const r = this.cropRect;
274
+ const s = this.e.state;
275
+ const size = this.e.size;
276
+ if (!r || !s || !size)
277
+ return null;
278
+ const o = orientedSize(size, s.transform.rotation);
279
+ const rr = clampRect(roundRect(r), o);
280
+ if (rr.x === 0 && rr.y === 0 && rr.width === o.width && rr.height === o.height && this.cropShape === 'rect')
281
+ return null;
282
+ return this.cropShape === 'circle' ? { ...rr, shape: 'circle' } : rr;
283
+ }
284
+ /** Keyboard: moves (or with `resize` grows) the crop draft by oriented pixels. */
285
+ nudgeCrop(dx, dy, resize) {
286
+ const r = this.cropRect;
287
+ const s = this.e.state;
288
+ const size = this.e.size;
289
+ if (!r || !s || !size)
290
+ return;
291
+ let next = resize ? { ...r, width: r.width + dx, height: r.height + dy } : { ...r, x: r.x + dx, y: r.y + dy };
292
+ if (resize && this.cropRatio)
293
+ next = { ...next, height: next.width / this.cropRatio };
294
+ this.cropRect = clampRect(next, orientedSize(size, s.transform.rotation));
295
+ this.e.host.cropDraft();
296
+ this.e.invalidate();
297
+ }
298
+ // ---------- zoom and pan ----------
299
+ /** Sets the live zoom around a CSS point (the center by default). */
300
+ zoomAround(next, at) {
301
+ const s = this.e.state;
302
+ const v = this.e.view();
303
+ if (!s || !v)
304
+ return;
305
+ const z = clamp(next, s.minZoom, s.maxZoom);
306
+ const vp = this.e.viewport;
307
+ this.e.pan = panForZoom(v.scene, vp, { zoom: this.e.zoom, pan: this.e.pan }, z, at ?? { x: vp.width / 2, y: vp.height / 2 });
308
+ this.e.zoom = z;
309
+ this.e.invalidate();
310
+ }
311
+ /** Commits the live zoom (cancellable through the host); reverts when cancelled. */
312
+ commitZoom(source) {
313
+ const s = this.e.state;
314
+ this.e.zoomPending = false;
315
+ if (!s || Math.abs(this.e.zoom - s.zoom) < 1e-6)
316
+ return;
317
+ if (!this.e.host.zoom(this.e.zoom, source)) {
318
+ this.e.zoom = clamp(s.zoom, s.minZoom, s.maxZoom);
319
+ const v = this.e.view();
320
+ if (v)
321
+ this.e.pan = clampPan(v.scene, this.e.viewport, v.scale, this.e.pan);
322
+ this.e.invalidate();
323
+ }
324
+ }
325
+ panBy(dx, dy, source) {
326
+ const v = this.e.view();
327
+ if (!v || !this.e.host.panStart(source))
328
+ return;
329
+ this.e.pan = clampPan(v.scene, this.e.viewport, v.scale, { x: this.e.pan.x + dx, y: this.e.pan.y + dy });
330
+ this.e.invalidate();
331
+ this.e.host.panEnd(this.e.pan, source);
332
+ }
333
+ wheel(ev) {
334
+ if (!(ev.ctrlKey || ev.metaKey) || !this.e.source || !this.e.canvas)
335
+ return;
336
+ ev.preventDefault();
337
+ const r = this.e.canvas.getBoundingClientRect();
338
+ this.zoomAround(this.e.zoom * Math.exp(-ev.deltaY * (ev.deltaMode === 1 ? 0.05 : 0.002)), { x: ev.clientX - r.left, y: ev.clientY - r.top });
339
+ this.e.zoomPending = true;
340
+ if (this.zoomTimer)
341
+ clearTimeout(this.zoomTimer);
342
+ this.zoomTimer = setTimeout(() => {
343
+ this.zoomTimer = 0;
344
+ this.commitZoom('pointer');
345
+ }, 200);
346
+ }
347
+ // ---------- pointer ----------
348
+ local(ev) {
349
+ const r = this.e.canvas.getBoundingClientRect();
350
+ return { x: ev.clientX - r.left, y: ev.clientY - r.top };
351
+ }
352
+ hitHandle(p, pts) {
353
+ const reach = this.handleSize() / 2 + 4;
354
+ for (const [h, q] of pts)
355
+ if (Math.abs(p.x - q.x) <= reach && Math.abs(p.y - q.y) <= reach)
356
+ return h;
357
+ return null;
358
+ }
359
+ hit(q) {
360
+ const s = this.e.state;
361
+ const m = this.e.mods.annotations;
362
+ return s && m ? m.hitTopmost(s.annotations, q, 4 / this.viewScale(), this.e.measure) : null;
363
+ }
364
+ /** Cursor for a CSS point (hover feedback). */
365
+ cursorAt(p) {
366
+ const s = this.e.state;
367
+ if (!s || !this.e.source)
368
+ return '';
369
+ if (s.tool === 'pan')
370
+ return 'grab';
371
+ const v = this.e.view();
372
+ if (s.tool === 'crop' && this.cropRect && v) {
373
+ const b = this.cropBox(v);
374
+ const h = this.hitHandle(p, this.boxHandles(b, 0));
375
+ return h ? CURSOR[h] : p.x >= b.x && p.x <= b.x + b.width && p.y >= b.y && p.y <= b.y + b.height ? 'move' : 'crosshair';
376
+ }
377
+ if (DRAG_KINDS.has(s.tool) || s.tool === 'path')
378
+ return 'crosshair';
379
+ if (s.tool === 'text')
380
+ return 'text';
381
+ const f = this.selectionFrame();
382
+ if (f) {
383
+ if (Math.hypot(p.x - f.knob.x, p.y - f.knob.y) <= this.handleSize())
384
+ return 'grab';
385
+ const h = this.hitHandle(p, f.handles);
386
+ if (h)
387
+ return CURSOR[h];
388
+ }
389
+ const q = this.toImage(p);
390
+ if (q && this.hit(q))
391
+ return 'move';
392
+ return this.e.zoom > 1 ? 'grab' : '';
393
+ }
394
+ pointerDown(ev, source) {
395
+ const s = this.e.state;
396
+ const canvas = this.e.canvas;
397
+ if (!s || !canvas || !this.e.source)
398
+ return;
399
+ if (ev.pointerType === 'mouse' && ev.button !== 0 && ev.button !== 1)
400
+ return;
401
+ const p = this.local(ev);
402
+ this.pointers.set(ev.pointerId, p);
403
+ try {
404
+ canvas.setPointerCapture(ev.pointerId);
405
+ }
406
+ catch { }
407
+ if (this.pointers.size === 2) {
408
+ // Two fingers: pinch zoom and two-finger pan; whatever one finger started is dropped.
409
+ this.e.draft = null;
410
+ if (this.gesture?.type === 'crop')
411
+ this.cropRect = this.gesture.rect;
412
+ const [a, b] = [...this.pointers.values()];
413
+ this.gesture = { type: 'pinch', dist: Math.max(1, Math.hypot(a.x - b.x, a.y - b.y)), mid: { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 }, zoom: this.e.zoom, pan: this.e.pan };
414
+ this.e.invalidate();
415
+ return;
416
+ }
417
+ if (this.pointers.size > 2)
418
+ return;
419
+ const q = this.toImage(p);
420
+ if (!q)
421
+ return;
422
+ const tool = s.tool;
423
+ if (ev.button === 1 || tool === 'pan')
424
+ return this.startPan(p, source);
425
+ if (tool === 'crop' && this.cropRect) {
426
+ const b = this.cropBox(this.e.view());
427
+ const inside = p.x >= b.x && p.x <= b.x + b.width && p.y >= b.y && p.y <= b.y + b.height;
428
+ this.gesture = { type: 'crop', handle: this.hitHandle(p, this.boxHandles(b, 0)) ?? (inside ? 'move' : 'new'), start: q, rect: this.cropRect };
429
+ return;
430
+ }
431
+ if (tool === 'image')
432
+ return this.e.host.pickImage(q);
433
+ if (tool === 'path')
434
+ return; // points are placed on pointer up
435
+ if (DRAG_KINDS.has(tool) || tool === 'text') {
436
+ const base = this.e.host.create(tool, q, s.annotations, tool === 'text');
437
+ if (!base)
438
+ return;
439
+ if (tool === 'text') {
440
+ // The text input takes focus; the canvas must not take it back on mousedown.
441
+ ev.preventDefault();
442
+ this.e.host.annotations([...s.annotations, base], 'add', source, base.id);
443
+ this.e.host.editText(base.id);
444
+ return;
445
+ }
446
+ this.gesture = { type: 'draw', kind: tool, start: q, base, moved: false };
447
+ void this.e.need([base]);
448
+ return;
449
+ }
450
+ // Select: the selection's knob and handles first, then the topmost annotation, else pan or deselect.
451
+ const model = this.e.mods.annotations;
452
+ const f = this.selectionFrame();
453
+ const a = f && model && s.selected ? model.findAnnotation(s.annotations, s.selected) : undefined;
454
+ if (f && a && model) {
455
+ if (Math.hypot(p.x - f.knob.x, p.y - f.knob.y) <= this.handleSize()) {
456
+ const c = centerOf(model.boundsOf(a, this.e.measure));
457
+ this.gesture = { type: 'rotate', id: a.id, base: a, center: c, angle: Math.atan2(q.y - c.y, q.x - c.x) };
458
+ return;
459
+ }
460
+ const h = this.hitHandle(p, f.handles);
461
+ if (h) {
462
+ this.gesture = { type: 'resize', id: a.id, handle: h, base: a };
463
+ return;
464
+ }
465
+ }
466
+ const hit = this.hit(q);
467
+ if (hit) {
468
+ if (hit.id !== s.selected)
469
+ this.e.host.select(hit.id, source);
470
+ this.gesture = { type: 'move', id: hit.id, start: q, base: hit, moved: false };
471
+ return;
472
+ }
473
+ if (s.selected)
474
+ this.e.host.select(null, source);
475
+ if (this.e.zoom > 1)
476
+ this.startPan(p, source);
477
+ }
478
+ startPan(p, source) {
479
+ if (this.e.host.panStart(source))
480
+ this.gesture = { type: 'pan', start: p, pan: this.e.pan };
481
+ }
482
+ pointerMove(ev) {
483
+ if (!this.pointers.has(ev.pointerId))
484
+ return;
485
+ const p = this.local(ev);
486
+ this.pointers.set(ev.pointerId, p);
487
+ const g = this.gesture;
488
+ const s = this.e.state;
489
+ const v = this.e.view();
490
+ if (!g || !s || !v)
491
+ return;
492
+ const vp = this.e.viewport;
493
+ if (g.type === 'pinch') {
494
+ const [a, b] = [...this.pointers.values()];
495
+ if (!a || !b)
496
+ return;
497
+ const mid = { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 };
498
+ const z = clamp((g.zoom * Math.max(1, Math.hypot(a.x - b.x, a.y - b.y))) / g.dist, s.minZoom, s.maxZoom);
499
+ const pan = panForZoom(v.scene, vp, { zoom: g.zoom, pan: g.pan }, z, g.mid);
500
+ this.e.zoom = z;
501
+ this.e.pan = clampPan(v.scene, vp, Math.min(vp.width / v.scene.width, vp.height / v.scene.height) * z, { x: pan.x + mid.x - g.mid.x, y: pan.y + mid.y - g.mid.y });
502
+ this.e.invalidate();
503
+ return;
504
+ }
505
+ if (g.type === 'pan') {
506
+ this.e.pan = clampPan(v.scene, vp, v.scale, { x: g.pan.x + p.x - g.start.x, y: g.pan.y + p.y - g.start.y });
507
+ this.e.invalidate();
508
+ return;
509
+ }
510
+ const q = this.toImage(p);
511
+ if (!q)
512
+ return;
513
+ if (g.type === 'crop') {
514
+ const o = orientedSize(this.e.size, s.transform.rotation);
515
+ let r;
516
+ if (g.handle === 'move')
517
+ r = { ...g.rect, x: g.rect.x + q.x - g.start.x, y: g.rect.y + q.y - g.start.y };
518
+ else if (g.handle === 'new') {
519
+ const w = q.x - g.start.x;
520
+ const h = this.cropRatio ? (Math.sign(q.y - g.start.y || 1) * Math.abs(w)) / this.cropRatio : q.y - g.start.y;
521
+ r = { x: Math.min(g.start.x, g.start.x + w), y: Math.min(g.start.y, g.start.y + h), width: Math.abs(w), height: Math.abs(h) };
522
+ }
523
+ else
524
+ r = resizeFromHandle(g.rect, 0, g.handle, { x: clamp(q.x, 0, o.width), y: clamp(q.y, 0, o.height) }, this.cropRatio, 4);
525
+ this.cropRect = clampRect(r, o, g.handle === 'new' ? 1 : 4);
526
+ this.e.invalidate();
527
+ return;
528
+ }
529
+ const list = s.annotations;
530
+ if (g.type === 'draw') {
531
+ g.moved = true;
532
+ this.e.draft = [...list, this.drawn(g, q, ev)];
533
+ this.e.invalidate();
534
+ return;
535
+ }
536
+ const model = this.e.mods.annotations;
537
+ if (!model)
538
+ return;
539
+ let next;
540
+ if (g.type === 'move') {
541
+ g.moved = true;
542
+ next = model.moveAnnotation(g.base, q.x - g.start.x, q.y - g.start.y);
543
+ }
544
+ else if (g.type === 'resize')
545
+ next = model.resizeAnnotation(g.base, g.handle, q, ev.shiftKey, this.e.measure);
546
+ else {
547
+ let deg = ((Math.atan2(q.y - g.center.y, q.x - g.center.x) - g.angle) * 180) / Math.PI;
548
+ if (ev.shiftKey)
549
+ deg = Math.round(deg / 15) * 15;
550
+ next = model.rotateAnnotation(g.base, deg);
551
+ }
552
+ this.e.draft = model.updateAnnotation(list, g.id, () => next);
553
+ this.e.invalidate();
554
+ }
555
+ drawn(g, q, ev) {
556
+ const b = g.base;
557
+ if ('points' in b) {
558
+ if (b.kind !== 'freehand')
559
+ return { ...b, points: [g.start.x, g.start.y, q.x, q.y] };
560
+ // Freehand records every coalesced sample since the last frame.
561
+ const prev = this.e.draft?.[this.e.draft.length - 1];
562
+ const pts = prev && prev.id === b.id && 'points' in prev ? [...prev.points] : [...b.points];
563
+ const samples = 'getCoalescedEvents' in ev ? ev.getCoalescedEvents() : [];
564
+ for (const c of samples.length ? samples : [ev]) {
565
+ const cq = c === ev ? q : this.toImage(this.local(c));
566
+ if (cq)
567
+ pts.push(r10(cq.x), r10(cq.y));
568
+ }
569
+ return { ...b, points: pts };
570
+ }
571
+ let w = q.x - g.start.x;
572
+ let h = q.y - g.start.y;
573
+ if (ev.shiftKey) {
574
+ const m = Math.max(Math.abs(w), Math.abs(h));
575
+ w = Math.sign(w || 1) * m;
576
+ h = Math.sign(h || 1) * m;
577
+ }
578
+ return { ...b, x: Math.min(g.start.x, g.start.x + w), y: Math.min(g.start.y, g.start.y + h), width: Math.abs(w), height: Math.abs(h) };
579
+ }
580
+ pointerUp(ev, source, canceled = false) {
581
+ if (!this.pointers.has(ev.pointerId))
582
+ return;
583
+ this.pointers.delete(ev.pointerId);
584
+ try {
585
+ this.e.canvas?.releasePointerCapture(ev.pointerId);
586
+ }
587
+ catch { }
588
+ const g = this.gesture;
589
+ const s = this.e.state;
590
+ if (g?.type === 'pinch') {
591
+ if (!this.pointers.size) {
592
+ this.gesture = null;
593
+ if (!canceled)
594
+ this.commitZoom(source);
595
+ }
596
+ return;
597
+ }
598
+ if (this.pointers.size || !s)
599
+ return;
600
+ this.gesture = null;
601
+ const host = this.e.host;
602
+ if (!g) {
603
+ if (s.tool === 'path' && !canceled) {
604
+ const q = this.toImage(this.local(ev));
605
+ if (q) {
606
+ this.setPath([...(this.pathPoints ?? []), Math.round(q.x), Math.round(q.y)]);
607
+ }
608
+ }
609
+ return;
610
+ }
611
+ const draft = this.e.draft;
612
+ this.e.draft = null;
613
+ this.e.invalidate();
614
+ if (canceled) {
615
+ if (g.type === 'crop')
616
+ this.cropRect = g.rect;
617
+ return;
618
+ }
619
+ if (g.type === 'pan')
620
+ return host.panEnd(this.e.pan, source);
621
+ if (g.type === 'crop') {
622
+ if (this.cropRect && (this.cropRect.width < 4 || this.cropRect.height < 4))
623
+ this.cropRect = g.rect;
624
+ return host.cropDraft();
625
+ }
626
+ if (g.type === 'draw') {
627
+ let a = draft ? draft[draft.length - 1] : g.base;
628
+ const b = this.e.mods.annotations?.boundsOf(a, this.e.measure);
629
+ // A click (no drag) places a default-sized shape.
630
+ if (a.kind !== 'freehand' && (!g.moved || (b && Math.max(b.width, b.height) < 3)))
631
+ a = host.create(g.kind, g.start, s.annotations, true) ?? a;
632
+ if (a.kind === 'freehand' && a.points.length < 4)
633
+ return;
634
+ return host.annotations([...s.annotations, roundAnnotation(a)], 'add', source, a.id);
635
+ }
636
+ if (g.type === 'move' && !g.moved)
637
+ return;
638
+ if (draft)
639
+ host.annotations(draft.map((a) => (a.id === g.id ? roundAnnotation(a) : a)), 'update', source);
640
+ }
641
+ setPath(points) {
642
+ this.pathPoints = points;
643
+ const s = this.e.state;
644
+ const base = points && points.length >= 2 && s ? this.e.host.create('path', { x: points[0], y: points[1] }, s.annotations, false) : null;
645
+ this.e.extra = base ? { ...base, id: '\u0000path', points } : null;
646
+ if (points)
647
+ void this.e.need([this.e.extra ?? { kind: 'path', id: '', points: [] }]);
648
+ this.e.invalidate();
649
+ }
650
+ /** Commits the path being placed (double click or Enter); fewer than two points is dropped. */
651
+ finishPath(source) {
652
+ const pts = this.pathPoints;
653
+ const s = this.e.state;
654
+ this.setPath(null);
655
+ if (!pts || !s)
656
+ return;
657
+ // A double click adds its point twice; drop exact repeats.
658
+ const clean = [];
659
+ for (let i = 0; i + 1 < pts.length; i += 2)
660
+ if (clean[clean.length - 2] !== pts[i] || clean[clean.length - 1] !== pts[i + 1])
661
+ clean.push(pts[i], pts[i + 1]);
662
+ if (clean.length < 4)
663
+ return;
664
+ const base = this.e.host.create('path', { x: clean[0], y: clean[1] }, s.annotations, false);
665
+ if (base)
666
+ this.e.host.annotations([...s.annotations, { ...base, points: clean }], 'add', source, base.id);
667
+ }
668
+ /** Escape: drops the gesture or the path being placed. */
669
+ cancelGesture() {
670
+ if (this.pathPoints) {
671
+ this.setPath(null);
672
+ return true;
673
+ }
674
+ const g = this.gesture;
675
+ if (!g)
676
+ return false;
677
+ if (g.type === 'crop')
678
+ this.cropRect = g.rect;
679
+ this.gesture = null;
680
+ this.e.draft = null;
681
+ this.pointers.clear();
682
+ this.e.invalidate();
683
+ return true;
684
+ }
685
+ }
686
+ //# sourceMappingURL=interact.js.map