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

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