@recogito/text-annotator 3.0.0-rc.10 → 3.0.0-rc.12

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