@embedpdf/react 3.0.0-next.0 → 3.0.0-next.10

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 (104) hide show
  1. package/dist/AnnotationListSnapshot-DDxHpJQU-BG32YXxG.d.cts +347 -0
  2. package/dist/AnnotationListSnapshot-DDxHpJQU-BG32YXxG.d.ts +347 -0
  3. package/dist/anchored.cjs +89 -0
  4. package/dist/anchored.cjs.map +1 -0
  5. package/dist/anchored.d.cts +45 -0
  6. package/dist/anchored.d.ts +45 -0
  7. package/dist/anchored.js +60 -0
  8. package/dist/anchored.js.map +1 -0
  9. package/dist/annotation-menu.cjs +33 -84
  10. package/dist/annotation-menu.cjs.map +1 -1
  11. package/dist/annotation-menu.d.cts +28 -2
  12. package/dist/annotation-menu.d.ts +28 -2
  13. package/dist/annotation-menu.js +27 -78
  14. package/dist/annotation-menu.js.map +1 -1
  15. package/dist/annotation.cjs +752 -16
  16. package/dist/annotation.cjs.map +1 -0
  17. package/dist/annotation.d.cts +159 -2
  18. package/dist/annotation.d.ts +159 -2
  19. package/dist/annotation.js +737 -3
  20. package/dist/annotation.js.map +1 -0
  21. package/dist/commands.js +1 -0
  22. package/dist/commands.js.map +1 -1
  23. package/dist/form.cjs +3 -3
  24. package/dist/form.cjs.map +1 -1
  25. package/dist/form.d.cts +2 -2
  26. package/dist/form.d.ts +2 -2
  27. package/dist/form.js +3 -3
  28. package/dist/form.js.map +1 -1
  29. package/dist/index.cjs +34 -5
  30. package/dist/index.d.cts +14 -8
  31. package/dist/index.d.ts +14 -8
  32. package/dist/index.js +8 -6
  33. package/dist/interaction.cjs +17 -4
  34. package/dist/interaction.cjs.map +1 -1
  35. package/dist/interaction.d.cts +2 -2
  36. package/dist/interaction.d.ts +2 -2
  37. package/dist/interaction.js +7 -5
  38. package/dist/interaction.js.map +1 -1
  39. package/dist/link.cjs +29 -6
  40. package/dist/link.cjs.map +1 -1
  41. package/dist/link.d.cts +12 -3
  42. package/dist/link.d.ts +12 -3
  43. package/dist/link.js +31 -8
  44. package/dist/link.js.map +1 -1
  45. package/dist/metadata.js +1 -0
  46. package/dist/metadata.js.map +1 -1
  47. package/dist/page-edit.js +1 -0
  48. package/dist/page-edit.js.map +1 -1
  49. package/dist/page-view.cjs +62 -38
  50. package/dist/page-view.cjs.map +1 -1
  51. package/dist/page-view.js +59 -37
  52. package/dist/page-view.js.map +1 -1
  53. package/dist/redaction.js +1 -0
  54. package/dist/redaction.js.map +1 -1
  55. package/dist/render.cjs +67 -50
  56. package/dist/render.cjs.map +1 -1
  57. package/dist/render.d.cts +6 -19
  58. package/dist/render.d.ts +6 -19
  59. package/dist/render.js +71 -52
  60. package/dist/render.js.map +1 -1
  61. package/dist/rolldown-runtime-BBjsoOtd.js +27 -0
  62. package/dist/{runtime-DV98q7I0.d.ts → runtime-Cvi0wQ4d.d.cts} +23 -10
  63. package/dist/{runtime-CMxIuF2O.d.cts → runtime-DXxr8T4L.d.ts} +23 -10
  64. package/dist/runtime.cjs +6 -5
  65. package/dist/runtime.cjs.map +1 -1
  66. package/dist/runtime.d.cts +1 -1
  67. package/dist/runtime.d.ts +1 -1
  68. package/dist/runtime.js +7 -5
  69. package/dist/runtime.js.map +1 -1
  70. package/dist/search.cjs +36 -19
  71. package/dist/search.cjs.map +1 -1
  72. package/dist/search.d.cts +1 -1
  73. package/dist/search.d.ts +1 -1
  74. package/dist/search.js +36 -19
  75. package/dist/search.js.map +1 -1
  76. package/dist/selection.cjs +217 -22
  77. package/dist/selection.cjs.map +1 -1
  78. package/dist/selection.d.cts +60 -2
  79. package/dist/selection.d.ts +60 -2
  80. package/dist/selection.js +214 -27
  81. package/dist/selection.js.map +1 -1
  82. package/dist/shell.js +1 -0
  83. package/dist/shell.js.map +1 -1
  84. package/dist/stage.cjs +47 -146
  85. package/dist/stage.cjs.map +1 -1
  86. package/dist/stage.d.cts +18 -5
  87. package/dist/stage.d.ts +18 -5
  88. package/dist/stage.js +50 -148
  89. package/dist/stage.js.map +1 -1
  90. package/dist/stamp.js +1 -0
  91. package/dist/stamp.js.map +1 -1
  92. package/dist/toolbar.js +1 -0
  93. package/dist/toolbar.js.map +1 -1
  94. package/dist/views.js +1 -0
  95. package/dist/views.js.map +1 -1
  96. package/package.json +27 -26
  97. package/dist/AnnotationListSnapshot-W0RCsyDE-dWna4TTJ.d.cts +0 -2593
  98. package/dist/AnnotationListSnapshot-W0RCsyDE-dWna4TTJ.d.ts +0 -2593
  99. package/dist/annotation-B6jwfTCh.d.cts +0 -173
  100. package/dist/annotation-Ct6dFtzJ.cjs +0 -913
  101. package/dist/annotation-Ct6dFtzJ.cjs.map +0 -1
  102. package/dist/annotation-DjGWJljY.js +0 -829
  103. package/dist/annotation-DjGWJljY.js.map +0 -1
  104. package/dist/annotation-nUMefyjw.d.ts +0 -173
@@ -1,4 +1,738 @@
1
- import "./runtime.js";
2
- import { a as sameAnchor, c as useAnnotationDefaults, d as useFilePickerProvider, f as useSelectionFlags, i as filePickerProvider, l as useAnnotationSelected, n as PageAnnotationDraftMenu, o as sameCreationDraftAnchor, p as useSelectionProps, r as PageAnnotationMenu, s as useAnnotation, t as AnnotationLayer, u as useAnnotationSelection } from "./annotation-DjGWJljY.js";
1
+ import "./rolldown-runtime-BBjsoOtd.js";
2
+ import { shallowArray, useCapability, useDocumentId, useKernelValue, useOptionalCapability, usePage, useSelector } from "./runtime.js";
3
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
4
+ import * as React from "react";
5
+ import { useEffect, useState } from "react";
6
+ import { pickFile } from "@embedpdf/web";
7
+ import { AnnotationToken, refKey } from "@embedpdf/plugin-annotation";
8
+ import { AnnotationToken as AnnotationToken$1 } from "@embedpdf/plugin-annotation/internal";
9
+ import { MITER_LIMIT, pdfToContentRect, scene } from "@embedpdf/core-annotation";
3
10
  export * from "@embedpdf/plugin-annotation";
4
- export { AnnotationLayer, PageAnnotationDraftMenu, PageAnnotationMenu, filePickerProvider, sameAnchor, sameCreationDraftAnchor, useAnnotation, useAnnotationDefaults, useAnnotationSelected, useAnnotationSelection, useFilePickerProvider, useSelectionFlags, useSelectionProps };
11
+ //#region src/annotation.tsx
12
+ /** `#rrggbb` → `rgba(...)` — the marquee's translucent fill derives from the
13
+ * accent, so one `setChrome({ accent })` restyles every piece of chrome. */
14
+ const rgba = (hex, alpha) => {
15
+ const m = /^#?([0-9a-f]{6})$/i.exec(hex);
16
+ if (!m) return hex;
17
+ const n = parseInt(m[1], 16);
18
+ return `rgba(${n >> 16 & 255},${n >> 8 & 255},${n & 255},${alpha})`;
19
+ };
20
+ /** Content rect → a view-px box (the page wrapper's own coordinate space). */
21
+ function boxOf(r, page) {
22
+ const tl = page.transform.toPixels({
23
+ x: r.x,
24
+ y: r.y
25
+ });
26
+ const br = page.transform.toPixels({
27
+ x: r.x + r.width,
28
+ y: r.y + r.height
29
+ });
30
+ return {
31
+ left: tl.x,
32
+ top: tl.y,
33
+ width: br.x - tl.x,
34
+ height: br.y - tl.y
35
+ };
36
+ }
37
+ /** Map a core `Paint` to SVG presentation attributes — the whole framework-facing
38
+ * surface. Everything else about appearance is decided in the core's `scene`. */
39
+ function paintAttrs(p) {
40
+ return {
41
+ fill: p.fill ?? "none",
42
+ stroke: p.stroke ?? "none",
43
+ strokeWidth: p.width,
44
+ opacity: p.opacity,
45
+ strokeLinejoin: p.join ?? "miter",
46
+ strokeMiterlimit: MITER_LIMIT,
47
+ strokeLinecap: p.cap,
48
+ strokeDasharray: p.dash ? p.dash.join(" ") : void 0,
49
+ ...p.blend ? { style: { mixBlendMode: p.blend } } : {}
50
+ };
51
+ }
52
+ /**
53
+ * The dumb painter. The pure core computed `item.box` and the painted `scene`; we
54
+ * size the <svg> to the box with a content-space `viewBox` and map each SceneNode
55
+ * to one element, applying its `paint`. No per-kind logic, no bounds math — so
56
+ * shapes, cloudy borders and every text-markup type all render here, and a Vue /
57
+ * Svelte painter is the same ~10-line loop.
58
+ */
59
+ function Shape({ item, page }) {
60
+ if (item.box.width <= 0 || item.box.height <= 0) return null;
61
+ const { left, top, width, height } = boxOf(item.box, page);
62
+ const vb = `${item.box.x} ${item.box.y} ${item.box.width} ${item.box.height}`;
63
+ const rot = item.geom.t === "rect" || item.geom.t === "caret" ? item.rot ?? 0 : 0;
64
+ return /* @__PURE__ */ jsx("svg", {
65
+ viewBox: vb,
66
+ style: {
67
+ position: "absolute",
68
+ left,
69
+ top,
70
+ width,
71
+ height,
72
+ overflow: "visible",
73
+ pointerEvents: "none",
74
+ ...rot ? {
75
+ transform: `rotate(${rot}deg)`,
76
+ transformOrigin: "center"
77
+ } : {}
78
+ },
79
+ children: sceneNodes(item)
80
+ });
81
+ }
82
+ /** Map a core scene to SVG children. */
83
+ function sceneNodes(item) {
84
+ return scene(item).map((n, i) => {
85
+ const a = paintAttrs(n.paint);
86
+ if (n.kind === "rect") return /* @__PURE__ */ jsx("rect", {
87
+ x: n.rect.x,
88
+ y: n.rect.y,
89
+ width: n.rect.width,
90
+ height: n.rect.height,
91
+ ...a
92
+ }, i);
93
+ if (n.kind === "ellipse") return /* @__PURE__ */ jsx("ellipse", {
94
+ cx: n.rect.x + n.rect.width / 2,
95
+ cy: n.rect.y + n.rect.height / 2,
96
+ rx: n.rect.width / 2,
97
+ ry: n.rect.height / 2,
98
+ ...a
99
+ }, i);
100
+ if (n.kind === "line") return /* @__PURE__ */ jsx("line", {
101
+ x1: n.a.x,
102
+ y1: n.a.y,
103
+ x2: n.b.x,
104
+ y2: n.b.y,
105
+ ...a
106
+ }, i);
107
+ if (n.kind === "path") return /* @__PURE__ */ jsx("path", {
108
+ d: n.d,
109
+ ...a
110
+ }, i);
111
+ if (n.kind === "text") return /* @__PURE__ */ jsx("text", {
112
+ x: n.at.x,
113
+ y: n.at.y,
114
+ fontSize: n.fontSize,
115
+ ...n.fontFamily ? { fontFamily: n.fontFamily } : {},
116
+ ...a,
117
+ children: n.text
118
+ }, i);
119
+ const pts = n.points.map((p) => `${p.x},${p.y}`).join(" ");
120
+ return n.closed ? /* @__PURE__ */ jsx("polygon", {
121
+ points: pts,
122
+ ...a
123
+ }, i) : /* @__PURE__ */ jsx("polyline", {
124
+ points: pts,
125
+ ...a
126
+ }, i);
127
+ });
128
+ }
129
+ function BakedImage({ box, url, page, blend, rot }) {
130
+ const b = boxOf(box, page);
131
+ return /* @__PURE__ */ jsx("img", {
132
+ src: url,
133
+ alt: "",
134
+ draggable: false,
135
+ style: {
136
+ position: "absolute",
137
+ left: b.left,
138
+ top: b.top,
139
+ width: b.width,
140
+ height: b.height,
141
+ maxWidth: "none",
142
+ maxHeight: "none",
143
+ pointerEvents: "none",
144
+ mixBlendMode: blend,
145
+ ...rot ? {
146
+ transform: `rotate(${rot}deg)`,
147
+ transformOrigin: "center"
148
+ } : {}
149
+ }
150
+ });
151
+ }
152
+ /**
153
+ * The armed stamp's IMAGE footprint ghost: a translucent render of the payload
154
+ * drawn in the EXACT box a click would place it (the plugin computes it with
155
+ * the same fit + clamp as placement). Vector footprint ghosts never reach this
156
+ * component — they ride `pageItems` like every draft preview. The preview
157
+ * bytes live in the capability closure; this layer owns only the object-URL
158
+ * lifetime, keyed on the arm epoch — a new arm swaps the image, a disarm (or
159
+ * tool change) drops it.
160
+ */
161
+ function ToolGhostImage({ page }) {
162
+ const anno = useCapability(AnnotationToken$1);
163
+ const ghost = useSelector(AnnotationToken$1, (c) => c.toolGhost(page.pon));
164
+ const epoch = useSelector(AnnotationToken$1, (c) => c.stampArmEpoch());
165
+ const [url, setUrl] = useState(null);
166
+ useEffect(() => {
167
+ const preview = anno.armedStampPreview();
168
+ if (!preview) {
169
+ setUrl(null);
170
+ return;
171
+ }
172
+ const body = new ArrayBuffer(preview.bytes.byteLength);
173
+ new Uint8Array(body).set(preview.bytes);
174
+ const blob = new Blob([body], preview.mimeType ? { type: preview.mimeType } : {});
175
+ const obj = URL.createObjectURL(blob);
176
+ setUrl(obj);
177
+ return () => {
178
+ URL.revokeObjectURL(obj);
179
+ setUrl(null);
180
+ };
181
+ }, [anno, epoch]);
182
+ if (!ghost || ghost.kind !== "image" || !url) return null;
183
+ const b = boxOf(ghost.box, page);
184
+ return /* @__PURE__ */ jsx("img", {
185
+ src: url,
186
+ alt: "",
187
+ draggable: false,
188
+ style: {
189
+ position: "absolute",
190
+ left: b.left,
191
+ top: b.top,
192
+ width: b.width,
193
+ height: b.height,
194
+ maxWidth: "none",
195
+ maxHeight: "none",
196
+ pointerEvents: "none",
197
+ opacity: .5,
198
+ ...ghost.rot ? {
199
+ transform: `rotate(${ghost.rot}deg)`,
200
+ transformOrigin: "center"
201
+ } : {}
202
+ }
203
+ });
204
+ }
205
+ function Chrome({ page }) {
206
+ const scale = page.transform.viewScale;
207
+ const rotation = page.transform.rotation;
208
+ const zoom = page.transform.zoom;
209
+ const nodes = useSelector(AnnotationToken$1, (c) => c.chrome(page.pon, scale, rotation, zoom), shallowArray);
210
+ const cs = useSelector(AnnotationToken$1, (c) => c.chromeSettings());
211
+ const outlineStroke = cs.outline.color ?? cs.accent;
212
+ const handleStroke = cs.handles.stroke ?? cs.accent;
213
+ const knobStroke = cs.knob.stroke ?? cs.accent;
214
+ const outlineDash = cs.outline.style === "dashed" ? "4 3" : void 0;
215
+ const chip = nodes.find((n) => n.kind === "angle-chip");
216
+ const chipAt = chip ? page.transform.toPixels(chip.at) : null;
217
+ return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("svg", {
218
+ style: {
219
+ position: "absolute",
220
+ inset: 0,
221
+ overflow: "visible",
222
+ pointerEvents: "none"
223
+ },
224
+ children: nodes.map((n, i) => {
225
+ if (n.kind === "angle-chip") return null;
226
+ if (n.kind === "handle") {
227
+ const p = page.transform.toPixels(n.at);
228
+ const hs = cs.handles.size;
229
+ return /* @__PURE__ */ jsx("rect", {
230
+ x: p.x - hs / 2,
231
+ y: p.y - hs / 2,
232
+ width: hs,
233
+ height: hs,
234
+ fill: cs.handles.fill,
235
+ stroke: handleStroke,
236
+ strokeWidth: 1.5,
237
+ ...n.rot ? { transform: `rotate(${n.rot} ${p.x} ${p.y})` } : {}
238
+ }, i);
239
+ }
240
+ if (n.kind === "guide") {
241
+ const a = page.transform.toPixels(n.axis === "x" ? {
242
+ x: n.at,
243
+ y: n.lo
244
+ } : {
245
+ x: n.lo,
246
+ y: n.at
247
+ });
248
+ const b = page.transform.toPixels(n.axis === "x" ? {
249
+ x: n.at,
250
+ y: n.hi
251
+ } : {
252
+ x: n.hi,
253
+ y: n.at
254
+ });
255
+ return /* @__PURE__ */ jsx("line", {
256
+ x1: a.x,
257
+ y1: a.y,
258
+ x2: b.x,
259
+ y2: b.y,
260
+ stroke: "#e91e63",
261
+ strokeWidth: 1.5,
262
+ shapeRendering: "crispEdges"
263
+ }, i);
264
+ }
265
+ if (n.kind === "obb") return /* @__PURE__ */ jsx("polygon", {
266
+ points: n.corners.map((c) => {
267
+ const p = page.transform.toPixels(c);
268
+ return `${p.x},${p.y}`;
269
+ }).join(" "),
270
+ fill: "none",
271
+ stroke: outlineStroke,
272
+ strokeWidth: cs.outline.width,
273
+ strokeDasharray: outlineDash
274
+ }, i);
275
+ if (n.kind === "rotate-guides") {
276
+ const guideDash = cs.guides.style === "dashed" ? "4 3" : void 0;
277
+ return /* @__PURE__ */ jsx("g", { children: n.lines.map((l, j) => {
278
+ const a = page.transform.toPixels(l.a);
279
+ const b = page.transform.toPixels(l.b);
280
+ const axis = l.role === "axis";
281
+ return /* @__PURE__ */ jsx("line", {
282
+ x1: a.x,
283
+ y1: a.y,
284
+ x2: b.x,
285
+ y2: b.y,
286
+ stroke: axis ? cs.guides.axisColor ?? cs.accent : cs.guides.indicatorColor ?? cs.accent,
287
+ opacity: axis ? cs.guides.axisOpacity : cs.guides.indicatorOpacity,
288
+ strokeWidth: cs.guides.width,
289
+ strokeDasharray: guideDash
290
+ }, j);
291
+ }) }, i);
292
+ }
293
+ if (n.kind === "rotate-knob") {
294
+ const at = page.transform.toPixels(n.at);
295
+ const from = page.transform.toPixels(n.from);
296
+ return /* @__PURE__ */ jsxs("g", { children: [cs.knob.stalk && /* @__PURE__ */ jsx("line", {
297
+ x1: from.x,
298
+ y1: from.y,
299
+ x2: at.x,
300
+ y2: at.y,
301
+ stroke: knobStroke,
302
+ strokeWidth: 1
303
+ }), /* @__PURE__ */ jsx("circle", {
304
+ cx: at.x,
305
+ cy: at.y,
306
+ r: cs.knob.size / 2,
307
+ fill: cs.knob.fill,
308
+ stroke: knobStroke,
309
+ strokeWidth: 1.5
310
+ })] }, i);
311
+ }
312
+ const b = boxOf(n.rect, page);
313
+ if (n.kind === "marquee") return /* @__PURE__ */ jsx("rect", {
314
+ x: b.left,
315
+ y: b.top,
316
+ width: b.width,
317
+ height: b.height,
318
+ fill: rgba(cs.accent, .08),
319
+ stroke: cs.accent,
320
+ strokeWidth: 1,
321
+ strokeDasharray: "4 3"
322
+ }, i);
323
+ return /* @__PURE__ */ jsx("rect", {
324
+ x: b.left,
325
+ y: b.top,
326
+ width: b.width,
327
+ height: b.height,
328
+ fill: "none",
329
+ stroke: outlineStroke,
330
+ strokeWidth: cs.outline.width,
331
+ strokeDasharray: outlineDash
332
+ }, i);
333
+ })
334
+ }), chip && chipAt && /* @__PURE__ */ jsxs("div", {
335
+ style: {
336
+ position: "absolute",
337
+ left: chipAt.x + 16,
338
+ top: chipAt.y - 28,
339
+ background: "rgba(0,0,0,0.8)",
340
+ color: "#fff",
341
+ padding: "2px 6px",
342
+ borderRadius: 4,
343
+ fontSize: 12,
344
+ fontFamily: "monospace",
345
+ pointerEvents: "none",
346
+ whiteSpace: "nowrap",
347
+ zIndex: 1
348
+ },
349
+ children: [chip.angle, "°"]
350
+ })] });
351
+ }
352
+ /**
353
+ * A free-text annotation: the SAME styled element for viewing and editing —
354
+ * `contentEditable` just toggles, so the text never jumps. The plugin handed us a
355
+ * ready-to-spread style (`item.css`); the browser owns layout, caret, selection,
356
+ * IME and clipboard; the plugin owns the text truth + the debounced engine write.
357
+ * This component is the ENTIRE per-framework surface for text editing.
358
+ */
359
+ function FreeText({ item, page }) {
360
+ const anno = useCapability(AnnotationToken$1);
361
+ const ref = React.useRef(null);
362
+ const box = boxOf(item.box, page);
363
+ const scale = item.box.width > 0 ? box.width / item.box.width : 1;
364
+ const pad = item.css.padding * scale;
365
+ const plate = {
366
+ left: box.left + pad,
367
+ top: box.top + pad,
368
+ width: Math.max(0, box.width - 2 * pad),
369
+ height: Math.max(0, box.height - 2 * pad)
370
+ };
371
+ useEffect(() => {
372
+ const el = ref.current;
373
+ if (el && document.activeElement !== el && el.innerText !== item.contents) el.innerText = item.contents;
374
+ }, [item.contents, item.editing]);
375
+ useEffect(() => {
376
+ if (item.editing) {
377
+ const el = ref.current;
378
+ if (!el) return;
379
+ el.focus();
380
+ el.scrollTop = 0;
381
+ }
382
+ }, [item.editing]);
383
+ useEffect(() => {
384
+ const el = ref.current;
385
+ if (!el) return;
386
+ const stop = (e) => e.stopPropagation();
387
+ el.addEventListener("pointerdown", stop);
388
+ return () => el.removeEventListener("pointerdown", stop);
389
+ }, []);
390
+ return /* @__PURE__ */ jsx("div", {
391
+ ref,
392
+ contentEditable: item.editing,
393
+ suppressContentEditableWarning: true,
394
+ onInput: () => item.ref && anno.setContents(item.ref, ref.current.innerText),
395
+ onBlur: (e) => {
396
+ if (e.relatedTarget == null && ref.current?.isConnected && anno.currentEditing() === item.id) ref.current.focus();
397
+ },
398
+ onPaste: (e) => {
399
+ e.preventDefault();
400
+ document.execCommand("insertText", false, e.clipboardData.getData("text/plain"));
401
+ },
402
+ style: {
403
+ position: "absolute",
404
+ left: plate.left,
405
+ top: plate.top,
406
+ width: plate.width,
407
+ height: plate.height,
408
+ fontFamily: item.css.fontFamily,
409
+ fontSize: item.css.fontSize * scale,
410
+ lineHeight: `${item.css.lineHeight * scale}px`,
411
+ color: item.css.color,
412
+ textAlign: item.css.align,
413
+ boxSizing: "border-box",
414
+ background: item.css.background ?? "transparent",
415
+ whiteSpace: "pre-wrap",
416
+ overflowWrap: "break-word",
417
+ overflowY: item.editing ? "auto" : "hidden",
418
+ overflowX: "hidden",
419
+ outline: item.editing ? "1px solid #3858e9" : "none",
420
+ cursor: item.editing ? "text" : "default",
421
+ ...item.rot ? {
422
+ transform: `rotate(${item.rot}deg)`,
423
+ transformOrigin: "center"
424
+ } : {},
425
+ pointerEvents: item.editing ? "auto" : "none"
426
+ }
427
+ });
428
+ }
429
+ /**
430
+ * Auto-registered behaviors for `interactive` renderer entries — refcounted
431
+ * per (capability, entry) because the layer mounts once PER PAGE: the first
432
+ * page registers, the last unregisters. Entry identity is the key, hence the
433
+ * "define entries outside render" rule on {@link AnnotationRenderer}.
434
+ */
435
+ const autoBehaviors = /* @__PURE__ */ new WeakMap();
436
+ let autoBehaviorSeq = 0;
437
+ function useAutoBehaviors(anno, renderers) {
438
+ useEffect(() => {
439
+ if (!renderers) return;
440
+ const released = [];
441
+ for (const r of renderers) {
442
+ if (!("for" in r) || !r.interactive) continue;
443
+ let perCap = autoBehaviors.get(anno);
444
+ if (!perCap) autoBehaviors.set(anno, perCap = /* @__PURE__ */ new Map());
445
+ let rec = perCap.get(r);
446
+ if (!rec) {
447
+ const id = r.id ?? `renderer:${++autoBehaviorSeq}`;
448
+ const engaged = typeof r.interactive === "function" ? r.interactive : () => true;
449
+ rec = {
450
+ id,
451
+ count: 0,
452
+ unregister: anno.registerBehavior({
453
+ id,
454
+ matches: r.for,
455
+ engaged
456
+ })
457
+ };
458
+ perCap.set(r, rec);
459
+ }
460
+ rec.count++;
461
+ const owned = rec;
462
+ released.push(() => {
463
+ owned.count--;
464
+ if (owned.count === 0) {
465
+ owned.unregister();
466
+ autoBehaviors.get(anno)?.delete(r);
467
+ }
468
+ });
469
+ }
470
+ return () => released.forEach((f) => f());
471
+ }, [anno, renderers]);
472
+ }
473
+ /** The behavior id a renderer entry answers for (plugin-owned or auto-registered). */
474
+ function rendererBehaviorId(anno, r) {
475
+ if ("behavior" in r) return r.behavior;
476
+ return autoBehaviors.get(anno)?.get(r)?.id ?? null;
477
+ }
478
+ /** React 18 spells the `inert` attribute as a string spread; it hard-disables
479
+ * pointer AND focus for the whole subtree — the mechanical guarantee that a
480
+ * non-interactive renderer entry (a skin) can never steal input. */
481
+ const INERT = { inert: "" };
482
+ function AnnotationLayer({ renderers } = {}) {
483
+ const page = usePage();
484
+ const anno = useCapability(AnnotationToken$1);
485
+ const viewZoom = page.transform.zoom;
486
+ const viewRotation = page.transform.rotation;
487
+ const items = useSelector(AnnotationToken$1, (c) => c.pageItems(page.pon, {
488
+ zoom: viewZoom,
489
+ rotation: viewRotation
490
+ }), shallowArray);
491
+ const texts = useSelector(AnnotationToken$1, (c) => c.textItems(page.pon, {
492
+ zoom: viewZoom,
493
+ rotation: viewRotation
494
+ }), shallowArray);
495
+ const [urls, setUrls] = useState({});
496
+ useAutoBehaviors(anno, renderers);
497
+ useEffect(() => {
498
+ anno.ensurePage(page.pon);
499
+ }, [anno, page.pon]);
500
+ const bakedKey = useSelector(AnnotationToken$1, (c) => c.appearanceEpoch(page.pon));
501
+ const bakeScale = useSelector(AnnotationToken$1, (c) => c.bakeScale(page.transform.renderScale));
502
+ useEffect(() => {
503
+ const controller = new AbortController();
504
+ const revokers = [];
505
+ (async () => {
506
+ try {
507
+ const imgs = await anno.appearances(page.pon, bakeScale, controller.signal);
508
+ const map = {};
509
+ for (const ap of imgs) {
510
+ const box = anno.toContentBox(page.pon, ap.rect);
511
+ if (!box) continue;
512
+ const obj = await ap.image.objectUrl(controller.signal);
513
+ if (controller.signal.aborted) {
514
+ obj.revoke();
515
+ return;
516
+ }
517
+ revokers.push(obj.revoke);
518
+ map[refKey(ap.ref)] = {
519
+ url: obj.url,
520
+ box
521
+ };
522
+ }
523
+ if (!controller.signal.aborted) setUrls(map);
524
+ } catch {}
525
+ })();
526
+ return () => {
527
+ controller.abort();
528
+ revokers.forEach((r) => r());
529
+ };
530
+ }, [
531
+ anno,
532
+ page.pon,
533
+ bakeScale,
534
+ bakedKey
535
+ ]);
536
+ return /* @__PURE__ */ jsxs("div", {
537
+ style: {
538
+ position: "absolute",
539
+ inset: 0,
540
+ pointerEvents: "none"
541
+ },
542
+ children: [
543
+ items.map((item) => {
544
+ const baked = urls[item.id];
545
+ const native = item.source === "baked" ? baked ? /* @__PURE__ */ jsx(BakedImage, {
546
+ box: item.apBox ?? baked.box,
547
+ url: baked.url,
548
+ page,
549
+ blend: item.blend,
550
+ rot: item.apRot
551
+ }) : null : /* @__PURE__ */ jsx(Shape, {
552
+ item,
553
+ page
554
+ });
555
+ const behavior = anno.behaviorFor({
556
+ subtype: item.subtype,
557
+ ref: item.ref
558
+ });
559
+ let out;
560
+ if (behavior) {
561
+ const entry = renderers?.find((r) => rendererBehaviorId(anno, r) === behavior.id);
562
+ if (entry) {
563
+ const Owner = entry.component;
564
+ out = /* @__PURE__ */ jsx(Owner, {
565
+ item,
566
+ page,
567
+ appearance: baked ?? null,
568
+ native,
569
+ interactive: true
570
+ });
571
+ } else out = null;
572
+ } else {
573
+ const entry = renderers?.find((r) => "for" in r && r.for({
574
+ subtype: item.subtype,
575
+ ref: item.ref
576
+ }));
577
+ if (entry) {
578
+ const Skin = entry.component;
579
+ out = /* @__PURE__ */ jsx("div", {
580
+ ...INERT,
581
+ style: { pointerEvents: "none" },
582
+ children: /* @__PURE__ */ jsx(Skin, {
583
+ item,
584
+ page,
585
+ appearance: baked ?? null,
586
+ native,
587
+ interactive: false
588
+ })
589
+ });
590
+ } else out = native;
591
+ }
592
+ return /* @__PURE__ */ jsx(React.Fragment, { children: out }, item.id);
593
+ }),
594
+ texts.map((t) => /* @__PURE__ */ jsx(FreeText, {
595
+ item: t,
596
+ page
597
+ }, t.id)),
598
+ /* @__PURE__ */ jsx(ToolGhostImage, { page }),
599
+ /* @__PURE__ */ jsx(Chrome, { page })
600
+ ]
601
+ });
602
+ }
603
+ /**
604
+ * The default file-picker provider: the built-in file dialog (from
605
+ * `@embedpdf/web`), honouring the tool's `accept` filter. This is the ADAPTER
606
+ * fulfilling the plugin's DOM-free port — the dialog lives here, in the
607
+ * framework layer, never in the plugin. A picked `File` carries its own name
608
+ * and mime, so it goes straight through as the engine's file source.
609
+ */
610
+ const filePickerProvider = async (req) => {
611
+ const file = await pickFile({ accept: req.accept ?? "*/*" });
612
+ return file ? { data: file } : null;
613
+ };
614
+ /**
615
+ * Install the file-picker provider for the active document — the ONE port
616
+ * behind every click-then-pick tool (a stamp `'prompt'` source, the file-
617
+ * attachment tool). Call ONCE at a document-scoped spot (not inside
618
+ * `<AnnotationLayer>`, which is per page). Defaults to
619
+ * {@link filePickerProvider}, so a bare `useFilePickerProvider()` makes all of
620
+ * them work out of the box; pass a custom provider (asset library, cloud
621
+ * drive — switch on `req.subtype` / `req.toolId`) or `null` to make
622
+ * click-then-pick tools inert. Cleared on unmount.
623
+ */
624
+ function useFilePickerProvider(provider = filePickerProvider) {
625
+ const anno = useOptionalCapability(AnnotationToken);
626
+ useEffect(() => {
627
+ if (!anno) return;
628
+ anno.setFilePickerProvider(provider);
629
+ return () => anno.setFilePickerProvider(null);
630
+ }, [anno, provider]);
631
+ }
632
+ function useAnnotation() {
633
+ return useCapability(AnnotationToken);
634
+ }
635
+ function useAnnotationSelection() {
636
+ return useSelector(AnnotationToken, (c) => c.selection(), shallowArray);
637
+ }
638
+ /** The selected annotations as engine DTOs — for selection-aware toolbars/sidebars. */
639
+ function useAnnotationSelected() {
640
+ return useSelector(AnnotationToken, (c) => c.getSelected(), shallowArray);
641
+ }
642
+ /** Structural equality for a resolved props bag — keeps the subscription from
643
+ * re-rendering on unrelated dispatches, since `currentDefaults` returns a fresh
644
+ * object each call. Small flat objects; JSON compare is exact and cheap here. */
645
+ const sameProps = (a, b) => a === b || JSON.stringify(a) === JSON.stringify(b);
646
+ /**
647
+ * A tool's RESOLVED defaults (base + per-tool override) as a full flat props
648
+ * bag, subscribed so a `setDefaults` re-renders the consumer. Use this — not the
649
+ * imperative `useAnnotation().currentDefaults(id)` — to drive default-editing
650
+ * controls, so they reflect changes live. Pair with `propsForTool(id)` for the
651
+ * specs to render.
652
+ */
653
+ function useAnnotationDefaults(toolId) {
654
+ return useSelector(AnnotationToken, (c) => c.currentDefaults(toolId), sameProps);
655
+ }
656
+ /**
657
+ * The selection's editable properties — ordered specs shared by every selected
658
+ * kind, current values, and which keys are mixed. THE hook a property sidebar
659
+ * renders from; write back with `useAnnotation().updateSelection({ [key]: v })`.
660
+ * Reference-stable between model changes (the capability memoizes by model
661
+ * identity), so the default equality is enough.
662
+ */
663
+ function useSelectionProps() {
664
+ return useSelector(AnnotationToken, (c) => c.getSelectionProps());
665
+ }
666
+ /**
667
+ * The selection's `/F` annotation flags — per-flag `true`/`false`, `null` where
668
+ * the selected annotations disagree (render an indeterminate control), `null`
669
+ * overall when nothing is selected. Write back with
670
+ * `useAnnotation().updateSelectionFlags({ locked: true })`. Reference-stable
671
+ * between model changes, like {@link useSelectionProps}.
672
+ */
673
+ function useSelectionFlags() {
674
+ return useSelector(AnnotationToken, (c) => c.getSelectionFlags());
675
+ }
676
+ /** Pure join behind {@link useCommentThreads} — exported for tests. */
677
+ function enrichCommentThreads(threads, pages) {
678
+ const byPon = new Map(pages.map((p) => [p.pageObjectNumber, p]));
679
+ return threads.map((t) => {
680
+ const page = byPon.get(t.pageObjectNumber);
681
+ return {
682
+ ...t,
683
+ pageIndex: page ? page.index : -1,
684
+ pageLabel: page ? page.label ?? String(page.index + 1) : "?",
685
+ contentRect: page ? pdfToContentRect(t.root.rect, page.boxes.crop) : null
686
+ };
687
+ });
688
+ }
689
+ /** The comments surface (verbs + `permissionsFor`) — imperative; pair with
690
+ * {@link useCommentThreads} for the subscribed view. */
691
+ function useComments() {
692
+ return useCapability(AnnotationToken).comments;
693
+ }
694
+ const EMPTY_PAGES = [];
695
+ /**
696
+ * Every comment thread in the document, display-ordered (page position →
697
+ * top of page → creation date) and enriched with `pageIndex`/`pageLabel`.
698
+ * Subscribed to BOTH stores: annotation writes (own, remote, hydration)
699
+ * recompute the threads; page moves/deletes re-run the join. Reference-
700
+ * stable between changes — safe to memo child renders on the array.
701
+ */
702
+ function useCommentThreads() {
703
+ const docId = useDocumentId();
704
+ const threads = useSelector(AnnotationToken, (c) => c.comments.threads());
705
+ const pages = useKernelValue((k) => docId ? k.getState().core.documents[docId]?.pages ?? EMPTY_PAGES : EMPTY_PAGES);
706
+ return React.useMemo(() => enrichCommentThreads(threads, pages), [threads, pages]);
707
+ }
708
+ /** The enriched thread containing ANY member ref (root, reply, grouped part,
709
+ * state annotation), or null. */
710
+ function useCommentThread(ref) {
711
+ const views = useCommentThreads();
712
+ const api = useComments();
713
+ if (ref == null) return null;
714
+ const t = api.thread(ref);
715
+ if (!t) return null;
716
+ return views.find((v) => refKey(v.root.ref) === refKey(t.root.ref)) ?? null;
717
+ }
718
+ /** Whole-document hydration status — the comments sidebar's honest loading
719
+ * state (`loading` until `listRawAll` lands, then `complete`/`error`). */
720
+ function useCommentsHydration() {
721
+ return useSelector(AnnotationToken, (c) => c.comments.hydration());
722
+ }
723
+ /** Structural equality for the selection anchor — keeps the menu from re-rendering
724
+ * on unrelated dispatches (the capability returns a fresh object each call). */
725
+ function sameAnchor(a, b) {
726
+ if (a === b) return true;
727
+ if (!a || !b) return false;
728
+ return a.pon === b.pon && a.bounds.x === b.bounds.x && a.bounds.y === b.bounds.y && a.bounds.width === b.bounds.width && a.bounds.height === b.bounds.height && a.knob?.x === b.knob?.x && a.knob?.y === b.knob?.y;
729
+ }
730
+ function sameCreationDraftAnchor(a, b) {
731
+ if (a === b) return true;
732
+ if (!a || !b) return false;
733
+ return a.kind === b.kind && a.subtype === b.subtype && a.pon === b.pon && a.pointCount === b.pointCount && a.minPoints === b.minPoints && a.canFinish === b.canFinish && a.bounds.x === b.bounds.x && a.bounds.y === b.bounds.y && a.bounds.width === b.bounds.width && a.bounds.height === b.bounds.height;
734
+ }
735
+ //#endregion
736
+ export { AnnotationLayer, enrichCommentThreads, filePickerProvider, sameAnchor, sameCreationDraftAnchor, useAnnotation, useAnnotationDefaults, useAnnotationSelected, useAnnotationSelection, useCommentThread, useCommentThreads, useComments, useCommentsHydration, useFilePickerProvider, useSelectionFlags, useSelectionProps };
737
+
738
+ //# sourceMappingURL=annotation.js.map