@soomo/text-annotator 3.4.2-staging.2 → 3.4.9-staging.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,482 +1,486 @@
1
- import { UAParser as ft } from "ua-parser-js";
2
- import { OS as ut } from "ua-parser-js/enums";
3
- import $ from "debounce";
4
- import { colord as Z } from "colord";
5
- import { dequal as gt } from "dequal/lite";
6
- import { v4 as st } from "uuid";
7
- import { serializeW3CBodies as mt, parseW3CBodies as pt, parseW3CUser as ht, createStore as bt, createSelectionState as yt, createHoverState as St, createViewportState as Ct, Origin as K, createAnonymousGuest as Et, createUndoStack as wt, createLifecycleObserver as At, createBaseAnnotator as vt } from "@annotorious/core";
8
- import { Origin as He, UserSelectAction as Pe, createBody as _e } from "@annotorious/core";
9
- import xt from "rbush";
10
- import { createNanoEvents as Tt } from "nanoevents";
11
- import Y from "hotkeys-js";
12
- import { poll as Rt } from "poll";
13
- const rt = "not-annotatable", W = `.${rt}`, z = (t, e) => {
14
- var r;
15
- return t.contains(e) ? !!(e instanceof HTMLElement ? e.closest(W) : (r = e.parentElement) == null ? void 0 : r.closest(W)) : !0;
16
- }, Lt = (t, e) => {
17
- const n = e.commonAncestorContainer;
18
- return !z(t, n);
19
- }, Nt = (t) => t.addEventListener("click", (e) => {
1
+ import { UAParser as be } from "ua-parser-js";
2
+ import { OS as Ee } from "ua-parser-js/enums";
3
+ import le from "debounce";
4
+ import { UserSelectAction as Ce, serializeW3CBodies as Se, parseW3CBodies as Ae, parseW3CUser as we, createStore as Re, createSelectionState as Te, createHoverState as ve, createViewportState as xe, Origin as K, createAnonymousGuest as Le, createUndoStack as Ne, createLifecycleObserver as Oe, createBaseAnnotator as Be } from "@annotorious/core";
5
+ import { Origin as Ft, UserSelectAction as Vt, createBody as Yt } from "@annotorious/core";
6
+ import { colord as ne } from "colord";
7
+ import { dequal as ke } from "dequal/lite";
8
+ import { v4 as de } from "uuid";
9
+ import Me from "rbush";
10
+ import { createNanoEvents as Ie } from "nanoevents";
11
+ import q from "hotkeys-js";
12
+ import { poll as _e } from "poll";
13
+ const ue = "not-annotatable", W = `.${ue}`, G = (e, t) => e.contains(t) ? !!(t instanceof HTMLElement ? t.closest(W) : t.parentElement?.closest(W)) : !0, He = (e, t) => {
14
+ const n = t.commonAncestorContainer;
15
+ return !G(e, n);
16
+ }, Pe = (e) => e.addEventListener("click", (t) => {
20
17
  // Allow clicks within not-annotatable elements
21
- !e.target.closest(W) && !e.target.closest("a") && e.preventDefault();
22
- }), G = (t) => ({
23
- ...t,
24
- type: t.type,
25
- x: t.x,
26
- y: t.y,
27
- clientX: t.clientX,
28
- clientY: t.clientY,
29
- offsetX: t.offsetX,
30
- offsetY: t.offsetY,
31
- screenX: t.screenX,
32
- screenY: t.screenY,
33
- isPrimary: t.isPrimary,
34
- altKey: t.altKey,
35
- ctrlKey: t.ctrlKey,
36
- metaKey: t.metaKey,
37
- shiftKey: t.shiftKey,
38
- button: t.button,
39
- buttons: t.buttons,
40
- currentTarget: t.currentTarget,
41
- target: t.target,
42
- defaultPrevented: t.defaultPrevented,
43
- detail: t.detail,
44
- eventPhase: t.eventPhase,
45
- pointerId: t.pointerId,
46
- pointerType: t.pointerType,
47
- timeStamp: t.timeStamp
48
- }), X = (t) => ({
49
- ...t,
50
- type: t.type,
51
- key: t.key,
52
- code: t.code,
53
- location: t.location,
54
- repeat: t.repeat,
55
- altKey: t.altKey,
56
- ctrlKey: t.ctrlKey,
57
- metaKey: t.metaKey,
58
- shiftKey: t.shiftKey,
59
- currentTarget: t.currentTarget,
60
- target: t.target,
61
- defaultPrevented: t.defaultPrevented,
62
- detail: t.detail,
63
- timeStamp: t.timeStamp
64
- }), Ot = ft(), kt = Ot.os.is(ut.MACOS), Bt = (t) => {
65
- !t.hasAttribute("tabindex") && t.tabIndex < 0 && t.setAttribute("tabindex", "-1"), t.classList.add("no-focus-outline");
66
- }, It = (t) => {
67
- const e = [], n = document.createNodeIterator(
68
- t.commonAncestorContainer,
18
+ !t.target.closest(W) && !t.target.closest("a") && t.preventDefault();
19
+ }), J = (e) => ({
20
+ ...e,
21
+ type: e.type,
22
+ x: e.x,
23
+ y: e.y,
24
+ clientX: e.clientX,
25
+ clientY: e.clientY,
26
+ offsetX: e.offsetX,
27
+ offsetY: e.offsetY,
28
+ screenX: e.screenX,
29
+ screenY: e.screenY,
30
+ isPrimary: e.isPrimary,
31
+ altKey: e.altKey,
32
+ ctrlKey: e.ctrlKey,
33
+ metaKey: e.metaKey,
34
+ shiftKey: e.shiftKey,
35
+ button: e.button,
36
+ buttons: e.buttons,
37
+ currentTarget: e.currentTarget,
38
+ target: e.target,
39
+ defaultPrevented: e.defaultPrevented,
40
+ detail: e.detail,
41
+ eventPhase: e.eventPhase,
42
+ pointerId: e.pointerId,
43
+ pointerType: e.pointerType,
44
+ timeStamp: e.timeStamp
45
+ }), j = (e) => ({
46
+ ...e,
47
+ type: e.type,
48
+ key: e.key,
49
+ code: e.code,
50
+ location: e.location,
51
+ repeat: e.repeat,
52
+ altKey: e.altKey,
53
+ ctrlKey: e.ctrlKey,
54
+ metaKey: e.metaKey,
55
+ shiftKey: e.shiftKey,
56
+ currentTarget: e.currentTarget,
57
+ target: e.target,
58
+ defaultPrevented: e.defaultPrevented,
59
+ detail: e.detail,
60
+ timeStamp: e.timeStamp
61
+ }), ee = (e, t = 10) => {
62
+ let n;
63
+ return ((...r) => {
64
+ clearTimeout(n), n = setTimeout(() => e.apply(void 0, r), t);
65
+ });
66
+ }, De = be(), fe = De.os.is(Ee.MACOS), Ue = (e) => {
67
+ (!e.hasAttribute("tabindex") || e.tabIndex < -1) && e.setAttribute("tabindex", "-1"), e.classList.add("no-focus-outline");
68
+ }, Ke = (e) => {
69
+ const t = [], n = document.createNodeIterator(
70
+ e.commonAncestorContainer,
69
71
  NodeFilter.SHOW_TEXT
70
72
  );
71
73
  let r;
72
74
  for (; r = n.nextNode(); )
73
- t.intersectsNode(r) && e.push(r);
74
- if (e.length < 2)
75
- return Array.from(t.getClientRects());
75
+ e.intersectsNode(r) && t.push(r);
76
+ if (t.length < 2)
77
+ return Array.from(e.getClientRects());
76
78
  {
77
- const o = e[0], s = e[e.length - 1], i = document.createRange();
78
- if (i.selectNode(o), t.startContainer.nodeType === Node.TEXT_NODE)
79
- i.setStart(o, t.startOffset);
79
+ const s = t[0], o = t[t.length - 1], i = document.createRange();
80
+ if (i.selectNode(s), e.startContainer.nodeType === Node.TEXT_NODE)
81
+ i.setStart(s, e.startOffset);
80
82
  else {
81
- const f = t.startContainer.childNodes[t.startOffset];
82
- f ? i.setStartBefore(f) : i.setStartAfter(t.startContainer);
83
+ const g = e.startContainer.childNodes[e.startOffset];
84
+ g ? i.setStartBefore(g) : i.setStartAfter(e.startContainer);
83
85
  }
84
86
  const c = document.createRange();
85
- if (c.selectNode(s), t.endContainer.nodeType === Node.TEXT_NODE)
86
- c.setEnd(s, t.endOffset);
87
+ if (c.selectNode(o), e.endContainer.nodeType === Node.TEXT_NODE)
88
+ c.setEnd(o, e.endOffset);
87
89
  else {
88
- const f = t.endContainer.childNodes[t.endOffset];
89
- f ? c.setEndBefore(f) : c.setEndAfter(t.endContainer);
90
+ const g = e.endContainer.childNodes[e.endOffset];
91
+ g ? c.setEndBefore(g) : c.setEndAfter(e.endContainer);
90
92
  }
91
- const l = (f) => {
92
- const p = document.createRange();
93
- return p.selectNode(f), Array.from(p.getClientRects());
93
+ const l = (g) => {
94
+ const d = document.createRange();
95
+ return d.selectNode(g), Array.from(d.getClientRects());
94
96
  };
95
97
  return [
96
98
  ...Array.from(i.getClientRects()),
97
- ...e.slice(1, -1).flatMap(l),
99
+ ...t.slice(1, -1).flatMap(l),
98
100
  ...Array.from(c.getClientRects())
99
101
  ];
100
102
  }
101
- }, Mt = function* (t) {
102
- const e = document.createNodeIterator(
103
- t.commonAncestorContainer,
103
+ }, We = function* (e) {
104
+ const t = document.createNodeIterator(
105
+ e.commonAncestorContainer,
104
106
  NodeFilter.SHOW_ELEMENT,
105
- (r) => r instanceof HTMLElement && r.classList.contains(rt) && !r.parentElement.closest(W) && t.intersectsNode(r) ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP
107
+ (r) => r instanceof HTMLElement && r.classList.contains(ue) && !r.parentElement.closest(W) && e.intersectsNode(r) ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP
106
108
  );
107
109
  let n;
108
- for (; n = e.nextNode(); )
110
+ for (; n = t.nextNode(); )
109
111
  n instanceof HTMLElement && (yield n);
110
- }, Ht = (t, e) => {
111
- if (!Lt(t, e)) return [];
112
+ }, $e = (e, t) => {
113
+ if (!He(e, t)) return [];
112
114
  const n = [];
113
115
  let r = null;
114
- for (const o of Mt(e)) {
115
- let s;
116
- r ? (s = document.createRange(), s.setStartAfter(r), s.setEndBefore(o)) : (s = e.cloneRange(), s.setEndBefore(o)), s.collapsed || n.push(s), r = o;
116
+ for (const s of We(t)) {
117
+ let o;
118
+ r ? (o = document.createRange(), o.setStartAfter(r), o.setEndBefore(s)) : (o = t.cloneRange(), o.setEndBefore(s)), o.collapsed || n.push(o), r = s;
117
119
  }
118
120
  if (r) {
119
- const o = e.cloneRange();
120
- o.setStartAfter(r), o.collapsed || n.push(o);
121
+ const s = t.cloneRange();
122
+ s.setStartAfter(r), s.collapsed || n.push(s);
121
123
  }
122
- return n.length > 0 ? n : [e];
123
- }, J = (t) => {
124
- const e = t.cloneContents();
125
- return e.querySelectorAll(W).forEach((n) => n.remove()), e;
126
- }, Pt = (t, e, n = 10, r) => {
127
- const o = r ? t.startContainer.parentElement.closest(r) : e, s = document.createRange();
128
- s.setStart(o, 0), s.setEnd(t.startContainer, t.startOffset);
129
- const i = J(s).textContent, c = document.createRange();
130
- c.setStart(t.endContainer, t.endOffset), o === document.body ? c.setEnd(o, o.childNodes.length) : c.setEndAfter(o);
131
- const l = J(c).textContent;
124
+ return n.length > 0 ? n : [t];
125
+ }, oe = (e) => {
126
+ const t = e.cloneContents();
127
+ return t.querySelectorAll(W).forEach((n) => n.remove()), t;
128
+ }, Fe = (e, t, n = 10, r) => {
129
+ const s = r ? e.startContainer.parentElement.closest(r) : t, o = document.createRange();
130
+ o.setStart(s, 0), o.setEnd(e.startContainer, e.startOffset);
131
+ const i = oe(o).textContent, c = document.createRange();
132
+ c.setStart(e.endContainer, e.endOffset), s === document.body ? c.setEnd(s, s.childNodes.length) : c.setEndAfter(s);
133
+ const l = oe(c).textContent;
132
134
  return {
133
135
  prefix: i.substring(i.length - n),
134
136
  suffix: l.substring(0, n)
135
137
  };
136
- }, P = (t) => t.every((e) => e.range instanceof Range && !e.range.collapsed), it = /^\s*$/, _t = (t) => it.test(t.toString()), Le = (t) => it.test(t.textContent || ""), Dt = (t, e, n, r) => {
137
- const o = (c) => Math.round(c * 10) / 10, s = {
138
- top: o(t.top),
139
- bottom: o(t.bottom),
140
- left: o(t.left),
141
- right: o(t.right)
138
+ }, I = (e) => e.every((t) => t.range instanceof Range && !t.range.collapsed), ge = /^\s*$/, Ve = (e) => ge.test(e.toString()), Ht = (e) => ge.test(e.textContent || ""), Ye = (e, t, n, r) => {
139
+ const s = (c) => Math.round(c * 10) / 10, o = {
140
+ top: s(e.top),
141
+ bottom: s(e.bottom),
142
+ left: s(e.left),
143
+ right: s(e.right)
142
144
  }, i = {
143
- top: o(e.top),
144
- bottom: o(e.bottom),
145
- left: o(e.left),
146
- right: o(e.right)
145
+ top: s(t.top),
146
+ bottom: s(t.bottom),
147
+ left: s(t.left),
148
+ right: s(t.right)
147
149
  };
148
- if (Math.abs(s.top - i.top) < r && Math.abs(s.bottom - i.bottom) < r) {
149
- if (Math.abs(s.left - i.right) < n || Math.abs(s.right - i.left) < n)
150
+ if (Math.abs(o.top - i.top) < r && Math.abs(o.bottom - i.bottom) < r) {
151
+ if (Math.abs(o.left - i.right) < n || Math.abs(o.right - i.left) < n)
150
152
  return "inline-adjacent";
151
- if (s.left >= i.left && s.right <= i.right)
153
+ if (o.left >= i.left && o.right <= i.right)
152
154
  return "inline-is-contained";
153
- if (s.left <= i.left && s.right >= i.right)
155
+ if (o.left <= i.left && o.right >= i.right)
154
156
  return "inline-contains";
155
- } else if (s.top <= i.top && s.bottom >= i.bottom) {
156
- if (s.left <= i.left && s.right >= i.right)
157
+ } else if (o.top <= i.top && o.bottom >= i.bottom) {
158
+ if (o.left <= i.left && o.right >= i.right)
157
159
  return "block-contains";
158
- } else if (s.top >= i.top && s.bottom <= i.bottom && s.left >= i.left && s.right <= i.right)
160
+ } else if (o.top >= i.top && o.bottom <= i.bottom && o.left >= i.left && o.right <= i.right)
159
161
  return "block-is-contained";
160
- }, Ut = (t, e) => {
161
- const n = Math.min(t.left, e.left), r = Math.max(t.right, e.right), o = Math.min(t.top, e.top), s = Math.max(t.bottom, e.bottom);
162
- return new DOMRect(n, o, r - n, s - o);
163
- }, Ft = (t, e = 0.5, n = 0.5) => t.reduce((r, o) => {
164
- if (o.width === 0 || o.height === 0)
162
+ }, Xe = (e, t) => {
163
+ const n = Math.min(e.left, t.left), r = Math.max(e.right, t.right), s = Math.min(e.top, t.top), o = Math.max(e.bottom, t.bottom);
164
+ return new DOMRect(n, s, r - n, o - s);
165
+ }, ze = (e, t = 0.5, n = 0.5) => e.reduce((r, s) => {
166
+ if (s.width === 0 || s.height === 0)
165
167
  return r;
166
- let s = [...r], i = !1;
168
+ let o = [...r], i = !1;
167
169
  for (const c of r) {
168
- const l = Dt(o, c, e, n);
170
+ const l = Ye(s, c, t, n);
169
171
  if (l === "inline-adjacent") {
170
- s = s.map((f) => f === c ? Ut(o, c) : f), i = !0;
172
+ o = o.map((g) => g === c ? Xe(s, c) : g), i = !0;
171
173
  break;
172
174
  } else if (l === "inline-contains") {
173
- s = s.map((f) => f === c ? o : f), i = !0;
175
+ o = o.map((g) => g === c ? s : g), i = !0;
174
176
  break;
175
177
  } else if (l === "inline-is-contained") {
176
178
  i = !0;
177
179
  break;
178
180
  } else if (l === "block-contains" || l === "block-is-contained") {
179
- o.width < c.width && (s = s.map((f) => f === c ? o : f)), o.width === c.width && o.height < c.width && (s = s.map((f) => f === c ? o : f)), i = !0;
181
+ s.width < c.width && (o = o.map((g) => g === c ? s : g)), s.width === c.width && s.height < c.width && (o = o.map((g) => g === c ? s : g)), i = !0;
180
182
  break;
181
183
  }
182
184
  }
183
- return i ? s : [...s, o];
184
- }, []), Ne = (t) => ({
185
- length: t.length,
186
- item: (e) => t[e],
185
+ return i ? o : [...o, s];
186
+ }, []), Pt = (e) => ({
187
+ length: e.length,
188
+ item: (t) => e[t],
187
189
  [Symbol.iterator]: function* () {
188
- for (let e = 0; e < this.length; e++)
189
- yield this.item(e);
190
+ for (let t = 0; t < this.length; t++)
191
+ yield this.item(t);
190
192
  }
191
- }), Kt = (t, e, n) => {
192
- const r = document.createRange(), o = n ? t.startContainer.parentElement.closest(n) : e;
193
- r.setStart(o, 0), r.setEnd(t.startContainer, t.startOffset);
194
- const s = J(r).textContent, i = t.toString(), c = s.length || 0, l = c + i.length;
195
- return n ? { quote: i, start: c, end: l, range: t, offsetReference: o } : { quote: i, start: c, end: l, range: t };
196
- }, $t = (t, e) => {
197
- const { left: n, top: r, right: o, bottom: s } = t;
198
- return new DOMRect(n - e.left, r - e.top, o - n, s - r);
199
- }, Oe = (t, e) => {
200
- const { left: n, top: r, right: o, bottom: s } = t;
201
- return new DOMRect(n + e.left, r + e.top, o - n, s - r);
202
- }, at = (t, e) => {
203
- var p, u;
204
- const { start: n, end: r } = t, o = t.offsetReference || e, s = document.createNodeIterator(
205
- e,
193
+ }), qe = (e) => {
194
+ if (e.length === 0) return [];
195
+ if (e.length === 1) return [e[0]];
196
+ e.sort((s, o) => {
197
+ const i = s.compareBoundaryPoints(Range.START_TO_START, o);
198
+ return i !== 0 ? i : s.compareBoundaryPoints(Range.END_TO_END, o);
199
+ });
200
+ const [t, ...n] = e, r = n.reduce((s, o) => s.current.compareBoundaryPoints(Range.START_TO_END, o) >= 0 ? (s.current.compareBoundaryPoints(Range.END_TO_END, o) < 0 && s.current.setEnd(o.endContainer, o.endOffset), s) : (s.merged.push(s.current), s.current = o.cloneRange(), s), { merged: [], current: t.cloneRange() });
201
+ return [...r.merged, r.current];
202
+ }, je = (e, t, n) => {
203
+ const r = document.createRange(), s = n ? e.startContainer.parentElement.closest(n) : t;
204
+ r.setStart(s, 0), r.setEnd(e.startContainer, e.startOffset);
205
+ const o = oe(r).textContent, i = e.toString(), c = o.length || 0, l = c + i.length;
206
+ return n ? { quote: i, start: c, end: l, range: e, offsetReference: s } : { quote: i, start: c, end: l, range: e };
207
+ }, Ge = (e, t) => {
208
+ const { left: n, top: r, right: s, bottom: o } = e;
209
+ return new DOMRect(n - t.left, r - t.top, s - n, o - r);
210
+ }, Dt = (e, t) => {
211
+ const { left: n, top: r, right: s, bottom: o } = e;
212
+ return new DOMRect(n + t.left, r + t.top, s - n, o - r);
213
+ }, me = (e, t) => {
214
+ const { start: n, end: r } = e, s = e.offsetReference || t, o = document.createNodeIterator(
215
+ t,
206
216
  NodeFilter.SHOW_TEXT,
207
- (m) => {
208
- var d;
209
- return (d = m.parentElement) != null && d.closest(W) ? NodeFilter.FILTER_SKIP : NodeFilter.FILTER_ACCEPT;
210
- }
217
+ (d) => d.parentElement?.closest(W) ? NodeFilter.FILTER_SKIP : NodeFilter.FILTER_ACCEPT
211
218
  );
212
219
  let i = 0;
213
220
  const c = document.createRange();
214
- let l = s.nextNode();
221
+ let l = o.nextNode();
215
222
  l === null && console.error("Could not revive annotation target. Content missing.");
216
- let f = !o;
223
+ let g = !s;
217
224
  for (; l !== null; ) {
218
- if (f || (f = typeof (o == null ? void 0 : o.contains) == "function" ? o.contains(l) : !1), f) {
219
- const m = ((p = l.textContent) == null ? void 0 : p.length) || 0;
220
- if (i + m > n) {
225
+ if (g ||= typeof s?.contains == "function" ? s.contains(l) : !1, g) {
226
+ const d = l.textContent?.length || 0;
227
+ if (i + d > n) {
221
228
  c.setStart(l, n - i);
222
229
  break;
223
230
  }
224
- i += m;
231
+ i += d;
225
232
  }
226
- l = s.nextNode();
233
+ l = o.nextNode();
227
234
  }
228
235
  for (; l !== null; ) {
229
- const m = ((u = l.textContent) == null ? void 0 : u.length) || 0;
230
- if (i + m >= r) {
236
+ const d = l.textContent?.length || 0;
237
+ if (i + d >= r) {
231
238
  c.setEnd(l, r - i);
232
239
  break;
233
240
  }
234
- i += m, l = s.nextNode();
241
+ i += d, l = o.nextNode();
235
242
  }
236
243
  return {
237
- ...t,
244
+ ...e,
238
245
  range: c
239
246
  };
240
- }, q = (t, e) => P(t.selector) ? t : {
241
- ...t,
242
- selector: t.selector.map((n) => n.range instanceof Range && !n.range.collapsed ? n : at(n, e))
243
- }, Q = (t, e) => P(t.target.selector) ? t : { ...t, target: q(t.target, e) }, Wt = (t, e) => {
244
- const n = t.cloneRange();
245
- return e.contains(n.startContainer) || n.setStart(e, 0), e.contains(n.endContainer) || n.setEnd(e, e.childNodes.length), n;
246
- }, ct = (t) => {
247
- if (!t)
247
+ }, Q = (e, t) => I(e.selector) ? e : {
248
+ ...e,
249
+ selector: e.selector.map((n) => n.range instanceof Range && !n.range.collapsed ? n : me(n, t))
250
+ }, te = (e, t) => I(e.target.selector) ? e : { ...e, target: Q(e.target, t) }, Qe = (e, t) => {
251
+ const n = e.cloneRange();
252
+ return t.contains(n.startContainer) || n.setStart(t, 0), t.contains(n.endContainer) || n.setEnd(t, t.childNodes.length), n;
253
+ }, pe = (e) => {
254
+ if (!e)
248
255
  return document.scrollingElement;
249
- const { overflowY: e } = window.getComputedStyle(t);
250
- return e !== "visible" && e !== "hidden" && t.scrollHeight > t.clientHeight ? t : ct(t.parentElement);
251
- }, et = (t, e, n) => {
252
- const r = n.getBoundingClientRect(), o = n.clientHeight, s = n.clientWidth, i = e.selector[0].range.getBoundingClientRect(), { width: c, height: l } = t.getAnnotationBounds(e.annotation), f = i.top - r.top, p = i.left - r.left, u = n.parentElement ? n.scrollTop : 0, m = n.parentElement ? n.scrollLeft : 0, d = f + u - (o - l) / 2, L = p + m - (s - c) / 2;
253
- n.scroll({ top: d, left: L, behavior: "smooth" });
254
- }, Vt = (t, e) => (n, r) => {
255
- const o = typeof n == "string" ? n : n.id, s = r ? typeof r == "string" ? document.getElementById(r) : r : ct(t);
256
- if (!s)
257
- return console.warn(`The scroll parent is missing for the annotation: ${o}`, { container: t }), !1;
258
- const i = e.getAnnotation(o);
256
+ const { overflowY: t } = window.getComputedStyle(e);
257
+ return t !== "visible" && t !== "hidden" && e.scrollHeight > e.clientHeight ? e : pe(e.parentElement);
258
+ }, ie = (e, t, n) => {
259
+ const r = n.getBoundingClientRect(), s = n.clientHeight, o = n.clientWidth, i = t.selector[0].range.getBoundingClientRect(), { width: c, height: l } = e.getAnnotationBounds(t.annotation), g = i.top - r.top, d = i.left - r.left, u = n.parentElement ? n.scrollTop : 0, y = n.parentElement ? n.scrollLeft : 0, w = g + u - (s - l) / 2, f = d + y - (o - c) / 2;
260
+ n.scroll({ top: w, left: f, behavior: "smooth" });
261
+ }, Ze = (e, t) => (n, r) => {
262
+ const s = typeof n == "string" ? n : n.id, o = r ? typeof r == "string" ? document.getElementById(r) : r : pe(e);
263
+ if (!o)
264
+ return console.warn(`The scroll parent is missing for the annotation: ${s}`, { container: e }), !1;
265
+ const i = t.getAnnotation(s);
259
266
  if (!i)
260
- return console.warn(`The annotation is missing in the store: ${o}`), !1;
267
+ return console.warn(`The annotation is missing in the store: ${s}`), !1;
261
268
  const { range: c } = i.target.selector[0];
262
269
  if (c && !c.collapsed)
263
- return et(e, i.target, s), !0;
264
- const l = q(i.target, t), { range: f } = l.selector[0];
265
- return f && !f.collapsed ? (et(e, l, s), !0) : !1;
270
+ return ie(t, i.target, o), !0;
271
+ const l = Q(i.target, e), { range: g } = l.selector[0];
272
+ return g && !g.collapsed ? (ie(t, l, o), !0) : !1;
266
273
  }, _ = {
267
274
  fill: "rgb(0, 128, 255)",
268
275
  fillOpacity: 0.18
269
- }, j = {
276
+ }, Z = {
270
277
  fill: "rgb(0, 128, 255)",
271
278
  fillOpacity: 0.45
272
- }, Yt = (t, e, n, r, o) => {
273
- var i, c;
274
- const s = n ? typeof n == "function" ? n(t.annotation, t.state, o) || ((i = t.state) != null && i.selected ? j : _) : n : (c = t.state) != null && c.selected ? j : _;
275
- return r && r.paint(t, e) || s;
276
- }, Xt = (t) => {
277
- const { top: e, left: n } = t.getBoundingClientRect(), { innerWidth: r, innerHeight: o } = window, s = -n, i = -e, c = r - n, l = o - e;
278
- return { top: e, left: n, minX: s, minY: i, maxX: c, maxY: l };
279
- }, zt = (t) => {
280
- let e = /* @__PURE__ */ new Set();
279
+ }, Je = (e) => {
280
+ const { top: t, left: n } = e.getBoundingClientRect(), { innerWidth: r, innerHeight: s } = window, o = -n, i = -t, c = r - n, l = s - t;
281
+ return { top: t, left: n, minX: o, minY: i, maxX: c, maxY: l };
282
+ }, et = (e) => {
283
+ let t = /* @__PURE__ */ new Set();
281
284
  return (r) => {
282
- const o = r.map((s) => s.id);
283
- (e.size !== o.length || o.some((s) => !e.has(s))) && t.set(o), e = new Set(o);
285
+ const s = r.map((o) => o.id);
286
+ (t.size !== s.length || s.some((o) => !t.has(o))) && e.set(s), t = new Set(s);
284
287
  };
285
- }, tt = (t, e, n, r) => {
286
- const { store: o, selection: s, hover: i } = e;
287
- let c, l, f;
288
- const p = zt(n), u = $((S) => {
289
- const { x: A, y: T } = t.getBoundingClientRect(), B = o.getAt(S.clientX - A, S.clientY - T, !1, l);
290
- B ? i.current !== B.id && (t.classList.add("hovered"), i.set(B.id)) : i.current && (t.classList.remove("hovered"), i.set(null));
291
- }, 10);
292
- t.addEventListener("pointermove", u);
293
- const m = $((S = !1) => requestAnimationFrame(() => {
294
- f && f.clear();
295
- const A = Xt(t), { minX: T, minY: B, maxX: y, maxY: v } = A, I = l ? o.getIntersecting(T, B, y, v).filter(({ annotation: O }) => l(O)) : o.getIntersecting(T, B, y, v), D = s.selected.map(({ id: O }) => O), M = I.map(({ annotation: O, rects: H }) => {
296
- const k = D.includes(O.id), U = O.id === i.current;
288
+ }, se = (e, t, n, r) => {
289
+ const { store: s, selection: o, hover: i } = t;
290
+ let c, l, g;
291
+ const d = et(n), u = (A) => {
292
+ const { x: v, y: N } = e.getBoundingClientRect(), O = s.getAt(A.clientX - v, A.clientY - N, !1, l);
293
+ O && t.selection.evalSelectAction(O) !== Ce.NONE ? i.current !== O.id && (e.classList.add("hovered"), i.set(O.id)) : i.current && (e.classList.remove("hovered"), i.set(null));
294
+ };
295
+ e.addEventListener("pointermove", u);
296
+ const y = ee((A = !1) => requestAnimationFrame(() => {
297
+ g && g.clear();
298
+ const v = Je(e), { minX: N, minY: O, maxX: D, maxY: Y } = v, X = l ? s.getIntersecting(N, O, D, Y).filter(({ annotation: h }) => l(h)) : s.getIntersecting(N, O, D, Y), $ = o.selected.map(({ id: h }) => h), re = X.map(({ annotation: h, rects: T }) => {
299
+ const k = $.includes(h.id), U = h.id === i.current;
297
300
  return {
298
- annotation: O,
299
- rects: H,
301
+ annotation: h,
302
+ rects: T,
300
303
  state: { selected: k, hovered: U }
301
304
  };
302
305
  });
303
- r.redraw(M, A, c, f, S), setTimeout(() => p(I.map(({ annotation: O }) => O)), 1);
304
- }), 10), d = (S) => {
305
- f = S, m();
306
- }, L = (S) => {
307
- c = S, m();
308
- }, w = (S) => {
309
- l = S, m(!1);
310
- }, x = () => m();
311
- o.observe(x);
312
- const R = s.subscribe(() => m()), C = i.subscribe(() => m()), N = () => m(!0);
313
- document.addEventListener("scroll", N, { capture: !0, passive: !0 });
314
- const E = $(() => {
315
- o.recalculatePositions(), f == null || f.reset(), m();
306
+ r.redraw(re, v, c, g, A), setTimeout(() => d(X.map(({ annotation: h }) => h)), 1);
307
+ }), 10), w = (A) => {
308
+ g = A, y();
309
+ }, f = (A) => {
310
+ c = A, y();
311
+ }, x = (A) => {
312
+ l = A, y(!1);
313
+ }, R = () => y();
314
+ s.observe(R);
315
+ const b = o.subscribe(() => y()), p = i.subscribe(() => y()), S = () => y(!0);
316
+ document.addEventListener("scroll", S, { capture: !0, passive: !0 });
317
+ const E = ee(() => {
318
+ s.recalculatePositions(), g?.reset(), y();
316
319
  }, 10);
317
320
  window.addEventListener("resize", E);
318
- const a = new ResizeObserver(E);
319
- a.observe(t);
320
- const g = { attributes: !0, childList: !0, subtree: !0 }, b = new MutationObserver($((S) => {
321
- S.every((T) => T.target === t || t.contains(T.target)) || m(!0);
321
+ const m = new ResizeObserver(E);
322
+ m.observe(e);
323
+ const a = { attributes: !0, childList: !0, subtree: !0 }, C = new MutationObserver(ee((A) => {
324
+ A.every((N) => N.target === e || e.contains(N.target)) || y(!0);
322
325
  }, 150));
323
- return b.observe(document.body, g), {
326
+ return C.observe(document.body, a), {
324
327
  destroy: () => {
325
- t.removeEventListener("pointermove", u), r.destroy(), o.unobserve(x), R(), C(), document.removeEventListener("scroll", N), E.clear(), window.removeEventListener("resize", E), a.disconnect(), b.disconnect();
328
+ e.removeEventListener("pointermove", u), r.destroy(), s.unobserve(R), b(), p(), document.removeEventListener("scroll", S), window.removeEventListener("resize", E), m.disconnect(), C.disconnect();
326
329
  },
327
- redraw: m,
328
- setStyle: L,
329
- setFilter: w,
330
- setPainter: d,
330
+ redraw: y,
331
+ setStyle: f,
332
+ setFilter: x,
333
+ setPainter: w,
331
334
  setVisible: r.setVisible
332
335
  };
333
- }, qt = () => {
334
- const t = document.createElement("canvas");
335
- return t.width = window.innerWidth, t.height = window.innerHeight, t.className = "r6o-canvas-highlight-layer bg", t;
336
- }, jt = (t, e) => {
337
- t.width = window.innerWidth, t.height = window.innerHeight;
338
- }, Gt = (t) => {
339
- t.classList.add("r6o-annotatable");
340
- const e = qt(), n = e.getContext("2d");
341
- document.body.appendChild(e);
342
- const r = (c, l, f, p) => requestAnimationFrame(() => {
343
- const { width: u, height: m } = e;
344
- n.clearRect(-0.5, -0.5, u + 1, m + 1), p && p.clear();
345
- const { top: d, left: L } = l;
346
- [...c].sort((x, R) => {
347
- const { annotation: { target: { created: C } } } = x, { annotation: { target: { created: N } } } = R;
348
- return C.getTime() - N.getTime();
349
- }).forEach((x) => {
350
- var E;
351
- const R = f ? typeof f == "function" ? f(x.annotation, x.state) : f : (E = x.state) != null && E.selected ? j : _, C = p && p.paint(x, l) || R, N = x.rects.map(({ x: a, y: g, width: b, height: h }) => ({
352
- x: a + L,
353
- y: g + d,
354
- width: b,
355
- height: h
336
+ }, tt = () => {
337
+ const e = document.createElement("canvas");
338
+ return e.width = window.innerWidth, e.height = window.innerHeight, e.className = "r6o-canvas-highlight-layer bg", e;
339
+ }, nt = (e, t) => {
340
+ e.width = window.innerWidth, e.height = window.innerHeight;
341
+ }, ot = (e) => {
342
+ console.warn('WARNING! Canvas renderer is deprecated and will be removed in the next major release. Please use the default "SPANS" renderer instead.'), e.classList.add("r6o-annotatable");
343
+ const t = tt(), n = t.getContext("2d");
344
+ document.body.appendChild(t);
345
+ const r = (c, l, g, d) => requestAnimationFrame(() => {
346
+ const { width: u, height: y } = t;
347
+ n.clearRect(-0.5, -0.5, u + 1, y + 1), d && d.clear();
348
+ const { top: w, left: f } = l;
349
+ [...c].sort((R, b) => {
350
+ const { annotation: { target: { created: p } } } = R, { annotation: { target: { created: S } } } = b;
351
+ return p.getTime() - S.getTime();
352
+ }).forEach((R) => {
353
+ const b = g ? typeof g == "function" ? g(R.annotation, R.state) : g : R.state?.selected ? Z : _, p = d && d.paint(R, l) || b, S = R.rects.map(({ x: E, y: m, width: a, height: C }) => ({
354
+ x: E + f,
355
+ y: m + w,
356
+ width: a,
357
+ height: C
356
358
  }));
357
- if (n.fillStyle = C.fill, n.globalAlpha = C.fillOpacity || 1, N.forEach(
358
- ({ x: a, y: g, width: b, height: h }) => n.fillRect(a, g, b, h)
359
- ), C.underlineColor) {
360
- n.globalAlpha = 1, n.strokeStyle = C.underlineColor, n.lineWidth = C.underlineThickness ?? 1;
361
- const a = C.underlineOffset ?? 0;
362
- N.forEach(({ x: g, y: b, width: h, height: S }) => {
363
- n.beginPath(), n.moveTo(g, b + S + a), n.lineTo(g + h, b + S + a), n.stroke();
359
+ if (n.fillStyle = p.fill, n.globalAlpha = p.fillOpacity || 1, S.forEach(
360
+ ({ x: E, y: m, width: a, height: C }) => n.fillRect(E, m, a, C)
361
+ ), p.underlineColor) {
362
+ n.globalAlpha = 1, n.strokeStyle = p.underlineColor, n.lineWidth = p.underlineThickness ?? 1;
363
+ const E = p.underlineOffset ?? 0;
364
+ S.forEach(({ x: m, y: a, width: C, height: L }) => {
365
+ n.beginPath(), n.moveTo(m, a + L + E), n.lineTo(m + C, a + L + E), n.stroke();
364
366
  });
365
367
  }
366
368
  });
367
- }), o = $(() => jt(e), 10);
368
- return window.addEventListener("resize", o), {
369
+ }), s = le(() => nt(t), 10);
370
+ return window.addEventListener("resize", s), {
369
371
  destroy: () => {
370
- e.remove(), o.clear(), window.removeEventListener("resize", o);
372
+ t.remove(), s.clear(), window.removeEventListener("resize", s);
371
373
  },
372
374
  setVisible: (c) => {
373
375
  console.log("setVisible not implemented on Canvas renderer");
374
376
  },
375
377
  redraw: r
376
378
  };
377
- }, Qt = (t, e, n) => tt(t, e, n, Gt(t)), Zt = (t) => [
378
- `background-color:${Z((t == null ? void 0 : t.fill) || _.fill).alpha((t == null ? void 0 : t.fillOpacity) === void 0 ? _.fillOpacity : t.fillOpacity).toHex()}`,
379
- t != null && t.underlineThickness ? "text-decoration:underline" : void 0,
380
- t != null && t.underlineColor ? `text-decoration-color:${t.underlineColor}` : void 0,
381
- t != null && t.underlineOffset ? `text-underline-offset:${t.underlineOffset}px` : void 0,
382
- t != null && t.underlineThickness ? `text-decoration-thickness:${t.underlineThickness}px` : void 0
383
- ].filter(Boolean).join(";"), Jt = () => {
384
- const t = document.createElement("style");
385
- document.getElementsByTagName("head")[0].appendChild(t);
386
- let e = /* @__PURE__ */ new Set();
379
+ }, st = (e, t, n) => se(e, t, n, ot(e)), rt = (e) => [
380
+ `background-color:${ne(e?.fill || _.fill).alpha(e?.fillOpacity === void 0 ? _.fillOpacity : e.fillOpacity).toHex()}`,
381
+ e?.underlineThickness ? "text-decoration:underline" : void 0,
382
+ e?.underlineColor ? `text-decoration-color:${e.underlineColor}` : void 0,
383
+ e?.underlineOffset ? `text-underline-offset:${e.underlineOffset}px` : void 0,
384
+ e?.underlineThickness ? `text-decoration-thickness:${e.underlineThickness}px` : void 0
385
+ ].filter(Boolean).join(";"), it = () => {
386
+ const e = document.createElement("style");
387
+ document.getElementsByTagName("head")[0].appendChild(e);
388
+ let t = /* @__PURE__ */ new Set();
387
389
  return {
388
390
  destroy: () => {
389
- CSS.highlights.clear(), t.remove();
391
+ CSS.highlights.clear(), e.remove();
390
392
  },
391
- setVisible: (s) => {
393
+ setVisible: (o) => {
392
394
  console.log("setVisible not implemented on CSS Custom Highlights renderer");
393
395
  },
394
- redraw: (s, i, c, l) => {
396
+ redraw: (o, i, c, l) => {
395
397
  l && l.clear();
396
- const f = new Set(s.map((u) => u.annotation.id));
397
- Array.from(e).filter((u) => !f.has(u));
398
- const p = s.map((u) => {
399
- var L;
400
- const m = c ? typeof c == "function" ? c(u.annotation, u.state) : c : (L = u.state) != null && L.selected ? j : _, d = l && l.paint(u, i) || m;
401
- return `::highlight(_${u.annotation.id}) { ${Zt(d)} }`;
398
+ const g = new Set(o.map((u) => u.annotation.id));
399
+ Array.from(t).filter((u) => !g.has(u));
400
+ const d = o.map((u) => {
401
+ const y = c ? typeof c == "function" ? c(u.annotation, u.state) : c : u.state?.selected ? Z : _, w = l && l.paint(u, i) || y;
402
+ return `::highlight(_${u.annotation.id}) { ${rt(w)} }`;
402
403
  });
403
- t.innerHTML = p.join(`
404
- `), CSS.highlights.clear(), s.forEach(({ annotation: u }) => {
405
- const m = u.target.selector.map((L) => L.range), d = new Highlight(...m);
406
- CSS.highlights.set(`_${u.id}`, d);
407
- }), e = f;
404
+ e.innerHTML = d.join(`
405
+ `), CSS.highlights.clear(), o.forEach(({ annotation: u }) => {
406
+ const y = u.target.selector.map((f) => f.range), w = new Highlight(...y);
407
+ CSS.highlights.set(`_${u.id}`, w);
408
+ }), t = g;
408
409
  }
409
410
  };
410
- }, te = (t, e, n) => tt(t, e, n, Jt()), ee = (t) => (t == null ? void 0 : t.fillOpacity) !== void 0 ? Z((t == null ? void 0 : t.fill) || _.fill).alpha(t.fillOpacity).toHex() : t != null && t.fill ? t.fill : Z(_.fill).alpha(_.fillOpacity).toHex(), ne = (t, e) => {
411
- const n = (s, i) => s.x <= i.x + i.width && s.x + s.width >= i.x && s.y <= i.y + i.height && s.y + s.height >= i.y, r = (s) => s.rects.reduce((i, c) => i + c.width, 0), o = e.filter(({ rects: s }) => s.some((i) => n(t, i)));
412
- return o.sort((s, i) => r(i) - r(s)), o.findIndex((s) => s.rects.includes(t));
413
- }, oe = (t) => {
414
- t.classList.add("r6o-annotatable");
415
- const e = document.createElement("div");
416
- e.className = "r6o-span-highlight-layer", t.insertBefore(e, t.firstChild);
411
+ }, at = (e, t, n) => se(e, t, n, it()), ct = (e) => e?.fillOpacity !== void 0 ? ne(e?.fill || _.fill).alpha(e.fillOpacity).toHex() : e?.fill ? e.fill : ne(_.fill).alpha(_.fillOpacity).toHex(), lt = (e, t, n, r, s) => {
412
+ const o = n ? typeof n == "function" ? n(e.annotation, e.state, s) || (e.state?.selected ? Z : _) : n : e.state?.selected ? Z : _;
413
+ return r && r.paint(e, t) || o;
414
+ }, dt = (e, t) => {
415
+ const n = (o, i) => o.x <= i.x + i.width && o.x + o.width >= i.x && o.y <= i.y + i.height && o.y + o.height >= i.y, r = (o) => o.rects.reduce((i, c) => i + c.width, 0), s = t.filter(({ rects: o }) => o.some((i) => n(e, i)));
416
+ return s.sort((o, i) => r(i) - r(o)), s.findIndex((o) => o.rects.includes(e));
417
+ }, ut = (e) => {
418
+ e.classList.add("r6o-annotatable");
419
+ const t = document.createElement("div");
420
+ t.className = "r6o-span-highlight-layer", e.insertBefore(t, e.firstChild);
417
421
  let n = [];
418
422
  return {
419
423
  destroy: () => {
420
- e.remove();
424
+ t.remove();
421
425
  },
422
- redraw: (i, c, l, f, p) => {
423
- const m = !(gt(n, i) && p);
424
- if (!f && !m) return;
425
- m && (e.innerHTML = ""), [...i].sort((L, w) => {
426
- const { annotation: { target: { created: x } } } = L, { annotation: { target: { created: R } } } = w;
427
- return x && R ? x.getTime() - R.getTime() : 0;
428
- }).forEach((L) => {
429
- L.rects.map((w) => {
430
- const x = ne(w, i), R = Yt(L, c, l, f, x);
431
- if (m) {
432
- const C = document.createElement("span");
433
- C.className = "r6o-annotation", C.dataset.annotation = L.annotation.id, C.style.left = `${w.x}px`, C.style.top = `${w.y}px`, C.style.width = `${w.width}px`, C.style.height = `${w.height}px`, C.style.backgroundColor = ee(R), R.underlineStyle && (C.style.borderStyle = R.underlineStyle), R.underlineColor && (C.style.borderColor = R.underlineColor), R.underlineThickness && (C.style.borderBottomWidth = `${R.underlineThickness}px`), R.underlineOffset && (C.style.paddingBottom = `${R.underlineOffset}px`), e.appendChild(C);
426
+ redraw: (i, c, l, g, d) => {
427
+ const y = !(ke(n, i) && d);
428
+ if (!g && !y) return;
429
+ y && (t.innerHTML = ""), [...i].sort((f, x) => {
430
+ const { annotation: { target: { created: R } } } = f, { annotation: { target: { created: b } } } = x;
431
+ return R && b ? R.getTime() - b.getTime() : 0;
432
+ }).forEach((f) => {
433
+ f.rects.map((x) => {
434
+ const R = dt(x, i), b = lt(f, c, l, g, R);
435
+ if (y) {
436
+ const p = document.createElement("span");
437
+ p.className = "r6o-annotation", p.dataset.annotation = f.annotation.id, p.style.left = `${x.x}px`, p.style.top = `${x.y}px`, p.style.width = `${x.width}px`, p.style.height = `${x.height}px`, p.style.backgroundColor = ct(b), b.underlineStyle && (p.style.borderStyle = b.underlineStyle), b.underlineColor && (p.style.borderColor = b.underlineColor), b.underlineThickness && (p.style.borderBottomWidth = `${b.underlineThickness}px`), b.underlineOffset && (p.style.paddingBottom = `${b.underlineOffset}px`), t.appendChild(p);
434
438
  }
435
439
  });
436
440
  }), n = i;
437
441
  },
438
442
  setVisible: (i) => {
439
- i ? e.classList.remove("hidden") : e.classList.add("hidden");
443
+ i ? t.classList.remove("hidden") : t.classList.add("hidden");
440
444
  }
441
445
  };
442
- }, se = (t, e, n) => tt(t, e, n, oe(t)), ke = (t, e) => ({
443
- parse: (n) => ae(n),
444
- serialize: (n) => ce(n, t, e)
445
- }), re = (t) => t.quote !== void 0 && t.start !== void 0 && t.end !== void 0, ie = (t) => {
446
+ }, ft = (e, t, n) => se(e, t, n, ut(e)), Ut = (e, t) => ({
447
+ parse: (n) => pt(n),
448
+ serialize: (n) => ht(n, e, t)
449
+ }), gt = (e) => e.quote !== void 0 && e.start !== void 0 && e.end !== void 0, mt = (e) => {
446
450
  const {
447
- id: e,
451
+ id: t,
448
452
  creator: n,
449
453
  created: r,
450
- modified: o,
451
- target: s
452
- } = t, i = Array.isArray(s) ? s : [s];
454
+ modified: s,
455
+ target: o
456
+ } = e, i = Array.isArray(o) ? o : [o];
453
457
  if (i.length === 0)
454
- return { error: Error(`No targets found for annotation: ${t.id}`) };
458
+ return { error: Error(`No targets found for annotation: ${e.id}`) };
455
459
  const c = {
456
- creator: ht(n),
460
+ creator: we(n),
457
461
  created: r ? new Date(r) : void 0,
458
- updated: o ? new Date(o) : void 0,
459
- annotation: e,
462
+ updated: s ? new Date(s) : void 0,
463
+ annotation: t,
460
464
  selector: [],
461
465
  // @ts-expect-error: `styleClass` is not part of the core `TextAnnotationTarget` type
462
466
  styleClass: "styleClass" in i[0] ? i[0].styleClass : void 0
463
467
  };
464
468
  for (const l of i) {
465
- const p = (Array.isArray(l.selector) ? l.selector : [l.selector]).reduce((u, m) => {
466
- switch (m.type) {
469
+ const d = (Array.isArray(l.selector) ? l.selector : [l.selector]).reduce((u, y) => {
470
+ switch (y.type) {
467
471
  case "TextQuoteSelector":
468
- u.quote = m.exact;
472
+ u.quote = y.exact;
469
473
  break;
470
474
  case "TextPositionSelector":
471
- u.start = m.start, u.end = m.end;
475
+ u.start = y.start, u.end = y.end;
472
476
  break;
473
477
  }
474
478
  return u;
475
479
  }, {});
476
- if (c.outdated || (c.outdated = "outdated" in l ? l.outdated : void 0), re(p))
480
+ if (c.outdated ||= "outdated" in l ? l.outdated : void 0, gt(d))
477
481
  c.selector.push(
478
482
  {
479
- ...p,
483
+ ...d,
480
484
  id: l.id,
481
485
  // @ts-expect-error: `scope` is not part of the core `TextSelector` type
482
486
  scope: l.scope
@@ -484,465 +488,498 @@ const rt = "not-annotatable", W = `.${rt}`, z = (t, e) => {
484
488
  );
485
489
  else {
486
490
  const u = [
487
- p.start ? void 0 : "TextPositionSelector",
488
- p.quote ? void 0 : "TextQuoteSelector"
491
+ d.start ? void 0 : "TextPositionSelector",
492
+ d.quote ? void 0 : "TextQuoteSelector"
489
493
  ].filter(Boolean);
490
- return { error: Error(`Missing selector types: ${u.join(" and ")} for annotation: ${t.id}`) };
494
+ return { error: Error(`Missing selector types: ${u.join(" and ")} for annotation: ${e.id}`) };
491
495
  }
492
496
  }
493
497
  return { parsed: c };
494
- }, ae = (t) => {
495
- const e = t.id || st(), {
498
+ }, pt = (e) => {
499
+ const t = e.id || de(), {
496
500
  creator: n,
497
501
  created: r,
498
- modified: o,
499
- body: s,
502
+ modified: s,
503
+ body: o,
500
504
  ...i
501
- } = t, c = pt(s, e), l = ie(t);
505
+ } = e, c = Ae(o, t), l = mt(e);
502
506
  return "error" in l ? { error: l.error } : {
503
507
  parsed: {
504
508
  ...i,
505
- id: e,
509
+ id: t,
506
510
  bodies: c,
507
511
  target: l.parsed
508
512
  }
509
513
  };
510
- }, ce = (t, e, n) => {
511
- const { bodies: r, target: o, ...s } = t, {
514
+ }, ht = (e, t, n) => {
515
+ const { bodies: r, target: s, ...o } = e, {
512
516
  selector: i,
513
517
  creator: c,
514
518
  created: l,
515
- updated: f,
516
- ...p
517
- } = o, u = i.map((m) => {
518
- const { id: d, quote: L, start: w, end: x, range: R } = m, C = {
519
+ updated: g,
520
+ ...d
521
+ } = s, u = i.map((y) => {
522
+ const { id: w, quote: f, start: x, end: R, range: b } = y, p = {
519
523
  type: "TextQuoteSelector",
520
- exact: L
524
+ exact: f
521
525
  };
522
526
  if (n) {
523
- const { prefix: E, suffix: a } = Pt(R, n);
524
- C.prefix = E, C.suffix = a;
527
+ const { prefix: E, suffix: m } = Fe(b, n);
528
+ p.prefix = E, p.suffix = m;
525
529
  }
526
- const N = {
530
+ const S = {
527
531
  type: "TextPositionSelector",
528
- start: w,
529
- end: x
532
+ start: x,
533
+ end: R
530
534
  };
531
535
  return {
532
- ...p,
533
- id: d,
536
+ ...d,
537
+ id: w,
534
538
  // @ts-expect-error: `scope` is not part of the core `TextSelector` type
535
- scope: "scope" in m ? m.scope : void 0,
536
- source: e,
537
- selector: [C, N]
539
+ scope: "scope" in y ? y.scope : void 0,
540
+ source: t,
541
+ selector: [p, S]
538
542
  };
539
543
  });
540
544
  return {
541
- ...s,
545
+ ...o,
542
546
  "@context": "http://www.w3.org/ns/anno.jsonld",
543
- id: t.id,
547
+ id: e.id,
544
548
  type: "Annotation",
545
- body: mt(t.bodies),
549
+ body: Se(e.bodies),
546
550
  creator: c,
547
- created: l == null ? void 0 : l.toISOString(),
548
- modified: f == null ? void 0 : f.toISOString(),
551
+ created: l?.toISOString(),
552
+ modified: g?.toISOString(),
549
553
  target: u
550
554
  };
551
- }, le = (t, e, n, r) => {
552
- const o = new xt(), s = /* @__PURE__ */ new Map(), i = Tt(), c = (a, g) => {
553
- const b = a.selector.flatMap((S) => {
554
- const A = P([S]) ? S.range : at(S, e).range;
555
- return It(A);
556
- }), h = Ft(b, n, r).map((S) => $t(S, g));
557
- return h.map((S) => {
558
- const { x: A, y: T, width: B, height: y } = S;
555
+ }, yt = () => {
556
+ const e = document.createElement("canvas");
557
+ e.width = 2 * window.innerWidth, e.height = 2 * window.innerHeight, e.className = "r6o-presence-layer";
558
+ const t = e.getContext("2d");
559
+ return t.scale(2, 2), t.translate(0.5, 0.5), e;
560
+ }, bt = (e, t = {}) => {
561
+ const n = yt(), r = n.getContext("2d");
562
+ document.body.appendChild(n);
563
+ const s = /* @__PURE__ */ new Map(), o = (d) => Array.from(s.entries()).filter(([u, y]) => y.presenceKey === d.presenceKey).map(([u, y]) => u);
564
+ return e.on("selectionChange", (d, u) => {
565
+ o(d).forEach((w) => s.delete(w)), u && u.forEach((w) => s.set(w, d));
566
+ }), {
567
+ clear: () => {
568
+ const { width: d, height: u } = n;
569
+ r.clearRect(-0.5, -0.5, d + 1, u + 1);
570
+ },
571
+ destroy: () => {
572
+ n.remove();
573
+ },
574
+ paint: (d, u, y) => {
575
+ t.font && (r.font = t.font);
576
+ const w = s.get(d.annotation.id);
577
+ if (w) {
578
+ const { height: f } = d.rects[0], x = d.rects[0].x + u.left, R = d.rects[0].y + u.top;
579
+ r.fillStyle = w.appearance.color, r.fillRect(x - 2, R - 2.5, 2, f + 5);
580
+ const b = r.measureText(w.appearance.label), p = b.width + 6, S = b.actualBoundingBoxAscent + b.actualBoundingBoxDescent + 8, E = b.fontBoundingBoxAscent ? 8 : 6.5;
581
+ return r.fillRect(x - 2, R - 2.5 - S, p, S), r.fillStyle = "#fff", r.fillText(w.appearance.label, x + 1, R - E), {
582
+ fill: w.appearance.color,
583
+ fillOpacity: y ? 0.45 : 0.18
584
+ };
585
+ }
586
+ },
587
+ reset: () => {
588
+ n.width = 2 * window.innerWidth, n.height = 2 * window.innerHeight;
589
+ const d = n.getContext("2d");
590
+ d.scale(2, 2), d.translate(0.5, 0.5);
591
+ }
592
+ };
593
+ }, Et = (e, t, n, r) => {
594
+ const s = new Me(), o = /* @__PURE__ */ new Map(), i = Ie(), c = (m, a) => {
595
+ const C = m.selector.flatMap((A) => {
596
+ const v = I([A]) ? A.range : me(A, t).range;
597
+ return Ke(v);
598
+ }), L = ze(C, n, r).map((A) => Ge(A, a));
599
+ return L.map((A) => {
600
+ const { x: v, y: N, width: O, height: D } = A;
559
601
  return {
560
- minX: A,
561
- minY: T,
562
- maxX: A + B,
563
- maxY: T + y,
602
+ minX: v,
603
+ minY: N,
604
+ maxX: v + O,
605
+ maxY: N + D,
564
606
  annotation: {
565
- id: a.annotation,
566
- rects: h
607
+ id: m.annotation,
608
+ rects: L
567
609
  }
568
610
  };
569
611
  });
570
- }, l = () => [...s.values()], f = () => {
571
- o.clear(), s.clear();
572
- }, p = (a) => {
573
- const g = c(a, e.getBoundingClientRect());
574
- g.length !== 0 && (g.forEach((b) => o.insert(b)), s.set(a.annotation, g));
575
- }, u = (a) => {
576
- const g = s.get(a.annotation);
577
- g && (g.forEach((b) => o.remove(b)), s.delete(a.annotation));
578
- }, m = (a) => {
579
- u(a), p(a);
580
- }, d = (a, g = !0) => {
581
- g && f();
582
- const b = e.getBoundingClientRect(), h = a.map((A) => ({ target: A, rects: c(A, b) }));
583
- h.forEach(({ target: A, rects: T }) => {
584
- T.length > 0 && s.set(A.annotation, T);
612
+ }, l = () => [...o.values()], g = () => {
613
+ s.clear(), o.clear();
614
+ }, d = (m) => {
615
+ const a = c(m, t.getBoundingClientRect());
616
+ a.length !== 0 && (a.forEach((C) => s.insert(C)), o.set(m.annotation, a));
617
+ }, u = (m) => {
618
+ const a = o.get(m.annotation);
619
+ a && (a.forEach((C) => s.remove(C)), o.delete(m.annotation));
620
+ }, y = (m) => {
621
+ u(m), d(m);
622
+ }, w = (m, a = !0) => {
623
+ a && g();
624
+ const C = t.getBoundingClientRect(), L = m.map((v) => ({ target: v, rects: c(v, C) }));
625
+ L.forEach(({ target: v, rects: N }) => {
626
+ N.length > 0 && o.set(v.annotation, N);
585
627
  });
586
- const S = h.flatMap(({ rects: A }) => A);
587
- o.load(S);
588
- }, L = (a, g, b = !1) => {
589
- const h = o.search({
590
- minX: a,
591
- minY: g,
592
- maxX: a,
593
- maxY: g
594
- }), S = (A) => A.annotation.rects.reduce((T, B) => T + B.width * B.height, 0);
595
- return h.length > 0 ? (h.sort((A, T) => S(A) - S(T)), b ? h.map((A) => A.annotation.id) : [h[0].annotation.id]) : [];
596
- }, w = (a) => {
597
- const g = x(a);
598
- if (g.length === 0)
628
+ const A = L.flatMap(({ rects: v }) => v);
629
+ s.load(A);
630
+ }, f = (m, a, C = !1) => {
631
+ const L = s.search({
632
+ minX: m,
633
+ minY: a,
634
+ maxX: m,
635
+ maxY: a
636
+ }), A = (v) => v.annotation.rects.reduce((N, O) => N + O.width * O.height, 0);
637
+ return L.length > 0 ? (L.sort((v, N) => A(v) - A(N)), C ? L.map((v) => v.annotation.id) : [L[0].annotation.id]) : [];
638
+ }, x = (m) => {
639
+ const a = R(m);
640
+ if (a.length === 0)
599
641
  return;
600
- let b = g[0].left, h = g[0].top, S = g[0].right, A = g[0].bottom;
601
- for (let T = 1; T < g.length; T++) {
602
- const B = g[T];
603
- b = Math.min(b, B.left), h = Math.min(h, B.top), S = Math.max(S, B.right), A = Math.max(A, B.bottom);
642
+ let C = a[0].left, L = a[0].top, A = a[0].right, v = a[0].bottom;
643
+ for (let N = 1; N < a.length; N++) {
644
+ const O = a[N];
645
+ C = Math.min(C, O.left), L = Math.min(L, O.top), A = Math.max(A, O.right), v = Math.max(v, O.bottom);
604
646
  }
605
- return new DOMRect(b, h, S - b, A - h);
606
- }, x = (a) => {
607
- const g = s.get(a);
608
- return g ? g[0].annotation.rects : [];
647
+ return new DOMRect(C, L, A - C, v - L);
648
+ }, R = (m) => {
649
+ const a = o.get(m);
650
+ return a ? a[0].annotation.rects : [];
609
651
  };
610
652
  return {
611
653
  all: l,
612
- clear: f,
613
- getAt: L,
614
- getAnnotationBounds: w,
615
- getAnnotationRects: x,
616
- getIntersecting: (a, g, b, h) => {
617
- const S = o.search({ minX: a, minY: g, maxX: b, maxY: h }), A = new Set(S.map((T) => T.annotation.id));
618
- return Array.from(A).map((T) => ({
619
- annotation: t.getAnnotation(T),
620
- rects: x(T)
621
- })).filter((T) => !!T.annotation);
654
+ clear: g,
655
+ getAt: f,
656
+ getAnnotationBounds: x,
657
+ getAnnotationRects: R,
658
+ getIntersecting: (m, a, C, L) => {
659
+ const A = s.search({ minX: m, minY: a, maxX: C, maxY: L }), v = new Set(A.map((N) => N.annotation.id));
660
+ return Array.from(v).map((N) => ({
661
+ annotation: e.getAnnotation(N),
662
+ rects: R(N)
663
+ })).filter((N) => !!N.annotation);
622
664
  },
623
- insert: p,
665
+ insert: d,
624
666
  recalculate: () => {
625
- d(t.all().map((a) => a.target), !0), i.emit("recalculate");
667
+ w(e.all().map((m) => m.target), !0), i.emit("recalculate");
626
668
  },
627
669
  remove: u,
628
- set: d,
629
- size: () => o.all().length,
630
- update: m,
631
- on: (a, g) => i.on(a, g)
670
+ set: w,
671
+ size: () => s.all().length,
672
+ update: y,
673
+ on: (m, a) => i.on(m, a)
632
674
  };
633
- }, de = (t, e) => {
634
- var C, N;
635
- const n = bt(), r = le(n, t, (C = e.mergeHighlights) == null ? void 0 : C.horizontalTolerance, (N = e.mergeHighlights) == null ? void 0 : N.verticalTolerance), o = yt(n, e.userSelectAction, e.adapter), s = St(n), i = Ct(), c = (E, a = K.LOCAL) => {
636
- const g = Q(E, t), b = P(g.target.selector);
637
- return b && n.addAnnotation(g, a), b;
638
- }, l = (E, a = !0, g = K.LOCAL) => {
639
- const b = E.map((S) => Q(S, t)), h = b.filter((S) => !P(S.target.selector));
640
- return n.bulkAddAnnotations(b, a, g), h;
641
- }, f = (E, a = K.LOCAL) => {
642
- const g = E.map((h) => Q(h, t)), b = g.filter((h) => !P(h.target.selector));
643
- return g.forEach((h) => {
644
- n.getAnnotation(h.id) ? n.updateAnnotation(h, a) : n.addAnnotation(h, a);
645
- }), b;
646
- }, p = (E, a = K.LOCAL) => {
647
- const g = q(E, t);
648
- n.updateTarget(g, a);
649
- }, u = (E, a = K.LOCAL) => {
650
- const g = E.map((b) => q(b, t));
651
- n.bulkUpdateTargets(g, a);
675
+ }, Ct = (e, t) => {
676
+ const n = Re(), r = Et(n, e, t.mergeHighlights?.horizontalTolerance, t.mergeHighlights?.verticalTolerance), s = Te(n, t.userSelectAction, t.adapter), o = ve(n), i = xe(), c = (p, S = K.LOCAL) => {
677
+ const E = te(p, e), m = I(E.target.selector);
678
+ return m && n.addAnnotation(E, S), m;
679
+ }, l = (p, S = !0, E = K.LOCAL) => {
680
+ const m = p.map((C) => te(C, e)), a = m.filter((C) => !I(C.target.selector));
681
+ return n.bulkAddAnnotations(m, S, E), a;
682
+ }, g = (p, S = K.LOCAL) => {
683
+ const E = p.map((a) => te(a, e)), m = E.filter((a) => !I(a.target.selector));
684
+ return E.forEach((a) => {
685
+ n.getAnnotation(a.id) ? n.updateAnnotation(a, S) : n.addAnnotation(a, S);
686
+ }), m;
687
+ }, d = (p, S = K.LOCAL) => {
688
+ const E = Q(p, e);
689
+ n.updateTarget(E, S);
690
+ }, u = (p, S = K.LOCAL) => {
691
+ const E = p.map((m) => Q(m, e));
692
+ n.bulkUpdateTargets(E, S);
652
693
  };
653
- function m(E, a, g, b) {
654
- const h = g || !!b, S = r.getAt(E, a, h).map((T) => n.getAnnotation(T)), A = b ? S.filter(b) : S;
655
- if (A.length !== 0)
656
- return g ? A : A[0];
694
+ function y(p, S, E, m) {
695
+ const a = E || !!m, C = r.getAt(p, S, a).map((A) => n.getAnnotation(A)), L = m ? C.filter(m) : C;
696
+ if (L.length !== 0)
697
+ return E ? L : L[0];
657
698
  }
658
- const d = (E) => r.getAnnotationRects(E).length > 0 ? r.getAnnotationBounds(E) : void 0, L = (E, a, g, b) => r.getIntersecting(E, a, g, b), w = (E) => r.getAnnotationRects(E), x = () => r.recalculate(), R = (E) => r.on("recalculate", E);
659
- return n.observe(({ changes: E }) => {
660
- const a = (E.deleted || []).filter((h) => P(h.target.selector)), g = (E.created || []).filter((h) => P(h.target.selector)), b = (E.updated || []).filter((h) => P(h.newValue.target.selector));
661
- (a == null ? void 0 : a.length) > 0 && a.forEach((h) => r.remove(h.target)), g.length > 0 && r.set(g.map((h) => h.target), !1), (b == null ? void 0 : b.length) > 0 && b.forEach(({ newValue: h }) => r.update(h.target));
699
+ const w = (p) => r.getAnnotationRects(p).length > 0 ? r.getAnnotationBounds(p) : void 0, f = (p, S, E, m) => r.getIntersecting(p, S, E, m), x = (p) => r.getAnnotationRects(p), R = () => r.recalculate(), b = (p) => r.on("recalculate", p);
700
+ return n.observe(({ changes: p }) => {
701
+ const S = (p.deleted || []).filter((a) => I(a.target.selector)), E = (p.created || []).filter((a) => I(a.target.selector)), m = (p.updated || []).filter((a) => I(a.newValue.target.selector));
702
+ S?.length > 0 && S.forEach((a) => r.remove(a.target)), E.length > 0 && r.set(E.map((a) => a.target), !1), m?.length > 0 && m.forEach(({ newValue: a }) => r.update(a.target));
662
703
  }), {
663
704
  store: {
664
705
  ...n,
665
706
  addAnnotation: c,
666
707
  bulkAddAnnotations: l,
667
708
  bulkUpdateTargets: u,
668
- bulkUpsertAnnotations: f,
669
- getAnnotationBounds: d,
670
- getAnnotationRects: w,
671
- getIntersecting: L,
672
- getAt: m,
673
- recalculatePositions: x,
674
- onRecalculatePositions: R,
675
- updateTarget: p
709
+ bulkUpsertAnnotations: g,
710
+ getAnnotationBounds: w,
711
+ getAnnotationRects: x,
712
+ getIntersecting: f,
713
+ getAt: y,
714
+ recalculatePositions: R,
715
+ onRecalculatePositions: b,
716
+ updateTarget: d
676
717
  },
677
- selection: o,
678
- hover: s,
718
+ selection: s,
719
+ hover: o,
679
720
  viewport: i
680
721
  };
681
- }, fe = () => {
682
- const t = document.createElement("canvas");
683
- t.width = 2 * window.innerWidth, t.height = 2 * window.innerHeight, t.className = "r6o-presence-layer";
684
- const e = t.getContext("2d");
685
- return e.scale(2, 2), e.translate(0.5, 0.5), t;
686
- }, ue = (t, e = {}) => {
687
- const n = fe(), r = n.getContext("2d");
688
- document.body.appendChild(n);
689
- const o = /* @__PURE__ */ new Map(), s = (p) => Array.from(o.entries()).filter(([u, m]) => m.presenceKey === p.presenceKey).map(([u, m]) => u);
690
- return t.on("selectionChange", (p, u) => {
691
- s(p).forEach((d) => o.delete(d)), u && u.forEach((d) => o.set(d, p));
692
- }), {
693
- clear: () => {
694
- const { width: p, height: u } = n;
695
- r.clearRect(-0.5, -0.5, p + 1, u + 1);
696
- },
697
- destroy: () => {
698
- n.remove();
699
- },
700
- paint: (p, u, m) => {
701
- e.font && (r.font = e.font);
702
- const d = o.get(p.annotation.id);
703
- if (d) {
704
- const { height: L } = p.rects[0], w = p.rects[0].x + u.left, x = p.rects[0].y + u.top;
705
- r.fillStyle = d.appearance.color, r.fillRect(w - 2, x - 2.5, 2, L + 5);
706
- const R = r.measureText(d.appearance.label), C = R.width + 6, N = R.actualBoundingBoxAscent + R.actualBoundingBoxDescent + 8, E = R.fontBoundingBoxAscent ? 8 : 6.5;
707
- return r.fillRect(w - 2, x - 2.5 - N, C, N), r.fillStyle = "#fff", r.fillText(d.appearance.label, w + 1, x - E), {
708
- fill: d.appearance.color,
709
- fillOpacity: m ? 0.45 : 0.18
722
+ }, ae = 300, he = ["up", "down", "left", "right"], ye = fe ? "⌘+a" : "ctrl+a", St = [
723
+ ...he.map((e) => `shift+${e}`),
724
+ ye
725
+ ], At = (e, t, n, r) => {
726
+ const { store: s, selection: o } = t;
727
+ let i;
728
+ const {
729
+ annotatingEnabled: c,
730
+ offsetReferenceSelector: l,
731
+ selectionMode: g,
732
+ dismissOnNotAnnotatable: d = "NEVER"
733
+ } = r;
734
+ let u, y = c, w = "CREATE_NEW", f, x, R, b;
735
+ const p = (h) => {
736
+ y = h, L.clear(), h || (x = void 0, f = void 0, R = void 0, b = void 0);
737
+ }, S = (h) => w = h || "CREATE_NEW", E = (h) => u = h, m = (h) => i = h, a = (h) => {
738
+ if (w === "ADD_TO_CURRENT")
739
+ return !0;
740
+ if (r.allowModifierSelect) {
741
+ const T = h;
742
+ return fe ? T.metaKey : T.ctrlKey;
743
+ } else
744
+ return !1;
745
+ }, C = () => {
746
+ if (!y || R === !1) return;
747
+ const { selected: h } = o;
748
+ if ((a(b) || w === "REPLACE_CURRENT") && h.length === 1 && h[0].editable) {
749
+ const k = s.getAnnotation(h[0].id);
750
+ if (k?.target) {
751
+ x = k.target, f = {
752
+ annotation: k.id,
753
+ selector: [],
754
+ created: x.created,
755
+ creator: x.creator,
756
+ updated: /* @__PURE__ */ new Date(),
757
+ updatedBy: i
710
758
  };
759
+ return;
711
760
  }
712
- },
713
- reset: () => {
714
- n.width = 2 * window.innerWidth, n.height = 2 * window.innerHeight;
715
- const p = n.getContext("2d");
716
- p.scale(2, 2), p.translate(0.5, 0.5);
717
761
  }
718
- };
719
- }, nt = 300, lt = ["up", "down", "left", "right"], dt = kt ? "⌘+a" : "ctrl+a", ge = [
720
- ...lt.map((t) => `shift+${t}`),
721
- dt
722
- ], me = (t, e, n) => {
723
- const { store: r, selection: o } = e;
724
- let s;
725
- const {
726
- annotatingEnabled: i,
727
- offsetReferenceSelector: c,
728
- selectionMode: l,
729
- dismissOnNotAnnotatable: f = "NEVER"
730
- } = n, p = (y) => s = y;
731
- let u;
732
- const m = (y) => u = y;
733
- let d, L, w, x = i;
734
- const R = (y) => {
735
- x = y, N.clear(), y || (d = void 0, L = void 0, w = void 0);
736
- }, C = () => {
737
- x && L !== !1 && (d = {
738
- annotation: st(),
762
+ x = void 0, f = {
763
+ annotation: de(),
739
764
  selector: [],
740
- creator: s,
741
- created: /* @__PURE__ */ new Date()
742
- });
743
- }, N = $((y) => {
744
- if (!x) return;
745
- const v = document.getSelection();
746
- if (!(v != null && v.anchorNode))
747
- return;
748
- const I = Array.from(Array(v.rangeCount).keys()).map((k) => v.getRangeAt(k));
749
- if (!I.some((k) => k.intersectsNode(t))) {
750
- d = void 0;
765
+ created: /* @__PURE__ */ new Date(),
766
+ creator: i
767
+ };
768
+ }, L = le((h) => {
769
+ if (!y) return;
770
+ const T = document.getSelection();
771
+ if (!T?.anchorNode) return;
772
+ const k = Array.from(Array(T.rangeCount).keys()).map((B) => T.getRangeAt(B));
773
+ if (!k.some((B) => B.intersectsNode(e))) {
774
+ f = void 0;
751
775
  return;
752
776
  }
753
- const D = y.timeStamp - ((w == null ? void 0 : w.timeStamp) || y.timeStamp);
754
- if ((w == null ? void 0 : w.type) === "pointerdown" && (D < 1e3 && !d || v.isCollapsed && D < nt) && C(), d || C(), v.isCollapsed) {
755
- r.getAnnotation(d.annotation) && (o.clear(), r.deleteAnnotation(d.annotation));
777
+ const U = h.timeStamp - (b?.timeStamp || h.timeStamp);
778
+ if (b?.type === "pointerdown" && (U < 1e3 && !f || T.isCollapsed && U < ae) && C(), !f && (C(), !f))
779
+ return;
780
+ if (T.isCollapsed) {
781
+ s.getAnnotation(f.annotation) && !(a(b) || w === "REPLACE_CURRENT") && (o.clear(), s.deleteAnnotation(f.annotation));
756
782
  return;
757
783
  }
758
- const M = I.map((k) => Wt(k, t));
759
- if (M.every((k) => _t(k))) return;
760
- const O = M.flatMap((k) => Ht(t, k.cloneRange()));
761
- (O.length > 0 && !d || O.length !== d.selector.length || O.some((k, U) => {
762
- var F;
763
- return k.toString() !== ((F = d.selector[U]) == null ? void 0 : F.quote);
764
- })) && (d = {
765
- ...d,
766
- selector: O.map((k) => Kt(k, t, c)),
784
+ const H = k.map((B) => Qe(B, e));
785
+ if (H.every((B) => Ve(B))) return;
786
+ const M = H.flatMap((B) => $e(e, B.cloneRange()));
787
+ if (!(M.length > 0 && !f || M.length !== f.selector.length || M.some((B, F) => B.toString() !== f.selector[F]?.quote))) return;
788
+ const z = a(b) && x ? qe([
789
+ ...x.selector.map((B) => B.range),
790
+ ...M
791
+ ]) : M;
792
+ f = {
793
+ ...f,
794
+ selector: z.map((B) => je(B, e, l)),
767
795
  updated: /* @__PURE__ */ new Date()
768
- }, r.getAnnotation(d.annotation) ? r.updateTarget(d, K.LOCAL) : o.clear());
769
- }, 10), E = (y) => {
770
- w = G(y), L = w.button === 0;
771
- }, a = async (y) => {
772
- if (!L) return;
773
- const v = G(y), I = () => {
774
- const { x: M, y: O } = t.getBoundingClientRect();
775
- if (z(t, v.target)) {
776
- (typeof f == "function" ? f(v, t) : f === "ALWAYS") && o.clear();
796
+ }, !(a(b) || w === "REPLACE_CURRENT") && (s.getAnnotation(f.annotation) ? s.updateTarget(f, K.LOCAL) : o.clear());
797
+ }, 10), A = (h) => {
798
+ b = J(h), R = b.button === 0;
799
+ }, v = async (h) => {
800
+ if (!R) return;
801
+ const T = J(h), k = () => {
802
+ const { x: H, y: M } = e.getBoundingClientRect();
803
+ if (G(e, T.target)) {
804
+ (typeof d == "function" ? d(T, e) : d === "ALWAYS") && o.clear();
777
805
  return;
778
806
  }
779
- const H = v.target instanceof Node && t.contains(v.target) && r.getAt(
780
- v.clientX - M,
781
- v.clientY - O,
782
- l === "all",
807
+ const P = T.target instanceof Node && e.contains(T.target) && s.getAt(
808
+ T.clientX - H,
809
+ T.clientY - M,
810
+ g === "all",
783
811
  u
784
812
  );
785
- if (H) {
786
- const { selected: k } = o, U = new Set(k.map((V) => V.id)), F = Array.isArray(H) ? H.map((V) => V.id) : [H.id];
787
- (U.size !== F.length || !F.every((V) => U.has(V))) && o.userSelect(F, v);
813
+ if (P) {
814
+ const { selected: z } = o, B = new Set(z.map((V) => V.id)), F = Array.isArray(P) ? P.map((V) => V.id) : [P.id];
815
+ (B.size !== F.length || !F.every((V) => B.has(V))) && (n.emit("clickAnnotation", P), o.userSelect(F, T));
788
816
  } else
789
817
  o.clear();
790
818
  };
791
- if (v.timeStamp - w.timeStamp < nt) {
792
- await g();
793
- const M = document.getSelection(), O = z(t, w.target), H = z(t, v.target);
794
- if (M != null && M.isCollapsed || O && H) {
795
- d = void 0, I();
819
+ if (T.timeStamp - b.timeStamp < ae) {
820
+ await N();
821
+ const H = document.getSelection(), M = G(e, b.target), P = G(e, T.target);
822
+ if (H?.isCollapsed || M && P) {
823
+ f = void 0, k();
796
824
  return;
797
825
  }
798
826
  }
799
- d && d.selector.length > 0 && (T(), o.userSelect(d.annotation, v));
800
- }, g = async () => {
801
- const y = document.getSelection();
802
- let v = !1, I = y == null ? void 0 : y.isCollapsed;
803
- const D = () => I || v, M = 1;
804
- return setTimeout(() => v = !0, 50), Rt(() => I = y == null ? void 0 : y.isCollapsed, M, D);
805
- }, b = (y) => {
806
- const v = document.getSelection();
807
- v != null && v.isCollapsed || ((!d || d.selector.length === 0) && N(y), d && (T(), o.userSelect(d.annotation, G(y))));
808
- }, h = (y) => {
809
- x && y.key === "Shift" && d && (document.getSelection().isCollapsed || (T(), o.userSelect(d.annotation, X(y))));
810
- }, S = (y) => {
811
- const v = () => setTimeout(() => {
812
- (d == null ? void 0 : d.selector.length) > 0 && (o.clear(), r.addAnnotation({
813
- id: d.annotation,
827
+ f && f.selector.length > 0 && ($(), o.userSelect(f.annotation, T));
828
+ }, N = async () => {
829
+ const h = document.getSelection();
830
+ let T = !1, k = h?.isCollapsed;
831
+ const U = () => k || T, H = 1;
832
+ return setTimeout(() => T = !0, 50), _e(() => k = h?.isCollapsed, H, U);
833
+ }, O = (h) => {
834
+ document.getSelection()?.isCollapsed || ((!f || f.selector.length === 0) && L(h), f && ($(), o.userSelect(f.annotation, J(h))));
835
+ }, D = (h) => {
836
+ y && h.key === "Shift" && f && (document.getSelection().isCollapsed || ($(), o.userSelect(f.annotation, j(h))));
837
+ }, Y = (h) => {
838
+ const T = () => setTimeout(() => {
839
+ f?.selector.length > 0 && (o.clear(), s.addAnnotation({
840
+ id: f.annotation,
814
841
  bodies: [],
815
- target: d
816
- }), o.userSelect(d.annotation, X(y))), document.removeEventListener("selectionchange", v);
842
+ target: f
843
+ }), o.userSelect(f.annotation, j(h))), document.removeEventListener("selectionchange", T);
817
844
  }, 100);
818
- document.addEventListener("selectionchange", v), C();
845
+ document.addEventListener("selectionchange", T), C();
819
846
  };
820
- Y(ge.join(","), { element: t, keydown: !0, keyup: !1 }, (y) => {
821
- y.repeat || (w = X(y));
822
- }), Y(dt, { keydown: !0, keyup: !1 }, (y) => {
823
- w = X(y), S(y);
847
+ q(St.join(","), { element: e, keydown: !0, keyup: !1 }, (h) => {
848
+ h.repeat || (b = j(h));
849
+ }), q(ye, { keydown: !0, keyup: !1 }, (h) => {
850
+ b = j(h), Y(h);
824
851
  });
825
- const A = (y) => {
826
- y.repeat || y.target !== t && y.target !== document.body || (d = void 0, o.clear());
852
+ const X = (h) => {
853
+ h.repeat || h.target !== e && h.target !== document.body || (f = void 0, o.clear());
827
854
  };
828
- Y(lt.join(","), { keydown: !0, keyup: !1 }, A);
829
- const T = () => {
830
- const y = r.getAnnotation(d.annotation);
831
- if (!y) {
832
- r.addAnnotation({
833
- id: d.annotation,
855
+ q(he.join(","), { keydown: !0, keyup: !1 }, X);
856
+ const $ = () => {
857
+ const h = s.getAnnotation(f.annotation);
858
+ if (!h)
859
+ s.addAnnotation({
860
+ id: f.annotation,
834
861
  bodies: [],
835
- target: d
862
+ target: f
836
863
  });
837
- return;
864
+ else {
865
+ const { target: { updated: T } } = h, { updated: k } = f;
866
+ (!T || !k || T < k) && s.updateTarget(f);
838
867
  }
839
- const { target: { updated: v } } = y, { updated: I } = d;
840
- (!v || !I || v < I) && r.updateTarget(d);
841
868
  };
842
- return document.addEventListener("pointerdown", E), document.addEventListener("pointerup", a), document.addEventListener("contextmenu", b), t.addEventListener("keyup", h), t.addEventListener("selectstart", C), document.addEventListener("selectionchange", N), {
869
+ return document.addEventListener("pointerdown", A), document.addEventListener("pointerup", v), document.addEventListener("contextmenu", O), e.addEventListener("keyup", D), e.addEventListener("selectstart", C), document.addEventListener("selectionchange", L), {
843
870
  destroy: () => {
844
- d = void 0, L = void 0, w = void 0, N.clear(), document.removeEventListener("pointerdown", E), document.removeEventListener("pointerup", a), document.removeEventListener("contextmenu", b), t.removeEventListener("keyup", h), t.removeEventListener("selectstart", C), document.removeEventListener("selectionchange", N), Y.unbind();
871
+ f = void 0, x = void 0, R = void 0, b = void 0, L.clear(), document.removeEventListener("pointerdown", A), document.removeEventListener("pointerup", v), document.removeEventListener("contextmenu", O), e.removeEventListener("keyup", D), e.removeEventListener("selectstart", C), document.removeEventListener("selectionchange", L), q.unbind();
845
872
  },
846
- setFilter: m,
847
- setUser: p,
848
- setAnnotatingEnabled: R
873
+ setFilter: E,
874
+ setUser: m,
875
+ setAnnotatingEnabled: p,
876
+ setAnnotatingMode: S
849
877
  };
850
- }, pe = (t, e) => ({
851
- ...t,
852
- annotatingEnabled: t.annotatingEnabled ?? e.annotatingEnabled,
853
- user: t.user || e.user
854
- }), ot = "SPANS", Be = (t, e = {}) => {
855
- Nt(t), Bt(t);
856
- const n = pe(e, {
878
+ }, wt = (e, t) => ({
879
+ ...e,
880
+ annotatingEnabled: e.annotatingEnabled ?? t.annotatingEnabled,
881
+ user: e.user || t.user
882
+ }), ce = "SPANS", Kt = (e, t = {}) => {
883
+ Pe(e), Ue(e);
884
+ const n = wt(t, {
857
885
  annotatingEnabled: !0,
858
- user: Et()
859
- }), r = de(t, n), { selection: o, viewport: s } = r, i = r.store, c = wt(i), l = At(r, c, n.adapter);
860
- let f = n.user;
861
- const p = n.renderer === "CSS_HIGHLIGHTS" ? CSS.highlights ? "CSS_HIGHLIGHTS" : ot : n.renderer || ot, u = p === "SPANS" ? se(t, r, s) : p === "CSS_HIGHLIGHTS" ? te(t, r, s) : p === "CANVAS" ? Qt(t, r, s) : void 0;
886
+ user: Le()
887
+ }), r = Ct(e, n), { selection: s, viewport: o } = r, i = r.store, c = Ne(i), l = Oe(r, c, n.adapter);
888
+ let g = n.user;
889
+ const d = typeof n.renderer != "function" ? n.renderer === "CSS_HIGHLIGHTS" ? CSS.highlights ? "CSS_HIGHLIGHTS" : ce : n.renderer || ce : null, u = d === null ? n.renderer(e, r, o) : d === "SPANS" ? ft(e, r, o) : d === "CSS_HIGHLIGHTS" ? at(e, r, o) : d === "CANVAS" ? st(e, r, o) : void 0;
862
890
  if (!u)
863
- throw `Unknown renderer implementation: ${p}`;
864
- console.debug(`Using ${p} renderer`), n.style && u.setStyle(n.style);
865
- const m = me(t, r, n);
866
- m.setUser(f);
867
- const d = vt(r, c, n.adapter), L = () => f, w = (a) => {
868
- m.setAnnotatingEnabled(
891
+ throw `Unknown renderer implementation: ${n.renderer}`;
892
+ console.debug(d ? `Using ${d} renderer` : "Using custom renderer implementation"), n.style && u.setStyle(n.style);
893
+ const y = At(e, r, l, n);
894
+ y.setUser(g);
895
+ const w = Be(r, c, n.adapter), f = () => g, x = (a) => {
896
+ y.setAnnotatingEnabled(
869
897
  a === void 0 ? !0 : a
870
898
  );
871
- }, x = (a) => {
872
- u.setFilter(a), m.setFilter(a);
873
899
  }, R = (a) => {
874
- f = a, m.setUser(a);
875
- }, C = (a) => {
876
- a && (u.setPainter(ue(a, n.presence)), a.on("selectionChange", () => u.redraw()));
877
- }, N = (a) => {
878
- a ? o.setSelected(a) : o.clear();
900
+ y.setAnnotatingMode(a);
901
+ }, b = (a) => {
902
+ u.setFilter(a), y.setFilter(a);
903
+ }, p = (a) => {
904
+ g = a, y.setUser(a);
905
+ }, S = (a) => {
906
+ a && (u.setPainter(bt(a, n.presence)), a.on("selectionChange", () => u.redraw()));
907
+ }, E = (a) => {
908
+ a ? s.setSelected(a) : s.clear();
879
909
  };
880
910
  return {
881
- ...d,
911
+ ...w,
882
912
  destroy: () => {
883
- u.destroy(), m.destroy(), c.destroy();
913
+ u.destroy(), y.destroy(), c.destroy();
884
914
  },
885
- element: t,
886
- getUser: L,
887
- setAnnotatingEnabled: w,
888
- setFilter: x,
915
+ element: e,
916
+ getUser: f,
917
+ setAnnotatingEnabled: x,
918
+ setAnnotatingMode: R,
919
+ setFilter: b,
889
920
  setStyle: u.setStyle.bind(u),
890
921
  redraw: u.redraw.bind(u),
891
- setUser: R,
892
- setSelected: N,
893
- setPresenceProvider: C,
922
+ setUser: p,
923
+ setSelected: E,
924
+ setPresenceProvider: S,
894
925
  setVisible: u.setVisible.bind(u),
895
926
  on: l.on,
896
927
  off: l.off,
897
- scrollIntoView: Vt(t, i),
928
+ scrollIntoView: Ze(e, i),
898
929
  state: r
899
930
  };
900
931
  };
901
932
  export {
902
- j as DEFAULT_SELECTED_STYLE,
933
+ Z as DEFAULT_SELECTED_STYLE,
903
934
  _ as DEFAULT_STYLE,
904
- rt as NOT_ANNOTATABLE_CLASS,
935
+ ue as NOT_ANNOTATABLE_CLASS,
905
936
  W as NOT_ANNOTATABLE_SELECTOR,
906
- He as Origin,
907
- Pe as UserSelectAction,
908
- ke as W3CTextFormat,
909
- Nt as cancelSingleClickEvents,
910
- X as cloneKeyboardEvent,
911
- G as clonePointerEvent,
912
- _e as createBody,
913
- Qt as createCanvasRenderer,
914
- te as createHighlightsRenderer,
915
- ue as createPresencePainter,
916
- Jt as createRenderer,
917
- me as createSelectionHandler,
918
- se as createSpansRenderer,
919
- Be as createTextAnnotator,
920
- de as createTextAnnotatorState,
921
- pe as fillDefaults,
922
- It as getHighlightClientRects,
923
- Pt as getQuoteContext,
924
- J as getRangeAnnotatableContents,
925
- kt as isMac,
926
- Le as isNodeWhitespaceOrEmpty,
927
- z as isNotAnnotatable,
928
- Lt as isRangeAnnotatable,
929
- _t as isRangeWhitespaceOrEmpty,
930
- P as isRevived,
931
- Ft as mergeClientRects,
932
- Yt as paint,
933
- ae as parseW3CTextAnnotation,
934
- Bt as programmaticallyFocusable,
935
- Kt as rangeToSelector,
936
- Q as reviveAnnotation,
937
- at as reviveSelector,
938
- q as reviveTarget,
939
- Vt as scrollIntoView,
940
- ce as serializeW3CTextAnnotation,
941
- Ht as splitAnnotatableRanges,
942
- Ne as toDomRectList,
943
- $t as toParentBounds,
944
- Oe as toViewportBounds,
945
- Wt as trimRangeToContainer,
946
- it as whitespaceOrEmptyRegex
937
+ Ft as Origin,
938
+ Vt as UserSelectAction,
939
+ Ut as W3CTextFormat,
940
+ Pe as cancelSingleClickEvents,
941
+ j as cloneKeyboardEvent,
942
+ J as clonePointerEvent,
943
+ se as createBaseRenderer,
944
+ Yt as createBody,
945
+ st as createCanvasRenderer,
946
+ at as createHighlightsRenderer,
947
+ bt as createPresencePainter,
948
+ it as createRenderer,
949
+ At as createSelectionHandler,
950
+ ft as createSpansRenderer,
951
+ Kt as createTextAnnotator,
952
+ Ct as createTextAnnotatorState,
953
+ ee as debounce,
954
+ wt as fillDefaults,
955
+ ct as getBackgroundColor,
956
+ Ke as getHighlightClientRects,
957
+ Fe as getQuoteContext,
958
+ oe as getRangeAnnotatableContents,
959
+ Je as getViewportBounds,
960
+ fe as isMac,
961
+ Ht as isNodeWhitespaceOrEmpty,
962
+ G as isNotAnnotatable,
963
+ He as isRangeAnnotatable,
964
+ Ve as isRangeWhitespaceOrEmpty,
965
+ I as isRevived,
966
+ ze as mergeClientRects,
967
+ qe as mergeRanges,
968
+ lt as paint,
969
+ pt as parseW3CTextAnnotation,
970
+ Ue as programmaticallyFocusable,
971
+ je as rangeToSelector,
972
+ te as reviveAnnotation,
973
+ me as reviveSelector,
974
+ Q as reviveTarget,
975
+ Ze as scrollIntoView,
976
+ ht as serializeW3CTextAnnotation,
977
+ $e as splitAnnotatableRanges,
978
+ Pt as toDomRectList,
979
+ Ge as toParentBounds,
980
+ Dt as toViewportBounds,
981
+ et as trackViewport,
982
+ Qe as trimRangeToContainer,
983
+ ge as whitespaceOrEmptyRegex
947
984
  };
948
985
  //# sourceMappingURL=text-annotator.es.js.map