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

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