@recogito/text-annotator 3.0.0-rc.1

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