@recogito/text-annotator 3.0.0-rc.1 → 3.0.0-rc.11

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,110 +1,113 @@
1
- const dt = { fill: "rgb(0, 128, 255", fillOpacity: 0.18 }, ht = { fill: "rgb(0, 128, 255)", fillOpacity: 0.45 }, ot = {
1
+ const yt = { fill: "rgb(0, 128, 255", fillOpacity: 0.18 }, vt = { fill: "rgb(0, 128, 255)", fillOpacity: 0.45 }, at = {
2
2
  paint: (t, e, n, o, i, s) => {
3
- const c = s ? typeof s == "function" ? s(t, i) : s : i ? ht : dt;
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));
3
+ const r = s ? typeof s == "function" ? s(t, i) : s : i ? vt : yt;
4
+ n.fillStyle = r.fill, n.globalAlpha = r.fillOpacity || 1, e.forEach(({ x: a, y: l, width: d, height: x }) => n.fillRect(a, l - 2.5, d, x + 5));
5
5
  }
6
- }, ut = (t) => {
6
+ }, Et = (t) => {
7
7
  let e = /* @__PURE__ */ new Set();
8
8
  return (o) => {
9
9
  const i = o.map((s) => s.id);
10
10
  (e.size !== i.length || i.some((s) => !e.has(s))) && t.set(i), e = new Set(i);
11
11
  };
12
- };
13
- const ft = (t, e = 10) => {
12
+ }, St = (t, e = 10) => {
14
13
  let n;
15
14
  return (...o) => {
16
15
  clearTimeout(n), n = setTimeout(() => t.apply(void 0, o), e);
17
16
  };
18
- }, Q = (t, e) => {
17
+ }, nt = (t, e) => {
19
18
  const n = document.createElement("canvas");
20
19
  if (n.width = e ? 2 * window.innerWidth : window.innerWidth, n.height = e ? 2 * window.innerHeight : window.innerHeight, n.className = t, e) {
21
20
  const o = n.getContext("2d");
22
21
  o.scale(2, 2), o.translate(0.5, 0.5);
23
22
  }
24
23
  return n;
25
- }, Z = (t, e) => {
24
+ }, ot = (t, e) => {
26
25
  if (t.width = e ? 2 * window.innerWidth : window.innerWidth, t.height = e ? 2 * window.innerHeight : window.innerHeight, e) {
27
26
  const n = t.getContext("2d");
28
27
  n.scale(2, 2), n.translate(0.5, 0.5);
29
28
  }
30
- }, gt = (t, e, n) => {
29
+ }, Tt = (t, e, n) => {
31
30
  const { store: o, selection: i, hover: s } = e;
32
- let c, r = ot;
33
- const l = ut(n);
31
+ let r, a, l = at;
32
+ const d = Et(n);
34
33
  t.classList.add("r6o-annotatable");
35
- const h = Q("r6o-highlight-layer bg"), g = Q("r6o-highlight-layer fg", !0), d = h.getContext("2d"), p = g.getContext("2d");
36
- t.insertBefore(h, t.firstChild), t.appendChild(g), t.addEventListener("pointermove", (m) => {
37
- const { x: T, y: b } = t.getBoundingClientRect(), C = o.getAt(m.clientX - T, m.clientY - b);
38
- C ? s.current !== C.id && (t.classList.add("hovered"), s.set(C.id)) : s.current && (t.classList.remove("hovered"), s.set(null));
39
- });
34
+ const x = nt("r6o-highlight-layer bg"), u = nt("r6o-highlight-layer fg", !0), T = x.getContext("2d"), h = u.getContext("2d");
35
+ t.insertBefore(x, t.firstChild), t.appendChild(u);
36
+ const w = (v) => {
37
+ const { x: C, y: L } = t.getBoundingClientRect(), X = o.getAt(v.clientX - C, v.clientY - L);
38
+ X && (!a || a(X)) ? s.current !== X.id && (t.classList.add("hovered"), s.set(X.id)) : s.current && (t.classList.remove("hovered"), s.set(null));
39
+ };
40
+ t.addEventListener("pointermove", w);
40
41
  const B = () => {
41
- const { top: m, left: T } = t.getBoundingClientRect(), { innerWidth: b, innerHeight: C } = window, S = -T, f = -m, y = b - T, E = C - m;
42
- return { top: m, left: T, minX: S, minY: f, maxX: y, maxY: E };
43
- }, x = () => u();
44
- document.addEventListener("scroll", x, { capture: !0, passive: !0 });
45
- const w = ft(() => {
46
- Z(h), Z(g, !0), o.recalculatePositions(), u();
47
- });
48
- window.addEventListener("resize", w), new ResizeObserver(w).observe(t);
49
- const u = () => requestAnimationFrame(() => {
50
- const { top: m, left: T, minX: b, minY: C, maxX: S, maxY: f } = B(), y = o.getIntersectingRects(b, C, S, f), { width: E, height: v } = g, M = new Set(i.selected.map(({ id: R }) => R));
51
- p.clearRect(-0.5, -0.5, E + 1, v + 1), d.clearRect(-0.5, -0.5, E + 1, v + 1), y.forEach(({ annotation: R, rects: X }) => {
52
- const q = X.map(({ x: rt, y: ct, width: at, height: lt }) => ({
53
- x: rt + T,
54
- y: ct + m,
55
- width: at,
56
- height: lt
57
- })), N = M.has(R.id);
58
- r.paint(R, q, d, p, N, c);
59
- }), setTimeout(() => l(y.map(({ annotation: R }) => R)), 1);
42
+ const { top: v, left: C } = t.getBoundingClientRect(), { innerWidth: L, innerHeight: X } = window, k = -C, H = -v, $ = L - C, F = X - v;
43
+ return { top: v, left: C, minX: k, minY: H, maxX: $, maxY: F };
44
+ }, b = () => requestAnimationFrame(() => {
45
+ const { top: v, left: C, minX: L, minY: X, maxX: k, maxY: H } = B(), $ = a ? o.getIntersectingRects(L, X, k, H).filter(({ annotation: Y }) => a(Y)) : o.getIntersectingRects(L, X, k, H), { width: F, height: et } = u, ft = new Set(i.selected.map(({ id: Y }) => Y));
46
+ h.clearRect(-0.5, -0.5, F + 1, et + 1), T.clearRect(-0.5, -0.5, F + 1, et + 1), $.forEach(({ annotation: Y, rects: pt }) => {
47
+ const gt = pt.map(({ x: bt, y: wt, width: xt, height: At }) => ({
48
+ x: bt + C,
49
+ y: wt + v,
50
+ width: xt,
51
+ height: At
52
+ })), mt = ft.has(Y.id);
53
+ l.paint(Y, gt, T, h, mt, r);
54
+ }), setTimeout(() => d($.map(({ annotation: Y }) => Y)), 1);
55
+ }), c = (v) => {
56
+ r = v, b();
57
+ }, f = (v) => {
58
+ a = v, b();
59
+ }, p = () => b();
60
+ o.observe(p);
61
+ const m = i.subscribe(() => b()), E = () => b();
62
+ document.addEventListener("scroll", E, { capture: !0, passive: !0 });
63
+ const S = St(() => {
64
+ console.log("onresize!"), ot(x), ot(u, !0), o.recalculatePositions(), b();
60
65
  });
61
- return o.observe(() => u()), i.subscribe(() => u()), {
62
- redraw: u,
63
- setDrawingStyle: (m) => {
64
- c = m, u();
66
+ window.addEventListener("resize", S);
67
+ const M = new ResizeObserver(S);
68
+ M.observe(t);
69
+ const g = { attributes: !0, childList: !0, subtree: !0 }, y = new MutationObserver(S);
70
+ return y.observe(document.body, g), {
71
+ destroy: () => {
72
+ t.removeEventListener("pointermove", w), t.removeChild(u), t.appendChild(x), o.unobserve(p), m(), document.removeEventListener("scroll", E), window.removeEventListener("resize", S), M.disconnect(), y.disconnect();
65
73
  },
66
- setPainter: (m) => r = m
67
- };
68
- }, k = (t, e) => {
69
- const { quote: n, start: o, end: i } = t.selector, s = t.selector.offsetReference ? t.selector.offsetReference : e;
70
- if (!s)
71
- return t;
72
- const c = document.createNodeIterator(e, NodeFilter.SHOW_TEXT);
73
- let r = 0, l = document.createRange(), h = c.nextNode();
74
- h === null && console.error("Could not revive annotation target. Content missing.");
75
- let g = !s;
76
- for (; h !== null; ) {
77
- const d = h.textContent.length;
78
- if (!g && s && (g = s.contains(h)), g) {
79
- if (r + d > o) {
80
- l.setStart(h, o - r);
81
- break;
82
- }
83
- r += d;
84
- }
85
- h = c.nextNode();
86
- }
87
- for (; h !== null; ) {
88
- const d = h.textContent.length;
89
- if (r + d > i) {
90
- l.setEnd(h, i - r);
91
- break;
92
- }
93
- r += d, h = c.nextNode();
94
- }
95
- return {
96
- ...t,
97
- selector: {
98
- ...t.selector,
99
- quote: n,
100
- start: o,
101
- end: i,
102
- range: l
103
- }
74
+ redraw: b,
75
+ setDrawingStyle: c,
76
+ setFilter: f,
77
+ setPainter: (v) => l = v
104
78
  };
105
79
  };
106
- var tt = Object.prototype.hasOwnProperty;
107
- function Y(t, e) {
80
+ let W;
81
+ const Bt = new Uint8Array(16);
82
+ function Ct() {
83
+ if (!W && (W = typeof crypto < "u" && crypto.getRandomValues && crypto.getRandomValues.bind(crypto), !W))
84
+ throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");
85
+ return W(Bt);
86
+ }
87
+ const R = [];
88
+ for (let t = 0; t < 256; ++t)
89
+ R.push((t + 256).toString(16).slice(1));
90
+ function Mt(t, e = 0) {
91
+ return R[t[e + 0]] + R[t[e + 1]] + R[t[e + 2]] + R[t[e + 3]] + "-" + R[t[e + 4]] + R[t[e + 5]] + "-" + R[t[e + 6]] + R[t[e + 7]] + "-" + R[t[e + 8]] + R[t[e + 9]] + "-" + R[t[e + 10]] + R[t[e + 11]] + R[t[e + 12]] + R[t[e + 13]] + R[t[e + 14]] + R[t[e + 15]];
92
+ }
93
+ const Lt = typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto), it = {
94
+ randomUUID: Lt
95
+ };
96
+ function ct(t, e, n) {
97
+ if (it.randomUUID && !e && !t)
98
+ return it.randomUUID();
99
+ t = t || {};
100
+ const o = t.random || (t.rng || Ct)();
101
+ if (o[6] = o[6] & 15 | 64, o[8] = o[8] & 63 | 128, e) {
102
+ n = n || 0;
103
+ for (let i = 0; i < 16; ++i)
104
+ e[n + i] = o[i];
105
+ return e;
106
+ }
107
+ return Mt(o);
108
+ }
109
+ var st = Object.prototype.hasOwnProperty;
110
+ function U(t, e) {
108
111
  var n, o;
109
112
  if (t === e)
110
113
  return !0;
@@ -115,60 +118,60 @@ function Y(t, e) {
115
118
  return t.toString() === e.toString();
116
119
  if (n === Array) {
117
120
  if ((o = t.length) === e.length)
118
- for (; o-- && Y(t[o], e[o]); )
121
+ for (; o-- && U(t[o], e[o]); )
119
122
  ;
120
123
  return o === -1;
121
124
  }
122
125
  if (!n || typeof t == "object") {
123
126
  o = 0;
124
127
  for (n in t)
125
- if (tt.call(t, n) && ++o && !tt.call(e, n) || !(n in e) || !Y(t[n], e[n]))
128
+ if (st.call(t, n) && ++o && !st.call(e, n) || !(n in e) || !U(t[n], e[n]))
126
129
  return !1;
127
130
  return Object.keys(e).length === o;
128
131
  }
129
132
  }
130
133
  return t !== t && e !== e;
131
134
  }
132
- function F() {
135
+ function G() {
133
136
  }
134
- function pt(t, e) {
137
+ function Ot(t, e) {
135
138
  return t != t ? e == e : t !== e || t && typeof t == "object" || typeof t == "function";
136
139
  }
137
- const _ = [];
138
- function K(t, e = F) {
140
+ const I = [];
141
+ function tt(t, e = G) {
139
142
  let n;
140
143
  const o = /* @__PURE__ */ new Set();
141
- function i(r) {
142
- if (pt(t, r) && (t = r, n)) {
143
- const l = !_.length;
144
- for (const h of o)
145
- h[1](), _.push(h, t);
144
+ function i(a) {
145
+ if (Ot(t, a) && (t = a, n)) {
146
+ const l = !I.length;
147
+ for (const d of o)
148
+ d[1](), I.push(d, t);
146
149
  if (l) {
147
- for (let h = 0; h < _.length; h += 2)
148
- _[h][0](_[h + 1]);
149
- _.length = 0;
150
+ for (let d = 0; d < I.length; d += 2)
151
+ I[d][0](I[d + 1]);
152
+ I.length = 0;
150
153
  }
151
154
  }
152
155
  }
153
- function s(r) {
154
- i(r(t));
156
+ function s(a) {
157
+ i(a(t));
155
158
  }
156
- function c(r, l = F) {
157
- const h = [r, l];
158
- return o.add(h), o.size === 1 && (n = e(i, s) || F), r(t), () => {
159
- o.delete(h), o.size === 0 && n && (n(), n = null);
159
+ function r(a, l = G) {
160
+ const d = [a, l];
161
+ return o.add(d), o.size === 1 && (n = e(i, s) || G), a(t), () => {
162
+ o.delete(d), o.size === 0 && n && (n(), n = null);
160
163
  };
161
164
  }
162
- return { set: i, update: s, subscribe: c };
165
+ return { set: i, update: s, subscribe: r };
163
166
  }
164
- const mt = (t) => {
165
- const { subscribe: e, set: n } = K(null);
166
- let o = null;
167
+ const Rt = (t) => {
168
+ const { subscribe: e, set: n } = tt();
169
+ let o;
167
170
  return e((i) => o = i), t.observe(({ changes: i }) => {
168
171
  if (o) {
169
- i.deleted.some((r) => r.id === o) && n(null);
170
- const c = i.updated.find(({ oldValue: r }) => r.id === o);
171
- c && n(c.newValue.id);
172
+ (i.deleted || []).some((r) => r.id === o) && n(void 0);
173
+ const s = (i.updated || []).find(({ oldValue: r }) => r.id === o);
174
+ s && n(s.newValue.id);
172
175
  }
173
176
  }), {
174
177
  get current() {
@@ -177,35 +180,35 @@ const mt = (t) => {
177
180
  subscribe: e,
178
181
  set: n
179
182
  };
180
- }, j = { selected: [] }, xt = (t, e = "EDIT") => {
181
- const { subscribe: n, set: o } = K(j);
182
- let i = j;
183
- n((d) => i = d);
184
- const s = () => o(j), c = () => {
185
- var d;
186
- return ((d = i.selected) == null ? void 0 : d.length) === 0;
187
- }, r = (d) => {
183
+ }, J = { selected: [] }, Xt = (t, e = "EDIT") => {
184
+ const { subscribe: n, set: o } = tt(J);
185
+ let i = J;
186
+ n((u) => i = u);
187
+ const s = () => o(J), r = () => {
188
+ var u;
189
+ return ((u = i.selected) == null ? void 0 : u.length) === 0;
190
+ }, a = (u) => {
188
191
  if (i.selected.length === 0)
189
192
  return !1;
190
- const p = typeof d == "string" ? d : d.id;
191
- return i.selected.some((B) => B.id === p);
192
- }, l = (d, p) => {
193
- const B = t.getAnnotation(d);
194
- if (B) {
195
- const x = At(B, e);
196
- o(x === "EDIT" ? { selected: [{ id: d, editable: !0 }], pointerEvent: p } : x === "SELECT" ? { selected: [{ id: d }], pointerEvent: p } : { selected: [], pointerEvent: p });
193
+ const T = typeof u == "string" ? u : u.id;
194
+ return i.selected.some((h) => h.id === T);
195
+ }, l = (u, T) => {
196
+ const h = t.getAnnotation(u);
197
+ if (h) {
198
+ const w = Yt(h, e);
199
+ o(w === "EDIT" ? { selected: [{ id: u, editable: !0 }], pointerEvent: T } : w === "SELECT" ? { selected: [{ id: u }], pointerEvent: T } : { selected: [], pointerEvent: T });
197
200
  } else
198
- console.warn("Invalid selection: " + d);
199
- }, h = (d, p = !0) => {
200
- const B = Array.isArray(d) ? d : [d], x = B.map((w) => t.getAnnotation(w)).filter((w) => w);
201
- o({ selected: x.map(({ id: w }) => ({ id: w, editable: p })) }), x.length !== B.length && console.warn("Invalid selection", d);
202
- }, g = (d) => {
201
+ console.warn("Invalid selection: " + u);
202
+ }, d = (u, T = !0) => {
203
+ const h = Array.isArray(u) ? u : [u], w = h.map((B) => t.getAnnotation(B)).filter(Boolean);
204
+ o({ selected: w.map(({ id: B }) => ({ id: B, editable: T })) }), w.length !== h.length && console.warn("Invalid selection", u);
205
+ }, x = (u) => {
203
206
  if (i.selected.length === 0)
204
207
  return !1;
205
- const { selected: p } = i;
206
- p.filter(({ id: x }) => d.includes(x)).length > 0 && o({ selected: p.filter(({ id: x }) => !d.includes(x)) });
208
+ const { selected: T } = i;
209
+ T.filter(({ id: h }) => u.includes(h)).length > 0 && o({ selected: T.filter(({ id: h }) => !u.includes(h)) });
207
210
  };
208
- return t.observe(({ changes: d }) => g(d.deleted.map((p) => p.id))), {
211
+ return t.observe(({ changes: u }) => x((u.deleted || []).map((T) => T.id))), {
209
212
  clear: s,
210
213
  clickSelect: l,
211
214
  get selected() {
@@ -214,88 +217,84 @@ const mt = (t) => {
214
217
  get pointerEvent() {
215
218
  return i ? i.pointerEvent : null;
216
219
  },
217
- isEmpty: c,
218
- isSelected: r,
219
- setSelected: h,
220
+ isEmpty: r,
221
+ isSelected: a,
222
+ setSelected: d,
220
223
  subscribe: n
221
224
  };
222
- }, At = (t, e) => typeof e == "function" ? e(t) || "EDIT" : e || "EDIT";
223
- let H;
224
- const yt = new Uint8Array(16);
225
- function Et() {
226
- if (!H && (H = typeof crypto < "u" && crypto.getRandomValues && crypto.getRandomValues.bind(crypto), !H))
227
- throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");
228
- return H(yt);
229
- }
230
- const O = [];
225
+ }, Yt = (t, e) => typeof e == "function" ? e(t) || "EDIT" : e || "EDIT", Dt = [];
231
226
  for (let t = 0; t < 256; ++t)
232
- O.push((t + 256).toString(16).slice(1));
233
- function wt(t, e = 0) {
234
- 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]];
235
- }
236
- const bt = typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto), et = {
237
- randomUUID: bt
238
- };
239
- function Tt(t, e, n) {
240
- if (et.randomUUID && !e && !t)
241
- return et.randomUUID();
242
- t = t || {};
243
- const o = t.random || (t.rng || Et)();
244
- if (o[6] = o[6] & 15 | 64, o[8] = o[8] & 63 | 128, e) {
245
- n = n || 0;
246
- for (let i = 0; i < 16; ++i)
247
- e[n + i] = o[i];
248
- return e;
249
- }
250
- return wt(o);
251
- }
252
- const vt = (t, e) => {
227
+ Dt.push((t + 256).toString(16).slice(1));
228
+ typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto);
229
+ const Ut = (t, e) => {
253
230
  const n = new Set(t.bodies.map((o) => o.id));
254
231
  return e.bodies.filter((o) => !n.has(o.id));
255
- }, Ct = (t, e) => {
232
+ }, It = (t, e) => {
256
233
  const n = new Set(e.bodies.map((o) => o.id));
257
234
  return t.bodies.filter((o) => !n.has(o.id));
258
- }, Bt = (t, e) => e.bodies.map((n) => {
235
+ }, Vt = (t, e) => e.bodies.map((n) => {
259
236
  const o = t.bodies.find((i) => i.id === n.id);
260
- return { newBody: n, oldBody: o && !Y(o, n) ? o : void 0 };
261
- }).filter(({ oldBody: n }) => n), Mt = (t, e) => !Y(t.target, e.target), Rt = (t, e) => ({
262
- oldValue: t,
263
- newValue: e,
264
- bodiesCreated: vt(t, e),
265
- bodiesDeleted: Ct(t, e),
266
- bodiesUpdated: Bt(t, e),
267
- targetUpdated: Mt(t, e) ? { oldTarget: t.target, newTarget: e.target } : void 0
268
- });
269
- var L = /* @__PURE__ */ ((t) => (t.LOCAL = "LOCAL", t.REMOTE = "REMOTE", t))(L || {});
270
- const Lt = (t, e) => {
271
- var s, c;
272
- const { changes: n, origin: o } = e;
273
- if (!(!t.options.origin || t.options.origin === o))
237
+ return { newBody: n, oldBody: o && !U(o, n) ? o : void 0 };
238
+ }).filter(({ oldBody: n }) => n).map(({ oldBody: n, newBody: o }) => ({ oldBody: n, newBody: o })), _t = (t, e) => !U(t.target, e.target), lt = (t, e) => {
239
+ const n = Ut(t, e), o = It(t, e), i = Vt(t, e);
240
+ return {
241
+ oldValue: t,
242
+ newValue: e,
243
+ bodiesCreated: n.length > 0 ? n : void 0,
244
+ bodiesDeleted: o.length > 0 ? o : void 0,
245
+ bodiesUpdated: i.length > 0 ? i : void 0,
246
+ targetUpdated: _t(t, e) ? { oldTarget: t.target, newTarget: e.target } : void 0
247
+ };
248
+ };
249
+ var O = /* @__PURE__ */ ((t) => (t.LOCAL = "LOCAL", t.REMOTE = "REMOTE", t))(O || {});
250
+ const kt = (t, e) => {
251
+ var n, o;
252
+ const { changes: i, origin: s } = e;
253
+ if (!(!t.options.origin || t.options.origin === s))
274
254
  return !1;
275
255
  if (t.options.ignore) {
276
- const { ignore: r } = t.options, l = (g) => (g == null ? void 0 : g.length) > 0;
277
- if (!(l(n.created) || l(n.deleted))) {
278
- const g = (s = n.updated) == null ? void 0 : s.some((p) => l(p.bodiesCreated) || l(p.bodiesDeleted) || l(p.bodiesUpdated)), d = (c = n.updated) == null ? void 0 : c.some((p) => p.targetUpdated);
279
- if (r === "BODY_ONLY" && g && !d || r === "TARGET_ONLY" && d && !g)
256
+ const { ignore: r } = t.options, a = (l) => l && l.length > 0;
257
+ if (!(a(i.created) || a(i.deleted))) {
258
+ const l = (n = i.updated) == null ? void 0 : n.some((x) => a(x.bodiesCreated) || a(x.bodiesDeleted) || a(x.bodiesUpdated)), d = (o = i.updated) == null ? void 0 : o.some((x) => x.targetUpdated);
259
+ if (r === "BODY_ONLY" && l && !d || r === "TARGET_ONLY" && d && !l)
280
260
  return !1;
281
261
  }
282
262
  }
283
263
  if (t.options.annotations) {
284
264
  const r = /* @__PURE__ */ new Set([
285
- ...n.created.map((h) => h.id),
286
- ...n.deleted.map((h) => h.id),
287
- ...n.updated.map(({ oldValue: h }) => h.id)
265
+ ...(i.created || []).map((a) => a.id),
266
+ ...(i.deleted || []).map((a) => a.id),
267
+ ...(i.updated || []).map(({ oldValue: a }) => a.id)
288
268
  ]);
289
- return !!(Array.isArray(t.options.annotations) ? t.options.annotations : [t.options.annotations]).find((h) => r.has(h));
269
+ return !!(Array.isArray(t.options.annotations) ? t.options.annotations : [t.options.annotations]).find((a) => r.has(a));
290
270
  } else
291
271
  return !0;
292
- }, St = (t) => t.id !== void 0, Ot = () => {
293
- const t = /* @__PURE__ */ new Map(), e = /* @__PURE__ */ new Map(), n = [], o = (f, y = {}) => n.push({ onChange: f, options: y }), i = (f) => {
294
- const y = n.findIndex((E) => E.onChange == f);
272
+ }, Nt = (t, e) => {
273
+ const n = new Set((t.created || []).map((u) => u.id)), o = new Set((t.updated || []).map(({ newValue: u }) => u.id)), i = new Set((e.created || []).map((u) => u.id)), s = new Set((e.deleted || []).map((u) => u.id)), r = new Set((e.updated || []).map(({ oldValue: u }) => u.id)), a = new Set((e.updated || []).filter(({ oldValue: u }) => n.has(u.id) || o.has(u.id)).map(({ oldValue: u }) => u.id)), l = [
274
+ ...(t.created || []).filter((u) => !s.has(u.id)).map((u) => r.has(u.id) ? e.updated.find(({ oldValue: T }) => T.id === u.id).newValue : u),
275
+ ...e.created || []
276
+ ], d = [
277
+ ...(t.deleted || []).filter((u) => !i.has(u.id)),
278
+ ...(e.deleted || []).filter((u) => !n.has(u.id))
279
+ ], x = [
280
+ ...(t.updated || []).filter(({ newValue: u }) => !s.has(u.id)).map((u) => {
281
+ const { oldValue: T, newValue: h } = u;
282
+ if (r.has(h.id)) {
283
+ const w = e.updated.find((B) => B.oldValue.id === h.id).newValue;
284
+ return lt(T, w);
285
+ } else
286
+ return u;
287
+ }),
288
+ ...(e.updated || []).filter(({ oldValue: u }) => !a.has(u.id))
289
+ ];
290
+ return { created: l, deleted: d, updated: x };
291
+ }, Pt = (t) => t.id !== void 0, zt = () => {
292
+ const t = /* @__PURE__ */ new Map(), e = /* @__PURE__ */ new Map(), n = [], o = (g, y = {}) => n.push({ onChange: g, options: y }), i = (g) => {
293
+ const y = n.findIndex((A) => A.onChange == g);
295
294
  y > -1 && n.splice(y, 1);
296
- }, s = (f, y) => {
297
- const E = {
298
- origin: f,
295
+ }, s = (g, y) => {
296
+ const A = {
297
+ origin: g,
299
298
  changes: {
300
299
  created: y.created || [],
301
300
  updated: y.updated || [],
@@ -304,311 +303,390 @@ const Lt = (t, e) => {
304
303
  state: [...t.values()]
305
304
  };
306
305
  n.forEach((v) => {
307
- Lt(v, E) && v.onChange(E);
306
+ kt(v, A) && v.onChange(A);
308
307
  });
309
- }, c = (f, y = L.LOCAL) => {
310
- if (t.get(f.id))
311
- throw Error(`Cannot add annotation ${f.id} - exists already`);
312
- t.set(f.id, f), f.bodies.forEach((v) => e.set(v.id, f.id)), s(y, { created: [f] });
313
- }, r = (f, y = L.LOCAL, E = L.LOCAL) => {
314
- const v = St(y) ? E : y, M = typeof f == "string" ? y : f, R = typeof f == "string" ? f : f.id, X = t.get(R);
315
- if (X) {
316
- const q = Rt(X, M);
317
- R === M.id ? t.set(R, M) : (t.delete(R), t.set(M.id, M)), X.bodies.forEach((N) => e.delete(N.id)), M.bodies.forEach((N) => e.set(N.id, M.id)), s(v, { updated: [q] });
308
+ }, r = (g, y = O.LOCAL) => {
309
+ if (t.get(g.id))
310
+ throw Error(`Cannot add annotation ${g.id} - exists already`);
311
+ t.set(g.id, g), g.bodies.forEach((A) => e.set(A.id, g.id)), s(y, { created: [g] });
312
+ }, a = (g, y) => {
313
+ const A = typeof g == "string" ? y : g, v = typeof g == "string" ? g : g.id, C = t.get(v);
314
+ if (C) {
315
+ const L = lt(C, A);
316
+ return v === A.id ? t.set(v, A) : (t.delete(v), t.set(A.id, A)), C.bodies.forEach((X) => e.delete(X.id)), A.bodies.forEach((X) => e.set(X.id, A.id)), L;
318
317
  } else
319
- throw Error(`Cannot update annotation ${R} - does not exist`);
320
- }, l = (f, y = L.LOCAL) => {
321
- const E = t.get(f.annotation);
322
- if (E) {
318
+ console.warn(`Cannot update annotation ${v} - does not exist`);
319
+ }, l = (g, y = O.LOCAL, A = O.LOCAL) => {
320
+ const v = Pt(y) ? A : y, C = a(g, y);
321
+ C && s(v, { updated: [C] });
322
+ }, d = (g, y = O.LOCAL) => {
323
+ const A = g.reduce((v, C) => {
324
+ const L = a(C);
325
+ return L ? [...v, L] : v;
326
+ }, []);
327
+ A.length > 0 && s(y, { updated: A });
328
+ }, x = (g, y = O.LOCAL) => {
329
+ const A = t.get(g.annotation);
330
+ if (A) {
323
331
  const v = {
324
- ...E,
325
- bodies: [...E.bodies, f]
332
+ ...A,
333
+ bodies: [...A.bodies, g]
326
334
  };
327
- t.set(E.id, v), e.set(f.id, v.id), s(y, { updated: [{
328
- oldValue: E,
335
+ t.set(A.id, v), e.set(g.id, v.id), s(y, { updated: [{
336
+ oldValue: A,
329
337
  newValue: v,
330
- bodiesCreated: [f]
338
+ bodiesCreated: [g]
331
339
  }] });
332
340
  } else
333
- console.warn(`Attempt to add body to missing annotation: ${f.annotation}`);
334
- }, h = () => [...t.values()], g = (f = L.LOCAL) => {
341
+ console.warn(`Attempt to add body to missing annotation: ${g.annotation}`);
342
+ }, u = () => [...t.values()], T = (g = O.LOCAL) => {
335
343
  const y = [...t.values()];
336
- t.clear(), e.clear(), s(f, { deleted: y });
337
- }, d = (f, y = !0, E = L.LOCAL) => {
344
+ t.clear(), e.clear(), s(g, { deleted: y });
345
+ }, h = (g, y = !0, A = O.LOCAL) => {
338
346
  if (y) {
339
347
  const v = [...t.values()];
340
- t.clear(), e.clear(), f.forEach((M) => {
341
- t.set(M.id, M), M.bodies.forEach((R) => e.set(R.id, M.id));
342
- }), s(E, { created: f, deleted: v });
348
+ t.clear(), e.clear(), g.forEach((C) => {
349
+ t.set(C.id, C), C.bodies.forEach((L) => e.set(L.id, C.id));
350
+ }), s(A, { created: g, deleted: v });
343
351
  } else {
344
- const v = f.reduce((M, R) => {
345
- const X = t.get(R.id);
346
- return X ? [...M, X] : M;
352
+ const v = g.reduce((C, L) => {
353
+ const X = t.get(L.id);
354
+ return X ? [...C, X] : C;
347
355
  }, []);
348
356
  if (v.length > 0)
349
- throw Error(`Bulk insert would overwrite the following annotations: ${v.map((M) => M.id).join(", ")}`);
350
- f.forEach((M) => {
351
- t.set(M.id, M), M.bodies.forEach((R) => e.set(R.id, M.id));
352
- }), s(E, { created: f });
357
+ throw Error(`Bulk insert would overwrite the following annotations: ${v.map((C) => C.id).join(", ")}`);
358
+ g.forEach((C) => {
359
+ t.set(C.id, C), C.bodies.forEach((L) => e.set(L.id, C.id));
360
+ }), s(A, { created: g });
353
361
  }
354
- }, p = (f) => {
355
- const y = typeof f == "string" ? f : f.id, E = t.get(y);
356
- if (E)
357
- return t.delete(y), E.bodies.forEach((v) => e.delete(v.id)), E;
362
+ }, w = (g) => {
363
+ const y = typeof g == "string" ? g : g.id, A = t.get(y);
364
+ if (A)
365
+ return t.delete(y), A.bodies.forEach((v) => e.delete(v.id)), A;
358
366
  console.warn(`Attempt to delete missing annotation: ${y}`);
359
- }, B = (f, y = L.LOCAL) => {
360
- const E = p(f);
361
- E && s(y, { deleted: [E] });
362
- }, x = (f, y = L.LOCAL) => {
363
- const E = f.reduce((v, M) => {
364
- const R = p(M);
365
- return R ? [...v, R] : v;
367
+ }, B = (g, y = O.LOCAL) => {
368
+ const A = w(g);
369
+ A && s(y, { deleted: [A] });
370
+ }, b = (g, y = O.LOCAL) => {
371
+ const A = g.reduce((v, C) => {
372
+ const L = w(C);
373
+ return L ? [...v, L] : v;
366
374
  }, []);
367
- E.length > 0 && s(y, { deleted: E });
368
- }, w = (f, y = L.LOCAL) => {
369
- const E = t.get(f.annotation);
370
- if (E) {
371
- const v = E.bodies.find((M) => M.id === f.id);
375
+ A.length > 0 && s(y, { deleted: A });
376
+ }, c = (g, y = O.LOCAL) => {
377
+ const A = t.get(g.annotation);
378
+ if (A) {
379
+ const v = A.bodies.find((C) => C.id === g.id);
372
380
  if (v) {
373
381
  e.delete(v.id);
374
- const M = {
375
- ...E,
376
- bodies: E.bodies.filter((X) => X.id !== f.id)
382
+ const C = {
383
+ ...A,
384
+ bodies: A.bodies.filter((L) => L.id !== g.id)
377
385
  };
378
- t.set(E.id, M), s(y, { updated: [{
379
- oldValue: E,
380
- newValue: M,
386
+ t.set(A.id, C), s(y, { updated: [{
387
+ oldValue: A,
388
+ newValue: C,
381
389
  bodiesDeleted: [v]
382
390
  }] });
383
391
  } else
384
- console.warn(`Attempt to delete missing body ${f.id} from annotation ${f.annotation}`);
392
+ console.warn(`Attempt to delete missing body ${g.id} from annotation ${g.annotation}`);
385
393
  } else
386
- console.warn(`Attempt to delete body from missing annotation ${f.annotation}`);
387
- }, A = (f) => {
388
- const y = t.get(f);
394
+ console.warn(`Attempt to delete body from missing annotation ${g.annotation}`);
395
+ }, f = (g) => {
396
+ const y = t.get(g);
389
397
  return y ? { ...y } : void 0;
390
- }, u = (f) => {
391
- const y = e.get(f);
398
+ }, p = (g) => {
399
+ const y = e.get(g);
392
400
  if (y) {
393
- const v = A(y).bodies.find((M) => M.id === f);
394
- if (v)
395
- return v;
396
- console.error(`Store integrity error: body ${f} in index, but not in annotation`);
401
+ const A = f(y).bodies.find((v) => v.id === g);
402
+ if (A)
403
+ return A;
404
+ console.error(`Store integrity error: body ${g} in index, but not in annotation`);
397
405
  } else
398
- console.warn(`Attempt to retrieve missing body: ${f}`);
399
- }, a = (f, y) => {
400
- if (f.annotation !== y.annotation)
406
+ console.warn(`Attempt to retrieve missing body: ${g}`);
407
+ }, m = (g, y) => {
408
+ if (g.annotation !== y.annotation)
401
409
  throw "Annotation integrity violation: annotation ID must be the same when updating bodies";
402
- const E = t.get(f.annotation);
403
- if (E) {
404
- const v = E.bodies.find((R) => R.id === f.id), M = {
405
- ...E,
406
- bodies: E.bodies.map((R) => R.id === v.id ? y : R)
410
+ const A = t.get(g.annotation);
411
+ if (A) {
412
+ const v = A.bodies.find((L) => L.id === g.id), C = {
413
+ ...A,
414
+ bodies: A.bodies.map((L) => L.id === v.id ? y : L)
407
415
  };
408
- return t.set(E.id, M), v.id !== y.id && (e.delete(v.id), e.set(y.id, M.id)), {
409
- oldValue: E,
410
- newValue: M,
416
+ return t.set(A.id, C), v.id !== y.id && (e.delete(v.id), e.set(y.id, C.id)), {
417
+ oldValue: A,
418
+ newValue: C,
411
419
  bodiesUpdated: [{ oldBody: v, newBody: y }]
412
420
  };
413
421
  } else
414
- console.warn(`Attempt to add body to missing annotation ${f.annotation}`);
415
- }, m = (f, y, E = L.LOCAL) => {
416
- const v = a(f, y);
417
- s(E, { updated: [v] });
418
- }, T = (f, y = L.LOCAL) => {
419
- const E = f.map((v) => a({ id: v.id, annotation: v.annotation }, v));
420
- s(y, { updated: E });
421
- }, b = (f) => {
422
- const y = t.get(f.annotation);
422
+ console.warn(`Attempt to add body to missing annotation ${g.annotation}`);
423
+ }, E = (g, y, A = O.LOCAL) => {
424
+ const v = m(g, y);
425
+ v && s(A, { updated: [v] });
426
+ }, S = (g, y = O.LOCAL) => {
427
+ const A = g.map((v) => m({ id: v.id, annotation: v.annotation }, v)).filter(Boolean);
428
+ s(y, { updated: A });
429
+ }, M = (g) => {
430
+ const y = t.get(g.annotation);
423
431
  if (y) {
424
- const E = {
432
+ const A = {
425
433
  ...y,
426
434
  target: {
427
435
  ...y.target,
428
- ...f
436
+ ...g
429
437
  }
430
438
  };
431
- return t.set(y.id, E), {
439
+ return t.set(y.id, A), {
432
440
  oldValue: y,
433
- newValue: E,
441
+ newValue: A,
434
442
  targetUpdated: {
435
443
  oldTarget: y.target,
436
- newTarget: f
444
+ newTarget: g
437
445
  }
438
446
  };
439
447
  } else
440
- console.warn(`Attempt to update target on missing annotation: ${f.annotation}`);
448
+ console.warn(`Attempt to update target on missing annotation: ${g.annotation}`);
441
449
  };
442
450
  return {
443
- addAnnotation: c,
444
- addBody: l,
445
- all: h,
446
- bulkAddAnnotation: d,
447
- bulkDeleteAnnotation: x,
448
- bulkUpdateBodies: T,
449
- bulkUpdateTargets: (f, y = L.LOCAL) => {
450
- const E = f.map(b).filter((v) => v);
451
- E.length > 0 && s(y, { updated: E });
451
+ addAnnotation: r,
452
+ addBody: x,
453
+ all: u,
454
+ bulkAddAnnotation: h,
455
+ bulkDeleteAnnotation: b,
456
+ bulkUpdateAnnotation: d,
457
+ bulkUpdateBodies: S,
458
+ bulkUpdateTargets: (g, y = O.LOCAL) => {
459
+ const A = g.map((v) => M(v)).filter(Boolean);
460
+ A.length > 0 && s(y, { updated: A });
452
461
  },
453
- clear: g,
462
+ clear: T,
454
463
  deleteAnnotation: B,
455
- deleteBody: w,
456
- getAnnotation: A,
457
- getBody: u,
464
+ deleteBody: c,
465
+ getAnnotation: f,
466
+ getBody: p,
458
467
  observe: o,
459
468
  unobserve: i,
460
- updateAnnotation: r,
461
- updateBody: m,
462
- updateTarget: (f, y = L.LOCAL) => {
463
- const E = b(f);
464
- E && s(y, { updated: [E] });
469
+ updateAnnotation: l,
470
+ updateBody: E,
471
+ updateTarget: (g, y = O.LOCAL) => {
472
+ const A = M(g);
473
+ A && s(y, { updated: [A] });
465
474
  }
466
475
  };
467
- }, Xt = () => {
468
- const { subscribe: t, set: e } = K([]);
476
+ };
477
+ let Ht = () => ({
478
+ emit(t, ...e) {
479
+ for (let n = 0, o = this.events[t] || [], i = o.length; n < i; n++)
480
+ o[n](...e);
481
+ },
482
+ events: {},
483
+ on(t, e) {
484
+ var n;
485
+ return ((n = this.events)[t] || (n[t] = [])).push(e), () => {
486
+ var o;
487
+ this.events[t] = (o = this.events[t]) == null ? void 0 : o.filter((i) => e !== i);
488
+ };
489
+ }
490
+ });
491
+ const $t = 250, Ft = (t) => {
492
+ const e = Ht(), n = [];
493
+ let o = -1, i = !1, s = 0;
494
+ const r = (h) => {
495
+ if (!i) {
496
+ const { changes: w } = h, B = performance.now();
497
+ if (B - s > $t)
498
+ n.splice(o + 1), n.push(w), o = n.length - 1;
499
+ else {
500
+ const b = n.length - 1;
501
+ n[b] = Nt(n[b], w);
502
+ }
503
+ s = B;
504
+ }
505
+ i = !1;
506
+ };
507
+ t.observe(r, { origin: O.LOCAL });
508
+ const a = (h) => h && h.length > 0 && t.bulkDeleteAnnotation(h), l = (h) => h && h.length > 0 && t.bulkAddAnnotation(h, !1), d = (h) => h && h.length > 0 && t.bulkUpdateAnnotation(h.map(({ oldValue: w }) => w)), x = (h) => h && h.length > 0 && t.bulkUpdateAnnotation(h.map(({ newValue: w }) => w)), u = (h) => h && h.length > 0 && t.bulkAddAnnotation(h, !1), T = (h) => h && h.length > 0 && t.bulkDeleteAnnotation(h);
509
+ return {
510
+ canRedo: () => n.length - 1 > o,
511
+ canUndo: () => o > -1,
512
+ destroy: () => t.unobserve(r),
513
+ on: (h, w) => e.on(h, w),
514
+ redo: () => {
515
+ if (n.length - 1 > o) {
516
+ i = !0;
517
+ const { created: h, updated: w, deleted: B } = n[o + 1];
518
+ l(h), x(w), T(B), e.emit("redo", n[o + 1]), o += 1;
519
+ }
520
+ },
521
+ undo: () => {
522
+ if (o > -1) {
523
+ i = !0;
524
+ const { created: h, updated: w, deleted: B } = n[o];
525
+ a(h), d(w), u(B), e.emit("undo", n[o]), o -= 1;
526
+ }
527
+ }
528
+ };
529
+ }, Wt = () => {
530
+ const { subscribe: t, set: e } = tt([]);
469
531
  return {
470
532
  subscribe: t,
471
533
  set: e
472
534
  };
473
- }, Yt = (t, e, n, o, i, s) => {
474
- const c = /* @__PURE__ */ new Map();
475
- let r = [], l, h;
476
- const g = (x, w) => {
477
- c.has(x) ? c.get(x).push(w) : c.set(x, [w]);
478
- }, d = (x, w) => {
479
- const A = c.get(x);
480
- A && A.indexOf(w) > 0 && A.splice(A.indexOf(w), 1);
481
- }, p = (x, w, A) => {
482
- c.has(x) && setTimeout(() => {
483
- c.get(x).forEach((u) => {
484
- if (i) {
485
- const a = Array.isArray(w) ? w.map((T) => i.serialize(T)) : i.serialize(w), m = A ? A instanceof PointerEvent ? A : i.serialize(A) : void 0;
486
- u(a, m);
535
+ }, qt = (t, e, n, o) => {
536
+ const { store: i, selection: s, hover: r, viewport: a } = t, l = /* @__PURE__ */ new Map();
537
+ let d = [], x, u;
538
+ const T = (c, f) => {
539
+ l.has(c) ? l.get(c).push(f) : l.set(c, [f]);
540
+ }, h = (c, f) => {
541
+ const p = l.get(c);
542
+ p && p.indexOf(f) > 0 && p.splice(p.indexOf(f), 1);
543
+ }, w = (c, f, p) => {
544
+ l.has(c) && setTimeout(() => {
545
+ l.get(c).forEach((m) => {
546
+ if (n) {
547
+ const E = Array.isArray(f) ? f.map((M) => n.serialize(M)) : n.serialize(f), S = p ? p instanceof PointerEvent ? p : n.serialize(p) : void 0;
548
+ m(E, S);
487
549
  } else
488
- u(w, A);
550
+ m(f, p);
489
551
  });
490
552
  }, 1);
491
553
  }, B = () => {
492
- const { selected: x } = e, w = x.map(({ id: A }) => t.getAnnotation(A));
493
- w.forEach((A) => {
494
- const u = r.find((a) => a.id === A.id);
495
- (!u || !Y(u, A)) && p("updateAnnotation", A, u);
496
- }), r = r.map((A) => {
497
- const u = w.find(({ id: a }) => a === A.id);
498
- return u || A;
499
- });
554
+ const { selected: c } = s, f = (c || []).map(({ id: p }) => i.getAnnotation(p));
555
+ f.forEach((p) => {
556
+ const m = d.find((E) => E.id === p.id);
557
+ (!m || !U(m, p)) && w("updateAnnotation", p, m);
558
+ }), d = d.map((p) => f.find(({ id: E }) => E === p.id) || p);
500
559
  };
501
- return e.subscribe(({ selected: x }) => {
502
- if (!(r.length === 0 && x.length === 0)) {
503
- if (r.length === 0 && x.length > 0)
504
- r = x.map(({ id: w }) => t.getAnnotation(w));
505
- else if (r.length > 0 && x.length === 0)
506
- r.forEach((w) => {
507
- const A = t.getAnnotation(w.id);
508
- A && !Y(A, w) && p("updateAnnotation", A, w);
509
- }), r = [];
560
+ s.subscribe(({ selected: c }) => {
561
+ if (!(d.length === 0 && c.length === 0)) {
562
+ if (d.length === 0 && c.length > 0)
563
+ d = c.map(({ id: f }) => i.getAnnotation(f));
564
+ else if (d.length > 0 && c.length === 0)
565
+ d.forEach((f) => {
566
+ const p = i.getAnnotation(f.id);
567
+ p && !U(p, f) && w("updateAnnotation", p, f);
568
+ }), d = [];
510
569
  else {
511
- const w = new Set(r.map((a) => a.id)), A = new Set(x.map(({ id: a }) => a));
512
- r.filter((a) => !A.has(a.id)).forEach((a) => {
513
- const m = t.getAnnotation(a.id);
514
- m && !Y(m, a) && p("updateAnnotation", m, a);
515
- }), r = [
570
+ const f = new Set(d.map((m) => m.id)), p = new Set(c.map(({ id: m }) => m));
571
+ d.filter((m) => !p.has(m.id)).forEach((m) => {
572
+ const E = i.getAnnotation(m.id);
573
+ E && !U(E, m) && w("updateAnnotation", E, m);
574
+ }), d = [
516
575
  // Remove annotations that were deselected
517
- ...r.filter((a) => A.has(a.id)),
576
+ ...d.filter((m) => p.has(m.id)),
518
577
  // Add editable annotations that were selected
519
- ...x.filter(({ id: a }) => !w.has(a)).map(({ id: a }) => t.getAnnotation(a))
578
+ ...c.filter(({ id: m }) => !f.has(m)).map(({ id: m }) => i.getAnnotation(m))
520
579
  ];
521
580
  }
522
- p("selectionChanged", r);
581
+ w("selectionChanged", d);
523
582
  }
524
- }), n.subscribe((x) => {
525
- !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;
526
- }), o == null || o.subscribe((x) => p("viewportIntersect", x.map(t.getAnnotation))), t.observe((x) => {
527
- s && (h && clearTimeout(h), h = setTimeout(B, 1e3));
528
- const { created: w, deleted: A } = x.changes;
529
- w.forEach((a) => p("createAnnotation", a)), A.forEach((a) => p("deleteAnnotation", a)), x.changes.updated.filter((a) => [
530
- ...a.bodiesCreated || [],
531
- ...a.bodiesDeleted || [],
532
- ...a.bodiesUpdated || []
533
- ].length > 0).forEach(({ oldValue: a, newValue: m }) => {
534
- const T = r.find((b) => b.id === a.id) || a;
535
- r = r.map((b) => b.id === a.id ? m : b), p("updateAnnotation", m, T);
583
+ }), r.subscribe((c) => {
584
+ !x && c ? w("mouseEnterAnnotation", i.getAnnotation(c)) : x && !c ? w("mouseLeaveAnnotation", i.getAnnotation(x)) : x && c && (w("mouseLeaveAnnotation", i.getAnnotation(x)), w("mouseEnterAnnotation", i.getAnnotation(c))), x = c;
585
+ }), a == null || a.subscribe((c) => w("viewportIntersect", c.map((f) => i.getAnnotation(f)))), i.observe((c) => {
586
+ o && (u && clearTimeout(u), u = setTimeout(B, 1e3));
587
+ const { created: f, deleted: p } = c.changes;
588
+ (f || []).forEach((m) => w("createAnnotation", m)), (p || []).forEach((m) => w("deleteAnnotation", m)), (c.changes.updated || []).filter((m) => [
589
+ ...m.bodiesCreated || [],
590
+ ...m.bodiesDeleted || [],
591
+ ...m.bodiesUpdated || []
592
+ ].length > 0).forEach(({ oldValue: m, newValue: E }) => {
593
+ const S = d.find((M) => M.id === m.id) || m;
594
+ d = d.map((M) => M.id === m.id ? E : M), w("updateAnnotation", E, S);
536
595
  });
537
- }, { origin: L.LOCAL }), t.observe((x) => {
538
- if (r) {
539
- const w = new Set(r.map((u) => u.id)), A = x.changes.updated.filter(({ newValue: u }) => w.has(u.id)).map(({ newValue: u }) => u);
540
- A.length > 0 && (r = r.map((u) => {
541
- const a = A.find((m) => m.id === u.id);
542
- return a || u;
543
- }));
596
+ }, { origin: O.LOCAL }), i.observe((c) => {
597
+ if (d) {
598
+ const f = new Set(d.map((m) => m.id)), p = (c.changes.updated || []).filter(({ newValue: m }) => f.has(m.id)).map(({ newValue: m }) => m);
599
+ p.length > 0 && (d = d.map((m) => p.find((S) => S.id === m.id) || m));
544
600
  }
545
- }, { origin: L.REMOTE }), { on: g, off: d, emit: p };
546
- }, re = (t) => (e) => e.map((n) => t.serialize(n)), _t = (t) => (e) => e.reduce((n, o) => {
601
+ }, { origin: O.REMOTE });
602
+ const b = (c) => (f) => {
603
+ const { created: p, deleted: m, updated: E } = f;
604
+ (p || []).forEach((S) => w("createAnnotation", S)), (m || []).forEach((S) => w("deleteAnnotation", S)), c ? (E || []).forEach((S) => w("updateAnnotation", S.oldValue, S.newValue)) : (E || []).forEach((S) => w("updateAnnotation", S.newValue, S.oldValue));
605
+ };
606
+ return e.on("undo", b(!0)), e.on("redo", b(!1)), { on: T, off: h, emit: w };
607
+ }, jt = (t) => (e) => e.reduce((n, o) => {
547
608
  const { parsed: i, error: s } = t.parse(o);
548
609
  return s ? {
549
610
  parsed: n.parsed,
550
611
  failed: [...n.failed, o]
551
- } : {
612
+ } : i ? {
552
613
  parsed: [...n.parsed, i],
553
614
  failed: n.failed
615
+ } : {
616
+ ...n
554
617
  };
555
- }, { parsed: [], failed: [] }), It = (t, e) => {
556
- const n = (g) => {
557
- if (e) {
558
- const { parsed: d, error: p } = e.parse(g);
559
- d ? t.addAnnotation(d, L.REMOTE) : console.error(p);
618
+ }, { parsed: [], failed: [] }), Qt = (t, e, n) => {
619
+ const { store: o, selection: i } = t, s = (b) => {
620
+ if (n) {
621
+ const { parsed: c, error: f } = n.parse(b);
622
+ c ? o.addAnnotation(c, O.REMOTE) : console.error(f);
560
623
  } else
561
- t.addAnnotation(g, L.REMOTE);
562
- }, o = () => t.clear(), i = (g) => {
563
- const d = t.getAnnotation(g);
564
- return e && d ? e.serialize(d) : d;
565
- }, s = () => e ? t.all().map(e.serialize) : t.all(), c = (g) => fetch(g).then((d) => d.json()).then((d) => (l(d), d)), r = (g) => {
566
- if (typeof g == "string") {
567
- const d = t.getAnnotation(g);
568
- return t.deleteAnnotation(g), e ? e.serialize(d) : d;
624
+ o.addAnnotation(b, O.REMOTE);
625
+ }, r = () => i.clear(), a = () => o.clear(), l = (b) => {
626
+ const c = o.getAnnotation(b);
627
+ return n && c ? n.serialize(c) : c;
628
+ }, d = () => n ? o.all().map(n.serialize) : o.all(), x = () => {
629
+ var b;
630
+ const c = (((b = i.selected) == null ? void 0 : b.map((f) => f.id)) || []).map((f) => o.getAnnotation(f)).filter(Boolean);
631
+ return n ? c.map(n.serialize) : c;
632
+ }, u = (b) => fetch(b).then((c) => c.json()).then((c) => (h(c), c)), T = (b) => {
633
+ if (typeof b == "string") {
634
+ const c = o.getAnnotation(b);
635
+ if (o.deleteAnnotation(b), c)
636
+ return n ? n.serialize(c) : c;
569
637
  } else {
570
- const d = e ? e.parse(g).parsed : g;
571
- return t.deleteAnnotation(d), g;
638
+ const c = n ? n.parse(b).parsed : b;
639
+ if (c)
640
+ return o.deleteAnnotation(c), b;
572
641
  }
573
- }, l = (g) => {
574
- if (e) {
575
- const { parsed: d, failed: p } = _t(e)(g);
576
- p.length > 0 && console.warn(`Discarded ${p.length} invalid annotations`, p), t.bulkAddAnnotation(d, !0, L.REMOTE);
642
+ }, h = (b) => {
643
+ if (n) {
644
+ const { parsed: c, failed: f } = jt(n)(b);
645
+ f.length > 0 && console.warn(`Discarded ${f.length} invalid annotations`, f), o.bulkAddAnnotation(c, !0, O.REMOTE);
577
646
  } else
578
- t.bulkAddAnnotation(g, !0, L.REMOTE);
647
+ o.bulkAddAnnotation(b, !0, O.REMOTE);
648
+ }, w = (b) => {
649
+ b ? i.setSelected(b) : i.clear();
650
+ }, B = (b) => {
651
+ if (n) {
652
+ const c = n.parse(b).parsed, f = n.serialize(o.getAnnotation(c.id));
653
+ return o.updateAnnotation(c), f;
654
+ } else {
655
+ const c = o.getAnnotation(b.id);
656
+ return o.updateAnnotation(b), c;
657
+ }
579
658
  };
580
659
  return {
581
- addAnnotation: n,
582
- clearAnnotations: o,
583
- getAnnotationById: i,
584
- getAnnotations: s,
585
- loadAnnotations: c,
586
- removeAnnotation: r,
587
- setAnnotations: l,
588
- updateAnnotation: (g) => {
589
- if (e) {
590
- const d = e.parse(g).parsed, p = e.serialize(t.getAnnotation(d.id));
591
- return t.updateAnnotation(d), p;
592
- } else {
593
- const d = t.getAnnotation(g.id);
594
- return t.updateAnnotation(g), d;
595
- }
596
- }
660
+ addAnnotation: s,
661
+ cancelSelected: r,
662
+ canRedo: e.canRedo,
663
+ canUndo: e.canUndo,
664
+ clearAnnotations: a,
665
+ getAnnotationById: l,
666
+ getAnnotations: d,
667
+ getSelected: x,
668
+ loadAnnotations: u,
669
+ redo: e.redo,
670
+ removeAnnotation: T,
671
+ setAnnotations: h,
672
+ setSelected: w,
673
+ undo: e.undo,
674
+ updateAnnotation: B
597
675
  };
598
676
  };
599
- let Dt = (t) => crypto.getRandomValues(new Uint8Array(t)), Nt = (t, e, n) => {
677
+ let Gt = (t) => crypto.getRandomValues(new Uint8Array(t)), Jt = (t, e, n) => {
600
678
  let o = (2 << Math.log(t.length - 1) / Math.LN2) - 1, i = -~(1.6 * o * e / t.length);
601
679
  return (s = e) => {
602
- let c = "";
680
+ let r = "";
603
681
  for (; ; ) {
604
- let r = n(i), l = i;
682
+ let a = n(i), l = i;
605
683
  for (; l--; )
606
- if (c += t[r[l] & o] || "", c.length === s)
607
- return c;
684
+ if (r += t[a[l] & o] || "", r.length === s)
685
+ return r;
608
686
  }
609
687
  };
610
- }, Ut = (t, e = 21) => Nt(t, e, Dt), kt = (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), "");
611
- const zt = () => ({ isGuest: !0, id: Ut("1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_", 20)() }), Pt = (t) => {
688
+ }, Kt = (t, e = 21) => Jt(t, e, Gt), Zt = (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), "");
689
+ const te = () => ({ isGuest: !0, id: Kt("1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_", 20)() }), ee = (t) => {
612
690
  const e = JSON.stringify(t);
613
691
  let n = 0;
614
692
  for (let o = 0, i = e.length; o < i; o++) {
@@ -616,51 +694,86 @@ const zt = () => ({ isGuest: !0, id: Ut("1234567890abcdefghijklmnopqrstuvwxyzABC
616
694
  n = (n << 5) - n + s, n |= 0;
617
695
  }
618
696
  return `${n}`;
619
- }, ce = (t, e) => (Array.isArray(t) ? t : [t]).map((n) => {
620
- const { id: o, type: i, purpose: s, value: c, created: r, creator: l, ...h } = n;
697
+ }, dt = (t) => t ? typeof t == "object" ? { ...t } : t : void 0, ne = (t, e) => (Array.isArray(t) ? t : [t]).map((n) => {
698
+ const { id: o, type: i, purpose: s, value: r, created: a, creator: l, ...d } = n;
621
699
  return {
622
- id: o || `temp-${Pt(n)}`,
700
+ id: o || `temp-${ee(n)}`,
623
701
  annotation: e,
624
702
  type: i,
625
703
  purpose: s,
626
- value: c,
627
- created: r,
628
- creator: l ? typeof l == "object" ? { ...l } : l : void 0,
629
- ...h
704
+ value: r,
705
+ created: a ? new Date(a) : void 0,
706
+ creator: dt(l),
707
+ ...d
630
708
  };
631
- }), ae = (t) => t.map((e) => {
632
- var o;
633
- const n = { ...e };
634
- return delete n.annotation, (o = n.id) != null && o.startsWith("temp-") && delete n.id, n;
709
+ }), oe = (t) => t.map((e) => {
710
+ var n, o;
711
+ const i = { ...e };
712
+ return delete i.annotation, (n = i.id) != null && n.startsWith("temp-") && delete i.id, { ...i, created: (o = i.created) == null ? void 0 : o.toISOString() };
635
713
  });
636
- kt();
637
- function Ht(t, e, n, o, i) {
638
- it(t, e, n || 0, o || t.length - 1, i || $t);
714
+ Zt();
715
+ const D = (t, e) => {
716
+ const { start: n, end: o } = t.selector, i = t.selector.offsetReference ? t.selector.offsetReference : e;
717
+ if (!i)
718
+ return t;
719
+ const s = document.createNodeIterator(e, NodeFilter.SHOW_TEXT);
720
+ let r = 0, a = document.createRange(), l = s.nextNode();
721
+ l === null && console.error("Could not revive annotation target. Content missing.");
722
+ let d = !i;
723
+ for (; l !== null; ) {
724
+ const x = l.textContent.length;
725
+ if (!d && i && (d = i.contains(l)), d) {
726
+ if (r + x > n) {
727
+ a.setStart(l, n - r);
728
+ break;
729
+ }
730
+ r += x;
731
+ }
732
+ l = s.nextNode();
733
+ }
734
+ for (; l !== null; ) {
735
+ const x = l.textContent.length;
736
+ if (r + x > o) {
737
+ a.setEnd(l, o - r);
738
+ break;
739
+ }
740
+ r += x, l = s.nextNode();
741
+ }
742
+ return {
743
+ ...t,
744
+ selector: {
745
+ ...t.selector,
746
+ range: a
747
+ }
748
+ };
749
+ };
750
+ function ie(t, e, n, o, i) {
751
+ ut(t, e, n || 0, o || t.length - 1, i || se);
639
752
  }
640
- function it(t, e, n, o, i) {
753
+ function ut(t, e, n, o, i) {
641
754
  for (; o > n; ) {
642
755
  if (o - n > 600) {
643
- 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)), d = Math.min(o, Math.floor(e + (s - c) * l / s + h));
644
- it(t, e, g, d, i);
756
+ var s = o - n + 1, r = e - n + 1, a = Math.log(s), l = 0.5 * Math.exp(2 * a / 3), d = 0.5 * Math.sqrt(a * l * (s - l) / s) * (r - s / 2 < 0 ? -1 : 1), x = Math.max(n, Math.floor(e - r * l / s + d)), u = Math.min(o, Math.floor(e + (s - r) * l / s + d));
757
+ ut(t, e, x, u, i);
645
758
  }
646
- var p = t[e], B = n, x = o;
647
- for (U(t, n, e), i(t[o], p) > 0 && U(t, n, o); B < x; ) {
648
- for (U(t, B, x), B++, x--; i(t[B], p) < 0; )
649
- B++;
650
- for (; i(t[x], p) > 0; )
651
- x--;
759
+ var T = t[e], h = n, w = o;
760
+ for (N(t, n, e), i(t[o], T) > 0 && N(t, n, o); h < w; ) {
761
+ for (N(t, h, w), h++, w--; i(t[h], T) < 0; )
762
+ h++;
763
+ for (; i(t[w], T) > 0; )
764
+ w--;
652
765
  }
653
- i(t[n], p) === 0 ? U(t, n, x) : (x++, U(t, x, o)), x <= e && (n = x + 1), e <= x && (o = x - 1);
766
+ i(t[n], T) === 0 ? N(t, n, w) : (w++, N(t, w, o)), w <= e && (n = w + 1), e <= w && (o = w - 1);
654
767
  }
655
768
  }
656
- function U(t, e, n) {
769
+ function N(t, e, n) {
657
770
  var o = t[e];
658
771
  t[e] = t[n], t[n] = o;
659
772
  }
660
- function $t(t, e) {
773
+ function se(t, e) {
661
774
  return t < e ? -1 : t > e ? 1 : 0;
662
775
  }
663
- class Vt {
776
+ class re {
664
777
  constructor(e = 9) {
665
778
  this._maxEntries = Math.max(4, e), this._minEntries = Math.max(2, Math.ceil(this._maxEntries * 0.4)), this.clear();
666
779
  }
@@ -670,13 +783,13 @@ class Vt {
670
783
  search(e) {
671
784
  let n = this.data;
672
785
  const o = [];
673
- if (!V(e, n))
786
+ if (!j(e, n))
674
787
  return o;
675
788
  const i = this.toBBox, s = [];
676
789
  for (; n; ) {
677
- for (let c = 0; c < n.children.length; c++) {
678
- const r = n.children[c], l = n.leaf ? i(r) : r;
679
- V(e, l) && (n.leaf ? o.push(r) : J(e, l) ? this._all(r, o) : s.push(r));
790
+ for (let r = 0; r < n.children.length; r++) {
791
+ const a = n.children[r], l = n.leaf ? i(a) : a;
792
+ j(e, l) && (n.leaf ? o.push(a) : Z(e, l) ? this._all(a, o) : s.push(a));
680
793
  }
681
794
  n = s.pop();
682
795
  }
@@ -684,14 +797,14 @@ class Vt {
684
797
  }
685
798
  collides(e) {
686
799
  let n = this.data;
687
- if (!V(e, n))
800
+ if (!j(e, n))
688
801
  return !1;
689
802
  const o = [];
690
803
  for (; n; ) {
691
804
  for (let i = 0; i < n.children.length; i++) {
692
- const s = n.children[i], c = n.leaf ? this.toBBox(s) : s;
693
- if (V(e, c)) {
694
- if (n.leaf || J(e, c))
805
+ const s = n.children[i], r = n.leaf ? this.toBBox(s) : s;
806
+ if (j(e, r)) {
807
+ if (n.leaf || Z(e, r))
695
808
  return !0;
696
809
  o.push(s);
697
810
  }
@@ -726,21 +839,21 @@ class Vt {
726
839
  return e && this._insert(e, this.data.height - 1), this;
727
840
  }
728
841
  clear() {
729
- return this.data = D([]), this;
842
+ return this.data = _([]), this;
730
843
  }
731
844
  remove(e, n) {
732
845
  if (!e)
733
846
  return this;
734
847
  let o = this.data;
735
- const i = this.toBBox(e), s = [], c = [];
736
- let r, l, h;
848
+ const i = this.toBBox(e), s = [], r = [];
849
+ let a, l, d;
737
850
  for (; o || s.length; ) {
738
- if (o || (o = s.pop(), l = s[s.length - 1], r = c.pop(), h = !0), o.leaf) {
739
- const g = Wt(e, o.children, n);
740
- if (g !== -1)
741
- return o.children.splice(g, 1), s.push(o), this._condense(s), this;
851
+ if (o || (o = s.pop(), l = s[s.length - 1], a = r.pop(), d = !0), o.leaf) {
852
+ const x = ae(e, o.children, n);
853
+ if (x !== -1)
854
+ return o.children.splice(x, 1), s.push(o), this._condense(s), this;
742
855
  }
743
- !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;
856
+ !d && !o.leaf && Z(o, i) ? (s.push(o), r.push(a), a = 0, l = o, o = o.children[0]) : l ? (a++, o = l.children[a], d = !1) : o = null;
744
857
  }
745
858
  return this;
746
859
  }
@@ -767,36 +880,36 @@ class Vt {
767
880
  }
768
881
  _build(e, n, o, i) {
769
882
  const s = o - n + 1;
770
- let c = this._maxEntries, r;
771
- if (s <= c)
772
- return r = D(e.slice(n, o + 1)), I(r, this.toBBox), r;
773
- i || (i = Math.ceil(Math.log(s) / Math.log(c)), c = Math.ceil(s / Math.pow(c, i - 1))), r = D([]), r.leaf = !1, r.height = i;
774
- const l = Math.ceil(s / c), h = l * Math.ceil(Math.sqrt(c));
775
- nt(e, n, o, h, this.compareMinX);
776
- for (let g = n; g <= o; g += h) {
777
- const d = Math.min(g + h - 1, o);
778
- nt(e, g, d, l, this.compareMinY);
779
- for (let p = g; p <= d; p += l) {
780
- const B = Math.min(p + l - 1, d);
781
- r.children.push(this._build(e, p, B, i - 1));
883
+ let r = this._maxEntries, a;
884
+ if (s <= r)
885
+ return a = _(e.slice(n, o + 1)), V(a, this.toBBox), a;
886
+ i || (i = Math.ceil(Math.log(s) / Math.log(r)), r = Math.ceil(s / Math.pow(r, i - 1))), a = _([]), a.leaf = !1, a.height = i;
887
+ const l = Math.ceil(s / r), d = l * Math.ceil(Math.sqrt(r));
888
+ rt(e, n, o, d, this.compareMinX);
889
+ for (let x = n; x <= o; x += d) {
890
+ const u = Math.min(x + d - 1, o);
891
+ rt(e, x, u, l, this.compareMinY);
892
+ for (let T = x; T <= u; T += l) {
893
+ const h = Math.min(T + l - 1, u);
894
+ a.children.push(this._build(e, T, h, i - 1));
782
895
  }
783
896
  }
784
- return I(r, this.toBBox), r;
897
+ return V(a, this.toBBox), a;
785
898
  }
786
899
  _chooseSubtree(e, n, o, i) {
787
900
  for (; i.push(n), !(n.leaf || i.length - 1 === o); ) {
788
- let s = 1 / 0, c = 1 / 0, r;
901
+ let s = 1 / 0, r = 1 / 0, a;
789
902
  for (let l = 0; l < n.children.length; l++) {
790
- const h = n.children[l], g = G(h), d = jt(e, h) - g;
791
- d < c ? (c = d, s = g < s ? g : s, r = h) : d === c && g < s && (s = g, r = h);
903
+ const d = n.children[l], x = K(d), u = de(e, d) - x;
904
+ u < r ? (r = u, s = x < s ? x : s, a = d) : u === r && x < s && (s = x, a = d);
792
905
  }
793
- n = r || n.children[0];
906
+ n = a || n.children[0];
794
907
  }
795
908
  return n;
796
909
  }
797
910
  _insert(e, n, o) {
798
- const i = o ? e : this.toBBox(e), s = [], c = this._chooseSubtree(i, this.data, n, s);
799
- for (c.children.push(e), P(c, i); n >= 0 && s[n].children.length > this._maxEntries; )
911
+ const i = o ? e : this.toBBox(e), s = [], r = this._chooseSubtree(i, this.data, n, s);
912
+ for (r.children.push(e), z(r, i); n >= 0 && s[n].children.length > this._maxEntries; )
800
913
  this._split(s, n), n--;
801
914
  this._adjustParentBBoxes(i, s, n);
802
915
  }
@@ -804,50 +917,50 @@ class Vt {
804
917
  _split(e, n) {
805
918
  const o = e[n], i = o.children.length, s = this._minEntries;
806
919
  this._chooseSplitAxis(o, s, i);
807
- const c = this._chooseSplitIndex(o, s, i), r = D(o.children.splice(c, o.children.length - c));
808
- r.height = o.height, r.leaf = o.leaf, I(o, this.toBBox), I(r, this.toBBox), n ? e[n - 1].children.push(r) : this._splitRoot(o, r);
920
+ const r = this._chooseSplitIndex(o, s, i), a = _(o.children.splice(r, o.children.length - r));
921
+ a.height = o.height, a.leaf = o.leaf, V(o, this.toBBox), V(a, this.toBBox), n ? e[n - 1].children.push(a) : this._splitRoot(o, a);
809
922
  }
810
923
  _splitRoot(e, n) {
811
- this.data = D([e, n]), this.data.height = e.height + 1, this.data.leaf = !1, I(this.data, this.toBBox);
924
+ this.data = _([e, n]), this.data.height = e.height + 1, this.data.leaf = !1, V(this.data, this.toBBox);
812
925
  }
813
926
  _chooseSplitIndex(e, n, o) {
814
- let i, s = 1 / 0, c = 1 / 0;
815
- for (let r = n; r <= o - n; r++) {
816
- const l = z(e, 0, r, this.toBBox), h = z(e, r, o, this.toBBox), g = Gt(l, h), d = G(l) + G(h);
817
- g < s ? (s = g, i = r, c = d < c ? d : c) : g === s && d < c && (c = d, i = r);
927
+ let i, s = 1 / 0, r = 1 / 0;
928
+ for (let a = n; a <= o - n; a++) {
929
+ const l = P(e, 0, a, this.toBBox), d = P(e, a, o, this.toBBox), x = ue(l, d), u = K(l) + K(d);
930
+ x < s ? (s = x, i = a, r = u < r ? u : r) : x === s && u < r && (r = u, i = a);
818
931
  }
819
932
  return i || o - n;
820
933
  }
821
934
  // sorts node children by the best axis for split
822
935
  _chooseSplitAxis(e, n, o) {
823
- const i = e.leaf ? this.compareMinX : qt, s = e.leaf ? this.compareMinY : Ft, c = this._allDistMargin(e, n, o, i), r = this._allDistMargin(e, n, o, s);
824
- c < r && e.children.sort(i);
936
+ const i = e.leaf ? this.compareMinX : ce, s = e.leaf ? this.compareMinY : le, r = this._allDistMargin(e, n, o, i), a = this._allDistMargin(e, n, o, s);
937
+ r < a && e.children.sort(i);
825
938
  }
826
939
  // total margin of all possible split distributions where each node is at least m full
827
940
  _allDistMargin(e, n, o, i) {
828
941
  e.children.sort(i);
829
- const s = this.toBBox, c = z(e, 0, n, s), r = z(e, o - n, o, s);
830
- let l = $(c) + $(r);
831
- for (let h = n; h < o - n; h++) {
832
- const g = e.children[h];
833
- P(c, e.leaf ? s(g) : g), l += $(c);
942
+ const s = this.toBBox, r = P(e, 0, n, s), a = P(e, o - n, o, s);
943
+ let l = q(r) + q(a);
944
+ for (let d = n; d < o - n; d++) {
945
+ const x = e.children[d];
946
+ z(r, e.leaf ? s(x) : x), l += q(r);
834
947
  }
835
- for (let h = o - n - 1; h >= n; h--) {
836
- const g = e.children[h];
837
- P(r, e.leaf ? s(g) : g), l += $(r);
948
+ for (let d = o - n - 1; d >= n; d--) {
949
+ const x = e.children[d];
950
+ z(a, e.leaf ? s(x) : x), l += q(a);
838
951
  }
839
952
  return l;
840
953
  }
841
954
  _adjustParentBBoxes(e, n, o) {
842
955
  for (let i = o; i >= 0; i--)
843
- P(n[i], e);
956
+ z(n[i], e);
844
957
  }
845
958
  _condense(e) {
846
959
  for (let n = e.length - 1, o; n >= 0; n--)
847
- e[n].children.length === 0 ? n > 0 ? (o = e[n - 1].children, o.splice(o.indexOf(e[n]), 1)) : this.clear() : I(e[n], this.toBBox);
960
+ e[n].children.length === 0 ? n > 0 ? (o = e[n - 1].children, o.splice(o.indexOf(e[n]), 1)) : this.clear() : V(e[n], this.toBBox);
848
961
  }
849
962
  }
850
- function Wt(t, e, n) {
963
+ function ae(t, e, n) {
851
964
  if (!n)
852
965
  return e.indexOf(t);
853
966
  for (let o = 0; o < e.length; o++)
@@ -855,46 +968,46 @@ function Wt(t, e, n) {
855
968
  return o;
856
969
  return -1;
857
970
  }
858
- function I(t, e) {
859
- z(t, 0, t.children.length, e, t);
971
+ function V(t, e) {
972
+ P(t, 0, t.children.length, e, t);
860
973
  }
861
- function z(t, e, n, o, i) {
862
- i || (i = D(null)), i.minX = 1 / 0, i.minY = 1 / 0, i.maxX = -1 / 0, i.maxY = -1 / 0;
974
+ function P(t, e, n, o, i) {
975
+ i || (i = _(null)), i.minX = 1 / 0, i.minY = 1 / 0, i.maxX = -1 / 0, i.maxY = -1 / 0;
863
976
  for (let s = e; s < n; s++) {
864
- const c = t.children[s];
865
- P(i, t.leaf ? o(c) : c);
977
+ const r = t.children[s];
978
+ z(i, t.leaf ? o(r) : r);
866
979
  }
867
980
  return i;
868
981
  }
869
- function P(t, e) {
982
+ function z(t, e) {
870
983
  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;
871
984
  }
872
- function qt(t, e) {
985
+ function ce(t, e) {
873
986
  return t.minX - e.minX;
874
987
  }
875
- function Ft(t, e) {
988
+ function le(t, e) {
876
989
  return t.minY - e.minY;
877
990
  }
878
- function G(t) {
991
+ function K(t) {
879
992
  return (t.maxX - t.minX) * (t.maxY - t.minY);
880
993
  }
881
- function $(t) {
994
+ function q(t) {
882
995
  return t.maxX - t.minX + (t.maxY - t.minY);
883
996
  }
884
- function jt(t, e) {
997
+ function de(t, e) {
885
998
  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));
886
999
  }
887
- function Gt(t, e) {
1000
+ function ue(t, e) {
888
1001
  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);
889
1002
  return Math.max(0, i - n) * Math.max(0, s - o);
890
1003
  }
891
- function J(t, e) {
1004
+ function Z(t, e) {
892
1005
  return t.minX <= e.minX && t.minY <= e.minY && e.maxX <= t.maxX && e.maxY <= t.maxY;
893
1006
  }
894
- function V(t, e) {
1007
+ function j(t, e) {
895
1008
  return e.minX <= t.maxX && e.minY <= t.maxY && e.maxX >= t.minX && e.maxY >= t.minY;
896
1009
  }
897
- function D(t) {
1010
+ function _(t) {
898
1011
  return {
899
1012
  children: t,
900
1013
  height: 1,
@@ -905,16 +1018,26 @@ function D(t) {
905
1018
  maxY: -1 / 0
906
1019
  };
907
1020
  }
908
- function nt(t, e, n, o, i) {
1021
+ function rt(t, e, n, o, i) {
909
1022
  const s = [e, n];
910
1023
  for (; s.length; ) {
911
1024
  if (n = s.pop(), e = s.pop(), n - e <= o)
912
1025
  continue;
913
- const c = e + Math.ceil((n - e) / o / 2) * o;
914
- Ht(t, c, e, n, i), s.push(e, c, c, n);
1026
+ const r = e + Math.ceil((n - e) / o / 2) * o;
1027
+ ie(t, r, e, n, i), s.push(e, r, r, n);
915
1028
  }
916
1029
  }
917
- const Jt = (t, e) => {
1030
+ const he = (t, e, n = 10, o) => {
1031
+ const i = o ? t.startContainer.parentElement.closest(o) : e, s = document.createRange();
1032
+ s.setStart(i, 0), s.setEnd(t.startContainer, t.startOffset);
1033
+ const r = s.toString(), a = document.createRange();
1034
+ a.setStart(t.endContainer, t.endOffset), i === document.body ? a.setEnd(i, i.childNodes.length) : a.setEndAfter(i);
1035
+ const l = a.toString();
1036
+ return {
1037
+ prefix: r.substring(r.length - n),
1038
+ suffix: l.substring(0, n)
1039
+ };
1040
+ }, fe = (t, e) => {
918
1041
  const n = (s) => Math.round(s * 10) / 10, o = {
919
1042
  top: n(t.top),
920
1043
  bottom: n(t.bottom),
@@ -938,305 +1061,392 @@ const Jt = (t, e) => {
938
1061
  return "block-contains";
939
1062
  } else if (o.top >= i.top && o.bottom <= i.bottom && o.left >= i.left && o.right <= i.right)
940
1063
  return "block-is-contained";
941
- }, Kt = (t, e) => {
1064
+ }, pe = (t, e) => {
942
1065
  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);
943
1066
  return new DOMRect(n, i, o - n, s - i);
944
- }, Qt = (t) => t.reduce((e, n) => {
1067
+ }, ge = (t) => t.reduce((e, n) => {
945
1068
  if (n.width === 0 || n.height === 0)
946
1069
  return e;
947
1070
  let o = [...e], i = !1;
948
1071
  for (const s of e) {
949
- const c = Jt(n, s);
950
- if (c === "inline-adjacent") {
951
- o = o.map((r) => r === s ? Kt(n, s) : r), i = !0;
1072
+ const r = fe(n, s);
1073
+ if (r === "inline-adjacent") {
1074
+ o = o.map((a) => a === s ? pe(n, s) : a), i = !0;
952
1075
  break;
953
- } else if (c === "inline-contains") {
954
- o = o.map((r) => r === s ? n : r), i = !0;
1076
+ } else if (r === "inline-contains") {
1077
+ o = o.map((a) => a === s ? n : a), i = !0;
955
1078
  break;
956
- } else if (c === "inline-is-contained") {
1079
+ } else if (r === "inline-is-contained") {
957
1080
  i = !0;
958
1081
  break;
959
- } else if (c === "block-contains" || c === "block-is-contained") {
960
- n.width < s.width && (o = o.map((r) => r === s ? n : r)), i = !0;
1082
+ } else if (r === "block-contains" || r === "block-is-contained") {
1083
+ n.width < s.width && (o = o.map((a) => a === s ? n : a)), i = !0;
961
1084
  break;
962
1085
  }
963
1086
  }
964
1087
  return i ? o : [...o, n];
965
- }, []), Zt = (t) => {
1088
+ }, []), me = (t) => {
1089
+ var i;
966
1090
  const { startContainer: e, endContainer: n } = t;
967
1091
  if (e.nodeType === Node.TEXT_NODE && n.nodeType === Node.TEXT_NODE)
968
1092
  return t;
969
1093
  if (e.nodeType !== Node.TEXT_NODE) {
970
- 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();
971
- t.setEnd(s, 0);
1094
+ 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();
1095
+ t.setEnd(r, 0);
972
1096
  }
973
1097
  if (n.nodeType !== Node.TEXT_NODE) {
974
- 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();
975
- t.setEnd(s, s.textContent.length);
1098
+ 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();
1099
+ t.setEnd(r, ((i = r == null ? void 0 : r.textContent) == null ? void 0 : i.length) || 0);
976
1100
  }
977
1101
  return t;
978
- }, te = (t, e) => {
979
- const n = new Vt(), o = /* @__PURE__ */ new Map(), i = (u) => {
980
- const a = e.getBoundingClientRect(), T = u.selector.range instanceof Range && !u.selector.range.collapsed && u.selector.range.startContainer.nodeType === Node.TEXT_NODE && u.selector.range.endContainer.nodeType === Node.TEXT_NODE ? u : k(u, e), b = Array.from(T.selector.range.getClientRects()), C = Qt(b);
981
- return C.map((S) => {
982
- const { x: f, y, width: E, height: v } = S;
1102
+ }, be = (t, e) => {
1103
+ const n = new re(), o = /* @__PURE__ */ new Map(), i = (c) => {
1104
+ const f = e.getBoundingClientRect(), m = c.selector.range instanceof Range && !c.selector.range.collapsed && c.selector.range.startContainer.nodeType === Node.TEXT_NODE && c.selector.range.endContainer.nodeType === Node.TEXT_NODE ? c : D(c, e), E = Array.from(m.selector.range.getClientRects()), S = ge(E);
1105
+ return S.map((M) => {
1106
+ const { x: g, y, width: A, height: v } = M;
983
1107
  return {
984
- minX: f - a.x,
985
- minY: y - a.y,
986
- maxX: f - a.x + E,
987
- maxY: y - a.y + v,
1108
+ minX: g - f.x,
1109
+ minY: y - f.y,
1110
+ maxX: g - f.x + A,
1111
+ maxY: y - f.y + v,
988
1112
  annotation: {
989
- id: u.annotation,
990
- rects: C
1113
+ id: c.annotation,
1114
+ rects: S
991
1115
  }
992
1116
  };
993
1117
  });
994
- }, s = () => [...o.values()], c = () => {
1118
+ }, s = () => [...o.values()], r = () => {
995
1119
  n.clear(), o.clear();
996
- }, r = (u) => {
997
- const a = i(u);
998
- a.forEach((m) => n.insert(m)), o.set(u.annotation, a);
999
- }, l = (u) => {
1000
- o.get(u.annotation).forEach((m) => n.remove(m)), o.delete(u.annotation);
1001
- }, h = (u) => {
1002
- l(u), r(u);
1003
- }, g = (u, a = !0) => {
1004
- a && c();
1005
- const m = u.map((b) => ({ target: b, rects: i(b) }));
1006
- m.forEach(({ target: b, rects: C }) => o.set(b.annotation, C));
1007
- const T = m.reduce((b, { rects: C }) => [...b, ...C], []);
1008
- n.load(T);
1009
- }, d = (u, a) => {
1010
- const m = n.search({
1011
- minX: u,
1012
- minY: a,
1013
- maxX: u,
1014
- maxY: a
1015
- }), T = (b) => b.annotation.rects.reduce((C, S) => C + S.width * S.height, 0);
1016
- if (m.length > 0)
1017
- return m.sort((b, C) => T(b) - T(C)), m[0].annotation.id;
1018
- }, p = (u) => {
1019
- const a = B(u);
1020
- if (a.length === 0)
1120
+ }, a = (c) => {
1121
+ const f = i(c);
1122
+ f.forEach((p) => n.insert(p)), o.set(c.annotation, f);
1123
+ }, l = (c) => {
1124
+ o.get(c.annotation).forEach((p) => n.remove(p)), o.delete(c.annotation);
1125
+ }, d = (c) => {
1126
+ l(c), a(c);
1127
+ }, x = (c, f = !0) => {
1128
+ f && r();
1129
+ const p = c.map((E) => ({ target: E, rects: i(E) }));
1130
+ p.forEach(({ target: E, rects: S }) => o.set(E.annotation, S));
1131
+ const m = p.reduce((E, { rects: S }) => [...E, ...S], []);
1132
+ n.load(m);
1133
+ }, u = (c, f) => {
1134
+ const p = n.search({
1135
+ minX: c,
1136
+ minY: f,
1137
+ maxX: c,
1138
+ maxY: f
1139
+ }), m = (E) => E.annotation.rects.reduce((S, M) => S + M.width * M.height, 0);
1140
+ if (p.length > 0)
1141
+ return p.sort((E, S) => m(E) - m(S)), p[0].annotation.id;
1142
+ }, T = (c) => {
1143
+ const f = h(c);
1144
+ if (f.length === 0)
1021
1145
  return;
1022
- let m = a[0].left, T = a[0].top, b = a[0].right, C = a[0].bottom;
1023
- for (let S = 1; S < a.length; S++) {
1024
- const f = a[S];
1025
- m = Math.min(m, f.left), T = Math.min(T, f.top), b = Math.max(b, f.right), C = Math.max(C, f.bottom);
1146
+ let p = f[0].left, m = f[0].top, E = f[0].right, S = f[0].bottom;
1147
+ for (let M = 1; M < f.length; M++) {
1148
+ const g = f[M];
1149
+ p = Math.min(p, g.left), m = Math.min(m, g.top), E = Math.max(E, g.right), S = Math.max(S, g.bottom);
1026
1150
  }
1027
- return new DOMRect(m, T, b - m, C - T);
1028
- }, B = (u) => {
1029
- const a = o.get(u);
1030
- return a ? a[0].annotation.rects : [];
1151
+ return new DOMRect(p, m, E - p, S - m);
1152
+ }, h = (c) => {
1153
+ const f = o.get(c);
1154
+ return f ? f[0].annotation.rects : [];
1031
1155
  };
1032
1156
  return {
1033
1157
  all: s,
1034
- clear: c,
1035
- getAt: d,
1036
- getBoundsForAnnotation: p,
1037
- getDOMRectsForAnnotation: B,
1038
- getIntersectingRects: (u, a, m, T) => n.search({ minX: u, minY: a, maxX: m, maxY: T }),
1039
- insert: r,
1040
- recalculate: () => g(t.all().map((u) => u.target), !0),
1158
+ clear: r,
1159
+ getAt: u,
1160
+ getBoundsForAnnotation: T,
1161
+ getDOMRectsForAnnotation: h,
1162
+ getIntersectingRects: (c, f, p, m) => n.search({ minX: c, minY: f, maxX: p, maxY: m }),
1163
+ insert: a,
1164
+ recalculate: () => x(t.all().map((c) => c.target), !0),
1041
1165
  remove: l,
1042
- set: g,
1166
+ set: x,
1043
1167
  size: () => n.all().length,
1044
- update: h
1168
+ update: d
1045
1169
  };
1046
- }, W = (t) => {
1170
+ }, Q = (t) => {
1047
1171
  const { range: e } = t.target.selector;
1048
1172
  return e instanceof Range && !e.collapsed;
1049
- }, ee = (t, e) => {
1050
- const n = Ot(), o = te(n, t), i = xt(n, e), s = mt(n), c = Xt(), r = (A, u = L.LOCAL) => {
1051
- const a = A.target.selector.range instanceof Range ? A : { ...A, target: k(A.target, t) }, { range: m } = a.target.selector, T = m && !m.collapsed;
1052
- return T && n.addAnnotation(a, u), T;
1053
- }, l = (A, u = !0, a = L.LOCAL) => {
1054
- const m = A.map((b) => W(b) ? b : { ...b, target: k(b.target, t) });
1055
- if (m.some((b) => b.target.selector.range.collapsed)) {
1056
- const b = m.filter((C) => C.target.selector.range.collapsed);
1057
- return console.warn("Could not revive all targets"), console.warn(b), n.bulkAddAnnotation(m, u, a), b;
1173
+ }, we = (t, e) => {
1174
+ const n = zt(), o = be(n, t), i = Xt(n, e), s = Rt(n), r = Wt(), a = (b, c = O.LOCAL) => {
1175
+ const f = b.target.selector.range instanceof Range ? b : { ...b, target: D(b.target, t) }, { range: p } = f.target.selector, m = p && !p.collapsed;
1176
+ return m && n.addAnnotation(f, c), m;
1177
+ }, l = (b, c = !0, f = O.LOCAL) => {
1178
+ const p = b.map((E) => Q(E) ? E : { ...E, target: D(E.target, t) });
1179
+ if (p.some((E) => E.target.selector.range.collapsed)) {
1180
+ const E = p.filter((S) => S.target.selector.range.collapsed);
1181
+ return console.warn("Could not revive all targets"), console.warn(E), n.bulkAddAnnotation(p, c, f), E;
1058
1182
  } else
1059
- return n.bulkAddAnnotation(m, u, a), [];
1060
- }, h = (A, u = L.LOCAL) => {
1061
- const a = A.selector.range instanceof Range ? A : k(A, t);
1062
- n.updateTarget(a, u);
1063
- }, g = (A, u = L.LOCAL) => {
1064
- const a = A.map((m) => m.selector.range instanceof Range ? m : k(m, t));
1065
- n.bulkUpdateTargets(a, u);
1066
- }, d = (A, u) => {
1067
- const a = o.getAt(A, u);
1068
- return a ? n.getAnnotation(a) : void 0;
1069
- }, p = (A, u, a, m) => {
1070
- const T = o.getIntersectingRects(A, u, a, m);
1071
- return Array.from(new Set(T.map((C) => C.annotation.id))).map((C) => n.getAnnotation(C)).filter((C) => C);
1072
- }, B = (A, u, a, m = 5) => {
1073
- const T = o.getDOMRectsForAnnotation(A);
1074
- if (T.length > 0) {
1075
- if (u && a) {
1076
- const b = T.find(({ top: C, right: S, bottom: f, left: y }) => u >= y - m && u <= S + m && a >= C - m && a <= f + m);
1077
- if (b)
1078
- return b;
1183
+ return n.bulkAddAnnotation(p, c, f), [];
1184
+ }, d = (b, c = O.LOCAL) => {
1185
+ const f = b.selector.range instanceof Range ? b : D(b, t);
1186
+ n.updateTarget(f, c);
1187
+ }, x = (b, c = O.LOCAL) => {
1188
+ const f = b.map((p) => p.selector.range instanceof Range ? p : D(p, t));
1189
+ n.bulkUpdateTargets(f, c);
1190
+ }, u = (b, c) => {
1191
+ const f = o.getAt(b, c);
1192
+ return f ? n.getAnnotation(f) : void 0;
1193
+ }, T = (b, c, f, p) => {
1194
+ const m = o.getIntersectingRects(b, c, f, p);
1195
+ return Array.from(new Set(m.map((S) => S.annotation.id))).map((S) => n.getAnnotation(S)).filter((S) => S);
1196
+ }, h = (b, c, f, p = 5) => {
1197
+ const m = o.getDOMRectsForAnnotation(b);
1198
+ if (m.length > 0) {
1199
+ if (c && f) {
1200
+ const E = m.find(({ top: S, right: M, bottom: g, left: y }) => c >= y - p && c <= M + p && f >= S - p && f <= g + p);
1201
+ if (E)
1202
+ return E;
1079
1203
  }
1080
- return o.getBoundsForAnnotation(A);
1204
+ return o.getBoundsForAnnotation(b);
1081
1205
  }
1082
- }, x = (A, u, a, m) => {
1083
- const b = o.getIntersectingRects(A, u, a, m).reduce((C, S) => ((C[S.annotation.id] = C[S.annotation.id] || []).push(S), C), {});
1084
- return Object.entries(b).map(([C, S]) => ({
1085
- annotation: n.getAnnotation(C),
1086
- rects: S.map(({ minX: f, minY: y, maxX: E, maxY: v }) => ({ x: f, y, width: E - f, height: v - y }))
1206
+ }, w = (b, c, f, p) => {
1207
+ const E = o.getIntersectingRects(b, c, f, p).reduce((S, M) => ((S[M.annotation.id] = S[M.annotation.id] || []).push(M), S), {});
1208
+ return Object.entries(E).map(([S, M]) => ({
1209
+ annotation: n.getAnnotation(S),
1210
+ rects: M.map(({ minX: g, minY: y, maxX: A, maxY: v }) => ({ x: g, y, width: A - g, height: v - y }))
1087
1211
  }));
1088
- }, w = () => o.recalculate();
1089
- return n.observe(({ changes: A }) => {
1090
- const u = (A.created || []).filter(W), a = (A.deleted || []).filter(W), m = (A.updated || []).filter((T) => W(T.newValue));
1091
- u.length > 0 && o.set(u.map((T) => T.target), !1), (a == null ? void 0 : a.length) > 0 && a.forEach((T) => o.remove(T.target)), (m == null ? void 0 : m.length) > 0 && m.forEach(({ newValue: T }) => o.update(T.target));
1212
+ }, B = () => o.recalculate();
1213
+ return n.observe(({ changes: b }) => {
1214
+ const c = (b.created || []).filter(Q), f = (b.deleted || []).filter(Q), p = (b.updated || []).filter((m) => Q(m.newValue));
1215
+ c.length > 0 && o.set(c.map((m) => m.target), !1), (f == null ? void 0 : f.length) > 0 && f.forEach((m) => o.remove(m.target)), (p == null ? void 0 : p.length) > 0 && p.forEach(({ newValue: m }) => o.update(m.target));
1092
1216
  }), {
1093
1217
  store: {
1094
1218
  ...n,
1095
- addAnnotation: r,
1219
+ addAnnotation: a,
1096
1220
  bulkAddAnnotation: l,
1097
- bulkUpdateTargets: g,
1098
- getAnnotationBounds: B,
1099
- getAt: d,
1100
- getIntersecting: p,
1101
- getIntersectingRects: x,
1102
- recalculatePositions: w,
1103
- updateTarget: h
1221
+ bulkUpdateTargets: x,
1222
+ getAnnotationBounds: h,
1223
+ getAt: u,
1224
+ getIntersecting: T,
1225
+ getIntersectingRects: w,
1226
+ recalculatePositions: B,
1227
+ updateTarget: d
1104
1228
  },
1105
1229
  selection: i,
1106
1230
  hover: s,
1107
- viewport: c
1231
+ viewport: r
1108
1232
  };
1109
- }, ne = (t, e = {}) => {
1110
- const n = /* @__PURE__ */ new Map(), o = (s) => Array.from(n.entries()).filter(([c, r]) => r.presenceKey === s.presenceKey).map(([c, r]) => c);
1111
- return t.on("selectionChange", (s, c) => {
1112
- o(s).forEach((l) => n.delete(l)), c && c.forEach((l) => n.set(l, s));
1113
- }), { paint: (s, c, r, l, h, g) => {
1233
+ }, Te = (t, e) => ({
1234
+ parse: (n) => xe(n, e),
1235
+ serialize: (n) => Ae(n, t, e)
1236
+ }), xe = (t, e) => {
1237
+ const n = t.id || ct(), {
1238
+ creator: o,
1239
+ created: i,
1240
+ modified: s,
1241
+ body: r,
1242
+ ...a
1243
+ } = t, l = ne(r, n), d = Array.isArray(t.target) ? t.target[0] : t.target, u = (Array.isArray(d.selector) ? d.selector : [d.selector]).reduce((h, w) => {
1244
+ switch (w.type) {
1245
+ case "TextQuoteSelector":
1246
+ h.quote = w.exact;
1247
+ break;
1248
+ case "TextPositionSelector":
1249
+ h.start = w.start, h.end = w.end;
1250
+ break;
1251
+ }
1252
+ return h;
1253
+ }, {});
1254
+ if (((h) => h.quote !== void 0 && h.start !== void 0 && h.end !== void 0)(u))
1255
+ return {
1256
+ parsed: {
1257
+ ...a,
1258
+ id: n,
1259
+ bodies: l,
1260
+ target: D({
1261
+ ...a.target,
1262
+ created: i ? new Date(i) : void 0,
1263
+ creator: dt(o),
1264
+ updated: s ? new Date(s) : void 0,
1265
+ annotation: n,
1266
+ selector: u
1267
+ }, e)
1268
+ }
1269
+ };
1270
+ {
1271
+ const h = [
1272
+ u.start ? void 0 : "TextPositionSelector",
1273
+ u.quote ? void 0 : "TextQuoteSelector"
1274
+ ].filter(Boolean);
1275
+ return {
1276
+ error: Error(`Missing selector types: ${h.join(" and ")}`)
1277
+ };
1278
+ }
1279
+ }, Ae = (t, e, n) => {
1280
+ const { bodies: o, target: i, ...s } = t, {
1281
+ selector: r,
1282
+ creator: a,
1283
+ created: l,
1284
+ updated: d,
1285
+ ...x
1286
+ } = i, { quote: u, start: T, end: h, range: w } = r, { prefix: B, suffix: b } = he(w, n), c = [{
1287
+ type: "TextQuoteSelector",
1288
+ exact: u,
1289
+ prefix: B,
1290
+ suffix: b
1291
+ }, {
1292
+ type: "TextPositionSelector",
1293
+ start: T,
1294
+ end: h
1295
+ }];
1296
+ return {
1297
+ ...s,
1298
+ "@context": "http://www.w3.org/ns/anno.jsonld",
1299
+ id: t.id,
1300
+ type: "Annotation",
1301
+ body: oe(t.bodies),
1302
+ creator: a,
1303
+ created: l == null ? void 0 : l.toISOString(),
1304
+ modified: d == null ? void 0 : d.toISOString(),
1305
+ target: {
1306
+ ...x,
1307
+ source: e,
1308
+ selector: c
1309
+ }
1310
+ };
1311
+ }, ye = (t, e = {}) => {
1312
+ const n = /* @__PURE__ */ new Map(), o = (s) => Array.from(n.entries()).filter(([r, a]) => a.presenceKey === s.presenceKey).map(([r, a]) => r);
1313
+ return t.on("selectionChange", (s, r) => {
1314
+ o(s).forEach((l) => n.delete(l)), r && r.forEach((l) => n.set(l, s));
1315
+ }), { paint: (s, r, a, l, d, x) => {
1114
1316
  e.font && (l.font = e.font);
1115
- const d = n.get(s.id);
1116
- if (d) {
1117
- const { x: p, y: B, height: x } = c[0];
1118
- l.fillStyle = d.appearance.color, l.fillRect(p - 2, B - 2.5, 2, x + 5);
1119
- const w = l.measureText(d.appearance.label), A = w.width + 6, u = w.actualBoundingBoxAscent + w.actualBoundingBoxDescent + 8, a = w.fontBoundingBoxAscent ? 8 : 6.5;
1120
- l.fillRect(p - 2, B - 2.5 - u, A, u), l.fillStyle = "#fff", l.fillText(d.appearance.label, p + 1, B - a), r.fillStyle = d.appearance.color, r.globalAlpha = h ? 0.45 : 0.18, c.forEach(({ x: m, y: T, width: b, height: C }) => r.fillRect(m, T - 2.5, b, C + 5));
1317
+ const u = n.get(s.id);
1318
+ if (u) {
1319
+ const { x: T, y: h, height: w } = r[0];
1320
+ l.fillStyle = u.appearance.color, l.fillRect(T - 2, h - 2.5, 2, w + 5);
1321
+ const B = l.measureText(u.appearance.label), b = B.width + 6, c = B.actualBoundingBoxAscent + B.actualBoundingBoxDescent + 8, f = B.fontBoundingBoxAscent ? 8 : 6.5;
1322
+ l.fillRect(T - 2, h - 2.5 - c, b, c), l.fillStyle = "#fff", l.fillText(u.appearance.label, T + 1, h - f), a.fillStyle = u.appearance.color, a.globalAlpha = d ? 0.45 : 0.18, r.forEach(({ x: p, y: m, width: E, height: S }) => a.fillRect(p, m - 2.5, E, S + 5));
1121
1323
  } else
1122
- ot.paint(s, c, r, l, h, g);
1324
+ at.paint(s, r, a, l, d, x);
1123
1325
  } };
1124
- }, st = (t) => t === null ? null : t.scrollHeight > t.clientHeight ? t : st(t.parentElement), oe = (t, e) => (n) => {
1125
- const o = st(t);
1126
- if (o) {
1127
- const i = e.getAnnotation(n.id), { range: s } = i.target.selector;
1128
- if (!s || s.collapsed)
1129
- return !1;
1130
- const c = o.getBoundingClientRect(), r = o.clientHeight, l = o.clientWidth, h = i.target.selector.range.getBoundingClientRect(), { width: g, height: d } = e.getAnnotationBounds(n.id), p = h.top - c.top, B = h.left - c.left, x = o.parentElement ? o.scrollTop : 0, w = o.parentElement ? o.scrollLeft : 0, A = p + x - (r - d) / 2, u = B + w - (l - g) / 2;
1131
- return o.scroll({ top: A, left: u, behavior: "smooth" }), !0;
1326
+ }, ht = (t) => {
1327
+ if (t === null)
1328
+ return document.scrollingElement;
1329
+ const { overflowY: e } = window.getComputedStyle(t);
1330
+ return e !== "visible" && e !== "hidden" && t.scrollHeight > t.clientHeight ? t : ht(t.parentElement);
1331
+ }, ve = (t, e) => (n) => {
1332
+ const o = (s) => {
1333
+ const r = i.getBoundingClientRect(), a = i.clientHeight, l = i.clientWidth, d = s.selector.range.getBoundingClientRect(), { width: x, height: u } = e.getAnnotationBounds(n.id), T = d.top - r.top, h = d.left - r.left, w = i.parentElement ? i.scrollTop : 0, B = i.parentElement ? i.scrollLeft : 0, b = T + w - (a - u) / 2, c = h + B - (l - x) / 2;
1334
+ i.scroll({ top: b, left: c, behavior: "smooth" });
1335
+ }, i = ht(t);
1336
+ if (i) {
1337
+ const s = e.getAnnotation(n.id), { range: r } = s.target.selector;
1338
+ if (r && !r.collapsed)
1339
+ return o(s.target), !0;
1340
+ {
1341
+ const a = D(s.target, t);
1342
+ if (a.selector.range && !a.selector.range.collapsed)
1343
+ return o(a), !0;
1344
+ }
1132
1345
  }
1133
- }, ie = (t, e, n) => {
1346
+ return !1;
1347
+ }, Ee = (t, e, n) => {
1134
1348
  const o = document.createRange(), i = n ? t.startContainer.parentElement.closest(n) : e;
1135
1349
  o.setStart(i, 0), o.setEnd(t.startContainer, t.startOffset);
1136
- const s = t.toString(), c = o.toString().length, r = c + s.length;
1137
- return n ? { quote: s, start: c, end: r, range: t, offsetReference: i } : { quote: s, start: c, end: r, range: t };
1138
- }, se = (t, e, n) => {
1350
+ const s = t.toString(), r = o.toString().length, a = r + s.length;
1351
+ return n ? { quote: s, start: r, end: a, range: t, offsetReference: i } : { quote: s, start: r, end: a, range: t };
1352
+ }, Se = (t, e, n) => {
1139
1353
  const { store: o, selection: i } = e;
1140
- let s, c = null;
1141
- const r = (p) => s = p;
1142
- let l = !1, h;
1143
- t.addEventListener("selectstart", (p) => {
1354
+ let s, r;
1355
+ const a = (h) => s = h;
1356
+ let l = !1, d;
1357
+ const x = (h) => {
1358
+ var B;
1144
1359
  if (!l)
1145
1360
  return;
1146
- !p.target.parentElement.closest(".not-annotatable") ? c = {
1147
- annotation: Tt(),
1361
+ !((B = h.target.parentElement) != null && B.closest(".not-annotatable")) ? r = {
1362
+ annotation: ct(),
1148
1363
  selector: void 0,
1149
1364
  creator: s,
1150
1365
  created: /* @__PURE__ */ new Date()
1151
- } : (c = null, h = void 0);
1152
- });
1153
- let g;
1154
- document.addEventListener("selectionchange", (p) => {
1155
- g && clearTimeout(g), g = setTimeout(() => d(), 50);
1156
- });
1157
- const d = () => {
1158
- var B, x;
1159
- const p = document.getSelection();
1160
- if (!p.isCollapsed && l && c) {
1161
- const w = Array.from(Array(p.rangeCount).keys()).map((a) => p.getRangeAt(a));
1162
- Zt(w[0]).toString() !== ((x = (B = c.selector) == null ? void 0 : B.range) == null ? void 0 : x.toString()) && (c = {
1163
- ...c,
1164
- selector: ie(w[0], t, n)
1165
- }, o.getAnnotation(c.annotation) ? o.updateTarget(c, L.LOCAL) : (o.addAnnotation({
1166
- id: c.annotation,
1366
+ } : r = void 0;
1367
+ };
1368
+ t.addEventListener("selectstart", x);
1369
+ let u;
1370
+ const T = (h) => {
1371
+ var B;
1372
+ const w = document.getSelection();
1373
+ if (h.timeStamp - d.timeStamp < 1e3 && !r && x(d), !w.isCollapsed && l && r) {
1374
+ const b = Array.from(Array(w.rangeCount).keys()).map((p) => w.getRangeAt(p));
1375
+ me(b[0]).toString() !== ((B = r.selector) == null ? void 0 : B.quote) && (r = {
1376
+ ...r,
1377
+ selector: Ee(b[0], t, n)
1378
+ }, o.getAnnotation(r.annotation) ? o.updateTarget(r, O.LOCAL) : (o.addAnnotation({
1379
+ id: r.annotation,
1167
1380
  bodies: [],
1168
- target: c
1169
- }), i.clickSelect(c.annotation, h)));
1381
+ target: r
1382
+ }), i.clickSelect(r.annotation, d)));
1170
1383
  }
1171
1384
  };
1172
- return t.addEventListener("pointerdown", (p) => {
1173
- h = p, l = p.button === 0;
1174
- }), document.addEventListener("pointerup", (p) => {
1175
- var x;
1176
- h = p, !((x = p.target.parentElement) != null && x.closest(".not-annotatable") || !l) && setTimeout(() => {
1177
- if (c != null && c.selector)
1178
- o.updateTarget(c, L.LOCAL), i.clickSelect(c.annotation, p), c = null, h = void 0;
1179
- else {
1180
- const { x: w, y: A } = t.getBoundingClientRect(), u = o.getAt(p.clientX - w, p.clientY - A);
1181
- if (u) {
1182
- const { selected: a } = i;
1183
- (a.length !== 1 || a[0].id !== u.id) && (i.clickSelect(u.id, p), h = void 0);
1184
- } else
1185
- i.isEmpty() || i.clear();
1186
- }
1187
- }, 50);
1385
+ return document.addEventListener("selectionchange", (h) => {
1386
+ u && clearTimeout(u), u = setTimeout(() => T(h), 10);
1387
+ }), t.addEventListener("pointerdown", (h) => {
1388
+ const { target: w, timeStamp: B, offsetX: b, offsetY: c, type: f } = h;
1389
+ d = { ...h, target: w, timeStamp: B, offsetX: b, offsetY: c, type: f }, l = h.button === 0;
1390
+ }), document.addEventListener("pointerup", (h) => {
1391
+ var c;
1392
+ if (!!((c = h.target.parentElement) != null && c.closest(".not-annotatable")) || !l)
1393
+ return;
1394
+ const B = () => {
1395
+ const { x: f, y: p } = t.getBoundingClientRect(), m = o.getAt(h.clientX - f, h.clientY - p);
1396
+ if (m) {
1397
+ const { selected: E } = i;
1398
+ (E.length !== 1 || E[0].id !== m.id) && i.clickSelect(m.id, h);
1399
+ } else
1400
+ i.isEmpty() || i.clear();
1401
+ }, b = h.timeStamp - d.timeStamp;
1402
+ document.getSelection().isCollapsed && b < 300 ? (r = void 0, B()) : i.clickSelect(r.annotation, h);
1188
1403
  }), {
1189
- setUser: r
1190
- };
1191
- };
1192
- const le = (t, e = {}) => {
1193
- const n = ee(t, e.pointerAction), { hover: o, selection: i, viewport: s } = n, c = n.store, r = Yt(c, i, o, s);
1194
- let l = zt(), h = e.style;
1195
- const g = gt(t, n, s), d = se(t, n, e.offsetReferenceSelector);
1196
- d.setUser(l);
1197
- const p = It(c, e.adapter), B = (m) => {
1198
- h = m, g.setDrawingStyle(m);
1404
+ setUser: a
1199
1405
  };
1200
- return {
1201
- ...p,
1406
+ }, Be = (t, e = {}) => {
1407
+ t.addEventListener("click", (p) => p.preventDefault());
1408
+ const n = we(t, e.pointerAction), { selection: o, viewport: i } = n, s = n.store, r = Ft(s), a = qt(
1409
+ n,
1410
+ r,
1411
+ e.adapter
1412
+ );
1413
+ let l = te();
1414
+ const d = Tt(t, n, i);
1415
+ e.style && d.setDrawingStyle(e.style);
1416
+ const x = Se(t, n, e.offsetReferenceSelector);
1417
+ return x.setUser(l), {
1418
+ ...Qt(n, r, e.adapter),
1202
1419
  destroy: () => {
1203
1420
  throw "Not implemented yet";
1204
1421
  },
1205
1422
  element: t,
1206
1423
  getUser: () => l,
1207
- setUser: (m) => {
1208
- l = m, d.setUser(m);
1424
+ setFilter: (p) => d.setFilter(p),
1425
+ setStyle: (p) => d.setDrawingStyle(p),
1426
+ setUser: (p) => {
1427
+ l = p, x.setUser(p);
1209
1428
  },
1210
- setSelected: (m) => {
1211
- m ? i.setSelected(m) : i.clear();
1429
+ setSelected: (p) => {
1430
+ p ? o.setSelected(p) : o.clear();
1212
1431
  },
1213
- setPresenceProvider: (m) => {
1214
- m && (g.setPainter(ne(m, e.presence)), m.on("selectionChange", () => g.redraw()));
1432
+ setPresenceProvider: (p) => {
1433
+ p && (d.setPainter(ye(p, e.presence)), p.on("selectionChange", () => d.redraw()));
1215
1434
  },
1216
- on: r.on,
1217
- off: r.off,
1218
- scrollIntoView: oe(t, c),
1219
- state: n,
1220
- get style() {
1221
- return h;
1222
- },
1223
- set style(m) {
1224
- B(m);
1225
- }
1435
+ on: a.on,
1436
+ off: a.off,
1437
+ scrollIntoView: ve(t, s),
1438
+ state: n
1226
1439
  };
1227
- }, de = L;
1440
+ };
1228
1441
  export {
1229
- de as Origin,
1230
- zt as createAnonymousGuest,
1231
- It as createBaseAnnotator,
1232
- gt as createHighlightLayer,
1233
- le as createTextAnnotator,
1234
- ee as createTextAnnotatorState,
1235
- ot as defaultPainter,
1236
- _t as parseAll,
1237
- ce as parseW3CBodies,
1238
- k as reviveTarget,
1239
- re as serializeAll,
1240
- ae as serializeW3CBodies
1442
+ O as Origin,
1443
+ Te as W3CTextFormat,
1444
+ Tt as createHighlightLayer,
1445
+ Be as createTextAnnotator,
1446
+ we as createTextAnnotatorState,
1447
+ at as defaultPainter,
1448
+ xe as parseW3CTextAnnotation,
1449
+ D as reviveTarget,
1450
+ Ae as serializeW3CTextAnnotation
1241
1451
  };
1242
1452
  //# sourceMappingURL=text-annotator.es.js.map