@recogito/text-annotator 3.0.0-rc.18 → 3.0.0-rc.2

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