@recogito/text-annotator 3.0.0-rc.3 → 3.0.0-rc.30

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