@recogito/text-annotator 3.0.0-rc.8 → 3.0.0

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