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

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