@recogito/text-annotator 3.0.0-rc.9 → 3.0.1

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