@soomo/text-annotator 3.0.0-staging.1 → 3.0.0-staging.10

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