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

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