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

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