@recogito/text-annotator 3.0.0-rc.4 → 3.0.0-rc.41

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 (53) hide show
  1. package/dist/src/SelectionHandler.d.ts +6 -6
  2. package/dist/src/TextAnnotator.d.ts +9 -5
  3. package/dist/src/TextAnnotatorOptions.d.ts +11 -5
  4. package/dist/src/api/scrollIntoView.d.ts +3 -3
  5. package/dist/src/highlight/Highlight.d.ts +5 -0
  6. package/dist/src/highlight/HighlightPainter.d.ts +10 -19
  7. package/dist/src/highlight/HighlightStyle.d.ts +11 -0
  8. package/dist/src/highlight/baseRenderer.d.ts +20 -0
  9. package/dist/src/highlight/canvas/canvasRenderer.d.ts +3 -0
  10. package/dist/src/highlight/canvas/index.d.ts +1 -0
  11. package/dist/src/highlight/highlights/highlightsRenderer.d.ts +5 -0
  12. package/dist/src/highlight/highlights/index.d.ts +1 -0
  13. package/dist/src/highlight/index.d.ts +5 -1
  14. package/dist/src/highlight/span/index.d.ts +1 -0
  15. package/dist/src/highlight/span/spansRenderer.d.ts +3 -0
  16. package/dist/src/highlight/viewport.d.ts +12 -0
  17. package/dist/src/index.d.ts +3 -0
  18. package/dist/src/model/{TextAnnotation.d.ts → core/TextAnnotation.d.ts} +3 -2
  19. package/dist/src/model/core/index.d.ts +1 -0
  20. package/dist/src/model/index.d.ts +2 -1
  21. package/dist/src/model/w3c/W3CTextAnnotation.d.ts +33 -0
  22. package/dist/src/model/w3c/W3CTextFormatAdapter.d.ts +12 -0
  23. package/dist/src/model/w3c/index.d.ts +2 -0
  24. package/dist/src/presence/PresencePainter.d.ts +4 -4
  25. package/dist/src/state/TextAnnotationStore.d.ts +7 -7
  26. package/dist/src/state/TextAnnotatorState.d.ts +6 -7
  27. package/dist/src/state/index.d.ts +0 -1
  28. package/dist/src/state/spatialTree.d.ts +9 -7
  29. package/dist/src/utils/cancelSingleClickEvents.d.ts +6 -0
  30. package/dist/src/utils/debounce.d.ts +1 -0
  31. package/dist/src/utils/getAnnotatableFragment.d.ts +5 -0
  32. package/dist/src/utils/getQuoteContext.d.ts +4 -0
  33. package/dist/src/utils/index.d.ts +12 -1
  34. package/dist/src/utils/isRevived.d.ts +2 -0
  35. package/dist/src/utils/isWhitespaceOrEmpty.d.ts +2 -0
  36. package/dist/src/utils/rangeToSelector.d.ts +2 -0
  37. package/dist/src/utils/reviveAnnotation.d.ts +2 -0
  38. package/dist/src/utils/reviveSelector.d.ts +11 -0
  39. package/dist/src/utils/reviveTarget.d.ts +2 -0
  40. package/dist/src/utils/splitAnnotatableRanges.d.ts +7 -0
  41. package/dist/src/utils/trimRangeToContainer.d.ts +1 -0
  42. package/dist/test/model/w3c/W3CTextFormatAdapter.test.d.ts +1 -0
  43. package/dist/test/model/w3c/fixtures.d.ts +3 -0
  44. package/dist/text-annotator.css +1 -1
  45. package/dist/text-annotator.es.js +1598 -966
  46. package/dist/text-annotator.es.js.map +1 -1
  47. package/dist/text-annotator.umd.js +2 -1
  48. package/dist/text-annotator.umd.js.map +1 -1
  49. package/package.json +19 -13
  50. package/dist/src/highlight/highlightLayer.d.ts +0 -10
  51. package/dist/src/highlight/trackViewport.d.ts +0 -3
  52. package/dist/src/state/reviveTarget.d.ts +0 -9
  53. package/dist/src/utils/trimRange.d.ts +0 -1
@@ -1,762 +1,1409 @@
1
- const ft = { fill: "rgb(0, 128, 255", fillOpacity: 0.18 }, gt = { fill: "rgb(0, 128, 255)", fillOpacity: 0.45 }, nt = {
2
- paint: (e, t, n, o, i, s) => {
3
- const a = s ? typeof s == "function" ? s(e, i) : s : i ? gt : ft;
4
- n.fillStyle = a.fill, n.globalAlpha = a.fillOpacity || 1, t.forEach(({ x: r, y: d, width: u, height: b }) => n.fillRect(r, d - 2.5, u, b + 5));
1
+ const P = {
2
+ fill: "rgb(0, 128, 255)",
3
+ fillOpacity: 0.18
4
+ }, it = {
5
+ fill: "rgb(0, 128, 255)",
6
+ fillOpacity: 0.45
7
+ }, Jt = (t, e, n, o, i) => {
8
+ var s, a;
9
+ const r = n ? typeof n == "function" ? n(t.annotation, t.state, i) || ((s = t.state) != null && s.selected ? it : P) : n : (a = t.state) != null && a.selected ? it : P;
10
+ return o && o.paint(t, e) || r;
11
+ }, Dt = "not-annotatable", X = `.${Dt}`, Zt = (t) => {
12
+ var n;
13
+ const e = t.commonAncestorContainer;
14
+ return e instanceof HTMLElement ? !e.closest(X) : !((n = e.parentElement) != null && n.closest(X));
15
+ }, te = function* (t) {
16
+ const e = document.createNodeIterator(
17
+ t.commonAncestorContainer,
18
+ NodeFilter.SHOW_ELEMENT,
19
+ (o) => o instanceof HTMLElement && o.classList.contains(Dt) && !o.parentElement.closest(X) && t.intersectsNode(o) ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP
20
+ );
21
+ let n;
22
+ for (; n = e.nextNode(); )
23
+ n instanceof HTMLElement && (yield n);
24
+ }, ee = (t) => {
25
+ if (!Zt(t)) return [];
26
+ const e = [];
27
+ let n = null;
28
+ for (const o of te(t)) {
29
+ let i;
30
+ n ? (i = document.createRange(), i.setStartAfter(n), i.setEndBefore(o)) : (i = t.cloneRange(), i.setEndBefore(o)), i.collapsed || e.push(i), n = o;
5
31
  }
6
- }, pt = (e) => {
7
- let t = /* @__PURE__ */ new Set();
8
- return (o) => {
9
- const i = o.map((s) => s.id);
10
- (t.size !== i.length || i.some((s) => !t.has(s))) && e.set(i), t = new Set(i);
11
- };
12
- };
13
- const mt = (e, t = 10) => {
32
+ if (n) {
33
+ const o = t.cloneRange();
34
+ o.setStartAfter(n), o.collapsed || e.push(o);
35
+ }
36
+ return e.length > 0 ? e : [t];
37
+ }, mt = (t) => {
38
+ const e = t.cloneContents();
39
+ return e.querySelectorAll(X).forEach((n) => n.remove()), e;
40
+ }, ne = (t) => {
41
+ t.addEventListener("click", (e) => {
42
+ // Allow clicks within not-annotatable elements
43
+ !e.target.closest(X) && !e.target.closest("a") && e.preventDefault();
44
+ });
45
+ }, vt = (t, e = 10) => {
14
46
  let n;
15
47
  return (...o) => {
16
- clearTimeout(n), n = setTimeout(() => e.apply(void 0, o), t);
48
+ clearTimeout(n), n = setTimeout(() => t.apply(void 0, o), e);
17
49
  };
18
- }, j = (e, t) => {
19
- const n = document.createElement("canvas");
20
- if (n.width = t ? 2 * window.innerWidth : window.innerWidth, n.height = t ? 2 * window.innerHeight : window.innerHeight, n.className = e, t) {
21
- const o = n.getContext("2d");
22
- o.scale(2, 2), o.translate(0.5, 0.5);
50
+ }, In = (t) => {
51
+ const e = t.cloneContents();
52
+ return e.querySelectorAll(X).forEach((n) => n.remove()), e;
53
+ }, xt = (t, e) => {
54
+ const n = document.createNodeIterator(e);
55
+ let o = 0, i = n.nextNode();
56
+ for (; i !== null; ) {
57
+ if (i === t)
58
+ return o;
59
+ o += 1, i = n.nextNode();
23
60
  }
24
- return n;
25
- }, Z = (e, t) => {
26
- if (e.width = t ? 2 * window.innerWidth : window.innerWidth, e.height = t ? 2 * window.innerHeight : window.innerHeight, t) {
27
- const n = e.getContext("2d");
28
- n.scale(2, 2), n.translate(0.5, 0.5);
61
+ }, At = (t, e) => {
62
+ const n = document.createNodeIterator(e);
63
+ let o = null;
64
+ for (let i = 0; i < t + 1; i++)
65
+ o = n.nextNode();
66
+ return o;
67
+ }, oe = (t) => {
68
+ const {
69
+ commonAncestorContainer: e,
70
+ startContainer: n,
71
+ startOffset: o,
72
+ endContainer: i,
73
+ endOffset: r
74
+ } = t, s = Array.from(e.childNodes).map((d) => {
75
+ const f = d.cloneNode(!0);
76
+ return d.nodeName === "CANVAS" ? d : f;
77
+ }), a = xt(n, e), u = xt(i, e), h = () => {
78
+ const d = e;
79
+ d.replaceChildren(...s);
80
+ const f = At(a, d), g = At(u, d);
81
+ return t.setStart(f, o), t.setEnd(g, r), t;
82
+ }, p = (d) => {
83
+ const f = document.createElement("SPAN");
84
+ return d.surroundContents(f), f;
85
+ };
86
+ if (n === i)
87
+ throw "Not implemented";
88
+ {
89
+ const d = document.createRange();
90
+ d.selectNodeContents(n), d.setStart(n, o);
91
+ const f = p(d), g = document.createRange();
92
+ g.selectNode(i), g.setEnd(i, r);
93
+ const A = p(g), m = ie(t).reverse().map((l) => {
94
+ var b;
95
+ const c = document.createElement("SPAN");
96
+ return (b = l.parentNode) == null || b.insertBefore(c, l), c.appendChild(l), c;
97
+ });
98
+ return { unwrap: h, nodes: [f, ...m, A] };
29
99
  }
30
- }, bt = (e, t, n) => {
31
- const { store: o, selection: i, hover: s } = t;
32
- let a, r, d = nt;
33
- const u = pt(n);
34
- e.classList.add("r6o-annotatable");
35
- const b = j("r6o-highlight-layer bg"), h = j("r6o-highlight-layer fg", !0), E = b.getContext("2d"), m = h.getContext("2d");
36
- e.insertBefore(b, e.firstChild), e.appendChild(h), e.addEventListener("pointermove", (y) => {
37
- const { x: T, y: L } = e.getBoundingClientRect(), f = o.getAt(y.clientX - T, y.clientY - L);
38
- f && (!r || r(f)) ? s.current !== f.id && (e.classList.add("hovered"), s.set(f.id)) : s.current && (e.classList.remove("hovered"), s.set(null));
39
- });
40
- const v = () => {
41
- const { top: y, left: T } = e.getBoundingClientRect(), { innerWidth: L, innerHeight: f } = window, x = -T, A = -y, B = L - T, S = f - y;
42
- return { top: y, left: T, minX: x, minY: A, maxX: B, maxY: S };
43
- }, C = () => c();
44
- document.addEventListener("scroll", C, { capture: !0, passive: !0 });
45
- const w = mt(() => {
46
- Z(b), Z(h, !0), o.recalculatePositions(), c();
47
- });
48
- window.addEventListener("resize", w), new ResizeObserver(w).observe(e);
49
- const c = () => requestAnimationFrame(() => {
50
- const { top: y, left: T, minX: L, minY: f, maxX: x, maxY: A } = v(), B = r ? o.getIntersectingRects(L, f, x, A).filter(({ annotation: U }) => r(U)) : o.getIntersectingRects(L, f, x, A), { width: S, height: R } = h, X = new Set(i.selected.map(({ id: U }) => U));
51
- m.clearRect(-0.5, -0.5, S + 1, R + 1), E.clearRect(-0.5, -0.5, S + 1, R + 1), B.forEach(({ annotation: U, rects: at }) => {
52
- const rt = at.map(({ x: ct, y: dt, width: ut, height: ht }) => ({
53
- x: ct + T,
54
- y: dt + y,
55
- width: ut,
56
- height: ht
57
- })), lt = X.has(U.id);
58
- d.paint(U, rt, E, m, lt, a);
59
- }), setTimeout(() => u(B.map(({ annotation: U }) => U)), 1);
60
- });
61
- return o.observe(() => c()), i.subscribe(() => c()), {
62
- redraw: c,
63
- setDrawingStyle: (y) => {
64
- a = y, c();
65
- },
66
- setFilter: (y) => {
67
- r = y, c();
68
- },
69
- setPainter: (y) => d = y
100
+ }, ie = (t) => {
101
+ const {
102
+ commonAncestorContainer: e,
103
+ startContainer: n,
104
+ endContainer: o
105
+ } = t, i = document.createNodeIterator(e, NodeFilter.SHOW_TEXT);
106
+ let r = i.nextNode(), s = !1;
107
+ const a = [];
108
+ for (; r != null; )
109
+ r === o && (s = !1), s && a.push(r), r === n && (s = !0), r = i.nextNode();
110
+ return a;
111
+ }, Vn = (t) => {
112
+ const { startContainer: e, endContainer: n } = t;
113
+ if (e === n)
114
+ return Array.from(t.getClientRects());
115
+ {
116
+ const { unwrap: o, nodes: i } = oe(t), r = i.reduce((s, a) => [...s, ...a.getClientRects()], []);
117
+ return o(), r;
118
+ }
119
+ }, re = (t, e, n = 10, o) => {
120
+ const i = o ? t.startContainer.parentElement.closest(o) : e, r = document.createRange();
121
+ r.setStart(i, 0), r.setEnd(t.startContainer, t.startOffset);
122
+ const s = mt(r).textContent, a = document.createRange();
123
+ a.setStart(t.endContainer, t.endOffset), i === document.body ? a.setEnd(i, i.childNodes.length) : a.setEndAfter(i);
124
+ const u = mt(a).textContent;
125
+ return {
126
+ prefix: s.substring(s.length - n),
127
+ suffix: u.substring(0, n)
70
128
  };
71
- }, k = (e, t) => {
72
- const { start: n, end: o } = e.selector, i = e.selector.offsetReference ? e.selector.offsetReference : t;
73
- if (!i)
129
+ }, se = /^\s*$/, ae = (t) => se.test(t.toString()), H = (t) => t.every((e) => e.range instanceof Range && !e.range.collapsed), ce = (t, e) => {
130
+ const n = (r) => Math.round(r * 10) / 10, o = {
131
+ top: n(t.top),
132
+ bottom: n(t.bottom),
133
+ left: n(t.left),
134
+ right: n(t.right)
135
+ }, i = {
136
+ top: n(e.top),
137
+ bottom: n(e.bottom),
138
+ left: n(e.left),
139
+ right: n(e.right)
140
+ };
141
+ if (Math.abs(o.top - i.top) < 0.5 && Math.abs(o.bottom - i.bottom) < 0.5) {
142
+ if (Math.abs(o.left - i.right) < 0.5 || Math.abs(o.right - i.left) < 0.5)
143
+ return "inline-adjacent";
144
+ if (o.left >= i.left && o.right <= i.right)
145
+ return "inline-is-contained";
146
+ if (o.left <= i.left && o.right >= i.right)
147
+ return "inline-contains";
148
+ } else if (o.top <= i.top && o.bottom >= i.bottom) {
149
+ if (o.left <= i.left && o.right >= i.right)
150
+ return "block-contains";
151
+ } else if (o.top >= i.top && o.bottom <= i.bottom && o.left >= i.left && o.right <= i.right)
152
+ return "block-is-contained";
153
+ }, le = (t, e) => {
154
+ const n = Math.min(t.left, e.left), o = Math.max(t.right, e.right), i = Math.min(t.top, e.top), r = Math.max(t.bottom, e.bottom);
155
+ return new DOMRect(n, i, o - n, r - i);
156
+ }, de = (t) => t.reduce((e, n) => {
157
+ if (n.width === 0 || n.height === 0)
74
158
  return e;
75
- const s = document.createNodeIterator(t, NodeFilter.SHOW_TEXT);
76
- let a = 0, r = document.createRange(), d = s.nextNode();
77
- d === null && console.error("Could not revive annotation target. Content missing.");
78
- let u = !i;
79
- for (; d !== null; ) {
80
- const b = d.textContent.length;
81
- if (!u && i && (u = i.contains(d)), u) {
82
- if (a + b > n) {
83
- r.setStart(d, n - a);
159
+ let o = [...e], i = !1;
160
+ for (const r of e) {
161
+ const s = ce(n, r);
162
+ if (s === "inline-adjacent") {
163
+ o = o.map((a) => a === r ? le(n, r) : a), i = !0;
164
+ break;
165
+ } else if (s === "inline-contains") {
166
+ o = o.map((a) => a === r ? n : a), i = !0;
167
+ break;
168
+ } else if (s === "inline-is-contained") {
169
+ i = !0;
170
+ break;
171
+ } else if (s === "block-contains" || s === "block-is-contained") {
172
+ n.width < r.width && (o = o.map((a) => a === r ? n : a)), i = !0;
173
+ break;
174
+ }
175
+ }
176
+ return i ? o : [...o, n];
177
+ }, []), ue = (t, e, n) => {
178
+ const o = document.createRange(), i = n ? t.startContainer.parentElement.closest(n) : e;
179
+ o.setStart(i, 0), o.setEnd(t.startContainer, t.startOffset);
180
+ const r = mt(o).textContent, s = t.toString(), a = r.length || 0, u = a + s.length;
181
+ return n ? { quote: s, start: a, end: u, range: t, offsetReference: i } : { quote: s, start: a, end: u, range: t };
182
+ }, Yt = (t, e) => {
183
+ var p, d;
184
+ const { start: n, end: o } = t, i = t.offsetReference || e, r = document.createNodeIterator(
185
+ e,
186
+ NodeFilter.SHOW_TEXT,
187
+ (f) => {
188
+ var g;
189
+ return (g = f.parentElement) != null && g.closest(X) ? NodeFilter.FILTER_SKIP : NodeFilter.FILTER_ACCEPT;
190
+ }
191
+ );
192
+ let s = 0;
193
+ const a = document.createRange();
194
+ let u = r.nextNode();
195
+ u === null && console.error("Could not revive annotation target. Content missing.");
196
+ let h = !i;
197
+ for (; u !== null; ) {
198
+ if (h || (h = i == null ? void 0 : i.contains(u)), h) {
199
+ const f = ((p = u.textContent) == null ? void 0 : p.length) || 0;
200
+ if (s + f > n) {
201
+ a.setStart(u, n - s);
84
202
  break;
85
203
  }
86
- a += b;
204
+ s += f;
87
205
  }
88
- d = s.nextNode();
206
+ u = r.nextNode();
89
207
  }
90
- for (; d !== null; ) {
91
- const b = d.textContent.length;
92
- if (a + b > o) {
93
- r.setEnd(d, o - a);
208
+ for (; u !== null; ) {
209
+ const f = ((d = u.textContent) == null ? void 0 : d.length) || 0;
210
+ if (s + f >= o) {
211
+ a.setEnd(u, o - s);
94
212
  break;
95
213
  }
96
- a += b, d = s.nextNode();
214
+ s += f, u = r.nextNode();
215
+ }
216
+ return {
217
+ ...t,
218
+ range: a
219
+ };
220
+ }, rt = (t, e) => H(t.selector) ? t : {
221
+ ...t,
222
+ selector: t.selector.map((n) => n.range instanceof Range && !n.range.collapsed ? n : Yt(n, e))
223
+ }, st = (t, e) => H(t.target.selector) ? t : { ...t, target: rt(t.target, e) }, he = (t, e) => {
224
+ const n = t.cloneRange();
225
+ return e.contains(n.startContainer) || n.setStart(e, 0), e.contains(n.endContainer) || n.setEnd(e, e.childNodes.length), n;
226
+ }, fe = (t) => {
227
+ const { top: e, left: n } = t.getBoundingClientRect(), { innerWidth: o, innerHeight: i } = window, r = -n, s = -e, a = o - n, u = i - e;
228
+ return { top: e, left: n, minX: r, minY: s, maxX: a, maxY: u };
229
+ }, ge = (t) => {
230
+ let e = /* @__PURE__ */ new Set();
231
+ return (o) => {
232
+ const i = o.map((r) => r.id);
233
+ (e.size !== i.length || i.some((r) => !e.has(r))) && t.set(i), e = new Set(i);
234
+ };
235
+ }, yt = (t, e, n, o) => {
236
+ const { store: i, selection: r, hover: s } = e;
237
+ let a, u, h;
238
+ const p = ge(n), d = (T) => {
239
+ const { x: O, y: v } = t.getBoundingClientRect(), x = i.getAt(T.clientX - O, T.clientY - v, u);
240
+ x ? s.current !== x.id && (t.classList.add("hovered"), s.set(x.id)) : s.current && (t.classList.remove("hovered"), s.set(null));
241
+ };
242
+ t.addEventListener("pointermove", d);
243
+ const f = (T = !1) => {
244
+ h && h.clear();
245
+ const O = fe(t), { minX: v, minY: x, maxX: w, maxY: S } = O, M = u ? i.getIntersecting(v, x, w, S).filter(({ annotation: _ }) => u(_)) : i.getIntersecting(v, x, w, S), B = r.selected.map(({ id: _ }) => _), k = M.map(({ annotation: _, rects: Gt }) => {
246
+ const Kt = B.includes(_.id), Qt = _.id === s.current;
247
+ return { annotation: _, rects: Gt, state: { selected: Kt, hover: Qt } };
248
+ });
249
+ o.redraw(k, O, a, h, T), setTimeout(() => p(M.map(({ annotation: _ }) => _)), 1);
250
+ }, g = (T) => {
251
+ h = T, f();
252
+ }, A = (T) => {
253
+ a = T, f();
254
+ }, y = (T) => {
255
+ u = T, f(!1);
256
+ }, m = () => f();
257
+ i.observe(m);
258
+ const l = r.subscribe(() => f()), c = () => f(!0);
259
+ document.addEventListener("scroll", c, { capture: !0, passive: !0 });
260
+ const b = vt(() => {
261
+ i.recalculatePositions(), h && h.reset(), f();
262
+ });
263
+ window.addEventListener("resize", b);
264
+ const C = new ResizeObserver(b);
265
+ C.observe(t);
266
+ const L = { attributes: !0, childList: !0, subtree: !0 }, E = new MutationObserver((T) => {
267
+ T.every((v) => v.target === t || t.contains(v.target)) || f(!0);
268
+ });
269
+ return E.observe(document.body, L), {
270
+ destroy: () => {
271
+ t.removeEventListener("pointermove", d), o.destroy(), i.unobserve(m), l(), document.removeEventListener("scroll", c), window.removeEventListener("resize", b), C.disconnect(), E.disconnect();
272
+ },
273
+ redraw: f,
274
+ setStyle: A,
275
+ setFilter: y,
276
+ setPainter: g,
277
+ setVisible: o.setVisible
278
+ };
279
+ }, pe = () => {
280
+ const t = document.createElement("canvas");
281
+ return t.width = window.innerWidth, t.height = window.innerHeight, t.className = "r6o-canvas-highlight-layer bg", t;
282
+ }, me = (t, e) => {
283
+ t.width = window.innerWidth, t.height = window.innerHeight;
284
+ }, be = (t) => {
285
+ t.classList.add("r6o-annotatable");
286
+ const e = pe(), n = e.getContext("2d");
287
+ document.body.appendChild(e);
288
+ const o = (a, u, h, p) => requestAnimationFrame(() => {
289
+ const { width: d, height: f } = e;
290
+ n.clearRect(-0.5, -0.5, d + 1, f + 1), p && p.clear();
291
+ const { top: g, left: A } = u;
292
+ [...a].sort((m, l) => {
293
+ const { annotation: { target: { created: c } } } = m, { annotation: { target: { created: b } } } = l;
294
+ return c.getTime() - b.getTime();
295
+ }).forEach((m) => {
296
+ var C;
297
+ const l = h ? typeof h == "function" ? h(m.annotation, m.state) : h : (C = m.state) != null && C.selected ? it : P, c = p && p.paint(m, u) || l, b = m.rects.map(({ x: L, y: E, width: R, height: T }) => ({
298
+ x: L + A,
299
+ y: E + g,
300
+ width: R,
301
+ height: T
302
+ }));
303
+ if (n.fillStyle = c.fill, n.globalAlpha = c.fillOpacity || 1, b.forEach(
304
+ ({ x: L, y: E, width: R, height: T }) => n.fillRect(L, E, R, T)
305
+ ), c.underlineColor) {
306
+ n.globalAlpha = 1, n.strokeStyle = c.underlineColor, n.lineWidth = c.underlineThickness ?? 1;
307
+ const L = c.underlineOffset ?? 0;
308
+ b.forEach(({ x: E, y: R, width: T, height: O }) => {
309
+ n.beginPath(), n.moveTo(E, R + O + L), n.lineTo(E + T, R + O + L), n.stroke();
310
+ });
311
+ }
312
+ });
313
+ }), i = vt(() => {
314
+ me(e);
315
+ });
316
+ return window.addEventListener("resize", i), {
317
+ destroy: () => {
318
+ e.remove(), window.removeEventListener("resize", i);
319
+ },
320
+ setVisible: (a) => {
321
+ console.log("setVisible not implemented on Canvas renderer");
322
+ },
323
+ redraw: o
324
+ };
325
+ }, ve = (t, e, n) => yt(t, e, n, be(t));
326
+ var ye = { grad: 0.9, turn: 360, rad: 360 / (2 * Math.PI) }, $ = function(t) {
327
+ return typeof t == "string" ? t.length > 0 : typeof t == "number";
328
+ }, I = function(t, e, n) {
329
+ return e === void 0 && (e = 0), n === void 0 && (n = Math.pow(10, e)), Math.round(n * t) / n + 0;
330
+ }, D = function(t, e, n) {
331
+ return e === void 0 && (e = 0), n === void 0 && (n = 1), t > n ? n : t > e ? t : e;
332
+ }, Xt = function(t) {
333
+ return (t = isFinite(t) ? t % 360 : 0) > 0 ? t : t + 360;
334
+ }, Et = function(t) {
335
+ return { r: D(t.r, 0, 255), g: D(t.g, 0, 255), b: D(t.b, 0, 255), a: D(t.a) };
336
+ }, at = function(t) {
337
+ return { r: I(t.r), g: I(t.g), b: I(t.b), a: I(t.a, 3) };
338
+ }, we = /^#([0-9a-f]{3,8})$/i, J = function(t) {
339
+ var e = t.toString(16);
340
+ return e.length < 2 ? "0" + e : e;
341
+ }, $t = function(t) {
342
+ var e = t.r, n = t.g, o = t.b, i = t.a, r = Math.max(e, n, o), s = r - Math.min(e, n, o), a = s ? r === e ? (n - o) / s : r === n ? 2 + (o - e) / s : 4 + (e - n) / s : 0;
343
+ return { h: 60 * (a < 0 ? a + 6 : a), s: r ? s / r * 100 : 0, v: r / 255 * 100, a: i };
344
+ }, Ht = function(t) {
345
+ var e = t.h, n = t.s, o = t.v, i = t.a;
346
+ e = e / 360 * 6, n /= 100, o /= 100;
347
+ var r = Math.floor(e), s = o * (1 - n), a = o * (1 - (e - r) * n), u = o * (1 - (1 - e + r) * n), h = r % 6;
348
+ return { r: 255 * [o, a, s, s, u, o][h], g: 255 * [u, o, o, a, s, s][h], b: 255 * [s, s, u, o, o, a][h], a: i };
349
+ }, St = function(t) {
350
+ return { h: Xt(t.h), s: D(t.s, 0, 100), l: D(t.l, 0, 100), a: D(t.a) };
351
+ }, Ct = function(t) {
352
+ return { h: I(t.h), s: I(t.s), l: I(t.l), a: I(t.a, 3) };
353
+ }, Lt = function(t) {
354
+ return Ht((n = (e = t).s, { h: e.h, s: (n *= ((o = e.l) < 50 ? o : 100 - o) / 100) > 0 ? 2 * n / (o + n) * 100 : 0, v: o + n, a: e.a }));
355
+ var e, n, o;
356
+ }, Q = function(t) {
357
+ return { h: (e = $t(t)).h, s: (i = (200 - (n = e.s)) * (o = e.v) / 100) > 0 && i < 200 ? n * o / 100 / (i <= 100 ? i : 200 - i) * 100 : 0, l: i / 2, a: e.a };
358
+ var e, n, o, i;
359
+ }, xe = /^hsla?\(\s*([+-]?\d*\.?\d+)(deg|rad|grad|turn)?\s*,\s*([+-]?\d*\.?\d+)%\s*,\s*([+-]?\d*\.?\d+)%\s*(?:,\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i, Ae = /^hsla?\(\s*([+-]?\d*\.?\d+)(deg|rad|grad|turn)?\s+([+-]?\d*\.?\d+)%\s+([+-]?\d*\.?\d+)%\s*(?:\/\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i, Ee = /^rgba?\(\s*([+-]?\d*\.?\d+)(%)?\s*,\s*([+-]?\d*\.?\d+)(%)?\s*,\s*([+-]?\d*\.?\d+)(%)?\s*(?:,\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i, Se = /^rgba?\(\s*([+-]?\d*\.?\d+)(%)?\s+([+-]?\d*\.?\d+)(%)?\s+([+-]?\d*\.?\d+)(%)?\s*(?:\/\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i, Rt = { string: [[function(t) {
360
+ var e = we.exec(t);
361
+ return e ? (t = e[1]).length <= 4 ? { r: parseInt(t[0] + t[0], 16), g: parseInt(t[1] + t[1], 16), b: parseInt(t[2] + t[2], 16), a: t.length === 4 ? I(parseInt(t[3] + t[3], 16) / 255, 2) : 1 } : t.length === 6 || t.length === 8 ? { r: parseInt(t.substr(0, 2), 16), g: parseInt(t.substr(2, 2), 16), b: parseInt(t.substr(4, 2), 16), a: t.length === 8 ? I(parseInt(t.substr(6, 2), 16) / 255, 2) : 1 } : null : null;
362
+ }, "hex"], [function(t) {
363
+ var e = Ee.exec(t) || Se.exec(t);
364
+ return e ? e[2] !== e[4] || e[4] !== e[6] ? null : Et({ r: Number(e[1]) / (e[2] ? 100 / 255 : 1), g: Number(e[3]) / (e[4] ? 100 / 255 : 1), b: Number(e[5]) / (e[6] ? 100 / 255 : 1), a: e[7] === void 0 ? 1 : Number(e[7]) / (e[8] ? 100 : 1) }) : null;
365
+ }, "rgb"], [function(t) {
366
+ var e = xe.exec(t) || Ae.exec(t);
367
+ if (!e) return null;
368
+ var n, o, i = St({ h: (n = e[1], o = e[2], o === void 0 && (o = "deg"), Number(n) * (ye[o] || 1)), s: Number(e[3]), l: Number(e[4]), a: e[5] === void 0 ? 1 : Number(e[5]) / (e[6] ? 100 : 1) });
369
+ return Lt(i);
370
+ }, "hsl"]], object: [[function(t) {
371
+ var e = t.r, n = t.g, o = t.b, i = t.a, r = i === void 0 ? 1 : i;
372
+ return $(e) && $(n) && $(o) ? Et({ r: Number(e), g: Number(n), b: Number(o), a: Number(r) }) : null;
373
+ }, "rgb"], [function(t) {
374
+ var e = t.h, n = t.s, o = t.l, i = t.a, r = i === void 0 ? 1 : i;
375
+ if (!$(e) || !$(n) || !$(o)) return null;
376
+ var s = St({ h: Number(e), s: Number(n), l: Number(o), a: Number(r) });
377
+ return Lt(s);
378
+ }, "hsl"], [function(t) {
379
+ var e = t.h, n = t.s, o = t.v, i = t.a, r = i === void 0 ? 1 : i;
380
+ if (!$(e) || !$(n) || !$(o)) return null;
381
+ var s = function(a) {
382
+ return { h: Xt(a.h), s: D(a.s, 0, 100), v: D(a.v, 0, 100), a: D(a.a) };
383
+ }({ h: Number(e), s: Number(n), v: Number(o), a: Number(r) });
384
+ return Ht(s);
385
+ }, "hsv"]] }, Tt = function(t, e) {
386
+ for (var n = 0; n < e.length; n++) {
387
+ var o = e[n][0](t);
388
+ if (o) return [o, e[n][1]];
389
+ }
390
+ return [null, void 0];
391
+ }, Ce = function(t) {
392
+ return typeof t == "string" ? Tt(t.trim(), Rt.string) : typeof t == "object" && t !== null ? Tt(t, Rt.object) : [null, void 0];
393
+ }, ct = function(t, e) {
394
+ var n = Q(t);
395
+ return { h: n.h, s: D(n.s + 100 * e, 0, 100), l: n.l, a: n.a };
396
+ }, lt = function(t) {
397
+ return (299 * t.r + 587 * t.g + 114 * t.b) / 1e3 / 255;
398
+ }, Bt = function(t, e) {
399
+ var n = Q(t);
400
+ return { h: n.h, s: n.s, l: D(n.l + 100 * e, 0, 100), a: n.a };
401
+ }, Nt = function() {
402
+ function t(e) {
403
+ this.parsed = Ce(e)[0], this.rgba = this.parsed || { r: 0, g: 0, b: 0, a: 1 };
404
+ }
405
+ return t.prototype.isValid = function() {
406
+ return this.parsed !== null;
407
+ }, t.prototype.brightness = function() {
408
+ return I(lt(this.rgba), 2);
409
+ }, t.prototype.isDark = function() {
410
+ return lt(this.rgba) < 0.5;
411
+ }, t.prototype.isLight = function() {
412
+ return lt(this.rgba) >= 0.5;
413
+ }, t.prototype.toHex = function() {
414
+ return e = at(this.rgba), n = e.r, o = e.g, i = e.b, s = (r = e.a) < 1 ? J(I(255 * r)) : "", "#" + J(n) + J(o) + J(i) + s;
415
+ var e, n, o, i, r, s;
416
+ }, t.prototype.toRgb = function() {
417
+ return at(this.rgba);
418
+ }, t.prototype.toRgbString = function() {
419
+ return e = at(this.rgba), n = e.r, o = e.g, i = e.b, (r = e.a) < 1 ? "rgba(" + n + ", " + o + ", " + i + ", " + r + ")" : "rgb(" + n + ", " + o + ", " + i + ")";
420
+ var e, n, o, i, r;
421
+ }, t.prototype.toHsl = function() {
422
+ return Ct(Q(this.rgba));
423
+ }, t.prototype.toHslString = function() {
424
+ return e = Ct(Q(this.rgba)), n = e.h, o = e.s, i = e.l, (r = e.a) < 1 ? "hsla(" + n + ", " + o + "%, " + i + "%, " + r + ")" : "hsl(" + n + ", " + o + "%, " + i + "%)";
425
+ var e, n, o, i, r;
426
+ }, t.prototype.toHsv = function() {
427
+ return e = $t(this.rgba), { h: I(e.h), s: I(e.s), v: I(e.v), a: I(e.a, 3) };
428
+ var e;
429
+ }, t.prototype.invert = function() {
430
+ return Y({ r: 255 - (e = this.rgba).r, g: 255 - e.g, b: 255 - e.b, a: e.a });
431
+ var e;
432
+ }, t.prototype.saturate = function(e) {
433
+ return e === void 0 && (e = 0.1), Y(ct(this.rgba, e));
434
+ }, t.prototype.desaturate = function(e) {
435
+ return e === void 0 && (e = 0.1), Y(ct(this.rgba, -e));
436
+ }, t.prototype.grayscale = function() {
437
+ return Y(ct(this.rgba, -1));
438
+ }, t.prototype.lighten = function(e) {
439
+ return e === void 0 && (e = 0.1), Y(Bt(this.rgba, e));
440
+ }, t.prototype.darken = function(e) {
441
+ return e === void 0 && (e = 0.1), Y(Bt(this.rgba, -e));
442
+ }, t.prototype.rotate = function(e) {
443
+ return e === void 0 && (e = 15), this.hue(this.hue() + e);
444
+ }, t.prototype.alpha = function(e) {
445
+ return typeof e == "number" ? Y({ r: (n = this.rgba).r, g: n.g, b: n.b, a: e }) : I(this.rgba.a, 3);
446
+ var n;
447
+ }, t.prototype.hue = function(e) {
448
+ var n = Q(this.rgba);
449
+ return typeof e == "number" ? Y({ h: e, s: n.s, l: n.l, a: n.a }) : I(n.h);
450
+ }, t.prototype.isEqual = function(e) {
451
+ return this.toHex() === Y(e).toHex();
452
+ }, t;
453
+ }(), Y = function(t) {
454
+ return t instanceof Nt ? t : new Nt(t);
455
+ };
456
+ const Le = (t) => [
457
+ `background-color:${Y((t == null ? void 0 : t.fill) || P.fill).alpha((t == null ? void 0 : t.fillOpacity) === void 0 ? P.fillOpacity : t.fillOpacity).toHex()}`,
458
+ t != null && t.underlineThickness ? "text-decoration:underline" : void 0,
459
+ t != null && t.underlineColor ? `text-decoration-color:${t.underlineColor}` : void 0,
460
+ t != null && t.underlineOffset ? `text-underline-offset:${t.underlineOffset}px` : void 0,
461
+ t != null && t.underlineThickness ? `text-decoration-thickness:${t.underlineThickness}px` : void 0
462
+ ].filter(Boolean).join(";"), Re = () => {
463
+ const t = document.createElement("style");
464
+ document.getElementsByTagName("head")[0].appendChild(t);
465
+ let e = /* @__PURE__ */ new Set();
466
+ return {
467
+ destroy: () => {
468
+ CSS.highlights.clear(), t.remove();
469
+ },
470
+ setVisible: (r) => {
471
+ console.log("setVisible not implemented on CSS Custom Highlights renderer");
472
+ },
473
+ redraw: (r, s, a, u) => {
474
+ u && u.clear();
475
+ const h = new Set(r.map((d) => d.annotation.id));
476
+ Array.from(e).filter((d) => !h.has(d));
477
+ const p = r.map((d) => {
478
+ var A;
479
+ const f = a ? typeof a == "function" ? a(d.annotation, d.state) : a : (A = d.state) != null && A.selected ? it : P, g = u && u.paint(d, s) || f;
480
+ return `::highlight(_${d.annotation.id}) { ${Le(g)} }`;
481
+ });
482
+ t.innerHTML = p.join(`
483
+ `), CSS.highlights.clear(), r.forEach(({ annotation: d }) => {
484
+ const f = d.target.selector.map((A) => A.range), g = new Highlight(...f);
485
+ CSS.highlights.set(`_${d.id}`, g);
486
+ }), e = h;
487
+ }
488
+ };
489
+ }, Te = (t, e, n) => yt(t, e, n, Re());
490
+ var Mt = Object.prototype.hasOwnProperty;
491
+ function bt(t, e) {
492
+ var n, o;
493
+ if (t === e) return !0;
494
+ if (t && e && (n = t.constructor) === e.constructor) {
495
+ if (n === Date) return t.getTime() === e.getTime();
496
+ if (n === RegExp) return t.toString() === e.toString();
497
+ if (n === Array) {
498
+ if ((o = t.length) === e.length)
499
+ for (; o-- && bt(t[o], e[o]); ) ;
500
+ return o === -1;
501
+ }
502
+ if (!n || typeof t == "object") {
503
+ o = 0;
504
+ for (n in t)
505
+ if (Mt.call(t, n) && ++o && !Mt.call(e, n) || !(n in e) || !bt(t[n], e[n])) return !1;
506
+ return Object.keys(e).length === o;
507
+ }
97
508
  }
509
+ return t !== t && e !== e;
510
+ }
511
+ const Be = (t, e) => {
512
+ const n = (r, s) => r.x <= s.x + s.width && r.x + r.width >= s.x && r.y <= s.y + s.height && r.y + r.height >= s.y, o = (r) => r.rects.reduce((s, a) => s + a.width, 0), i = e.filter(({ rects: r }) => r.some((s) => n(t, s)));
513
+ return i.sort((r, s) => o(s) - o(r)), i.findIndex((r) => r.rects.includes(t));
514
+ }, Ne = (t) => {
515
+ t.classList.add("r6o-annotatable");
516
+ const e = document.createElement("div");
517
+ e.className = "r6o-span-highlight-layer", t.insertBefore(e, t.firstChild);
518
+ let n = [];
98
519
  return {
99
- ...e,
100
- selector: {
101
- ...e.selector,
102
- range: r
520
+ destroy: () => {
521
+ e.remove();
522
+ },
523
+ redraw: (s, a, u, h, p) => {
524
+ const f = !(bt(n, s) && p);
525
+ !h && !f || (f && (e.innerHTML = ""), s.forEach((g) => {
526
+ g.rects.map((A) => {
527
+ const y = Be(A, s), m = Jt(g, a, u, h, y);
528
+ if (f) {
529
+ const l = document.createElement("span");
530
+ l.className = "r6o-annotation", l.dataset.annotation = g.annotation.id, l.style.left = `${A.x}px`, l.style.top = `${A.y}px`, l.style.width = `${A.width}px`, l.style.height = `${A.height}px`, l.style.backgroundColor = Y((m == null ? void 0 : m.fill) || P.fill).alpha((m == null ? void 0 : m.fillOpacity) === void 0 ? P.fillOpacity : m.fillOpacity).toHex(), m.underlineStyle && (l.style.borderStyle = m.underlineStyle), m.underlineColor && (l.style.borderColor = m.underlineColor), m.underlineThickness && (l.style.borderBottomWidth = `${m.underlineThickness}px`), m.underlineOffset && (l.style.paddingBottom = `${m.underlineOffset}px`), e.appendChild(l);
531
+ }
532
+ });
533
+ }), n = s);
534
+ },
535
+ setVisible: (s) => {
536
+ s ? e.classList.remove("hidden") : e.classList.add("hidden");
103
537
  }
104
538
  };
539
+ }, Me = (t, e, n) => yt(t, e, n, Ne(t));
540
+ var V = [];
541
+ for (var dt = 0; dt < 256; ++dt)
542
+ V.push((dt + 256).toString(16).slice(1));
543
+ function Oe(t, e = 0) {
544
+ return (V[t[e + 0]] + V[t[e + 1]] + V[t[e + 2]] + V[t[e + 3]] + "-" + V[t[e + 4]] + V[t[e + 5]] + "-" + V[t[e + 6]] + V[t[e + 7]] + "-" + V[t[e + 8]] + V[t[e + 9]] + "-" + V[t[e + 10]] + V[t[e + 11]] + V[t[e + 12]] + V[t[e + 13]] + V[t[e + 14]] + V[t[e + 15]]).toLowerCase();
545
+ }
546
+ var Z, Ie = new Uint8Array(16);
547
+ function Ve() {
548
+ if (!Z && (Z = typeof crypto < "u" && crypto.getRandomValues && crypto.getRandomValues.bind(crypto), !Z))
549
+ throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");
550
+ return Z(Ie);
551
+ }
552
+ var Ue = typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto);
553
+ const Ot = {
554
+ randomUUID: Ue
105
555
  };
106
- var Q = Object.prototype.hasOwnProperty;
107
- function Y(e, t) {
556
+ function Pt(t, e, n) {
557
+ if (Ot.randomUUID && !e && !t)
558
+ return Ot.randomUUID();
559
+ t = t || {};
560
+ var o = t.random || (t.rng || Ve)();
561
+ return o[6] = o[6] & 15 | 64, o[8] = o[8] & 63 | 128, Oe(o);
562
+ }
563
+ var It = Object.prototype.hasOwnProperty;
564
+ function z(t, e) {
108
565
  var n, o;
109
- if (e === t)
110
- return !0;
111
- if (e && t && (n = e.constructor) === t.constructor) {
112
- if (n === Date)
113
- return e.getTime() === t.getTime();
114
- if (n === RegExp)
115
- return e.toString() === t.toString();
566
+ if (t === e) return !0;
567
+ if (t && e && (n = t.constructor) === e.constructor) {
568
+ if (n === Date) return t.getTime() === e.getTime();
569
+ if (n === RegExp) return t.toString() === e.toString();
116
570
  if (n === Array) {
117
- if ((o = e.length) === t.length)
118
- for (; o-- && Y(e[o], t[o]); )
119
- ;
571
+ if ((o = t.length) === e.length)
572
+ for (; o-- && z(t[o], e[o]); ) ;
120
573
  return o === -1;
121
574
  }
122
- if (!n || typeof e == "object") {
575
+ if (!n || typeof t == "object") {
123
576
  o = 0;
124
- for (n in e)
125
- if (Q.call(e, n) && ++o && !Q.call(t, n) || !(n in t) || !Y(e[n], t[n]))
126
- return !1;
127
- return Object.keys(t).length === o;
577
+ for (n in t)
578
+ if (It.call(t, n) && ++o && !It.call(e, n) || !(n in e) || !z(t[n], e[n])) return !1;
579
+ return Object.keys(e).length === o;
128
580
  }
129
581
  }
130
- return e !== e && t !== t;
582
+ return t !== t && e !== e;
131
583
  }
132
- function W() {
584
+ function ut() {
133
585
  }
134
- function At(e, t) {
135
- return e != e ? t == t : e !== t || e && typeof e == "object" || typeof e == "function";
586
+ function ke(t, e) {
587
+ return t != t ? e == e : t !== e || t && typeof t == "object" || typeof t == "function";
136
588
  }
137
- const D = [];
138
- function J(e, t = W) {
589
+ const F = [];
590
+ function wt(t, e = ut) {
139
591
  let n;
140
592
  const o = /* @__PURE__ */ new Set();
141
- function i(r) {
142
- if (At(e, r) && (e = r, n)) {
143
- const d = !D.length;
144
- for (const u of o)
145
- u[1](), D.push(u, e);
146
- if (d) {
147
- for (let u = 0; u < D.length; u += 2)
148
- D[u][0](D[u + 1]);
149
- D.length = 0;
593
+ function i(a) {
594
+ if (ke(t, a) && (t = a, n)) {
595
+ const u = !F.length;
596
+ for (const h of o)
597
+ h[1](), F.push(h, t);
598
+ if (u) {
599
+ for (let h = 0; h < F.length; h += 2)
600
+ F[h][0](F[h + 1]);
601
+ F.length = 0;
150
602
  }
151
603
  }
152
604
  }
153
- function s(r) {
154
- i(r(e));
605
+ function r(a) {
606
+ i(a(t));
155
607
  }
156
- function a(r, d = W) {
157
- const u = [r, d];
158
- return o.add(u), o.size === 1 && (n = t(i) || W), r(e), () => {
159
- o.delete(u), o.size === 0 && n && (n(), n = null);
608
+ function s(a, u = ut) {
609
+ const h = [a, u];
610
+ return o.add(h), o.size === 1 && (n = e(i, r) || ut), a(t), () => {
611
+ o.delete(h), o.size === 0 && n && (n(), n = null);
160
612
  };
161
613
  }
162
- return { set: i, update: s, subscribe: a };
614
+ return { set: i, update: r, subscribe: s };
163
615
  }
164
- const xt = (e) => {
165
- const { subscribe: t, set: n } = J(null);
166
- let o = null;
167
- return t((i) => o = i), e.observe(({ changes: i }) => {
616
+ const _e = (t) => {
617
+ const { subscribe: e, set: n } = wt();
618
+ let o;
619
+ return e((i) => o = i), t.observe(({ changes: i }) => {
168
620
  if (o) {
169
- i.deleted.some((a) => a.id === o) && n(null);
170
- const s = i.updated.find(({ oldValue: a }) => a.id === o);
171
- s && n(s.newValue.id);
621
+ (i.deleted || []).some((s) => s.id === o) && n(void 0);
622
+ const r = (i.updated || []).find(({ oldValue: s }) => s.id === o);
623
+ r && n(r.newValue.id);
172
624
  }
173
625
  }), {
174
626
  get current() {
175
627
  return o;
176
628
  },
177
- subscribe: t,
629
+ subscribe: e,
178
630
  set: n
179
631
  };
180
- }, q = { selected: [] }, vt = (e, t = "EDIT") => {
181
- const { subscribe: n, set: o } = J(q);
182
- let i = q;
183
- n((h) => i = h);
184
- const s = () => o(q), a = () => {
185
- var h;
186
- return ((h = i.selected) == null ? void 0 : h.length) === 0;
187
- }, r = (h) => {
188
- if (i.selected.length === 0)
632
+ };
633
+ var De = /* @__PURE__ */ ((t) => (t.EDIT = "EDIT", t.SELECT = "SELECT", t.NONE = "NONE", t))(De || {});
634
+ const tt = { selected: [] }, Ye = (t) => {
635
+ const { subscribe: e, set: n } = wt(tt);
636
+ let o, i = tt;
637
+ e((f) => i = f);
638
+ const r = () => {
639
+ z(i, tt) || n(tt);
640
+ }, s = () => {
641
+ var f;
642
+ return ((f = i.selected) == null ? void 0 : f.length) === 0;
643
+ }, a = (f) => {
644
+ if (s())
189
645
  return !1;
190
- const E = typeof h == "string" ? h : h.id;
191
- return i.selected.some((m) => m.id === E);
192
- }, d = (h, E) => {
193
- const m = e.getAnnotation(h);
194
- if (m) {
195
- const v = wt(m, t);
196
- o(v === "EDIT" ? { selected: [{ id: h, editable: !0 }], pointerEvent: E } : v === "SELECT" ? { selected: [{ id: h }], pointerEvent: E } : { selected: [], pointerEvent: E });
197
- } else
198
- console.warn("Invalid selection: " + h);
199
- }, u = (h, E = !0) => {
200
- const m = Array.isArray(h) ? h : [h], v = m.map((C) => e.getAnnotation(C)).filter((C) => C);
201
- o({ selected: v.map(({ id: C }) => ({ id: C, editable: E })) }), v.length !== m.length && console.warn("Invalid selection", h);
202
- }, b = (h) => {
203
- if (i.selected.length === 0)
646
+ const g = typeof f == "string" ? f : f.id;
647
+ return i.selected.some((A) => A.id === g);
648
+ }, u = (f, g) => {
649
+ const A = t.getAnnotation(f);
650
+ if (!A) {
651
+ console.warn("Invalid selection: " + f);
652
+ return;
653
+ }
654
+ switch (Vt(A, o)) {
655
+ case "EDIT":
656
+ n({ selected: [{ id: f, editable: !0 }], event: g });
657
+ break;
658
+ case "SELECT":
659
+ n({ selected: [{ id: f }], event: g });
660
+ break;
661
+ default:
662
+ n({ selected: [], event: g });
663
+ }
664
+ }, h = (f, g) => {
665
+ const A = Array.isArray(f) ? f : [f], y = A.map((m) => t.getAnnotation(m)).filter((m) => !!m);
666
+ n({
667
+ selected: y.map((m) => {
668
+ const l = g === void 0 ? Vt(m, o) === "EDIT" : g;
669
+ return { id: m.id, editable: l };
670
+ })
671
+ }), y.length !== A.length && console.warn("Invalid selection", f);
672
+ }, p = (f) => {
673
+ if (s())
204
674
  return !1;
205
- const { selected: E } = i;
206
- E.filter(({ id: m }) => h.includes(m)).length > 0 && o({ selected: E.filter(({ id: m }) => !h.includes(m)) });
207
- };
208
- return e.observe(({ changes: h }) => b(h.deleted.map((E) => E.id))), {
209
- clear: s,
210
- clickSelect: d,
675
+ const { selected: g } = i;
676
+ g.some(({ id: A }) => f.includes(A)) && n({ selected: g.filter(({ id: A }) => !f.includes(A)) });
677
+ }, d = (f) => o = f;
678
+ return t.observe(
679
+ ({ changes: f }) => p((f.deleted || []).map((g) => g.id))
680
+ ), {
681
+ get event() {
682
+ return i ? i.event : null;
683
+ },
211
684
  get selected() {
212
685
  return i ? [...i.selected] : null;
213
686
  },
214
- get pointerEvent() {
215
- return i ? i.pointerEvent : null;
687
+ get userSelectAction() {
688
+ return o;
216
689
  },
217
- isEmpty: a,
218
- isSelected: r,
219
- setSelected: u,
220
- subscribe: n
690
+ clear: r,
691
+ isEmpty: s,
692
+ isSelected: a,
693
+ setSelected: h,
694
+ setUserSelectAction: d,
695
+ subscribe: e,
696
+ userSelect: u
221
697
  };
222
- }, wt = (e, t) => typeof t == "function" ? t(e) || "EDIT" : t || "EDIT", yt = [];
223
- for (let e = 0; e < 256; ++e)
224
- yt.push((e + 256).toString(16).slice(1));
225
- typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto);
226
- const Et = (e, t) => {
227
- const n = new Set(e.bodies.map((o) => o.id));
228
- return t.bodies.filter((o) => !n.has(o.id));
229
- }, Tt = (e, t) => {
698
+ }, Vt = (t, e) => typeof e == "function" ? e(t) : e || "EDIT";
699
+ var U = [];
700
+ for (var ht = 0; ht < 256; ++ht)
701
+ U.push((ht + 256).toString(16).slice(1));
702
+ function Xe(t, e = 0) {
703
+ return (U[t[e + 0]] + U[t[e + 1]] + U[t[e + 2]] + U[t[e + 3]] + "-" + U[t[e + 4]] + U[t[e + 5]] + "-" + U[t[e + 6]] + U[t[e + 7]] + "-" + U[t[e + 8]] + U[t[e + 9]] + "-" + U[t[e + 10]] + U[t[e + 11]] + U[t[e + 12]] + U[t[e + 13]] + U[t[e + 14]] + U[t[e + 15]]).toLowerCase();
704
+ }
705
+ var et, $e = new Uint8Array(16);
706
+ function He() {
707
+ if (!et && (et = typeof crypto < "u" && crypto.getRandomValues && crypto.getRandomValues.bind(crypto), !et))
708
+ throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");
709
+ return et($e);
710
+ }
711
+ var Pe = typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto);
712
+ const Ut = {
713
+ randomUUID: Pe
714
+ };
715
+ function zt(t, e, n) {
716
+ if (Ut.randomUUID && !e && !t)
717
+ return Ut.randomUUID();
718
+ t = t || {};
719
+ var o = t.random || (t.rng || He)();
720
+ return o[6] = o[6] & 15 | 64, o[8] = o[8] & 63 | 128, Xe(o);
721
+ }
722
+ const Un = (t, e, n, o) => ({
723
+ id: zt(),
724
+ annotation: typeof t == "string" ? t : t.id,
725
+ created: n || /* @__PURE__ */ new Date(),
726
+ creator: o,
727
+ ...e
728
+ }), ze = (t, e) => {
230
729
  const n = new Set(t.bodies.map((o) => o.id));
231
730
  return e.bodies.filter((o) => !n.has(o.id));
232
- }, Bt = (e, t) => t.bodies.map((n) => {
233
- const o = e.bodies.find((i) => i.id === n.id);
234
- return { newBody: n, oldBody: o && !Y(o, n) ? o : void 0 };
235
- }).filter(({ oldBody: n }) => n), St = (e, t) => !Y(e.target, t.target), ot = (e, t) => {
236
- const n = Et(e, t), o = Tt(e, t), i = Bt(e, t);
731
+ }, Fe = (t, e) => {
732
+ const n = new Set(e.bodies.map((o) => o.id));
733
+ return t.bodies.filter((o) => !n.has(o.id));
734
+ }, je = (t, e) => e.bodies.map((n) => {
735
+ const o = t.bodies.find((i) => i.id === n.id);
736
+ return { newBody: n, oldBody: o && !z(o, n) ? o : void 0 };
737
+ }).filter(({ oldBody: n }) => n).map(({ oldBody: n, newBody: o }) => ({ oldBody: n, newBody: o })), We = (t, e) => !z(t.target, e.target), Ft = (t, e) => {
738
+ const n = ze(t, e), o = Fe(t, e), i = je(t, e);
237
739
  return {
238
- oldValue: e,
239
- newValue: t,
740
+ oldValue: t,
741
+ newValue: e,
240
742
  bodiesCreated: n.length > 0 ? n : void 0,
241
743
  bodiesDeleted: o.length > 0 ? o : void 0,
242
744
  bodiesUpdated: i.length > 0 ? i : void 0,
243
- targetUpdated: St(e, t) ? { oldTarget: e.target, newTarget: t.target } : void 0
745
+ targetUpdated: We(t, e) ? { oldTarget: t.target, newTarget: e.target } : void 0
244
746
  };
245
747
  };
246
- var M = /* @__PURE__ */ ((e) => (e.LOCAL = "LOCAL", e.REMOTE = "REMOTE", e))(M || {});
247
- const Ct = (e, t) => {
748
+ var N = /* @__PURE__ */ ((t) => (t.LOCAL = "LOCAL", t.REMOTE = "REMOTE", t))(N || {});
749
+ const qe = (t, e) => {
248
750
  var n, o;
249
- const { changes: i, origin: s } = t;
250
- if (!(!e.options.origin || e.options.origin === s))
751
+ const { changes: i, origin: r } = e;
752
+ if (!(!t.options.origin || t.options.origin === r))
251
753
  return !1;
252
- if (e.options.ignore) {
253
- const { ignore: a } = e.options, r = (d) => (d == null ? void 0 : d.length) > 0;
254
- if (!(r(i.created) || r(i.deleted))) {
255
- const d = (n = i.updated) == null ? void 0 : n.some((b) => r(b.bodiesCreated) || r(b.bodiesDeleted) || r(b.bodiesUpdated)), u = (o = i.updated) == null ? void 0 : o.some((b) => b.targetUpdated);
256
- if (a === "BODY_ONLY" && d && !u || a === "TARGET_ONLY" && u && !d)
754
+ if (t.options.ignore) {
755
+ const { ignore: s } = t.options, a = (u) => u && u.length > 0;
756
+ if (!(a(i.created) || a(i.deleted))) {
757
+ const u = (n = i.updated) == null ? void 0 : n.some((p) => a(p.bodiesCreated) || a(p.bodiesDeleted) || a(p.bodiesUpdated)), h = (o = i.updated) == null ? void 0 : o.some((p) => p.targetUpdated);
758
+ if (s === "BODY_ONLY" && u && !h || s === "TARGET_ONLY" && h && !u)
257
759
  return !1;
258
760
  }
259
761
  }
260
- if (e.options.annotations) {
261
- const a = /* @__PURE__ */ new Set([
262
- ...i.created.map((r) => r.id),
263
- ...i.deleted.map((r) => r.id),
264
- ...i.updated.map(({ oldValue: r }) => r.id)
762
+ if (t.options.annotations) {
763
+ const s = /* @__PURE__ */ new Set([
764
+ ...(i.created || []).map((a) => a.id),
765
+ ...(i.deleted || []).map((a) => a.id),
766
+ ...(i.updated || []).map(({ oldValue: a }) => a.id)
265
767
  ]);
266
- return !!(Array.isArray(e.options.annotations) ? e.options.annotations : [e.options.annotations]).find((r) => a.has(r));
768
+ return !!(Array.isArray(t.options.annotations) ? t.options.annotations : [t.options.annotations]).find((a) => s.has(a));
267
769
  } else
268
770
  return !0;
269
- }, Lt = (e, t) => {
270
- const n = new Set((e.created || []).map((h) => h.id)), o = new Set((e.updated || []).map(({ newValue: h }) => h.id)), i = new Set((t.created || []).map((h) => h.id)), s = new Set((t.deleted || []).map((h) => h.id)), a = new Set((t.updated || []).map(({ oldValue: h }) => h.id)), r = new Set((t.updated || []).filter(({ oldValue: h }) => n.has(h.id) || o.has(h.id)).map(({ oldValue: h }) => h.id)), d = [
271
- ...(e.created || []).filter((h) => !s.has(h.id)).map((h) => a.has(h.id) ? t.updated.find(({ oldValue: E }) => E.id === h.id).newValue : h),
272
- ...t.created || []
273
- ], u = [
274
- ...(e.deleted || []).filter((h) => !i.has(h.id)),
275
- ...(t.deleted || []).filter((h) => !n.has(h.id))
276
- ], b = [
277
- ...(e.updated || []).filter(({ newValue: h }) => !s.has(h.id)).map((h) => {
278
- const { oldValue: E, newValue: m } = h;
279
- if (a.has(m.id)) {
280
- const v = t.updated.find((C) => C.oldValue.id === m.id).newValue;
281
- return ot(E, v);
771
+ }, Ge = (t, e) => {
772
+ const n = new Set((t.created || []).map((d) => d.id)), o = new Set((t.updated || []).map(({ newValue: d }) => d.id)), i = new Set((e.created || []).map((d) => d.id)), r = new Set((e.deleted || []).map((d) => d.id)), s = new Set((e.updated || []).map(({ oldValue: d }) => d.id)), a = new Set((e.updated || []).filter(({ oldValue: d }) => n.has(d.id) || o.has(d.id)).map(({ oldValue: d }) => d.id)), u = [
773
+ ...(t.created || []).filter((d) => !r.has(d.id)).map((d) => s.has(d.id) ? e.updated.find(({ oldValue: f }) => f.id === d.id).newValue : d),
774
+ ...e.created || []
775
+ ], h = [
776
+ ...(t.deleted || []).filter((d) => !i.has(d.id)),
777
+ ...(e.deleted || []).filter((d) => !n.has(d.id))
778
+ ], p = [
779
+ ...(t.updated || []).filter(({ newValue: d }) => !r.has(d.id)).map((d) => {
780
+ const { oldValue: f, newValue: g } = d;
781
+ if (s.has(g.id)) {
782
+ const A = e.updated.find((y) => y.oldValue.id === g.id).newValue;
783
+ return Ft(f, A);
282
784
  } else
283
- return h;
785
+ return d;
284
786
  }),
285
- ...(t.updated || []).filter(({ oldValue: h }) => !r.has(h.id))
787
+ ...(e.updated || []).filter(({ oldValue: d }) => !a.has(d.id))
286
788
  ];
287
- return { created: d, deleted: u, updated: b };
288
- }, Mt = (e) => e.id !== void 0, Rt = () => {
289
- const e = /* @__PURE__ */ new Map(), t = /* @__PURE__ */ new Map(), n = [], o = (f, x = {}) => n.push({ onChange: f, options: x }), i = (f) => {
290
- const x = n.findIndex((A) => A.onChange == f);
789
+ return { created: u, deleted: h, updated: p };
790
+ }, ft = (t) => {
791
+ const e = t.id === void 0 ? zt() : t.id;
792
+ return {
793
+ ...t,
794
+ id: e,
795
+ bodies: t.bodies === void 0 ? [] : t.bodies.map((n) => ({
796
+ ...n,
797
+ annotation: e
798
+ })),
799
+ target: {
800
+ ...t.target,
801
+ annotation: e
802
+ }
803
+ };
804
+ }, Ke = (t) => t.id !== void 0, Qe = () => {
805
+ const t = /* @__PURE__ */ new Map(), e = /* @__PURE__ */ new Map(), n = [], o = (v, x = {}) => n.push({ onChange: v, options: x }), i = (v) => {
806
+ const x = n.findIndex((w) => w.onChange == v);
291
807
  x > -1 && n.splice(x, 1);
292
- }, s = (f, x) => {
293
- const A = {
294
- origin: f,
808
+ }, r = (v, x) => {
809
+ const w = {
810
+ origin: v,
295
811
  changes: {
296
812
  created: x.created || [],
297
813
  updated: x.updated || [],
298
814
  deleted: x.deleted || []
299
815
  },
300
- state: [...e.values()]
816
+ state: [...t.values()]
301
817
  };
302
- n.forEach((B) => {
303
- Ct(B, A) && B.onChange(A);
818
+ n.forEach((S) => {
819
+ qe(S, w) && S.onChange(w);
304
820
  });
305
- }, a = (f, x = M.LOCAL) => {
306
- if (e.get(f.id))
307
- throw Error(`Cannot add annotation ${f.id} - exists already`);
308
- e.set(f.id, f), f.bodies.forEach((A) => t.set(A.id, f.id)), s(x, { created: [f] });
309
- }, r = (f, x) => {
310
- const A = typeof f == "string" ? x : f, B = typeof f == "string" ? f : f.id, S = e.get(B);
311
- if (S) {
312
- const R = ot(S, A);
313
- return B === A.id ? e.set(B, A) : (e.delete(B), e.set(A.id, A)), S.bodies.forEach((X) => t.delete(X.id)), A.bodies.forEach((X) => t.set(X.id, A.id)), R;
821
+ }, s = (v, x = N.LOCAL) => {
822
+ if (v.id && t.get(v.id))
823
+ throw Error(`Cannot add annotation ${v.id} - exists already`);
824
+ {
825
+ const w = ft(v);
826
+ t.set(w.id, w), w.bodies.forEach((S) => e.set(S.id, w.id)), r(x, { created: [w] });
827
+ }
828
+ }, a = (v, x) => {
829
+ const w = ft(typeof v == "string" ? x : v), S = typeof v == "string" ? v : v.id, M = S && t.get(S);
830
+ if (M) {
831
+ const B = Ft(M, w);
832
+ return S === w.id ? t.set(S, w) : (t.delete(S), t.set(w.id, w)), M.bodies.forEach((k) => e.delete(k.id)), w.bodies.forEach((k) => e.set(k.id, w.id)), B;
314
833
  } else
315
- console.warn(`Cannot update annotation ${B} - does not exist`);
316
- }, d = (f, x = M.LOCAL, A = M.LOCAL) => {
317
- const B = Mt(x) ? A : x, S = r(f, x);
318
- S && s(B, { updated: [S] });
319
- }, u = (f, x = M.LOCAL) => {
320
- const A = f.reduce((B, S) => {
321
- const R = r(S);
322
- return R ? [...B, R] : B;
834
+ console.warn(`Cannot update annotation ${S} - does not exist`);
835
+ }, u = (v, x = N.LOCAL, w = N.LOCAL) => {
836
+ const S = Ke(x) ? w : x, M = a(v, x);
837
+ M && r(S, { updated: [M] });
838
+ }, h = (v, x = N.LOCAL) => {
839
+ const w = v.reduce((S, M) => {
840
+ const B = a(M);
841
+ return B ? [...S, B] : S;
323
842
  }, []);
324
- A.length > 0 && s(x, { updated: A });
325
- }, b = (f, x = M.LOCAL) => {
326
- const A = e.get(f.annotation);
327
- if (A) {
328
- const B = {
329
- ...A,
330
- bodies: [...A.bodies, f]
843
+ w.length > 0 && r(x, { updated: w });
844
+ }, p = (v, x = N.LOCAL) => {
845
+ const w = t.get(v.annotation);
846
+ if (w) {
847
+ const S = {
848
+ ...w,
849
+ bodies: [...w.bodies, v]
331
850
  };
332
- e.set(A.id, B), t.set(f.id, B.id), s(x, { updated: [{
333
- oldValue: A,
334
- newValue: B,
335
- bodiesCreated: [f]
851
+ t.set(w.id, S), e.set(v.id, S.id), r(x, { updated: [{
852
+ oldValue: w,
853
+ newValue: S,
854
+ bodiesCreated: [v]
336
855
  }] });
337
856
  } else
338
- console.warn(`Attempt to add body to missing annotation: ${f.annotation}`);
339
- }, h = () => [...e.values()], E = (f = M.LOCAL) => {
340
- const x = [...e.values()];
341
- e.clear(), t.clear(), s(f, { deleted: x });
342
- }, m = (f, x = !0, A = M.LOCAL) => {
857
+ console.warn(`Attempt to add body to missing annotation: ${v.annotation}`);
858
+ }, d = () => [...t.values()], f = (v = N.LOCAL) => {
859
+ const x = [...t.values()];
860
+ t.clear(), e.clear(), r(v, { deleted: x });
861
+ }, g = (v, x = !0, w = N.LOCAL) => {
862
+ const S = v.map(ft);
343
863
  if (x) {
344
- const B = [...e.values()];
345
- e.clear(), t.clear(), f.forEach((S) => {
346
- e.set(S.id, S), S.bodies.forEach((R) => t.set(R.id, S.id));
347
- }), s(A, { created: f, deleted: B });
864
+ const M = [...t.values()];
865
+ t.clear(), e.clear(), S.forEach((B) => {
866
+ t.set(B.id, B), B.bodies.forEach((k) => e.set(k.id, B.id));
867
+ }), r(w, { created: S, deleted: M });
348
868
  } else {
349
- const B = f.reduce((S, R) => {
350
- const X = e.get(R.id);
351
- return X ? [...S, X] : S;
869
+ const M = v.reduce((B, k) => {
870
+ const _ = k.id && t.get(k.id);
871
+ return _ ? [...B, _] : B;
352
872
  }, []);
353
- if (B.length > 0)
354
- throw Error(`Bulk insert would overwrite the following annotations: ${B.map((S) => S.id).join(", ")}`);
355
- f.forEach((S) => {
356
- e.set(S.id, S), S.bodies.forEach((R) => t.set(R.id, S.id));
357
- }), s(A, { created: f });
873
+ if (M.length > 0)
874
+ throw Error(`Bulk insert would overwrite the following annotations: ${M.map((B) => B.id).join(", ")}`);
875
+ S.forEach((B) => {
876
+ t.set(B.id, B), B.bodies.forEach((k) => e.set(k.id, B.id));
877
+ }), r(w, { created: S });
358
878
  }
359
- }, v = (f) => {
360
- const x = typeof f == "string" ? f : f.id, A = e.get(x);
361
- if (A)
362
- return e.delete(x), A.bodies.forEach((B) => t.delete(B.id)), A;
879
+ }, A = (v) => {
880
+ const x = typeof v == "string" ? v : v.id, w = t.get(x);
881
+ if (w)
882
+ return t.delete(x), w.bodies.forEach((S) => e.delete(S.id)), w;
363
883
  console.warn(`Attempt to delete missing annotation: ${x}`);
364
- }, C = (f, x = M.LOCAL) => {
365
- const A = v(f);
366
- A && s(x, { deleted: [A] });
367
- }, w = (f, x = M.LOCAL) => {
368
- const A = f.reduce((B, S) => {
369
- const R = v(S);
370
- return R ? [...B, R] : B;
884
+ }, y = (v, x = N.LOCAL) => {
885
+ const w = A(v);
886
+ w && r(x, { deleted: [w] });
887
+ }, m = (v, x = N.LOCAL) => {
888
+ const w = v.reduce((S, M) => {
889
+ const B = A(M);
890
+ return B ? [...S, B] : S;
371
891
  }, []);
372
- A.length > 0 && s(x, { deleted: A });
373
- }, l = (f, x = M.LOCAL) => {
374
- const A = e.get(f.annotation);
375
- if (A) {
376
- const B = A.bodies.find((S) => S.id === f.id);
377
- if (B) {
378
- t.delete(B.id);
892
+ w.length > 0 && r(x, { deleted: w });
893
+ }, l = (v) => {
894
+ const x = t.get(v.annotation);
895
+ if (x) {
896
+ const w = x.bodies.find((S) => S.id === v.id);
897
+ if (w) {
898
+ e.delete(w.id);
379
899
  const S = {
380
- ...A,
381
- bodies: A.bodies.filter((R) => R.id !== f.id)
900
+ ...x,
901
+ bodies: x.bodies.filter((M) => M.id !== v.id)
382
902
  };
383
- e.set(A.id, S), s(x, { updated: [{
384
- oldValue: A,
903
+ return t.set(x.id, S), {
904
+ oldValue: x,
385
905
  newValue: S,
386
- bodiesDeleted: [B]
387
- }] });
906
+ bodiesDeleted: [w]
907
+ };
388
908
  } else
389
- console.warn(`Attempt to delete missing body ${f.id} from annotation ${f.annotation}`);
909
+ console.warn(`Attempt to delete missing body ${v.id} from annotation ${v.annotation}`);
390
910
  } else
391
- console.warn(`Attempt to delete body from missing annotation ${f.annotation}`);
392
- }, c = (f) => {
393
- const x = e.get(f);
911
+ console.warn(`Attempt to delete body from missing annotation ${v.annotation}`);
912
+ }, c = (v, x = N.LOCAL) => {
913
+ const w = l(v);
914
+ w && r(x, { updated: [w] });
915
+ }, b = (v, x = N.LOCAL) => {
916
+ const w = v.map((S) => l(S)).filter(Boolean);
917
+ w.length > 0 && r(x, { updated: w });
918
+ }, C = (v) => {
919
+ const x = t.get(v);
394
920
  return x ? { ...x } : void 0;
395
- }, g = (f) => {
396
- const x = t.get(f);
921
+ }, L = (v) => {
922
+ const x = e.get(v);
397
923
  if (x) {
398
- const A = c(x).bodies.find((B) => B.id === f);
399
- if (A)
400
- return A;
401
- console.error(`Store integrity error: body ${f} in index, but not in annotation`);
924
+ const w = C(x).bodies.find((S) => S.id === v);
925
+ if (w)
926
+ return w;
927
+ console.error(`Store integrity error: body ${v} in index, but not in annotation`);
402
928
  } else
403
- console.warn(`Attempt to retrieve missing body: ${f}`);
404
- }, p = (f, x) => {
405
- if (f.annotation !== x.annotation)
929
+ console.warn(`Attempt to retrieve missing body: ${v}`);
930
+ }, E = (v, x) => {
931
+ if (v.annotation !== x.annotation)
406
932
  throw "Annotation integrity violation: annotation ID must be the same when updating bodies";
407
- const A = e.get(f.annotation);
408
- if (A) {
409
- const B = A.bodies.find((R) => R.id === f.id), S = {
410
- ...A,
411
- bodies: A.bodies.map((R) => R.id === B.id ? x : R)
933
+ const w = t.get(v.annotation);
934
+ if (w) {
935
+ const S = w.bodies.find((B) => B.id === v.id), M = {
936
+ ...w,
937
+ bodies: w.bodies.map((B) => B.id === S.id ? x : B)
412
938
  };
413
- return e.set(A.id, S), B.id !== x.id && (t.delete(B.id), t.set(x.id, S.id)), {
414
- oldValue: A,
415
- newValue: S,
416
- bodiesUpdated: [{ oldBody: B, newBody: x }]
939
+ return t.set(w.id, M), S.id !== x.id && (e.delete(S.id), e.set(x.id, M.id)), {
940
+ oldValue: w,
941
+ newValue: M,
942
+ bodiesUpdated: [{ oldBody: S, newBody: x }]
417
943
  };
418
944
  } else
419
- console.warn(`Attempt to add body to missing annotation ${f.annotation}`);
420
- }, y = (f, x, A = M.LOCAL) => {
421
- const B = p(f, x);
422
- s(A, { updated: [B] });
423
- }, T = (f, x = M.LOCAL) => {
424
- const A = f.map((B) => p({ id: B.id, annotation: B.annotation }, B));
425
- s(x, { updated: A });
426
- }, L = (f) => {
427
- const x = e.get(f.annotation);
945
+ console.warn(`Attempt to add body to missing annotation ${v.annotation}`);
946
+ }, R = (v, x, w = N.LOCAL) => {
947
+ const S = E(v, x);
948
+ S && r(w, { updated: [S] });
949
+ }, T = (v, x = N.LOCAL) => {
950
+ const w = v.map((S) => E({ id: S.id, annotation: S.annotation }, S)).filter(Boolean);
951
+ r(x, { updated: w });
952
+ }, O = (v) => {
953
+ const x = t.get(v.annotation);
428
954
  if (x) {
429
- const A = {
955
+ const w = {
430
956
  ...x,
431
957
  target: {
432
958
  ...x.target,
433
- ...f
959
+ ...v
434
960
  }
435
961
  };
436
- return e.set(x.id, A), {
962
+ return t.set(x.id, w), {
437
963
  oldValue: x,
438
- newValue: A,
964
+ newValue: w,
439
965
  targetUpdated: {
440
966
  oldTarget: x.target,
441
- newTarget: f
967
+ newTarget: v
442
968
  }
443
969
  };
444
970
  } else
445
- console.warn(`Attempt to update target on missing annotation: ${f.annotation}`);
971
+ console.warn(`Attempt to update target on missing annotation: ${v.annotation}`);
446
972
  };
447
973
  return {
448
- addAnnotation: a,
449
- addBody: b,
450
- all: h,
451
- bulkAddAnnotation: m,
452
- bulkDeleteAnnotation: w,
453
- bulkUpdateAnnotation: u,
974
+ addAnnotation: s,
975
+ addBody: p,
976
+ all: d,
977
+ bulkAddAnnotation: g,
978
+ bulkDeleteAnnotation: m,
979
+ bulkDeleteBodies: b,
980
+ bulkUpdateAnnotation: h,
454
981
  bulkUpdateBodies: T,
455
- bulkUpdateTargets: (f, x = M.LOCAL) => {
456
- const A = f.map(L).filter((B) => B);
457
- A.length > 0 && s(x, { updated: A });
982
+ bulkUpdateTargets: (v, x = N.LOCAL) => {
983
+ const w = v.map((S) => O(S)).filter(Boolean);
984
+ w.length > 0 && r(x, { updated: w });
458
985
  },
459
- clear: E,
460
- deleteAnnotation: C,
461
- deleteBody: l,
462
- getAnnotation: c,
463
- getBody: g,
986
+ clear: f,
987
+ deleteAnnotation: y,
988
+ deleteBody: c,
989
+ getAnnotation: C,
990
+ getBody: L,
464
991
  observe: o,
465
992
  unobserve: i,
466
- updateAnnotation: d,
467
- updateBody: y,
468
- updateTarget: (f, x = M.LOCAL) => {
469
- const A = L(f);
470
- A && s(x, { updated: [A] });
993
+ updateAnnotation: u,
994
+ updateBody: R,
995
+ updateTarget: (v, x = N.LOCAL) => {
996
+ const w = O(v);
997
+ w && r(x, { updated: [w] });
471
998
  }
472
999
  };
473
1000
  };
474
- let Ot = () => ({
475
- emit(e, ...t) {
476
- let n = this.events[e] || [];
477
- for (let o = 0, i = n.length; o < i; o++)
478
- n[o](...t);
1001
+ let Je = () => ({
1002
+ emit(t, ...e) {
1003
+ for (let n = 0, o = this.events[t] || [], i = o.length; n < i; n++)
1004
+ o[n](...e);
479
1005
  },
480
1006
  events: {},
481
- on(e, t) {
1007
+ on(t, e) {
482
1008
  var n;
483
- return (n = this.events[e]) != null && n.push(t) || (this.events[e] = [t]), () => {
1009
+ return ((n = this.events)[t] || (n[t] = [])).push(e), () => {
484
1010
  var o;
485
- this.events[e] = (o = this.events[e]) == null ? void 0 : o.filter((i) => t !== i);
1011
+ this.events[t] = (o = this.events[t]) == null ? void 0 : o.filter((i) => e !== i);
486
1012
  };
487
1013
  }
488
1014
  });
489
- const Xt = 250, Ut = (e) => {
490
- const t = Ot(), n = [];
491
- let o = -1, i = !1, s = 0;
492
- const a = (m) => {
1015
+ const Ze = 250, tn = (t) => {
1016
+ const e = Je(), n = [];
1017
+ let o = -1, i = !1, r = 0;
1018
+ const s = (g) => {
493
1019
  if (!i) {
494
- const { changes: v } = m, C = performance.now();
495
- if (C - s > Xt)
496
- n.splice(o + 1), n.push(v), o = n.length - 1;
1020
+ const { changes: A } = g, y = performance.now();
1021
+ if (y - r > Ze)
1022
+ n.splice(o + 1), n.push(A), o = n.length - 1;
497
1023
  else {
498
- const w = n.length - 1;
499
- n[w] = Lt(n[w], v);
1024
+ const m = n.length - 1;
1025
+ n[m] = Ge(n[m], A);
500
1026
  }
501
- s = C;
1027
+ r = y;
502
1028
  }
503
1029
  i = !1;
504
1030
  };
505
- e.observe(a, { origin: M.LOCAL });
506
- const r = (m) => (m == null ? void 0 : m.length) > 0 && e.bulkDeleteAnnotation(m), d = (m) => (m == null ? void 0 : m.length) > 0 && e.bulkAddAnnotation(m, !1), u = (m) => (m == null ? void 0 : m.length) > 0 && e.bulkUpdateAnnotation(m.map(({ oldValue: v }) => v)), b = (m) => (m == null ? void 0 : m.length) > 0 && e.bulkUpdateAnnotation(m.map(({ newValue: v }) => v)), h = (m) => (m == null ? void 0 : m.length) > 0 && e.bulkAddAnnotation(m, !1), E = (m) => (m == null ? void 0 : m.length) > 0 && e.bulkDeleteAnnotation(m);
1031
+ t.observe(s, { origin: N.LOCAL });
1032
+ const a = (g) => g && g.length > 0 && t.bulkDeleteAnnotation(g), u = (g) => g && g.length > 0 && t.bulkAddAnnotation(g, !1), h = (g) => g && g.length > 0 && t.bulkUpdateAnnotation(g.map(({ oldValue: A }) => A)), p = (g) => g && g.length > 0 && t.bulkUpdateAnnotation(g.map(({ newValue: A }) => A)), d = (g) => g && g.length > 0 && t.bulkAddAnnotation(g, !1), f = (g) => g && g.length > 0 && t.bulkDeleteAnnotation(g);
507
1033
  return {
508
1034
  canRedo: () => n.length - 1 > o,
509
1035
  canUndo: () => o > -1,
510
- destroy: () => e.unobserve(a),
511
- on: (m, v) => t.on(m, v),
1036
+ destroy: () => t.unobserve(s),
1037
+ on: (g, A) => e.on(g, A),
512
1038
  redo: () => {
513
1039
  if (n.length - 1 > o) {
514
1040
  i = !0;
515
- const { created: m, updated: v, deleted: C } = n[o + 1];
516
- d(m), b(v), E(C), t.emit("redo", n[o + 1]), o += 1;
1041
+ const { created: g, updated: A, deleted: y } = n[o + 1];
1042
+ u(g), p(A), f(y), e.emit("redo", n[o + 1]), o += 1;
517
1043
  }
518
1044
  },
519
1045
  undo: () => {
520
1046
  if (o > -1) {
521
1047
  i = !0;
522
- const { created: m, updated: v, deleted: C } = n[o];
523
- r(m), u(v), h(C), t.emit("undo", n[o]), o -= 1;
1048
+ const { created: g, updated: A, deleted: y } = n[o];
1049
+ a(g), h(A), d(y), e.emit("undo", n[o]), o -= 1;
524
1050
  }
525
1051
  }
526
1052
  };
527
- }, Yt = () => {
528
- const { subscribe: e, set: t } = J([]);
1053
+ }, en = () => {
1054
+ const { subscribe: t, set: e } = wt([]);
529
1055
  return {
530
- subscribe: e,
531
- set: t
1056
+ subscribe: t,
1057
+ set: e
532
1058
  };
533
- }, Dt = (e, t, n, o) => {
534
- const { store: i, selection: s, hover: a, viewport: r } = e, d = /* @__PURE__ */ new Map();
535
- let u = [], b, h;
536
- const E = (l, c) => {
537
- d.has(l) ? d.get(l).push(c) : d.set(l, [c]);
538
- }, m = (l, c) => {
539
- const g = d.get(l);
540
- g && g.indexOf(c) > 0 && g.splice(g.indexOf(c), 1);
541
- }, v = (l, c, g) => {
542
- d.has(l) && setTimeout(() => {
543
- d.get(l).forEach((p) => {
1059
+ }, nn = (t, e, n, o) => {
1060
+ const { store: i, selection: r, hover: s, viewport: a } = t, u = /* @__PURE__ */ new Map();
1061
+ let h = [], p;
1062
+ const d = (y, m) => {
1063
+ u.has(y) ? u.get(y).push(m) : u.set(y, [m]);
1064
+ }, f = (y, m) => {
1065
+ const l = u.get(y);
1066
+ if (l) {
1067
+ const c = l.indexOf(m);
1068
+ c !== -1 && l.splice(c, 1);
1069
+ }
1070
+ }, g = (y, m, l) => {
1071
+ u.has(y) && setTimeout(() => {
1072
+ u.get(y).forEach((c) => {
544
1073
  if (n) {
545
- const y = Array.isArray(c) ? c.map((L) => n.serialize(L)) : n.serialize(c), T = g ? g instanceof PointerEvent ? g : n.serialize(g) : void 0;
546
- p(y, T);
1074
+ const b = Array.isArray(m) ? m.map((L) => n.serialize(L)) : n.serialize(m), C = l ? l instanceof PointerEvent ? l : n.serialize(l) : void 0;
1075
+ c(b, C);
547
1076
  } else
548
- p(c, g);
1077
+ c(m, l);
549
1078
  });
550
1079
  }, 1);
551
- }, C = () => {
552
- const { selected: l } = s, c = l.map(({ id: g }) => i.getAnnotation(g));
553
- c.forEach((g) => {
554
- const p = u.find((y) => y.id === g.id);
555
- (!p || !Y(p, g)) && v("updateAnnotation", g, p);
556
- }), u = u.map((g) => c.find(({ id: y }) => y === g.id) || g);
557
1080
  };
558
- s.subscribe(({ selected: l }) => {
559
- if (!(u.length === 0 && l.length === 0)) {
560
- if (u.length === 0 && l.length > 0)
561
- u = l.map(({ id: c }) => i.getAnnotation(c));
562
- else if (u.length > 0 && l.length === 0)
563
- u.forEach((c) => {
564
- const g = i.getAnnotation(c.id);
565
- g && !Y(g, c) && v("updateAnnotation", g, c);
566
- }), u = [];
1081
+ r.subscribe(({ selected: y }) => {
1082
+ if (!(h.length === 0 && y.length === 0)) {
1083
+ if (h.length === 0 && y.length > 0)
1084
+ h = y.map(({ id: m }) => i.getAnnotation(m));
1085
+ else if (h.length > 0 && y.length === 0)
1086
+ h.forEach((m) => {
1087
+ const l = i.getAnnotation(m.id);
1088
+ l && !z(l, m) && g("updateAnnotation", l, m);
1089
+ }), h = [];
567
1090
  else {
568
- const c = new Set(u.map((p) => p.id)), g = new Set(l.map(({ id: p }) => p));
569
- u.filter((p) => !g.has(p.id)).forEach((p) => {
570
- const y = i.getAnnotation(p.id);
571
- y && !Y(y, p) && v("updateAnnotation", y, p);
572
- }), u = [
1091
+ const m = new Set(h.map((c) => c.id)), l = new Set(y.map(({ id: c }) => c));
1092
+ h.filter((c) => !l.has(c.id)).forEach((c) => {
1093
+ const b = i.getAnnotation(c.id);
1094
+ b && !z(b, c) && g("updateAnnotation", b, c);
1095
+ }), h = [
573
1096
  // Remove annotations that were deselected
574
- ...u.filter((p) => g.has(p.id)),
1097
+ ...h.filter((c) => l.has(c.id)),
575
1098
  // Add editable annotations that were selected
576
- ...l.filter(({ id: p }) => !c.has(p)).map(({ id: p }) => i.getAnnotation(p))
1099
+ ...y.filter(({ id: c }) => !m.has(c)).map(({ id: c }) => i.getAnnotation(c))
577
1100
  ];
578
1101
  }
579
- v("selectionChanged", u);
1102
+ g("selectionChanged", h);
580
1103
  }
581
- }), a.subscribe((l) => {
582
- !b && l ? v("mouseEnterAnnotation", i.getAnnotation(l)) : b && !l ? v("mouseLeaveAnnotation", i.getAnnotation(b)) : b && l && (v("mouseLeaveAnnotation", i.getAnnotation(b)), v("mouseEnterAnnotation", i.getAnnotation(l))), b = l;
583
- }), r == null || r.subscribe((l) => v("viewportIntersect", l.map(i.getAnnotation))), i.observe((l) => {
584
- o && (h && clearTimeout(h), h = setTimeout(C, 1e3));
585
- const { created: c, deleted: g } = l.changes;
586
- c.forEach((p) => v("createAnnotation", p)), g.forEach((p) => v("deleteAnnotation", p)), l.changes.updated.filter((p) => [
587
- ...p.bodiesCreated || [],
588
- ...p.bodiesDeleted || [],
589
- ...p.bodiesUpdated || []
590
- ].length > 0).forEach(({ oldValue: p, newValue: y }) => {
591
- const T = u.find((L) => L.id === p.id) || p;
592
- u = u.map((L) => L.id === p.id ? y : L), v("updateAnnotation", y, T);
1104
+ }), s.subscribe((y) => {
1105
+ !p && y ? g("mouseEnterAnnotation", i.getAnnotation(y)) : p && !y ? g("mouseLeaveAnnotation", i.getAnnotation(p)) : p && y && (g("mouseLeaveAnnotation", i.getAnnotation(p)), g("mouseEnterAnnotation", i.getAnnotation(y))), p = y;
1106
+ }), a == null || a.subscribe((y) => g("viewportIntersect", y.map((m) => i.getAnnotation(m)))), i.observe((y) => {
1107
+ const { created: m, deleted: l } = y.changes;
1108
+ (m || []).forEach((c) => g("createAnnotation", c)), (l || []).forEach((c) => g("deleteAnnotation", c)), (y.changes.updated || []).filter((c) => [
1109
+ ...c.bodiesCreated || [],
1110
+ ...c.bodiesDeleted || [],
1111
+ ...c.bodiesUpdated || []
1112
+ ].length > 0).forEach(({ oldValue: c, newValue: b }) => {
1113
+ const C = h.find((L) => L.id === c.id) || c;
1114
+ h = h.map((L) => L.id === c.id ? b : L), g("updateAnnotation", b, C);
593
1115
  });
594
- }, { origin: M.LOCAL }), i.observe((l) => {
595
- if (u) {
596
- const c = new Set(u.map((p) => p.id)), g = l.changes.updated.filter(({ newValue: p }) => c.has(p.id)).map(({ newValue: p }) => p);
597
- g.length > 0 && (u = u.map((p) => g.find((T) => T.id === p.id) || p));
1116
+ }, { origin: N.LOCAL }), i.observe((y) => {
1117
+ if (h) {
1118
+ const m = new Set(h.map((c) => c.id)), l = (y.changes.updated || []).filter(({ newValue: c }) => m.has(c.id)).map(({ newValue: c }) => c);
1119
+ l.length > 0 && (h = h.map((c) => l.find((C) => C.id === c.id) || c));
598
1120
  }
599
- }, { origin: M.REMOTE });
600
- const w = (l) => (c) => {
601
- const { created: g, deleted: p, updated: y } = c;
602
- g.forEach((T) => v("createAnnotation", T)), p.forEach((T) => v("deleteAnnotation", T)), l ? y.forEach((T) => v("updateAnnotation", T.oldValue, T.newValue)) : y.forEach((T) => v("updateAnnotation", T.newValue, T.oldValue));
1121
+ }, { origin: N.REMOTE });
1122
+ const A = (y) => (m) => {
1123
+ const { updated: l } = m;
1124
+ y ? (l || []).forEach((c) => g("updateAnnotation", c.oldValue, c.newValue)) : (l || []).forEach((c) => g("updateAnnotation", c.newValue, c.oldValue));
603
1125
  };
604
- return t.on("undo", w(!0)), t.on("redo", w(!1)), { on: E, off: m, emit: v };
605
- }, It = (e) => (t) => t.reduce((n, o) => {
606
- const { parsed: i, error: s } = e.parse(o);
607
- return s ? {
1126
+ return e.on("undo", A(!0)), e.on("redo", A(!1)), { on: d, off: f, emit: g };
1127
+ }, on = (t) => (e) => e.reduce((n, o) => {
1128
+ const { parsed: i, error: r } = t.parse(o);
1129
+ return r ? {
608
1130
  parsed: n.parsed,
609
1131
  failed: [...n.failed, o]
610
- } : {
1132
+ } : i ? {
611
1133
  parsed: [...n.parsed, i],
612
1134
  failed: n.failed
1135
+ } : {
1136
+ ...n
613
1137
  };
614
- }, { parsed: [], failed: [] }), Vt = (e, t, n) => {
615
- const { store: o, selection: i } = e, s = (w) => {
1138
+ }, { parsed: [], failed: [] }), rn = (t, e, n) => {
1139
+ const { store: o, selection: i } = t, r = (l) => {
616
1140
  if (n) {
617
- const { parsed: l, error: c } = n.parse(w);
618
- l ? o.addAnnotation(l, M.REMOTE) : console.error(c);
1141
+ const { parsed: c, error: b } = n.parse(l);
1142
+ c ? o.addAnnotation(c, N.REMOTE) : console.error(b);
619
1143
  } else
620
- o.addAnnotation(w, M.REMOTE);
621
- }, a = () => i.clear(), r = () => o.clear(), d = (w) => {
622
- const l = o.getAnnotation(w);
623
- return n && l ? n.serialize(l) : l;
624
- }, u = () => n ? o.all().map(n.serialize) : o.all(), b = () => {
625
- var w;
626
- const l = (((w = i.selected) == null ? void 0 : w.map((c) => c.id)) || []).map((c) => o.getAnnotation(c));
627
- return n ? l.map(n.serialize) : l;
628
- }, h = (w) => fetch(w).then((l) => l.json()).then((l) => (m(l), l)), E = (w) => {
629
- if (typeof w == "string") {
630
- const l = o.getAnnotation(w);
631
- return o.deleteAnnotation(w), n ? n.serialize(l) : l;
1144
+ o.addAnnotation(l, N.REMOTE);
1145
+ }, s = () => i.clear(), a = () => o.clear(), u = (l) => {
1146
+ const c = o.getAnnotation(l);
1147
+ return n && c ? n.serialize(c) : c;
1148
+ }, h = () => n ? o.all().map(n.serialize) : o.all(), p = () => {
1149
+ var l;
1150
+ const c = (((l = i.selected) == null ? void 0 : l.map((b) => b.id)) || []).map((b) => o.getAnnotation(b)).filter(Boolean);
1151
+ return n ? c.map(n.serialize) : c;
1152
+ }, d = (l, c = !0) => fetch(l).then((b) => b.json()).then((b) => (g(b, c), b)), f = (l) => {
1153
+ if (typeof l == "string") {
1154
+ const c = o.getAnnotation(l);
1155
+ if (o.deleteAnnotation(l), c)
1156
+ return n ? n.serialize(c) : c;
632
1157
  } else {
633
- const l = n ? n.parse(w).parsed : w;
634
- return o.deleteAnnotation(l), w;
1158
+ const c = n ? n.parse(l).parsed : l;
1159
+ if (c)
1160
+ return o.deleteAnnotation(c), l;
635
1161
  }
636
- }, m = (w) => {
1162
+ }, g = (l, c = !0) => {
637
1163
  if (n) {
638
- const { parsed: l, failed: c } = It(n)(w);
639
- c.length > 0 && console.warn(`Discarded ${c.length} invalid annotations`, c), o.bulkAddAnnotation(l, !0, M.REMOTE);
1164
+ const { parsed: b, failed: C } = on(n)(l);
1165
+ C.length > 0 && console.warn(`Discarded ${C.length} invalid annotations`, C), o.bulkAddAnnotation(b, c, N.REMOTE);
640
1166
  } else
641
- o.bulkAddAnnotation(w, !0, M.REMOTE);
642
- }, v = (w) => {
643
- w ? i.setSelected(w) : i.clear();
644
- }, C = (w) => {
1167
+ o.bulkAddAnnotation(l, c, N.REMOTE);
1168
+ }, A = (l, c) => {
1169
+ l ? i.setSelected(l, c) : i.clear();
1170
+ }, y = (l) => {
1171
+ i.clear(), i.setUserSelectAction(l);
1172
+ }, m = (l) => {
645
1173
  if (n) {
646
- const l = n.parse(w).parsed, c = n.serialize(o.getAnnotation(l.id));
647
- return o.updateAnnotation(l), c;
1174
+ const c = n.parse(l).parsed, b = n.serialize(o.getAnnotation(c.id));
1175
+ return o.updateAnnotation(c), b;
648
1176
  } else {
649
- const l = o.getAnnotation(w.id);
650
- return o.updateAnnotation(w), l;
1177
+ const c = o.getAnnotation(l.id);
1178
+ return o.updateAnnotation(l), c;
651
1179
  }
652
1180
  };
653
1181
  return {
654
- addAnnotation: s,
655
- cancelSelected: a,
656
- canRedo: t.canRedo,
657
- canUndo: t.canUndo,
658
- clearAnnotations: r,
659
- getAnnotationById: d,
660
- getAnnotations: u,
661
- getSelected: b,
662
- loadAnnotations: h,
663
- redo: t.redo,
664
- removeAnnotation: E,
665
- setAnnotations: m,
666
- setSelected: v,
667
- undo: t.undo,
668
- updateAnnotation: C
1182
+ addAnnotation: r,
1183
+ cancelSelected: s,
1184
+ canRedo: e.canRedo,
1185
+ canUndo: e.canUndo,
1186
+ clearAnnotations: a,
1187
+ getAnnotationById: u,
1188
+ getAnnotations: h,
1189
+ getSelected: p,
1190
+ loadAnnotations: d,
1191
+ redo: e.redo,
1192
+ removeAnnotation: f,
1193
+ setAnnotations: g,
1194
+ setSelected: A,
1195
+ setUserSelectAction: y,
1196
+ undo: e.undo,
1197
+ updateAnnotation: m
669
1198
  };
670
- };
671
- let _t = (e) => crypto.getRandomValues(new Uint8Array(e)), kt = (e, t, n) => {
672
- let o = (2 << Math.log(e.length - 1) / Math.LN2) - 1, i = -~(1.6 * o * t / e.length);
673
- return (s = t) => {
674
- let a = "";
1199
+ }, sn = "useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";
1200
+ let an = (t) => crypto.getRandomValues(new Uint8Array(t)), cn = (t, e, n) => {
1201
+ let o = (2 << Math.log(t.length - 1) / Math.LN2) - 1, i = -~(1.6 * o * e / t.length);
1202
+ return (r = e) => {
1203
+ let s = "";
675
1204
  for (; ; ) {
676
- let r = n(i), d = i;
677
- for (; d--; )
678
- if (a += e[r[d] & o] || "", a.length === s)
679
- return a;
1205
+ let a = n(i), u = i;
1206
+ for (; u--; )
1207
+ if (s += t[a[u] & o] || "", s.length === r) return s;
680
1208
  }
681
1209
  };
682
- }, Nt = (e, t = 21) => kt(e, t, _t), Pt = (e = 21) => crypto.getRandomValues(new Uint8Array(e)).reduce((t, n) => (n &= 63, n < 36 ? t += n.toString(36) : n < 62 ? t += (n - 26).toString(36).toUpperCase() : n > 62 ? t += "-" : t += "_", t), "");
683
- const zt = () => ({ isGuest: !0, id: Nt("1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_", 20)() });
684
- Pt();
685
- function Ht(e, t, n, o, i) {
686
- it(e, t, n || 0, o || e.length - 1, i || Ft);
687
- }
688
- function it(e, t, n, o, i) {
1210
+ }, ln = (t, e = 21) => cn(t, e, an), dn = (t = 21) => {
1211
+ let e = "", n = crypto.getRandomValues(new Uint8Array(t));
1212
+ for (; t--; )
1213
+ e += sn[n[t] & 63];
1214
+ return e;
1215
+ };
1216
+ const un = () => ({ isGuest: !0, id: ln("1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_", 20)() }), hn = (t) => {
1217
+ const e = JSON.stringify(t);
1218
+ let n = 0;
1219
+ for (let o = 0, i = e.length; o < i; o++) {
1220
+ let r = e.charCodeAt(o);
1221
+ n = (n << 5) - n + r, n |= 0;
1222
+ }
1223
+ return `${n}`;
1224
+ }, jt = (t) => t ? typeof t == "object" ? { ...t } : t : void 0, fn = (t, e) => (Array.isArray(t) ? t : [t]).map((n) => {
1225
+ const { id: o, type: i, purpose: r, value: s, created: a, modified: u, creator: h, ...p } = n;
1226
+ return {
1227
+ id: o || `temp-${hn(n)}`,
1228
+ annotation: e,
1229
+ type: i,
1230
+ purpose: r,
1231
+ value: s,
1232
+ creator: jt(h),
1233
+ created: a ? new Date(a) : void 0,
1234
+ updated: u ? new Date(u) : void 0,
1235
+ ...p
1236
+ };
1237
+ }), gn = (t) => t.map((e) => {
1238
+ var n;
1239
+ const { annotation: o, created: i, updated: r, ...s } = e, a = {
1240
+ ...s,
1241
+ created: i == null ? void 0 : i.toISOString(),
1242
+ modified: r == null ? void 0 : r.toISOString()
1243
+ };
1244
+ return (n = a.id) != null && n.startsWith("temp-") && delete a.id, a;
1245
+ });
1246
+ dn();
1247
+ const kn = (t, e) => ({
1248
+ parse: (n) => bn(n),
1249
+ serialize: (n) => vn(n, t, e)
1250
+ }), pn = (t) => t.quote !== void 0 && t.start !== void 0 && t.end !== void 0, mn = (t) => {
1251
+ const {
1252
+ id: e,
1253
+ creator: n,
1254
+ created: o,
1255
+ modified: i,
1256
+ target: r
1257
+ } = t, s = Array.isArray(r) ? r : [r], a = {
1258
+ creator: jt(n),
1259
+ created: o ? new Date(o) : void 0,
1260
+ updated: i ? new Date(i) : void 0,
1261
+ annotation: e,
1262
+ selector: []
1263
+ };
1264
+ for (const u of s) {
1265
+ const p = (Array.isArray(u.selector) ? u.selector : [u.selector]).reduce((d, f) => {
1266
+ switch (f.type) {
1267
+ case "TextQuoteSelector":
1268
+ d.quote = f.exact;
1269
+ break;
1270
+ case "TextPositionSelector":
1271
+ d.start = f.start, d.end = f.end;
1272
+ break;
1273
+ }
1274
+ return d;
1275
+ }, {});
1276
+ if (pn(p))
1277
+ a.selector.push({ id: u.id, ...p });
1278
+ else {
1279
+ const d = [
1280
+ p.start ? void 0 : "TextPositionSelector",
1281
+ p.quote ? void 0 : "TextQuoteSelector"
1282
+ ].filter(Boolean);
1283
+ return { error: Error(`Missing selector types: ${d.join(" and ")} for annotation: ${t.id}`) };
1284
+ }
1285
+ }
1286
+ return { parsed: a };
1287
+ }, bn = (t) => {
1288
+ const e = t.id || Pt(), {
1289
+ creator: n,
1290
+ created: o,
1291
+ modified: i,
1292
+ body: r,
1293
+ ...s
1294
+ } = t, a = fn(r, e), u = mn(t);
1295
+ return "error" in u ? { error: u.error } : {
1296
+ parsed: {
1297
+ ...s,
1298
+ id: e,
1299
+ bodies: a,
1300
+ target: u.parsed
1301
+ }
1302
+ };
1303
+ }, vn = (t, e, n) => {
1304
+ const { bodies: o, target: i, ...r } = t, {
1305
+ selector: s,
1306
+ creator: a,
1307
+ created: u,
1308
+ updated: h,
1309
+ ...p
1310
+ } = i, d = s.map((f) => {
1311
+ const { quote: g, start: A, end: y, range: m } = f, { prefix: l, suffix: c } = re(m, n), b = [{
1312
+ type: "TextQuoteSelector",
1313
+ exact: g,
1314
+ prefix: l,
1315
+ suffix: c
1316
+ }, {
1317
+ type: "TextPositionSelector",
1318
+ start: A,
1319
+ end: y
1320
+ }];
1321
+ return {
1322
+ ...p,
1323
+ id: f.id,
1324
+ source: e,
1325
+ selector: b
1326
+ };
1327
+ });
1328
+ return {
1329
+ ...r,
1330
+ "@context": "http://www.w3.org/ns/anno.jsonld",
1331
+ id: t.id,
1332
+ type: "Annotation",
1333
+ body: gn(t.bodies),
1334
+ creator: a,
1335
+ created: u == null ? void 0 : u.toISOString(),
1336
+ modified: h == null ? void 0 : h.toISOString(),
1337
+ target: d
1338
+ };
1339
+ };
1340
+ function Wt(t, e, n = 0, o = t.length - 1, i = yn) {
689
1341
  for (; o > n; ) {
690
1342
  if (o - n > 600) {
691
- var s = o - n + 1, a = t - n + 1, r = Math.log(s), d = 0.5 * Math.exp(2 * r / 3), u = 0.5 * Math.sqrt(r * d * (s - d) / s) * (a - s / 2 < 0 ? -1 : 1), b = Math.max(n, Math.floor(t - a * d / s + u)), h = Math.min(o, Math.floor(t + (s - a) * d / s + u));
692
- it(e, t, b, h, i);
1343
+ const u = o - n + 1, h = e - n + 1, p = Math.log(u), d = 0.5 * Math.exp(2 * p / 3), f = 0.5 * Math.sqrt(p * d * (u - d) / u) * (h - u / 2 < 0 ? -1 : 1), g = Math.max(n, Math.floor(e - h * d / u + f)), A = Math.min(o, Math.floor(e + (u - h) * d / u + f));
1344
+ Wt(t, e, g, A, i);
693
1345
  }
694
- var E = e[t], m = n, v = o;
695
- for (_(e, n, t), i(e[o], E) > 0 && _(e, n, o); m < v; ) {
696
- for (_(e, m, v), m++, v--; i(e[m], E) < 0; )
697
- m++;
698
- for (; i(e[v], E) > 0; )
699
- v--;
1346
+ const r = t[e];
1347
+ let s = n, a = o;
1348
+ for (q(t, n, e), i(t[o], r) > 0 && q(t, n, o); s < a; ) {
1349
+ for (q(t, s, a), s++, a--; i(t[s], r) < 0; ) s++;
1350
+ for (; i(t[a], r) > 0; ) a--;
700
1351
  }
701
- i(e[n], E) === 0 ? _(e, n, v) : (v++, _(e, v, o)), v <= t && (n = v + 1), t <= v && (o = v - 1);
1352
+ i(t[n], r) === 0 ? q(t, n, a) : (a++, q(t, a, o)), a <= e && (n = a + 1), e <= a && (o = a - 1);
702
1353
  }
703
1354
  }
704
- function _(e, t, n) {
705
- var o = e[t];
706
- e[t] = e[n], e[n] = o;
1355
+ function q(t, e, n) {
1356
+ const o = t[e];
1357
+ t[e] = t[n], t[n] = o;
707
1358
  }
708
- function Ft(e, t) {
709
- return e < t ? -1 : e > t ? 1 : 0;
1359
+ function yn(t, e) {
1360
+ return t < e ? -1 : t > e ? 1 : 0;
710
1361
  }
711
- class $t {
712
- constructor(t = 9) {
713
- this._maxEntries = Math.max(4, t), this._minEntries = Math.max(2, Math.ceil(this._maxEntries * 0.4)), this.clear();
1362
+ class wn {
1363
+ constructor(e = 9) {
1364
+ this._maxEntries = Math.max(4, e), this._minEntries = Math.max(2, Math.ceil(this._maxEntries * 0.4)), this.clear();
714
1365
  }
715
1366
  all() {
716
1367
  return this._all(this.data, []);
717
1368
  }
718
- search(t) {
1369
+ search(e) {
719
1370
  let n = this.data;
720
1371
  const o = [];
721
- if (!H(t, n))
722
- return o;
723
- const i = this.toBBox, s = [];
1372
+ if (!ot(e, n)) return o;
1373
+ const i = this.toBBox, r = [];
724
1374
  for (; n; ) {
725
- for (let a = 0; a < n.children.length; a++) {
726
- const r = n.children[a], d = n.leaf ? i(r) : r;
727
- H(t, d) && (n.leaf ? o.push(r) : G(t, d) ? this._all(r, o) : s.push(r));
1375
+ for (let s = 0; s < n.children.length; s++) {
1376
+ const a = n.children[s], u = n.leaf ? i(a) : a;
1377
+ ot(e, u) && (n.leaf ? o.push(a) : pt(e, u) ? this._all(a, o) : r.push(a));
728
1378
  }
729
- n = s.pop();
1379
+ n = r.pop();
730
1380
  }
731
1381
  return o;
732
1382
  }
733
- collides(t) {
1383
+ collides(e) {
734
1384
  let n = this.data;
735
- if (!H(t, n))
736
- return !1;
1385
+ if (!ot(e, n)) return !1;
737
1386
  const o = [];
738
1387
  for (; n; ) {
739
1388
  for (let i = 0; i < n.children.length; i++) {
740
- const s = n.children[i], a = n.leaf ? this.toBBox(s) : s;
741
- if (H(t, a)) {
742
- if (n.leaf || G(t, a))
743
- return !0;
744
- o.push(s);
1389
+ const r = n.children[i], s = n.leaf ? this.toBBox(r) : r;
1390
+ if (ot(e, s)) {
1391
+ if (n.leaf || pt(e, s)) return !0;
1392
+ o.push(r);
745
1393
  }
746
1394
  }
747
1395
  n = o.pop();
748
1396
  }
749
1397
  return !1;
750
1398
  }
751
- load(t) {
752
- if (!(t && t.length))
753
- return this;
754
- if (t.length < this._minEntries) {
755
- for (let o = 0; o < t.length; o++)
756
- this.insert(t[o]);
1399
+ load(e) {
1400
+ if (!(e && e.length)) return this;
1401
+ if (e.length < this._minEntries) {
1402
+ for (let o = 0; o < e.length; o++)
1403
+ this.insert(e[o]);
757
1404
  return this;
758
1405
  }
759
- let n = this._build(t.slice(), 0, t.length - 1, 0);
1406
+ let n = this._build(e.slice(), 0, e.length - 1, 0);
760
1407
  if (!this.data.children.length)
761
1408
  this.data = n;
762
1409
  else if (this.data.height === n.height)
@@ -770,181 +1417,178 @@ class $t {
770
1417
  }
771
1418
  return this;
772
1419
  }
773
- insert(t) {
774
- return t && this._insert(t, this.data.height - 1), this;
1420
+ insert(e) {
1421
+ return e && this._insert(e, this.data.height - 1), this;
775
1422
  }
776
1423
  clear() {
777
- return this.data = V([]), this;
1424
+ return this.data = W([]), this;
778
1425
  }
779
- remove(t, n) {
780
- if (!t)
781
- return this;
1426
+ remove(e, n) {
1427
+ if (!e) return this;
782
1428
  let o = this.data;
783
- const i = this.toBBox(t), s = [], a = [];
784
- let r, d, u;
785
- for (; o || s.length; ) {
786
- if (o || (o = s.pop(), d = s[s.length - 1], r = a.pop(), u = !0), o.leaf) {
787
- const b = Wt(t, o.children, n);
788
- if (b !== -1)
789
- return o.children.splice(b, 1), s.push(o), this._condense(s), this;
1429
+ const i = this.toBBox(e), r = [], s = [];
1430
+ let a, u, h;
1431
+ for (; o || r.length; ) {
1432
+ if (o || (o = r.pop(), u = r[r.length - 1], a = s.pop(), h = !0), o.leaf) {
1433
+ const p = xn(e, o.children, n);
1434
+ if (p !== -1)
1435
+ return o.children.splice(p, 1), r.push(o), this._condense(r), this;
790
1436
  }
791
- !u && !o.leaf && G(o, i) ? (s.push(o), a.push(r), r = 0, d = o, o = o.children[0]) : d ? (r++, o = d.children[r], u = !1) : o = null;
1437
+ !h && !o.leaf && pt(o, i) ? (r.push(o), s.push(a), a = 0, u = o, o = o.children[0]) : u ? (a++, o = u.children[a], h = !1) : o = null;
792
1438
  }
793
1439
  return this;
794
1440
  }
795
- toBBox(t) {
796
- return t;
1441
+ toBBox(e) {
1442
+ return e;
797
1443
  }
798
- compareMinX(t, n) {
799
- return t.minX - n.minX;
1444
+ compareMinX(e, n) {
1445
+ return e.minX - n.minX;
800
1446
  }
801
- compareMinY(t, n) {
802
- return t.minY - n.minY;
1447
+ compareMinY(e, n) {
1448
+ return e.minY - n.minY;
803
1449
  }
804
1450
  toJSON() {
805
1451
  return this.data;
806
1452
  }
807
- fromJSON(t) {
808
- return this.data = t, this;
1453
+ fromJSON(e) {
1454
+ return this.data = e, this;
809
1455
  }
810
- _all(t, n) {
1456
+ _all(e, n) {
811
1457
  const o = [];
812
- for (; t; )
813
- t.leaf ? n.push(...t.children) : o.push(...t.children), t = o.pop();
1458
+ for (; e; )
1459
+ e.leaf ? n.push(...e.children) : o.push(...e.children), e = o.pop();
814
1460
  return n;
815
1461
  }
816
- _build(t, n, o, i) {
817
- const s = o - n + 1;
818
- let a = this._maxEntries, r;
819
- if (s <= a)
820
- return r = V(t.slice(n, o + 1)), I(r, this.toBBox), r;
821
- i || (i = Math.ceil(Math.log(s) / Math.log(a)), a = Math.ceil(s / Math.pow(a, i - 1))), r = V([]), r.leaf = !1, r.height = i;
822
- const d = Math.ceil(s / a), u = d * Math.ceil(Math.sqrt(a));
823
- tt(t, n, o, u, this.compareMinX);
824
- for (let b = n; b <= o; b += u) {
825
- const h = Math.min(b + u - 1, o);
826
- tt(t, b, h, d, this.compareMinY);
827
- for (let E = b; E <= h; E += d) {
828
- const m = Math.min(E + d - 1, h);
829
- r.children.push(this._build(t, E, m, i - 1));
1462
+ _build(e, n, o, i) {
1463
+ const r = o - n + 1;
1464
+ let s = this._maxEntries, a;
1465
+ if (r <= s)
1466
+ return a = W(e.slice(n, o + 1)), j(a, this.toBBox), a;
1467
+ i || (i = Math.ceil(Math.log(r) / Math.log(s)), s = Math.ceil(r / Math.pow(s, i - 1))), a = W([]), a.leaf = !1, a.height = i;
1468
+ const u = Math.ceil(r / s), h = u * Math.ceil(Math.sqrt(s));
1469
+ kt(e, n, o, h, this.compareMinX);
1470
+ for (let p = n; p <= o; p += h) {
1471
+ const d = Math.min(p + h - 1, o);
1472
+ kt(e, p, d, u, this.compareMinY);
1473
+ for (let f = p; f <= d; f += u) {
1474
+ const g = Math.min(f + u - 1, d);
1475
+ a.children.push(this._build(e, f, g, i - 1));
830
1476
  }
831
1477
  }
832
- return I(r, this.toBBox), r;
1478
+ return j(a, this.toBBox), a;
833
1479
  }
834
- _chooseSubtree(t, n, o, i) {
1480
+ _chooseSubtree(e, n, o, i) {
835
1481
  for (; i.push(n), !(n.leaf || i.length - 1 === o); ) {
836
- let s = 1 / 0, a = 1 / 0, r;
837
- for (let d = 0; d < n.children.length; d++) {
838
- const u = n.children[d], b = K(u), h = Gt(t, u) - b;
839
- h < a ? (a = h, s = b < s ? b : s, r = u) : h === a && b < s && (s = b, r = u);
1482
+ let r = 1 / 0, s = 1 / 0, a;
1483
+ for (let u = 0; u < n.children.length; u++) {
1484
+ const h = n.children[u], p = gt(h), d = Sn(e, h) - p;
1485
+ d < s ? (s = d, r = p < r ? p : r, a = h) : d === s && p < r && (r = p, a = h);
840
1486
  }
841
- n = r || n.children[0];
1487
+ n = a || n.children[0];
842
1488
  }
843
1489
  return n;
844
1490
  }
845
- _insert(t, n, o) {
846
- const i = o ? t : this.toBBox(t), s = [], a = this._chooseSubtree(i, this.data, n, s);
847
- for (a.children.push(t), P(a, i); n >= 0 && s[n].children.length > this._maxEntries; )
848
- this._split(s, n), n--;
849
- this._adjustParentBBoxes(i, s, n);
1491
+ _insert(e, n, o) {
1492
+ const i = o ? e : this.toBBox(e), r = [], s = this._chooseSubtree(i, this.data, n, r);
1493
+ for (s.children.push(e), K(s, i); n >= 0 && r[n].children.length > this._maxEntries; )
1494
+ this._split(r, n), n--;
1495
+ this._adjustParentBBoxes(i, r, n);
850
1496
  }
851
1497
  // split overflowed node into two
852
- _split(t, n) {
853
- const o = t[n], i = o.children.length, s = this._minEntries;
854
- this._chooseSplitAxis(o, s, i);
855
- const a = this._chooseSplitIndex(o, s, i), r = V(o.children.splice(a, o.children.length - a));
856
- r.height = o.height, r.leaf = o.leaf, I(o, this.toBBox), I(r, this.toBBox), n ? t[n - 1].children.push(r) : this._splitRoot(o, r);
1498
+ _split(e, n) {
1499
+ const o = e[n], i = o.children.length, r = this._minEntries;
1500
+ this._chooseSplitAxis(o, r, i);
1501
+ const s = this._chooseSplitIndex(o, r, i), a = W(o.children.splice(s, o.children.length - s));
1502
+ a.height = o.height, a.leaf = o.leaf, j(o, this.toBBox), j(a, this.toBBox), n ? e[n - 1].children.push(a) : this._splitRoot(o, a);
857
1503
  }
858
- _splitRoot(t, n) {
859
- this.data = V([t, n]), this.data.height = t.height + 1, this.data.leaf = !1, I(this.data, this.toBBox);
1504
+ _splitRoot(e, n) {
1505
+ this.data = W([e, n]), this.data.height = e.height + 1, this.data.leaf = !1, j(this.data, this.toBBox);
860
1506
  }
861
- _chooseSplitIndex(t, n, o) {
862
- let i, s = 1 / 0, a = 1 / 0;
863
- for (let r = n; r <= o - n; r++) {
864
- const d = N(t, 0, r, this.toBBox), u = N(t, r, o, this.toBBox), b = Jt(d, u), h = K(d) + K(u);
865
- b < s ? (s = b, i = r, a = h < a ? h : a) : b === s && h < a && (a = h, i = r);
1507
+ _chooseSplitIndex(e, n, o) {
1508
+ let i, r = 1 / 0, s = 1 / 0;
1509
+ for (let a = n; a <= o - n; a++) {
1510
+ const u = G(e, 0, a, this.toBBox), h = G(e, a, o, this.toBBox), p = Cn(u, h), d = gt(u) + gt(h);
1511
+ p < r ? (r = p, i = a, s = d < s ? d : s) : p === r && d < s && (s = d, i = a);
866
1512
  }
867
1513
  return i || o - n;
868
1514
  }
869
1515
  // sorts node children by the best axis for split
870
- _chooseSplitAxis(t, n, o) {
871
- const i = t.leaf ? this.compareMinX : qt, s = t.leaf ? this.compareMinY : Kt, a = this._allDistMargin(t, n, o, i), r = this._allDistMargin(t, n, o, s);
872
- a < r && t.children.sort(i);
1516
+ _chooseSplitAxis(e, n, o) {
1517
+ const i = e.leaf ? this.compareMinX : An, r = e.leaf ? this.compareMinY : En, s = this._allDistMargin(e, n, o, i), a = this._allDistMargin(e, n, o, r);
1518
+ s < a && e.children.sort(i);
873
1519
  }
874
1520
  // total margin of all possible split distributions where each node is at least m full
875
- _allDistMargin(t, n, o, i) {
876
- t.children.sort(i);
877
- const s = this.toBBox, a = N(t, 0, n, s), r = N(t, o - n, o, s);
878
- let d = z(a) + z(r);
879
- for (let u = n; u < o - n; u++) {
880
- const b = t.children[u];
881
- P(a, t.leaf ? s(b) : b), d += z(a);
1521
+ _allDistMargin(e, n, o, i) {
1522
+ e.children.sort(i);
1523
+ const r = this.toBBox, s = G(e, 0, n, r), a = G(e, o - n, o, r);
1524
+ let u = nt(s) + nt(a);
1525
+ for (let h = n; h < o - n; h++) {
1526
+ const p = e.children[h];
1527
+ K(s, e.leaf ? r(p) : p), u += nt(s);
882
1528
  }
883
- for (let u = o - n - 1; u >= n; u--) {
884
- const b = t.children[u];
885
- P(r, t.leaf ? s(b) : b), d += z(r);
1529
+ for (let h = o - n - 1; h >= n; h--) {
1530
+ const p = e.children[h];
1531
+ K(a, e.leaf ? r(p) : p), u += nt(a);
886
1532
  }
887
- return d;
1533
+ return u;
888
1534
  }
889
- _adjustParentBBoxes(t, n, o) {
1535
+ _adjustParentBBoxes(e, n, o) {
890
1536
  for (let i = o; i >= 0; i--)
891
- P(n[i], t);
1537
+ K(n[i], e);
892
1538
  }
893
- _condense(t) {
894
- for (let n = t.length - 1, o; n >= 0; n--)
895
- t[n].children.length === 0 ? n > 0 ? (o = t[n - 1].children, o.splice(o.indexOf(t[n]), 1)) : this.clear() : I(t[n], this.toBBox);
1539
+ _condense(e) {
1540
+ for (let n = e.length - 1, o; n >= 0; n--)
1541
+ e[n].children.length === 0 ? n > 0 ? (o = e[n - 1].children, o.splice(o.indexOf(e[n]), 1)) : this.clear() : j(e[n], this.toBBox);
896
1542
  }
897
1543
  }
898
- function Wt(e, t, n) {
899
- if (!n)
900
- return t.indexOf(e);
901
- for (let o = 0; o < t.length; o++)
902
- if (n(e, t[o]))
903
- return o;
1544
+ function xn(t, e, n) {
1545
+ if (!n) return e.indexOf(t);
1546
+ for (let o = 0; o < e.length; o++)
1547
+ if (n(t, e[o])) return o;
904
1548
  return -1;
905
1549
  }
906
- function I(e, t) {
907
- N(e, 0, e.children.length, t, e);
1550
+ function j(t, e) {
1551
+ G(t, 0, t.children.length, e, t);
908
1552
  }
909
- function N(e, t, n, o, i) {
910
- i || (i = V(null)), i.minX = 1 / 0, i.minY = 1 / 0, i.maxX = -1 / 0, i.maxY = -1 / 0;
911
- for (let s = t; s < n; s++) {
912
- const a = e.children[s];
913
- P(i, e.leaf ? o(a) : a);
1553
+ function G(t, e, n, o, i) {
1554
+ i || (i = W(null)), i.minX = 1 / 0, i.minY = 1 / 0, i.maxX = -1 / 0, i.maxY = -1 / 0;
1555
+ for (let r = e; r < n; r++) {
1556
+ const s = t.children[r];
1557
+ K(i, t.leaf ? o(s) : s);
914
1558
  }
915
1559
  return i;
916
1560
  }
917
- function P(e, t) {
918
- return e.minX = Math.min(e.minX, t.minX), e.minY = Math.min(e.minY, t.minY), e.maxX = Math.max(e.maxX, t.maxX), e.maxY = Math.max(e.maxY, t.maxY), e;
1561
+ function K(t, e) {
1562
+ return t.minX = Math.min(t.minX, e.minX), t.minY = Math.min(t.minY, e.minY), t.maxX = Math.max(t.maxX, e.maxX), t.maxY = Math.max(t.maxY, e.maxY), t;
919
1563
  }
920
- function qt(e, t) {
921
- return e.minX - t.minX;
1564
+ function An(t, e) {
1565
+ return t.minX - e.minX;
922
1566
  }
923
- function Kt(e, t) {
924
- return e.minY - t.minY;
1567
+ function En(t, e) {
1568
+ return t.minY - e.minY;
925
1569
  }
926
- function K(e) {
927
- return (e.maxX - e.minX) * (e.maxY - e.minY);
1570
+ function gt(t) {
1571
+ return (t.maxX - t.minX) * (t.maxY - t.minY);
928
1572
  }
929
- function z(e) {
930
- return e.maxX - e.minX + (e.maxY - e.minY);
1573
+ function nt(t) {
1574
+ return t.maxX - t.minX + (t.maxY - t.minY);
931
1575
  }
932
- function Gt(e, t) {
933
- return (Math.max(t.maxX, e.maxX) - Math.min(t.minX, e.minX)) * (Math.max(t.maxY, e.maxY) - Math.min(t.minY, e.minY));
1576
+ function Sn(t, e) {
1577
+ return (Math.max(e.maxX, t.maxX) - Math.min(e.minX, t.minX)) * (Math.max(e.maxY, t.maxY) - Math.min(e.minY, t.minY));
934
1578
  }
935
- function Jt(e, t) {
936
- const n = Math.max(e.minX, t.minX), o = Math.max(e.minY, t.minY), i = Math.min(e.maxX, t.maxX), s = Math.min(e.maxY, t.maxY);
937
- return Math.max(0, i - n) * Math.max(0, s - o);
1579
+ function Cn(t, e) {
1580
+ const n = Math.max(t.minX, e.minX), o = Math.max(t.minY, e.minY), i = Math.min(t.maxX, e.maxX), r = Math.min(t.maxY, e.maxY);
1581
+ return Math.max(0, i - n) * Math.max(0, r - o);
938
1582
  }
939
- function G(e, t) {
940
- return e.minX <= t.minX && e.minY <= t.minY && t.maxX <= e.maxX && t.maxY <= e.maxY;
1583
+ function pt(t, e) {
1584
+ return t.minX <= e.minX && t.minY <= e.minY && e.maxX <= t.maxX && e.maxY <= t.maxY;
941
1585
  }
942
- function H(e, t) {
943
- return t.minX <= e.maxX && t.minY <= e.maxY && t.maxX >= e.minX && t.maxY >= e.minY;
1586
+ function ot(t, e) {
1587
+ return e.minX <= t.maxX && e.minY <= t.maxY && e.maxX >= t.minX && e.maxY >= t.minY;
944
1588
  }
945
- function V(e) {
1589
+ function W(t) {
946
1590
  return {
947
- children: e,
1591
+ children: t,
948
1592
  height: 1,
949
1593
  leaf: !0,
950
1594
  minX: 1 / 0,
@@ -953,359 +1597,347 @@ function V(e) {
953
1597
  maxY: -1 / 0
954
1598
  };
955
1599
  }
956
- function tt(e, t, n, o, i) {
957
- const s = [t, n];
958
- for (; s.length; ) {
959
- if (n = s.pop(), t = s.pop(), n - t <= o)
960
- continue;
961
- const a = t + Math.ceil((n - t) / o / 2) * o;
962
- Ht(e, a, t, n, i), s.push(t, a, a, n);
1600
+ function kt(t, e, n, o, i) {
1601
+ const r = [e, n];
1602
+ for (; r.length; ) {
1603
+ if (n = r.pop(), e = r.pop(), n - e <= o) continue;
1604
+ const s = e + Math.ceil((n - e) / o / 2) * o;
1605
+ Wt(t, s, e, n, i), r.push(e, s, s, n);
963
1606
  }
964
1607
  }
965
- const jt = (e, t) => {
966
- const n = (s) => Math.round(s * 10) / 10, o = {
967
- top: n(e.top),
968
- bottom: n(e.bottom),
969
- left: n(e.left),
970
- right: n(e.right)
971
- }, i = {
972
- top: n(t.top),
973
- bottom: n(t.bottom),
974
- left: n(t.left),
975
- right: n(t.right)
976
- };
977
- if (Math.abs(o.top - i.top) < 0.5 && Math.abs(o.bottom - i.bottom) < 0.5) {
978
- if (Math.abs(o.left - i.right) < 0.5 || Math.abs(o.right - i.left) < 0.5)
979
- return "inline-adjacent";
980
- if (o.left >= i.left && o.right <= i.right)
981
- return "inline-is-contained";
982
- if (o.left <= i.left && o.right >= i.right)
983
- return "inline-contains";
984
- } else if (o.top <= i.top && o.bottom >= i.bottom) {
985
- if (o.left <= i.left && o.right >= i.right)
986
- return "block-contains";
987
- } else if (o.top >= i.top && o.bottom <= i.bottom && o.left >= i.left && o.right <= i.right)
988
- return "block-is-contained";
989
- }, Zt = (e, t) => {
990
- const n = Math.min(e.left, t.left), o = Math.max(e.right, t.right), i = Math.min(e.top, t.top), s = Math.max(e.bottom, t.bottom);
991
- return new DOMRect(n, i, o - n, s - i);
992
- }, Qt = (e) => e.reduce((t, n) => {
993
- if (n.width === 0 || n.height === 0)
994
- return t;
995
- let o = [...t], i = !1;
996
- for (const s of t) {
997
- const a = jt(n, s);
998
- if (a === "inline-adjacent") {
999
- o = o.map((r) => r === s ? Zt(n, s) : r), i = !0;
1000
- break;
1001
- } else if (a === "inline-contains") {
1002
- o = o.map((r) => r === s ? n : r), i = !0;
1003
- break;
1004
- } else if (a === "inline-is-contained") {
1005
- i = !0;
1006
- break;
1007
- } else if (a === "block-contains" || a === "block-is-contained") {
1008
- n.width < s.width && (o = o.map((r) => r === s ? n : r)), i = !0;
1009
- break;
1010
- }
1011
- }
1012
- return i ? o : [...o, n];
1013
- }, []), te = (e) => {
1014
- const { startContainer: t, endContainer: n } = e;
1015
- if (t.nodeType === Node.TEXT_NODE && n.nodeType === Node.TEXT_NODE)
1016
- return e;
1017
- if (t.nodeType !== Node.TEXT_NODE) {
1018
- const i = t.nextSibling || t.parentNode, s = i.nodeType === Node.TEXT_NODE ? i : Array.from(i.childNodes).filter((a) => a.nodeType === Node.TEXT_NODE).shift();
1019
- e.setEnd(s, 0);
1020
- }
1021
- if (n.nodeType !== Node.TEXT_NODE) {
1022
- const i = n.previousSibling || n.parentNode, s = i.nodeType === Node.TEXT_NODE ? i : Array.from(i.childNodes).filter((a) => a.nodeType === Node.TEXT_NODE).pop();
1023
- e.setEnd(s, s.textContent.length);
1024
- }
1025
- return e;
1026
- }, ee = (e, t) => {
1027
- const n = new $t(), o = /* @__PURE__ */ new Map(), i = (l) => {
1028
- const c = t.getBoundingClientRect(), p = l.selector.range instanceof Range && !l.selector.range.collapsed && l.selector.range.startContainer.nodeType === Node.TEXT_NODE && l.selector.range.endContainer.nodeType === Node.TEXT_NODE ? l : k(l, t), y = Array.from(p.selector.range.getClientRects()), T = Qt(y);
1029
- return T.map((L) => {
1030
- const { x: f, y: x, width: A, height: B } = L;
1608
+ const Ln = (t, e) => {
1609
+ const n = new wn(), o = /* @__PURE__ */ new Map(), i = (l, c) => {
1610
+ const b = l.selector.flatMap((L) => {
1611
+ const E = H([L]) ? L.range : Yt(L, e).range;
1612
+ return Array.from(E.getClientRects());
1613
+ }), C = de(b).map(({ left: L, top: E, right: R, bottom: T }) => new DOMRect(L - c.left, E - c.top, R - L, T - E));
1614
+ return C.map((L) => {
1615
+ const { x: E, y: R, width: T, height: O } = L;
1031
1616
  return {
1032
- minX: f - c.x,
1033
- minY: x - c.y,
1034
- maxX: f - c.x + A,
1035
- maxY: x - c.y + B,
1617
+ minX: E,
1618
+ minY: R,
1619
+ maxX: E + T,
1620
+ maxY: R + O,
1036
1621
  annotation: {
1037
1622
  id: l.annotation,
1038
- rects: T
1623
+ rects: C
1039
1624
  }
1040
1625
  };
1041
1626
  });
1042
- }, s = () => [...o.values()], a = () => {
1627
+ }, r = () => [...o.values()], s = () => {
1043
1628
  n.clear(), o.clear();
1044
- }, r = (l) => {
1045
- const c = i(l);
1046
- c.forEach((g) => n.insert(g)), o.set(l.annotation, c);
1047
- }, d = (l) => {
1048
- o.get(l.annotation).forEach((g) => n.remove(g)), o.delete(l.annotation);
1629
+ }, a = (l) => {
1630
+ const c = i(l, e.getBoundingClientRect());
1631
+ c.forEach((b) => n.insert(b)), o.set(l.annotation, c);
1049
1632
  }, u = (l) => {
1050
- d(l), r(l);
1051
- }, b = (l, c = !0) => {
1052
- c && a();
1053
- const g = l.map((y) => ({ target: y, rects: i(y) }));
1054
- g.forEach(({ target: y, rects: T }) => o.set(y.annotation, T));
1055
- const p = g.reduce((y, { rects: T }) => [...y, ...T], []);
1056
- n.load(p);
1057
- }, h = (l, c) => {
1058
- const g = n.search({
1633
+ const c = o.get(l.annotation);
1634
+ c && (c.forEach((b) => n.remove(b)), o.delete(l.annotation));
1635
+ }, h = (l) => {
1636
+ u(l), a(l);
1637
+ }, p = (l, c = !0) => {
1638
+ c && s();
1639
+ const b = e.getBoundingClientRect(), C = l.map((E) => ({ target: E, rects: i(E, b) }));
1640
+ C.forEach(({ target: E, rects: R }) => o.set(E.annotation, R));
1641
+ const L = C.flatMap(({ rects: E }) => E);
1642
+ n.load(L);
1643
+ }, d = (l, c, b = !1) => {
1644
+ const C = n.search({
1059
1645
  minX: l,
1060
1646
  minY: c,
1061
1647
  maxX: l,
1062
1648
  maxY: c
1063
- }), p = (y) => y.annotation.rects.reduce((T, L) => T + L.width * L.height, 0);
1064
- if (g.length > 0)
1065
- return g.sort((y, T) => p(y) - p(T)), g[0].annotation.id;
1066
- }, E = (l) => {
1067
- const c = m(l);
1649
+ }), L = (E) => E.annotation.rects.reduce((R, T) => R + T.width * T.height, 0);
1650
+ return C.length > 0 ? (C.sort((E, R) => L(E) - L(R)), b ? C.map((E) => E.annotation.id) : [C[0].annotation.id]) : [];
1651
+ }, f = (l) => {
1652
+ const c = g(l);
1068
1653
  if (c.length === 0)
1069
1654
  return;
1070
- let g = c[0].left, p = c[0].top, y = c[0].right, T = c[0].bottom;
1071
- for (let L = 1; L < c.length; L++) {
1072
- const f = c[L];
1073
- g = Math.min(g, f.left), p = Math.min(p, f.top), y = Math.max(y, f.right), T = Math.max(T, f.bottom);
1655
+ let b = c[0].left, C = c[0].top, L = c[0].right, E = c[0].bottom;
1656
+ for (let R = 1; R < c.length; R++) {
1657
+ const T = c[R];
1658
+ b = Math.min(b, T.left), C = Math.min(C, T.top), L = Math.max(L, T.right), E = Math.max(E, T.bottom);
1074
1659
  }
1075
- return new DOMRect(g, p, y - g, T - p);
1076
- }, m = (l) => {
1660
+ return new DOMRect(b, C, L - b, E - C);
1661
+ }, g = (l) => {
1077
1662
  const c = o.get(l);
1078
1663
  return c ? c[0].annotation.rects : [];
1079
1664
  };
1080
1665
  return {
1081
- all: s,
1082
- clear: a,
1083
- getAt: h,
1084
- getBoundsForAnnotation: E,
1085
- getDOMRectsForAnnotation: m,
1086
- getIntersectingRects: (l, c, g, p) => n.search({ minX: l, minY: c, maxX: g, maxY: p }),
1087
- insert: r,
1088
- recalculate: () => b(e.all().map((l) => l.target), !0),
1089
- remove: d,
1090
- set: b,
1666
+ all: r,
1667
+ clear: s,
1668
+ getAt: d,
1669
+ getAnnotationBounds: f,
1670
+ getAnnotationRects: g,
1671
+ getIntersecting: (l, c, b, C) => {
1672
+ const L = n.search({ minX: l, minY: c, maxX: b, maxY: C }), E = new Set(L.map((R) => R.annotation.id));
1673
+ return Array.from(E).map((R) => ({
1674
+ annotation: t.getAnnotation(R),
1675
+ rects: g(R)
1676
+ })).filter((R) => !!R.annotation);
1677
+ },
1678
+ insert: a,
1679
+ recalculate: () => p(t.all().map((l) => l.target), !0),
1680
+ remove: u,
1681
+ set: p,
1091
1682
  size: () => n.all().length,
1092
- update: u
1683
+ update: h
1093
1684
  };
1094
- }, F = (e) => {
1095
- const { range: t } = e.target.selector;
1096
- return t instanceof Range && !t.collapsed;
1097
- }, ne = (e, t) => {
1098
- const n = Rt(), o = ee(n, e), i = vt(n, t), s = xt(n), a = Yt(), r = (w, l = M.LOCAL) => {
1099
- const c = w.target.selector.range instanceof Range ? w : { ...w, target: k(w.target, e) }, { range: g } = c.target.selector, p = g && !g.collapsed;
1100
- return p && n.addAnnotation(c, l), p;
1101
- }, d = (w, l = !0, c = M.LOCAL) => {
1102
- const g = w.map((y) => F(y) ? y : { ...y, target: k(y.target, e) });
1103
- if (g.some((y) => y.target.selector.range.collapsed)) {
1104
- const y = g.filter((T) => T.target.selector.range.collapsed);
1105
- return console.warn("Could not revive all targets"), console.warn(y), n.bulkAddAnnotation(g, l, c), y;
1106
- } else
1107
- return n.bulkAddAnnotation(g, l, c), [];
1108
- }, u = (w, l = M.LOCAL) => {
1109
- const c = w.selector.range instanceof Range ? w : k(w, e);
1110
- n.updateTarget(c, l);
1111
- }, b = (w, l = M.LOCAL) => {
1112
- const c = w.map((g) => g.selector.range instanceof Range ? g : k(g, e));
1113
- n.bulkUpdateTargets(c, l);
1114
- }, h = (w, l) => {
1115
- const c = o.getAt(w, l);
1116
- return c ? n.getAnnotation(c) : void 0;
1117
- }, E = (w, l, c, g) => {
1118
- const p = o.getIntersectingRects(w, l, c, g);
1119
- return Array.from(new Set(p.map((T) => T.annotation.id))).map((T) => n.getAnnotation(T)).filter((T) => T);
1120
- }, m = (w, l, c, g = 5) => {
1121
- const p = o.getDOMRectsForAnnotation(w);
1122
- if (p.length > 0) {
1123
- if (l && c) {
1124
- const y = p.find(({ top: T, right: L, bottom: f, left: x }) => l >= x - g && l <= L + g && c >= T - g && c <= f + g);
1125
- if (y)
1126
- return y;
1685
+ }, Rn = (t, e) => {
1686
+ const n = Qe(), o = Ln(n, t), i = Ye(n);
1687
+ i.setUserSelectAction(e);
1688
+ const r = _e(n), s = en(), a = (y, m = N.LOCAL) => {
1689
+ const l = st(y, t), c = H(l.target.selector);
1690
+ return c && n.addAnnotation(l, m), c;
1691
+ }, u = (y, m = !0, l = N.LOCAL) => {
1692
+ const c = y.map((C) => st(C, t)), b = c.filter((C) => !H(C.target.selector));
1693
+ return b.length > 0 ? (console.warn("Could not revive all targets for these annotations:", b), n.bulkAddAnnotation(c, m, l), b) : (n.bulkAddAnnotation(c, m, l), []);
1694
+ }, h = (y, m = N.LOCAL) => {
1695
+ const l = y.map((b) => st(b, t)), c = l.filter((b) => !H(b.target.selector));
1696
+ return c.length > 0 && console.warn("Could not revive all targets for these annotations:", c), l.forEach((b) => {
1697
+ n.getAnnotation(b.id) ? n.updateAnnotation(b, m) : n.addAnnotation(b, m);
1698
+ }), c;
1699
+ }, p = (y, m = N.LOCAL) => {
1700
+ const l = rt(y, t);
1701
+ n.updateTarget(l, m);
1702
+ }, d = (y, m = N.LOCAL) => {
1703
+ const l = y.map((c) => rt(c, t));
1704
+ n.bulkUpdateTargets(l, m);
1705
+ }, f = (y, m, l) => {
1706
+ const c = o.getAt(y, m, !!l).map((C) => n.getAnnotation(C)), b = l ? c.filter(l) : c;
1707
+ return b.length > 0 ? b[0] : void 0;
1708
+ }, g = (y, m, l, c = 5) => {
1709
+ const b = o.getAnnotationRects(y);
1710
+ if (b.length !== 0) {
1711
+ if (m && l) {
1712
+ const C = b.find(({ top: L, right: E, bottom: R, left: T }) => m >= T - c && m <= E + c && l >= L - c && l <= R + c);
1713
+ if (C) return C;
1127
1714
  }
1128
- return o.getBoundsForAnnotation(w);
1715
+ return o.getAnnotationBounds(y);
1129
1716
  }
1130
- }, v = (w, l, c, g) => {
1131
- const y = o.getIntersectingRects(w, l, c, g).reduce((T, L) => ((T[L.annotation.id] = T[L.annotation.id] || []).push(L), T), {});
1132
- return Object.entries(y).map(([T, L]) => ({
1133
- annotation: n.getAnnotation(T),
1134
- rects: L.map(({ minX: f, minY: x, maxX: A, maxY: B }) => ({ x: f, y: x, width: A - f, height: B - x }))
1135
- }));
1136
- }, C = () => o.recalculate();
1137
- return n.observe(({ changes: w }) => {
1138
- const l = (w.created || []).filter(F), c = (w.deleted || []).filter(F), g = (w.updated || []).filter((p) => F(p.newValue));
1139
- l.length > 0 && o.set(l.map((p) => p.target), !1), (c == null ? void 0 : c.length) > 0 && c.forEach((p) => o.remove(p.target)), (g == null ? void 0 : g.length) > 0 && g.forEach(({ newValue: p }) => o.update(p.target));
1717
+ }, A = () => o.recalculate();
1718
+ return n.observe(({ changes: y }) => {
1719
+ const m = (y.deleted || []).filter((b) => H(b.target.selector)), l = (y.created || []).filter((b) => H(b.target.selector)), c = (y.updated || []).filter((b) => H(b.newValue.target.selector));
1720
+ (m == null ? void 0 : m.length) > 0 && m.forEach((b) => o.remove(b.target)), l.length > 0 && o.set(l.map((b) => b.target), !1), (c == null ? void 0 : c.length) > 0 && c.forEach(({ newValue: b }) => o.update(b.target));
1140
1721
  }), {
1141
1722
  store: {
1142
1723
  ...n,
1143
- addAnnotation: r,
1144
- bulkAddAnnotation: d,
1145
- bulkUpdateTargets: b,
1146
- getAnnotationBounds: m,
1147
- getAt: h,
1148
- getIntersecting: E,
1149
- getIntersectingRects: v,
1150
- recalculatePositions: C,
1151
- updateTarget: u
1724
+ addAnnotation: a,
1725
+ bulkAddAnnotation: u,
1726
+ bulkUpdateTargets: d,
1727
+ bulkUpsertAnnotations: h,
1728
+ getAnnotationBounds: g,
1729
+ getAt: f,
1730
+ getIntersecting: o.getIntersecting,
1731
+ recalculatePositions: A,
1732
+ updateTarget: p
1152
1733
  },
1153
1734
  selection: i,
1154
- hover: s,
1155
- viewport: a
1735
+ hover: r,
1736
+ viewport: s
1156
1737
  };
1157
- }, oe = (e, t = {}) => {
1158
- const n = /* @__PURE__ */ new Map(), o = (s) => Array.from(n.entries()).filter(([a, r]) => r.presenceKey === s.presenceKey).map(([a, r]) => a);
1159
- return e.on("selectionChange", (s, a) => {
1160
- o(s).forEach((d) => n.delete(d)), a && a.forEach((d) => n.set(d, s));
1161
- }), { paint: (s, a, r, d, u, b) => {
1162
- t.font && (d.font = t.font);
1163
- const h = n.get(s.id);
1164
- if (h) {
1165
- const { x: E, y: m, height: v } = a[0];
1166
- d.fillStyle = h.appearance.color, d.fillRect(E - 2, m - 2.5, 2, v + 5);
1167
- const C = d.measureText(h.appearance.label), w = C.width + 6, l = C.actualBoundingBoxAscent + C.actualBoundingBoxDescent + 8, c = C.fontBoundingBoxAscent ? 8 : 6.5;
1168
- d.fillRect(E - 2, m - 2.5 - l, w, l), d.fillStyle = "#fff", d.fillText(h.appearance.label, E + 1, m - c), r.fillStyle = h.appearance.color, r.globalAlpha = u ? 0.45 : 0.18, a.forEach(({ x: g, y: p, width: y, height: T }) => r.fillRect(g, p - 2.5, y, T + 5));
1169
- } else
1170
- nt.paint(s, a, r, d, u, b);
1171
- } };
1172
- }, st = (e) => e === null ? null : e.scrollHeight > e.clientHeight ? e : st(e.parentElement), ie = (e, t) => (n) => {
1173
- const o = st(e);
1174
- if (o) {
1175
- const i = t.getAnnotation(n.id), { range: s } = i.target.selector;
1176
- if (!s || s.collapsed)
1177
- return !1;
1178
- const a = o.getBoundingClientRect(), r = o.clientHeight, d = o.clientWidth, u = i.target.selector.range.getBoundingClientRect(), { width: b, height: h } = t.getAnnotationBounds(n.id), E = u.top - a.top, m = u.left - a.left, v = o.parentElement ? o.scrollTop : 0, C = o.parentElement ? o.scrollLeft : 0, w = E + v - (r - h) / 2, l = m + C - (d - b) / 2;
1179
- return o.scroll({ top: w, left: l, behavior: "smooth" }), !0;
1180
- }
1181
- };
1182
- let $;
1183
- const se = new Uint8Array(16);
1184
- function ae() {
1185
- if (!$ && ($ = typeof crypto < "u" && crypto.getRandomValues && crypto.getRandomValues.bind(crypto), !$))
1186
- throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");
1187
- return $(se);
1188
- }
1189
- const O = [];
1190
- for (let e = 0; e < 256; ++e)
1191
- O.push((e + 256).toString(16).slice(1));
1192
- function re(e, t = 0) {
1193
- return O[e[t + 0]] + O[e[t + 1]] + O[e[t + 2]] + O[e[t + 3]] + "-" + O[e[t + 4]] + O[e[t + 5]] + "-" + O[e[t + 6]] + O[e[t + 7]] + "-" + O[e[t + 8]] + O[e[t + 9]] + "-" + O[e[t + 10]] + O[e[t + 11]] + O[e[t + 12]] + O[e[t + 13]] + O[e[t + 14]] + O[e[t + 15]];
1194
- }
1195
- const le = typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto), et = {
1196
- randomUUID: le
1197
- };
1198
- function ce(e, t, n) {
1199
- if (et.randomUUID && !t && !e)
1200
- return et.randomUUID();
1201
- e = e || {};
1202
- const o = e.random || (e.rng || ae)();
1203
- if (o[6] = o[6] & 15 | 64, o[8] = o[8] & 63 | 128, t) {
1204
- n = n || 0;
1205
- for (let i = 0; i < 16; ++i)
1206
- t[n + i] = o[i];
1207
- return t;
1738
+ }, Tn = () => {
1739
+ const t = document.createElement("canvas");
1740
+ t.width = 2 * window.innerWidth, t.height = 2 * window.innerHeight, t.className = "r6o-presence-layer";
1741
+ const e = t.getContext("2d");
1742
+ return e.scale(2, 2), e.translate(0.5, 0.5), t;
1743
+ }, Bn = (t, e, n = {}) => {
1744
+ const o = Tn(), i = o.getContext("2d");
1745
+ document.body.appendChild(o);
1746
+ const r = /* @__PURE__ */ new Map(), s = (d) => Array.from(r.entries()).filter(([f, g]) => g.presenceKey === d.presenceKey).map(([f, g]) => f);
1747
+ return e.on("selectionChange", (d, f) => {
1748
+ s(d).forEach((A) => r.delete(A)), f && f.forEach((A) => r.set(A, d));
1749
+ }), {
1750
+ clear: () => {
1751
+ const { width: d, height: f } = o;
1752
+ i.clearRect(-0.5, -0.5, d + 1, f + 1);
1753
+ },
1754
+ destroy: () => {
1755
+ o.remove();
1756
+ },
1757
+ paint: (d, f, g) => {
1758
+ n.font && (i.font = n.font);
1759
+ const A = r.get(d.annotation.id);
1760
+ if (A) {
1761
+ const { height: y } = d.rects[0], m = d.rects[0].x + f.left, l = d.rects[0].y + f.top;
1762
+ i.fillStyle = A.appearance.color, i.fillRect(m - 2, l - 2.5, 2, y + 5);
1763
+ const c = i.measureText(A.appearance.label), b = c.width + 6, C = c.actualBoundingBoxAscent + c.actualBoundingBoxDescent + 8, L = c.fontBoundingBoxAscent ? 8 : 6.5;
1764
+ return i.fillRect(m - 2, l - 2.5 - C, b, C), i.fillStyle = "#fff", i.fillText(A.appearance.label, m + 1, l - L), {
1765
+ fill: A.appearance.color,
1766
+ fillOpacity: g ? 0.45 : 0.18
1767
+ };
1768
+ }
1769
+ },
1770
+ reset: () => {
1771
+ o.width = 2 * window.innerWidth, o.height = 2 * window.innerHeight;
1772
+ const d = o.getContext("2d");
1773
+ d.scale(2, 2), d.translate(0.5, 0.5);
1774
+ }
1775
+ };
1776
+ }, qt = (t) => {
1777
+ if (t === null)
1778
+ return document.scrollingElement;
1779
+ const { overflowY: e } = window.getComputedStyle(t);
1780
+ return e !== "visible" && e !== "hidden" && t.scrollHeight > t.clientHeight ? t : qt(t.parentElement);
1781
+ }, Nn = (t, e) => (n) => {
1782
+ const o = typeof n == "string" ? n : n.id, i = (s) => {
1783
+ const a = r.getBoundingClientRect(), u = r.clientHeight, h = r.clientWidth, p = s.selector[0].range.getBoundingClientRect(), { width: d, height: f } = e.getAnnotationBounds(o), g = p.top - a.top, A = p.left - a.left, y = r.parentElement ? r.scrollTop : 0, m = r.parentElement ? r.scrollLeft : 0, l = g + y - (u - f) / 2, c = A + m - (h - d) / 2;
1784
+ r.scroll({ top: l, left: c, behavior: "smooth" });
1785
+ }, r = qt(t);
1786
+ if (r) {
1787
+ const s = e.getAnnotation(o), { range: a } = s.target.selector[0];
1788
+ if (a && !a.collapsed)
1789
+ return i(s.target), !0;
1790
+ {
1791
+ const u = rt(s.target, t), { range: h } = u.selector[0];
1792
+ if (h && !h.collapsed)
1793
+ return i(u), !0;
1794
+ }
1208
1795
  }
1209
- return re(o);
1210
- }
1211
- const de = (e, t, n) => {
1212
- const o = document.createRange(), i = n ? e.startContainer.parentElement.closest(n) : t;
1213
- o.setStart(i, 0), o.setEnd(e.startContainer, e.startOffset);
1214
- const s = e.toString(), a = o.toString().length, r = a + s.length;
1215
- return n ? { quote: s, start: a, end: r, range: e, offsetReference: i } : { quote: s, start: a, end: r, range: e };
1216
- }, ue = (e, t, n) => {
1217
- const { store: o, selection: i } = t;
1218
- let s, a = null;
1219
- const r = (E) => s = E;
1220
- let d = !1, u;
1221
- e.addEventListener("selectstart", (E) => {
1222
- if (!d)
1223
- return;
1224
- !E.target.parentElement.closest(".not-annotatable") ? a = {
1225
- annotation: ce(),
1226
- selector: void 0,
1227
- creator: s,
1796
+ return !1;
1797
+ }, Mn = (t, e) => ({
1798
+ ...t,
1799
+ annotatingEnabled: t.annotatingEnabled ?? e.annotatingEnabled,
1800
+ user: t.user || e.user
1801
+ }), On = (t, e, n, o) => {
1802
+ let i;
1803
+ const r = (c) => i = c;
1804
+ let s;
1805
+ const a = (c) => s = c, { store: u, selection: h } = e;
1806
+ let p, d = !1, f;
1807
+ const g = (c) => {
1808
+ var C;
1809
+ if (!d) return;
1810
+ !((C = c.target.parentElement) != null && C.closest(X)) ? p = {
1811
+ annotation: Pt(),
1812
+ selector: [],
1813
+ creator: i,
1228
1814
  created: /* @__PURE__ */ new Date()
1229
- } : (a = null, u = void 0);
1230
- });
1231
- let b;
1232
- document.addEventListener("selectionchange", (E) => {
1233
- b && clearTimeout(b), b = setTimeout(() => h(), 50);
1234
- });
1235
- const h = () => {
1236
- var m, v;
1237
- const E = document.getSelection();
1238
- if (!E.isCollapsed && d && a) {
1239
- const C = Array.from(Array(E.rangeCount).keys()).map((c) => E.getRangeAt(c));
1240
- te(C[0]).toString() !== ((v = (m = a.selector) == null ? void 0 : m.range) == null ? void 0 : v.toString()) && (a = {
1241
- ...a,
1242
- selector: de(C[0], e, n)
1243
- }, o.getAnnotation(a.annotation) ? o.updateTarget(a, M.LOCAL) : (o.addAnnotation({
1244
- id: a.annotation,
1245
- bodies: [],
1246
- target: a
1247
- }), i.clickSelect(a.annotation, u)));
1815
+ } : p = void 0;
1816
+ };
1817
+ n && t.addEventListener("selectstart", g);
1818
+ const A = vt((c) => {
1819
+ var v, x;
1820
+ const b = document.getSelection();
1821
+ if (!!((x = (v = b.anchorNode) == null ? void 0 : v.parentElement) != null && x.closest(X))) {
1822
+ p = void 0;
1823
+ return;
1248
1824
  }
1825
+ if (c.timeStamp - ((f == null ? void 0 : f.timeStamp) || c.timeStamp) < 1e3 && !p && g(f), b.isCollapsed || !d || !p) return;
1826
+ const E = b.getRangeAt(0), R = he(E, t);
1827
+ if (ae(R)) return;
1828
+ const T = ee(R.cloneRange());
1829
+ (T.length !== p.selector.length || T.some((w, S) => {
1830
+ var M;
1831
+ return w.toString() !== ((M = p.selector[S]) == null ? void 0 : M.quote);
1832
+ })) && (p = {
1833
+ ...p,
1834
+ selector: T.map((w) => ue(w, t, o)),
1835
+ updated: /* @__PURE__ */ new Date()
1836
+ }, u.getAnnotation(p.annotation) ? u.updateTarget(p, N.LOCAL) : (h.clear(), u.addAnnotation({
1837
+ id: p.annotation,
1838
+ bodies: [],
1839
+ target: p
1840
+ }), h.userSelect(p.annotation, f)));
1841
+ });
1842
+ n && document.addEventListener("selectionchange", A);
1843
+ const y = (c) => {
1844
+ const { target: b, timeStamp: C, offsetX: L, offsetY: E, type: R } = c;
1845
+ f = { ...c, target: b, timeStamp: C, offsetX: L, offsetY: E, type: R }, d = c.button === 0;
1249
1846
  };
1250
- return e.addEventListener("pointerdown", (E) => {
1251
- u = E, d = E.button === 0;
1252
- }), document.addEventListener("pointerup", (E) => {
1253
- var v;
1254
- u = E, !((v = E.target.parentElement) != null && v.closest(".not-annotatable") || !d) && setTimeout(() => {
1255
- if (a != null && a.selector)
1256
- o.updateTarget(a, M.LOCAL), i.clickSelect(a.annotation, E), a = null, u = void 0;
1257
- else {
1258
- const { x: C, y: w } = e.getBoundingClientRect(), l = o.getAt(E.clientX - C, E.clientY - w);
1259
- if (l) {
1260
- const { selected: c } = i;
1261
- (c.length !== 1 || c[0].id !== l.id) && (i.clickSelect(l.id, E), u = void 0);
1262
- } else
1263
- i.isEmpty() || i.clear();
1264
- }
1265
- }, 50);
1266
- }), {
1847
+ document.addEventListener("pointerdown", y);
1848
+ const m = (c) => {
1849
+ var E;
1850
+ if (!!((E = c.target.parentElement) != null && E.closest(X)) || !d)
1851
+ return;
1852
+ const C = () => {
1853
+ const { x: R, y: T } = t.getBoundingClientRect(), O = c.target instanceof Node && t.contains(c.target) && u.getAt(c.clientX - R, c.clientY - T, s);
1854
+ if (O) {
1855
+ const { selected: v } = h;
1856
+ (v.length !== 1 || v[0].id !== O.id) && h.userSelect(O.id, c);
1857
+ } else h.isEmpty() || h.clear();
1858
+ }, L = c.timeStamp - f.timeStamp;
1859
+ document.getSelection().isCollapsed && L < 300 ? (p = void 0, C()) : p && h.userSelect(p.annotation, c);
1860
+ };
1861
+ return document.addEventListener("pointerup", m), {
1862
+ destroy: () => {
1863
+ t.removeEventListener("selectstart", g), document.removeEventListener("selectionchange", A), document.removeEventListener("pointerdown", y), document.removeEventListener("pointerup", m);
1864
+ },
1865
+ setFilter: a,
1267
1866
  setUser: r
1268
1867
  };
1269
- };
1270
- const he = (e, t = {}) => {
1271
- const n = ne(e, t.pointerAction), { selection: o, viewport: i } = n, s = n.store, a = Ut(s), r = Dt(
1272
- n,
1273
- a,
1274
- t.adapter
1275
- );
1276
- let d = zt();
1277
- const u = bt(e, n, i);
1278
- t.style && u.setDrawingStyle(t.style);
1279
- const b = ue(e, n, t.offsetReferenceSelector);
1280
- return b.setUser(d), {
1281
- ...Vt(n, a, t.adapter),
1868
+ }, _t = "SPANS", _n = (t, e = {}) => {
1869
+ ne(t);
1870
+ const n = Mn(e, {
1871
+ annotatingEnabled: !0,
1872
+ user: un()
1873
+ }), o = Rn(t, n.userSelectAction), { selection: i, viewport: r } = o, s = o.store, a = tn(s), u = nn(o, a, n.adapter);
1874
+ let h = n.user;
1875
+ const p = n.renderer === "CSS_HIGHLIGHTS" ? CSS.highlights ? "CSS_HIGHLIGHTS" : _t : n.renderer || _t, d = p === "SPANS" ? Me(t, o, r) : p === "CSS_HIGHLIGHTS" ? Te(t, o, r) : p === "CANVAS" ? ve(t, o, r) : void 0;
1876
+ if (!d)
1877
+ throw `Unknown renderer implementation: ${p}`;
1878
+ console.debug(`Using ${p} renderer`), n.style && d.setStyle(n.style);
1879
+ const f = On(t, o, n.annotatingEnabled, n.offsetReferenceSelector);
1880
+ return f.setUser(h), {
1881
+ ...rn(o, a, n.adapter),
1282
1882
  destroy: () => {
1283
- throw "Not implemented yet";
1883
+ d.destroy(), f.destroy(), a.destroy();
1884
+ },
1885
+ element: t,
1886
+ getUser: () => h,
1887
+ setFilter: (E) => {
1888
+ d.setFilter(E), f.setFilter(E);
1284
1889
  },
1285
- element: e,
1286
- getUser: () => d,
1287
- setFilter: (g) => u.setFilter(g),
1288
- setStyle: (g) => u.setDrawingStyle(g),
1289
- setUser: (g) => {
1290
- d = g, b.setUser(g);
1890
+ setStyle: (E) => d.setStyle(E),
1891
+ setUser: (E) => {
1892
+ h = E, f.setUser(E);
1291
1893
  },
1292
- setSelected: (g) => {
1293
- g ? o.setSelected(g) : o.clear();
1894
+ setSelected: (E) => {
1895
+ E ? i.setSelected(E) : i.clear();
1294
1896
  },
1295
- setPresenceProvider: (g) => {
1296
- g && (u.setPainter(oe(g, t.presence)), g.on("selectionChange", () => u.redraw()));
1897
+ setPresenceProvider: (E) => {
1898
+ E && (d.setPainter(Bn(t, E, n.presence)), E.on("selectionChange", () => d.redraw()));
1297
1899
  },
1298
- on: r.on,
1299
- off: r.off,
1300
- scrollIntoView: ie(e, s),
1301
- state: n
1900
+ setVisible: (E) => d.setVisible(E),
1901
+ on: u.on,
1902
+ off: u.off,
1903
+ scrollIntoView: Nn(t, s),
1904
+ state: o
1302
1905
  };
1303
1906
  };
1304
1907
  export {
1305
- bt as createHighlightLayer,
1306
- he as createTextAnnotator,
1307
- ne as createTextAnnotatorState,
1308
- nt as defaultPainter,
1309
- k as reviveTarget
1908
+ it as DEFAULT_SELECTED_STYLE,
1909
+ P as DEFAULT_STYLE,
1910
+ Dt as NOT_ANNOTATABLE_CLASS,
1911
+ X as NOT_ANNOTATABLE_SELECTOR,
1912
+ N as Origin,
1913
+ De as UserSelectAction,
1914
+ kn as W3CTextFormat,
1915
+ ne as cancelSingleClickEvents,
1916
+ Un as createBody,
1917
+ ve as createCanvasRenderer,
1918
+ Te as createHighlightsRenderer,
1919
+ Re as createRenderer,
1920
+ Me as createSpansRenderer,
1921
+ _n as createTextAnnotator,
1922
+ Rn as createTextAnnotatorState,
1923
+ vt as debounce,
1924
+ Mn as fillDefaults,
1925
+ In as getAnnotatableFragment,
1926
+ Vn as getClientRectsPonyfill,
1927
+ re as getQuoteContext,
1928
+ mt as getRangeAnnotatableContents,
1929
+ H as isRevived,
1930
+ ae as isWhitespaceOrEmpty,
1931
+ de as mergeClientRects,
1932
+ Jt as paint,
1933
+ bn as parseW3CTextAnnotation,
1934
+ ue as rangeToSelector,
1935
+ st as reviveAnnotation,
1936
+ Yt as reviveSelector,
1937
+ rt as reviveTarget,
1938
+ vn as serializeW3CTextAnnotation,
1939
+ ee as splitAnnotatableRanges,
1940
+ he as trimRangeToContainer,
1941
+ se as whitespaceOrEmptyRegex
1310
1942
  };
1311
1943
  //# sourceMappingURL=text-annotator.es.js.map