@grida/svg-editor 1.0.0-alpha.2 → 1.0.0-alpha.4

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.
@@ -1,14 +1,531 @@
1
- import { a as capture_resize_baseline, c as is_resizable, i as apply_translate, o as capture_translate_baseline, r as apply_resize, s as compute_resize_factors, t as parse_paint } from "./paint-DuCg6Y-K.mjs";
1
+ import { a as capture_resize_baseline, c as is_resizable, i as apply_translate, l as is_text_input_focused, o as capture_translate_baseline, r as apply_resize, s as compute_resize_factors, t as parse_paint } from "./paint-Cfiw4g_J.mjs";
2
2
  import { createTextEditor } from "@grida/text-editor/dom";
3
3
  import { NO_MODS, Surface, measurementToHUDDraw } from "@grida/hud";
4
4
  import { measure } from "@grida/cmath/_measurement";
5
5
  import cmath from "@grida/cmath";
6
+ //#region src/core/camera.ts
7
+ /**
8
+ * Surface-scoped pan/zoom state.
9
+ *
10
+ * The public shape leads with the peer convention (`center` / `zoom` /
11
+ * `bounds`) and keeps the matrix as an advanced read. Methods mirror
12
+ * Figma/Penpot where they overlap.
13
+ */
14
+ var Camera = class {
15
+ constructor(opts) {
16
+ this.viewport_w = 0;
17
+ this.viewport_h = 0;
18
+ this.listeners = /* @__PURE__ */ new Set();
19
+ this._constraints = null;
20
+ this._transform = opts.initial ?? cmath.transform.identity;
21
+ this.resolve_bounds = opts.resolve_bounds;
22
+ }
23
+ /**
24
+ * Current viewport constraint, or `null` for free pan/zoom. Set with
25
+ * `camera.constraints = { type: 'cover', bounds: '<root>', padding: 80 }`
26
+ * to clamp zoom + pan; assign `null` to clear.
27
+ *
28
+ * Constraints are applied synchronously inside `set_transform` (and
29
+ * `_set_viewport_size`), so every public mutation respects them
30
+ * automatically — the host never needs to subscribe-and-clamp itself.
31
+ */
32
+ get constraints() {
33
+ return this._constraints;
34
+ }
35
+ set constraints(c) {
36
+ this._constraints = c;
37
+ if (c) this.reenforce();
38
+ }
39
+ /** Underlying 2D affine. World→screen. */
40
+ get transform() {
41
+ return this._transform;
42
+ }
43
+ /** Uniform scale factor. 1 = 100 %. */
44
+ get zoom() {
45
+ return this._transform[0][0];
46
+ }
47
+ /** World-space point currently at viewport center. */
48
+ get center() {
49
+ return this.screen_to_world({
50
+ x: this.viewport_w / 2,
51
+ y: this.viewport_h / 2
52
+ });
53
+ }
54
+ /** World-space rectangle visible in the viewport. */
55
+ get bounds() {
56
+ const tl = this.screen_to_world({
57
+ x: 0,
58
+ y: 0
59
+ });
60
+ const br = this.screen_to_world({
61
+ x: this.viewport_w,
62
+ y: this.viewport_h
63
+ });
64
+ return {
65
+ x: tl.x,
66
+ y: tl.y,
67
+ width: br.x - tl.x,
68
+ height: br.y - tl.y
69
+ };
70
+ }
71
+ /** Translate the camera by a screen-space delta. */
72
+ pan(delta_screen) {
73
+ const t = this._transform;
74
+ this.set_transform([[
75
+ t[0][0],
76
+ t[0][1],
77
+ t[0][2] + delta_screen.x
78
+ ], [
79
+ t[1][0],
80
+ t[1][1],
81
+ t[1][2] + delta_screen.y
82
+ ]]);
83
+ }
84
+ /**
85
+ * Multiply zoom by `factor` keeping `origin_screen` fixed in world space.
86
+ * Used by wheel-zoom-at-cursor and pinch-zoom.
87
+ */
88
+ zoom_at(factor, origin_screen) {
89
+ const t = this._transform;
90
+ const s2 = t[0][0] * factor;
91
+ const tx2 = origin_screen.x * (1 - factor) + factor * t[0][2];
92
+ const ty2 = origin_screen.y * (1 - factor) + factor * t[1][2];
93
+ this.set_transform([[
94
+ s2,
95
+ 0,
96
+ tx2
97
+ ], [
98
+ 0,
99
+ s2,
100
+ ty2
101
+ ]]);
102
+ }
103
+ /** Pan so `c` lands at the viewport center. Zoom unchanged. */
104
+ set_center(c) {
105
+ const s = this._transform[0][0];
106
+ const tx = this.viewport_w / 2 - s * c.x;
107
+ const ty = this.viewport_h / 2 - s * c.y;
108
+ this.set_transform([[
109
+ s,
110
+ 0,
111
+ tx
112
+ ], [
113
+ 0,
114
+ s,
115
+ ty
116
+ ]]);
117
+ }
118
+ /** Set zoom directly; pivot defaults to viewport center. */
119
+ set_zoom(z, pivot_screen) {
120
+ const current = this._transform[0][0];
121
+ if (current === 0) return;
122
+ const factor = z / current;
123
+ const pivot = pivot_screen ?? {
124
+ x: this.viewport_w / 2,
125
+ y: this.viewport_h / 2
126
+ };
127
+ this.zoom_at(factor, pivot);
128
+ }
129
+ /**
130
+ * Replace the entire transform.
131
+ *
132
+ * Idempotent: when the new transform is element-wise equal to the current
133
+ * one, this is a no-op (no notification fires). This is the seam that
134
+ * makes external constraint loops (e.g. "subscribe → compute clamped →
135
+ * set_transform") terminate: the clamp re-emits the same transform on
136
+ * the second pass, set_transform short-circuits, no recursion.
137
+ *
138
+ * When `camera.constraints` is non-null, the input transform is clamped
139
+ * synchronously before being stored — every public mutation respects the
140
+ * constraint automatically.
141
+ */
142
+ set_transform(t) {
143
+ const next = this.apply_constraints(t);
144
+ if (transform_equal(this._transform, next)) return;
145
+ this._transform = next;
146
+ this.notify();
147
+ }
148
+ /** Viewport size in screen pixels. Read by host code computing constraints. */
149
+ get viewport_size() {
150
+ return {
151
+ width: this.viewport_w,
152
+ height: this.viewport_h
153
+ };
154
+ }
155
+ /**
156
+ * Fit a target into the viewport.
157
+ *
158
+ * - `"<root>"` — the document root's content bounds (host-resolved).
159
+ * - `"<selection>"` — current editor.state.selection's union bounds.
160
+ * - `NodeId` — that node's content bounds.
161
+ * - `Rect` — an explicit world-space rectangle.
162
+ *
163
+ * No-ops if the target resolves to `null` (e.g. empty selection) or if
164
+ * the viewport size is 0 (no container).
165
+ */
166
+ fit(target, opts) {
167
+ if (this.viewport_w <= 0 || this.viewport_h <= 0) return;
168
+ const rect = typeof target === "string" ? this.resolve_bounds(target) : target;
169
+ if (!rect || rect.width <= 0 || rect.height <= 0) return;
170
+ const margin = opts?.margin ?? 64;
171
+ const viewport = {
172
+ x: 0,
173
+ y: 0,
174
+ width: this.viewport_w,
175
+ height: this.viewport_h
176
+ };
177
+ this.set_transform(cmath.ext.viewport.transformToFit(viewport, rect, margin));
178
+ }
179
+ /** Snap back to identity. */
180
+ reset() {
181
+ this.set_transform(cmath.transform.identity);
182
+ }
183
+ /**
184
+ * Subscribe to camera changes. Fires on every mutation. Cheap channel —
185
+ * does NOT bump `editor.state.version`. Same pattern as
186
+ * `editor.subscribe_surface_hover`.
187
+ */
188
+ subscribe(cb) {
189
+ this.listeners.add(cb);
190
+ return () => {
191
+ this.listeners.delete(cb);
192
+ };
193
+ }
194
+ /** @internal Surface drives this on container resize. */
195
+ _set_viewport_size(w, h) {
196
+ if (w === this.viewport_w && h === this.viewport_h) return;
197
+ this.viewport_w = w;
198
+ this.viewport_h = h;
199
+ if (this._constraints) {
200
+ const next = this.apply_constraints(this._transform);
201
+ if (!transform_equal(this._transform, next)) this._transform = next;
202
+ }
203
+ this.notify();
204
+ }
205
+ /** Convert a screen-space point to world-space. */
206
+ screen_to_world(p) {
207
+ const inv = cmath.transform.invert(this._transform);
208
+ const [wx, wy] = cmath.vector2.transform([p.x, p.y], inv);
209
+ return {
210
+ x: wx,
211
+ y: wy
212
+ };
213
+ }
214
+ /** Convert a world-space point to screen-space. */
215
+ world_to_screen(p) {
216
+ const [sx, sy] = cmath.vector2.transform([p.x, p.y], this._transform);
217
+ return {
218
+ x: sx,
219
+ y: sy
220
+ };
221
+ }
222
+ /**
223
+ * Apply the current constraint (if any) to a candidate transform.
224
+ * Pure: returns the clamped result, never mutates state. Returns the
225
+ * input unchanged when constraints are null / bounds are unresolvable /
226
+ * viewport is 0.
227
+ */
228
+ apply_constraints(t) {
229
+ if (!this._constraints) return t;
230
+ if (this.viewport_w <= 0 || this.viewport_h <= 0) return t;
231
+ switch (this._constraints.type) {
232
+ case "cover": return clamp_cover(t, this._constraints, this.viewport_w, this.viewport_h, this.resolve_bounds);
233
+ }
234
+ }
235
+ /**
236
+ * Re-clamp the stored transform against the current constraint. Called
237
+ * from the `constraints` setter; `_set_viewport_size` has its own
238
+ * notify-inclusive path.
239
+ */
240
+ reenforce() {
241
+ if (!this._constraints) return;
242
+ const next = this.apply_constraints(this._transform);
243
+ if (transform_equal(this._transform, next)) return;
244
+ this._transform = next;
245
+ this.notify();
246
+ }
247
+ notify() {
248
+ for (const cb of this.listeners) cb();
249
+ }
250
+ };
251
+ /**
252
+ * Clamp a transform under a `'cover'` constraint:
253
+ * - Zoom lower-bounded at fit-with-padding (the slide always fills the
254
+ * viewport edge-to-edge).
255
+ * - When at min-zoom the slide is locked centered (bounds smaller than
256
+ * viewport on the constrained axis is impossible above min_zoom; below
257
+ * is impossible because zoom is clamped up).
258
+ * - When zoomed in, pan is clamped so the slide always covers the viewport
259
+ * (no black bars).
260
+ *
261
+ * Returns the input transform unchanged when bounds can't be resolved or
262
+ * are degenerate.
263
+ */
264
+ function clamp_cover(t, c, vp_w, vp_h, resolve) {
265
+ const bounds = typeof c.bounds === "string" ? resolve(c.bounds) : c.bounds;
266
+ if (!bounds || bounds.width <= 0 || bounds.height <= 0) return t;
267
+ const padding = c.padding ?? 0;
268
+ const eff_w = vp_w - 2 * padding;
269
+ const eff_h = vp_h - 2 * padding;
270
+ if (eff_w <= 0 || eff_h <= 0) return t;
271
+ const min_zoom = Math.min(eff_w / bounds.width, eff_h / bounds.height);
272
+ const s = Math.max(t[0][0], min_zoom);
273
+ const sw = s * bounds.width;
274
+ const sh = s * bounds.height;
275
+ const tx = sw > vp_w ? cmath.clamp(t[0][2], vp_w - s * (bounds.x + bounds.width), -s * bounds.x) : (vp_w - sw) / 2 - s * bounds.x;
276
+ const ty = sh > vp_h ? cmath.clamp(t[1][2], vp_h - s * (bounds.y + bounds.height), -s * bounds.y) : (vp_h - sh) / 2 - s * bounds.y;
277
+ return [[
278
+ s,
279
+ 0,
280
+ tx
281
+ ], [
282
+ 0,
283
+ s,
284
+ ty
285
+ ]];
286
+ }
287
+ function transform_equal(a, b) {
288
+ return a[0][0] === b[0][0] && a[0][1] === b[0][1] && a[0][2] === b[0][2] && a[1][0] === b[1][0] && a[1][1] === b[1][1] && a[1][2] === b[1][2];
289
+ }
290
+ //#endregion
291
+ //#region src/gestures/gestures.ts
292
+ /**
293
+ * Sibling to `Keymap`. Owns a list of installed gesture bindings; each
294
+ * binding's `install(ctx)` is called eagerly when bound and uninstalled
295
+ * on `unbind` or surface detach.
296
+ */
297
+ var Gestures = class {
298
+ constructor(ctx) {
299
+ this.ctx = ctx;
300
+ this.entries = [];
301
+ }
302
+ /**
303
+ * Install a gesture binding. Returns an unbind function.
304
+ * Re-binding the same `id` does NOT replace — both will be active.
305
+ * Use `unbind({ id })` first if you want a clean swap.
306
+ */
307
+ bind(binding) {
308
+ const uninstall = binding.install(this.ctx);
309
+ const entry = {
310
+ binding,
311
+ uninstall
312
+ };
313
+ this.entries.push(entry);
314
+ return () => {
315
+ const i = this.entries.indexOf(entry);
316
+ if (i < 0) return;
317
+ this.entries.splice(i, 1);
318
+ uninstall();
319
+ };
320
+ }
321
+ /**
322
+ * Remove bindings matching the spec. With `{ id }`, all bindings with
323
+ * that id are uninstalled. With no spec, this is a no-op (use
324
+ * `dispose()` to nuke everything).
325
+ */
326
+ unbind(spec) {
327
+ if (spec.id === void 0) return;
328
+ const remaining = [];
329
+ for (const entry of this.entries) if (entry.binding.id === spec.id) entry.uninstall();
330
+ else remaining.push(entry);
331
+ this.entries = remaining;
332
+ }
333
+ /** All currently installed bindings. Order is registration order. */
334
+ bindings() {
335
+ return this.entries.map((e) => e.binding);
336
+ }
337
+ /** @internal Uninstall every binding. Surface calls on detach. */
338
+ _dispose() {
339
+ for (const entry of this.entries) entry.uninstall();
340
+ this.entries = [];
341
+ }
342
+ };
343
+ //#endregion
344
+ //#region src/gestures/defaults.ts
345
+ /** Default margin for `camera.fit` from keyboard shortcuts. */
346
+ const KEYBOARD_FIT_MARGIN = 64;
347
+ /** Default zoom step for `Cmd/Ctrl+=` / `Cmd/Ctrl+-`. */
348
+ const ZOOM_STEP = 1.2;
349
+ /** Per-wheel-unit zoom sensitivity for Cmd/Ctrl+wheel + pinch. */
350
+ const WHEEL_ZOOM_SENSITIVITY = .01;
351
+ /** Min/max zoom clamps. Generous; hosts that want tighter limits can
352
+ * unbind these defaults and bind their own. */
353
+ const MIN_ZOOM = .02;
354
+ const MAX_ZOOM = 256;
355
+ function clamp_zoom(z) {
356
+ return cmath.clamp(z, MIN_ZOOM, MAX_ZOOM);
357
+ }
358
+ /** wheel-pan-zoom: plain wheel = pan, Cmd/Ctrl+wheel + pinch = zoom-at-cursor. */
359
+ const WHEEL_PAN_ZOOM = {
360
+ id: "wheel-pan-zoom",
361
+ install({ container, camera }) {
362
+ const on_wheel = (e) => {
363
+ e.preventDefault();
364
+ if (e.ctrlKey || e.metaKey) {
365
+ const factor = 1 - e.deltaY * WHEEL_ZOOM_SENSITIVITY;
366
+ const eff = clamp_zoom(camera.zoom * factor) / camera.zoom;
367
+ if (eff === 1) return;
368
+ const rect = container.getBoundingClientRect();
369
+ camera.zoom_at(eff, {
370
+ x: e.clientX - rect.left,
371
+ y: e.clientY - rect.top
372
+ });
373
+ } else camera.pan({
374
+ x: -e.deltaX,
375
+ y: -e.deltaY
376
+ });
377
+ };
378
+ container.addEventListener("wheel", on_wheel, { passive: false });
379
+ return () => container.removeEventListener("wheel", on_wheel);
380
+ }
381
+ };
382
+ /**
383
+ * Begin a drag-pan from a pointerdown. Attaches `pointermove` / `pointerup`
384
+ * listeners scoped to the gesture lifetime, then detaches them on release.
385
+ * This is the d3-drag pattern: global listeners only exist while a drag is
386
+ * in flight, not for the surface's whole lifetime.
387
+ */
388
+ function begin_drag_pan(e, container, camera, on_release) {
389
+ let last_x = e.clientX;
390
+ let last_y = e.clientY;
391
+ try {
392
+ container.setPointerCapture(e.pointerId);
393
+ } catch {}
394
+ e.preventDefault();
395
+ e.stopPropagation();
396
+ const win = container.ownerDocument.defaultView ?? window;
397
+ const on_pointermove = (ev) => {
398
+ const dx = ev.clientX - last_x;
399
+ const dy = ev.clientY - last_y;
400
+ last_x = ev.clientX;
401
+ last_y = ev.clientY;
402
+ camera.pan({
403
+ x: dx,
404
+ y: dy
405
+ });
406
+ ev.preventDefault();
407
+ ev.stopPropagation();
408
+ };
409
+ const cleanup = () => {
410
+ win.removeEventListener("pointermove", on_pointermove, true);
411
+ win.removeEventListener("pointerup", on_pointerup, true);
412
+ win.removeEventListener("pointercancel", on_pointerup, true);
413
+ on_release?.();
414
+ };
415
+ const on_pointerup = () => cleanup();
416
+ win.addEventListener("pointermove", on_pointermove, true);
417
+ win.addEventListener("pointerup", on_pointerup, true);
418
+ win.addEventListener("pointercancel", on_pointerup, true);
419
+ }
420
+ /** The data-driven default set. Order = install order. */
421
+ const DEFAULT_GESTURE_BINDINGS = [
422
+ WHEEL_PAN_ZOOM,
423
+ {
424
+ id: "space-drag-pan",
425
+ install({ container, camera }) {
426
+ let space_held = false;
427
+ let prev_cursor = null;
428
+ const set_cursor = (next) => {
429
+ if (prev_cursor === null) prev_cursor = container.style.cursor;
430
+ container.style.cursor = next ?? prev_cursor ?? "";
431
+ if (next === null) prev_cursor = null;
432
+ };
433
+ const on_keydown = (e) => {
434
+ if (e.code !== "Space" || e.repeat) return;
435
+ if (is_text_input_focused()) return;
436
+ space_held = true;
437
+ set_cursor("grab");
438
+ e.preventDefault();
439
+ };
440
+ const on_keyup = (e) => {
441
+ if (e.code !== "Space") return;
442
+ space_held = false;
443
+ set_cursor(null);
444
+ };
445
+ const on_pointerdown = (e) => {
446
+ if (!space_held || e.button !== 0) return;
447
+ set_cursor("grabbing");
448
+ begin_drag_pan(e, container, camera, () => set_cursor(space_held ? "grab" : null));
449
+ };
450
+ const on_blur = () => {
451
+ space_held = false;
452
+ set_cursor(null);
453
+ };
454
+ const win = container.ownerDocument.defaultView ?? window;
455
+ win.addEventListener("keydown", on_keydown);
456
+ win.addEventListener("keyup", on_keyup);
457
+ container.addEventListener("pointerdown", on_pointerdown, true);
458
+ win.addEventListener("blur", on_blur);
459
+ return () => {
460
+ win.removeEventListener("keydown", on_keydown);
461
+ win.removeEventListener("keyup", on_keyup);
462
+ container.removeEventListener("pointerdown", on_pointerdown, true);
463
+ win.removeEventListener("blur", on_blur);
464
+ if (prev_cursor !== null) container.style.cursor = prev_cursor;
465
+ };
466
+ }
467
+ },
468
+ {
469
+ id: "middle-mouse-pan",
470
+ install({ container, camera }) {
471
+ const on_pointerdown = (e) => {
472
+ if (e.button !== 1) return;
473
+ begin_drag_pan(e, container, camera);
474
+ };
475
+ const on_auxclick = (e) => {
476
+ if (e.button === 1) e.preventDefault();
477
+ };
478
+ container.addEventListener("pointerdown", on_pointerdown, true);
479
+ container.addEventListener("auxclick", on_auxclick);
480
+ return () => {
481
+ container.removeEventListener("pointerdown", on_pointerdown, true);
482
+ container.removeEventListener("auxclick", on_auxclick);
483
+ };
484
+ }
485
+ },
486
+ {
487
+ id: "keyboard-zoom",
488
+ install({ container, camera }) {
489
+ const owner_doc = container.ownerDocument;
490
+ const on_keydown = (e) => {
491
+ const active = owner_doc.activeElement;
492
+ if (active && active !== owner_doc.body && !container.contains(active)) return;
493
+ if (is_text_input_focused()) return;
494
+ const mod = e.metaKey || e.ctrlKey;
495
+ if (e.shiftKey && !mod && (e.code === "Digit0" || e.code === "Numpad0")) {
496
+ camera.reset();
497
+ e.preventDefault();
498
+ } else if (e.shiftKey && !mod && (e.code === "Digit1" || e.code === "Digit9" || e.code === "Numpad1" || e.code === "Numpad9")) {
499
+ camera.fit("<root>", { margin: KEYBOARD_FIT_MARGIN });
500
+ e.preventDefault();
501
+ } else if (e.shiftKey && !mod && (e.code === "Digit2" || e.code === "Numpad2")) {
502
+ camera.fit("<selection>", { margin: KEYBOARD_FIT_MARGIN });
503
+ e.preventDefault();
504
+ } else if (mod && (e.code === "Equal" || e.code === "NumpadAdd")) {
505
+ camera.set_zoom(clamp_zoom(camera.zoom * ZOOM_STEP));
506
+ e.preventDefault();
507
+ } else if (mod && (e.code === "Minus" || e.code === "NumpadSubtract")) {
508
+ camera.set_zoom(clamp_zoom(camera.zoom / ZOOM_STEP));
509
+ e.preventDefault();
510
+ }
511
+ };
512
+ owner_doc.addEventListener("keydown", on_keydown);
513
+ return () => owner_doc.removeEventListener("keydown", on_keydown);
514
+ }
515
+ }
516
+ ];
517
+ /** Install every default binding into the gesture layer. */
518
+ function applyDefaultGestures(gestures) {
519
+ for (const b of DEFAULT_GESTURE_BINDINGS) gestures.bind(b);
520
+ }
521
+ //#endregion
6
522
  //#region src/text-surface.ts
7
523
  const SVG_NS = "http://www.w3.org/2000/svg";
8
524
  const XML_NS = "http://www.w3.org/XML/1998/namespace";
9
525
  var SvgTextSurface = class {
10
526
  constructor(textEl) {
11
527
  this.prevXmlSpace = void 0;
528
+ this.prevPointerEvents = void 0;
12
529
  this.last_caret_idx = -1;
13
530
  this.last_caret_visible = false;
14
531
  this.last_sel_start = -1;
@@ -22,6 +539,8 @@ var SvgTextSurface = class {
22
539
  this.prevXmlSpace = textEl.getAttributeNS(XML_NS, "space");
23
540
  textEl.setAttributeNS(XML_NS, "xml:space", "preserve");
24
541
  }
542
+ this.prevPointerEvents = textEl.getAttribute("pointer-events");
543
+ textEl.setAttribute("pointer-events", "bounding-box");
25
544
  const selection = ownerDoc.createElementNS(SVG_NS, "rect");
26
545
  selection.setAttribute("fill", "#2563eb");
27
546
  selection.setAttribute("fill-opacity", "0.25");
@@ -79,7 +598,10 @@ var SvgTextSurface = class {
79
598
  this.selectionRect.remove();
80
599
  if (this.prevXmlSpace !== void 0 && !keepEditMutations) if (this.prevXmlSpace === null) this.textEl.removeAttributeNS(XML_NS, "space");
81
600
  else this.textEl.setAttributeNS(XML_NS, "xml:space", this.prevXmlSpace);
601
+ if (this.prevPointerEvents !== void 0) if (this.prevPointerEvents === null) this.textEl.removeAttribute("pointer-events");
602
+ else this.textEl.setAttribute("pointer-events", this.prevPointerEvents);
82
603
  this.prevXmlSpace = void 0;
604
+ this.prevPointerEvents = void 0;
83
605
  }
84
606
  positionAtPoint(clientX, clientY) {
85
607
  const ctm = this.textEl.getScreenCTM();
@@ -169,20 +691,28 @@ const IS_MODIFIER_KEY = {
169
691
  * live `<text>` element out from under the about-to-mount text surface. */
170
692
  const TEXT_EDIT_PENDING = { __pending: true };
171
693
  /**
172
- * Attach a DOM surface to a headless editor. Returns a `SurfaceHandle` whose
173
- * `detach()` is the inverse — DOM cleared, listeners removed.
694
+ * Attach a DOM surface to a headless editor. Returns a `DomSurfaceHandle`
695
+ * whose `detach()` is the inverse — DOM cleared, listeners removed,
696
+ * gestures uninstalled.
174
697
  *
175
698
  * Usage is one-shot per container: the surface owns the container's children
176
699
  * for its lifetime, and `detach()` restores it to empty.
177
700
  */
178
701
  function attach_dom_surface(editor, options) {
179
- const surface = new DomSurface(editor, options.container);
180
- return editor.attach(surface);
702
+ const surface = new DomSurface(editor, options);
703
+ const inner = editor.attach(surface);
704
+ return {
705
+ detach: () => {
706
+ surface.detach_gestures();
707
+ inner.detach();
708
+ },
709
+ camera: surface.camera,
710
+ gestures: surface.gestures
711
+ };
181
712
  }
182
713
  var DomSurface = class {
183
- constructor(editor, container) {
714
+ constructor(editor, options) {
184
715
  this.editor = editor;
185
- this.container = container;
186
716
  this.svg_root = null;
187
717
  this.teardown = [];
188
718
  this.element_index = /* @__PURE__ */ new Map();
@@ -192,6 +722,9 @@ var DomSurface = class {
192
722
  this.text_edit_target = null;
193
723
  this.text_edit_original = "";
194
724
  this.editor_hover_internal = null;
725
+ this.container = options.container;
726
+ const container = this.container;
727
+ this.fit_on_attach = options.fit === true;
195
728
  if (getComputedStyle(container).position === "static") container.style.position = "relative";
196
729
  container.style.userSelect = "none";
197
730
  container.style.webkitUserSelect = "none";
@@ -209,6 +742,14 @@ var DomSurface = class {
209
742
  onIntent: (i) => this.commit_intent(i),
210
743
  style: { chromeColor: editor.style.chrome_color }
211
744
  });
745
+ this.camera = new Camera({
746
+ resolve_bounds: (target) => this.resolve_world_bounds(target),
747
+ initial: options.initial_camera
748
+ });
749
+ this.teardown.push(this.camera.subscribe(() => {
750
+ this.apply_camera_transform();
751
+ this.redraw();
752
+ }));
212
753
  this.render();
213
754
  this.sync_canvas_size();
214
755
  this.sync_surface_selection();
@@ -216,9 +757,19 @@ var DomSurface = class {
216
757
  const win = container.ownerDocument.defaultView ?? window;
217
758
  const raf = win.requestAnimationFrame(() => {
218
759
  this.sync_canvas_size();
760
+ this.honor_initial_fit();
219
761
  this.redraw();
220
762
  });
221
763
  this.teardown.push(() => win.cancelAnimationFrame(raf));
764
+ this.gestures = new Gestures({
765
+ container,
766
+ svg_root: () => this.svg_root,
767
+ hud_canvas: this.hud_canvas,
768
+ camera: this.camera,
769
+ editor,
770
+ handle: { detach: () => {} }
771
+ });
772
+ if (options.gestures !== false) applyDefaultGestures(this.gestures);
222
773
  const unsub = editor.subscribe(() => {
223
774
  this.render();
224
775
  this.sync_surface_selection();
@@ -290,6 +841,7 @@ var DomSurface = class {
290
841
  this.text_edit = null;
291
842
  this.text_edit_target = null;
292
843
  }
844
+ this.gestures._dispose();
293
845
  for (const fn of this.teardown) fn();
294
846
  this.teardown = [];
295
847
  this.hud.dispose();
@@ -299,6 +851,10 @@ var DomSurface = class {
299
851
  this.element_index.clear();
300
852
  this.active_preview = null;
301
853
  }
854
+ /** Public — invoked by the `DomSurfaceHandle` wrapper before `detach()`. */
855
+ detach_gestures() {
856
+ this.gestures._dispose();
857
+ }
302
858
  render() {
303
859
  if (this.text_edit) return;
304
860
  const owner_doc = this.container.ownerDocument;
@@ -311,6 +867,8 @@ var DomSurface = class {
311
867
  if (this.svg_root) this.svg_root.replaceWith(new_svg);
312
868
  else this.container.insertBefore(new_svg, this.hud_canvas);
313
869
  this.svg_root = new_svg;
870
+ this.apply_svg_layout();
871
+ this.apply_camera_transform();
314
872
  this.element_index.clear();
315
873
  const ids = doc.all_elements();
316
874
  let i = 0;
@@ -327,8 +885,142 @@ var DomSurface = class {
327
885
  sync_canvas_size() {
328
886
  const cr = this.container.getBoundingClientRect();
329
887
  this.hud.setSize(cr.width, cr.height);
888
+ this.camera._set_viewport_size(cr.width, cr.height);
330
889
  this.redraw();
331
890
  }
891
+ /**
892
+ * Apply absolute positioning + transform-origin to the SVG so the camera's
893
+ * CSS matrix maps SVG-coord (0,0) cleanly to container-screen (tx, ty).
894
+ * Called after every render() that may have replaced the root element.
895
+ */
896
+ apply_svg_layout() {
897
+ if (!this.svg_root) return;
898
+ const style = this.svg_root.style;
899
+ style.position = "absolute";
900
+ style.left = "0";
901
+ style.top = "0";
902
+ style.transformOrigin = "0 0";
903
+ }
904
+ /**
905
+ * Push the current camera transform to the SVG as a CSS `matrix(...)`.
906
+ * The HUD canvas stays at identity — selection chrome reads node bounds
907
+ * via `getScreenCTM()`, which already includes the CSS transform, so
908
+ * chrome aligns automatically and stays 1px sharp at any zoom.
909
+ */
910
+ apply_camera_transform() {
911
+ if (!this.svg_root) return;
912
+ const t = this.camera.transform;
913
+ this.svg_root.style.transform = `matrix(${t[0][0]}, ${t[1][0]}, ${t[0][1]}, ${t[1][1]}, ${t[0][2]}, ${t[1][2]})`;
914
+ }
915
+ /** One-shot fit-on-attach. Runs after layout has settled. */
916
+ honor_initial_fit() {
917
+ if (!this.fit_on_attach) return;
918
+ this.fit_on_attach = false;
919
+ this.camera.fit("<root>");
920
+ }
921
+ /**
922
+ * BoundsResolver for `Camera.fit(target)`. The Camera class handles Rect
923
+ * passthrough itself; this resolver only sees string targets — sentinels
924
+ * ("<root>", "<selection>") and NodeIds.
925
+ */
926
+ resolve_world_bounds(target) {
927
+ if (target === "<root>") return this.root_world_bounds();
928
+ if (target === "<selection>") {
929
+ const sel = this.editor.state.selection;
930
+ if (sel.length === 0) return null;
931
+ const rects = [];
932
+ for (const id of sel) {
933
+ const r = this.node_world_bounds(id);
934
+ if (r) rects.push(r);
935
+ }
936
+ if (rects.length === 0) return null;
937
+ return cmath.rect.union(rects);
938
+ }
939
+ return this.node_world_bounds(target);
940
+ }
941
+ /**
942
+ * World-space bounds of the root document. Prefer `viewBox` (the SVG's
943
+ * declared world rect), fall back to `width`/`height` attrs, then the
944
+ * SVG root's `getBBox()` as a last resort.
945
+ */
946
+ root_world_bounds() {
947
+ const root_id = this.editor.tree().root;
948
+ const doc = this.editor.document;
949
+ const view_box = doc.get_attr(root_id, "viewBox");
950
+ if (view_box) {
951
+ const parts = view_box.trim().split(/[\s,]+/).map(Number);
952
+ if (parts.length === 4 && parts.every((n) => Number.isFinite(n))) return {
953
+ x: parts[0],
954
+ y: parts[1],
955
+ width: parts[2],
956
+ height: parts[3]
957
+ };
958
+ }
959
+ const w = parseFloat(doc.get_attr(root_id, "width") ?? "");
960
+ const h = parseFloat(doc.get_attr(root_id, "height") ?? "");
961
+ if (Number.isFinite(w) && Number.isFinite(h) && w > 0 && h > 0) return {
962
+ x: 0,
963
+ y: 0,
964
+ width: w,
965
+ height: h
966
+ };
967
+ if (this.svg_root) try {
968
+ const b = this.svg_root.getBBox();
969
+ if (b.width > 0 && b.height > 0) return {
970
+ x: b.x,
971
+ y: b.y,
972
+ width: b.width,
973
+ height: b.height
974
+ };
975
+ } catch {}
976
+ return null;
977
+ }
978
+ /**
979
+ * World-space bounds of a single node. Uses `getBBox()` (element-local)
980
+ * and projects through `getCTM()` (local → nearest viewport = SVG root).
981
+ */
982
+ node_world_bounds(id) {
983
+ const el = this.element_index.get(id);
984
+ if (!el) return null;
985
+ const ge = el;
986
+ if (typeof ge.getBBox !== "function" || typeof ge.getCTM !== "function") return null;
987
+ let bbox;
988
+ try {
989
+ const b = ge.getBBox();
990
+ bbox = {
991
+ x: b.x,
992
+ y: b.y,
993
+ width: b.width,
994
+ height: b.height
995
+ };
996
+ } catch {
997
+ return null;
998
+ }
999
+ const ctm = ge.getCTM();
1000
+ if (!ctm) return bbox;
1001
+ const project = (px, py) => ({
1002
+ x: ctm.a * px + ctm.c * py + ctm.e,
1003
+ y: ctm.b * px + ctm.d * py + ctm.f
1004
+ });
1005
+ const corners = [
1006
+ project(bbox.x, bbox.y),
1007
+ project(bbox.x + bbox.width, bbox.y),
1008
+ project(bbox.x + bbox.width, bbox.y + bbox.height),
1009
+ project(bbox.x, bbox.y + bbox.height)
1010
+ ];
1011
+ const xs = corners.map((c) => c.x);
1012
+ const ys = corners.map((c) => c.y);
1013
+ const left = Math.min(...xs);
1014
+ const top = Math.min(...ys);
1015
+ const right = Math.max(...xs);
1016
+ const bottom = Math.max(...ys);
1017
+ return {
1018
+ x: left,
1019
+ y: top,
1020
+ width: right - left,
1021
+ height: bottom - top
1022
+ };
1023
+ }
332
1024
  /** Single per-frame draw entry — merges host-fed extras with surface chrome. */
333
1025
  redraw() {
334
1026
  this.hud.draw(this.compute_measurement_extra());
@@ -544,8 +1236,10 @@ var DomSurface = class {
544
1236
  dispatch_pointer(e, kind) {
545
1237
  if (this.text_edit) {
546
1238
  if (kind === "pointer_down") {
1239
+ e.preventDefault();
547
1240
  const el = this.text_edit_target ? this.element_index.get(this.text_edit_target) : null;
548
1241
  if (el && e.target instanceof Element && (e.target === el || el.contains(e.target))) this.text_edit.pointerDown(e.clientX, e.clientY, e.shiftKey);
1242
+ else this.text_edit.commit();
549
1243
  } else if (kind === "pointer_move") this.text_edit.pointerMove(e.clientX, e.clientY);
550
1244
  else if (kind === "pointer_up") this.text_edit.pointerUp();
551
1245
  return;