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

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