@recogito/text-annotator 3.0.0-rc.7 → 3.0.0-rc.8

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,4 @@
1
+ export declare const getContext: (range: Range, container: HTMLElement, length?: number, offsetReferenceSelector?: string) => {
2
+ prefix: string;
3
+ suffix: string;
4
+ };
@@ -1 +1 @@
1
- html,body{overscroll-behavior-y:none}.r6o-annotatable,.r6o-annotatable *{position:relative}.r6o-annotatable.hovered{cursor:pointer}.r6o-highlight-layer{height:100vh;left:0;position:fixed;top:0;pointer-events:none;width:100vw}*::selection,::selection{background:rgba(0,128,255,.18)}::-moz-selection{background:rgba(0,128,255,.18)}
1
+ html,body{overscroll-behavior-y:none}.r6o-annotatable,.r6o-annotatable *{position:relative}.r6o-annotatable.hovered{cursor:pointer}.r6o-highlight-layer{height:100vh;left:0;position:fixed;top:0;pointer-events:none;width:100vw}*::selection,::selection{background:#0080ff2e}::-moz-selection{background:#0080ff2e}
@@ -9,8 +9,7 @@ const ft = { fill: "rgb(0, 128, 255", fillOpacity: 0.18 }, gt = { fill: "rgb(0,
9
9
  const i = o.map((s) => s.id);
10
10
  (t.size !== i.length || i.some((s) => !t.has(s))) && e.set(i), t = new Set(i);
11
11
  };
12
- };
13
- const mt = (e, t = 10) => {
12
+ }, mt = (e, t = 10) => {
14
13
  let n;
15
14
  return (...o) => {
16
15
  clearTimeout(n), n = setTimeout(() => e.apply(void 0, o), t);
@@ -38,8 +37,8 @@ const mt = (e, t = 10) => {
38
37
  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
38
  });
40
39
  const x = () => {
41
- const { top: E, left: T } = e.getBoundingClientRect(), { innerWidth: L, innerHeight: f } = window, w = -T, A = -E, B = L - T, S = f - E;
42
- return { top: E, left: T, minX: w, minY: A, maxX: B, maxY: S };
40
+ const { top: E, left: T } = e.getBoundingClientRect(), { innerWidth: L, innerHeight: f } = window, A = -T, w = -E, B = L - T, S = f - E;
41
+ return { top: E, left: T, minX: A, minY: w, maxX: B, maxY: S };
43
42
  }, M = () => c();
44
43
  document.addEventListener("scroll", M, { capture: !0, passive: !0 });
45
44
  const y = mt(() => {
@@ -47,7 +46,7 @@ const mt = (e, t = 10) => {
47
46
  });
48
47
  window.addEventListener("resize", y), new ResizeObserver(y).observe(e);
49
48
  const c = () => requestAnimationFrame(() => {
50
- const { top: E, left: T, minX: L, minY: f, maxX: w, maxY: A } = x(), B = r ? o.getIntersectingRects(L, f, w, A).filter(({ annotation: Y }) => r(Y)) : o.getIntersectingRects(L, f, w, A), { width: S, height: R } = h, X = new Set(i.selected.map(({ id: Y }) => Y));
49
+ const { top: E, left: T, minX: L, minY: f, maxX: A, maxY: w } = x(), B = r ? o.getIntersectingRects(L, f, A, w).filter(({ annotation: Y }) => r(Y)) : o.getIntersectingRects(L, f, A, w), { width: S, height: R } = h, X = new Set(i.selected.map(({ id: Y }) => Y));
51
50
  m.clearRect(-0.5, -0.5, S + 1, R + 1), v.clearRect(-0.5, -0.5, S + 1, R + 1), B.forEach(({ annotation: Y, rects: at }) => {
52
51
  const rt = at.map(({ x: ct, y: dt, width: ut, height: ht }) => ({
53
52
  x: ct + T,
@@ -131,7 +130,7 @@ function U(e, t) {
131
130
  }
132
131
  function W() {
133
132
  }
134
- function At(e, t) {
133
+ function wt(e, t) {
135
134
  return e != e ? t == t : e !== t || e && typeof e == "object" || typeof e == "function";
136
135
  }
137
136
  const D = [];
@@ -139,7 +138,7 @@ function J(e, t = W) {
139
138
  let n;
140
139
  const o = /* @__PURE__ */ new Set();
141
140
  function i(r) {
142
- if (At(e, r) && (e = r, n)) {
141
+ if (wt(e, r) && (e = r, n)) {
143
142
  const d = !D.length;
144
143
  for (const u of o)
145
144
  u[1](), D.push(u, e);
@@ -161,7 +160,7 @@ function J(e, t = W) {
161
160
  }
162
161
  return { set: i, update: s, subscribe: a };
163
162
  }
164
- const wt = (e) => {
163
+ const At = (e) => {
165
164
  const { subscribe: t, set: n } = J();
166
165
  let o;
167
166
  return t((i) => o = i), e.observe(({ changes: i }) => {
@@ -286,65 +285,65 @@ const Mt = (e, t) => {
286
285
  ];
287
286
  return { created: d, deleted: u, updated: b };
288
287
  }, Ct = (e) => e.id !== void 0, Rt = () => {
289
- const e = /* @__PURE__ */ new Map(), t = /* @__PURE__ */ new Map(), n = [], o = (f, w = {}) => n.push({ onChange: f, options: w }), i = (f) => {
290
- const w = n.findIndex((A) => A.onChange == f);
291
- w > -1 && n.splice(w, 1);
292
- }, s = (f, w) => {
293
- const A = {
288
+ const e = /* @__PURE__ */ new Map(), t = /* @__PURE__ */ new Map(), n = [], o = (f, A = {}) => n.push({ onChange: f, options: A }), i = (f) => {
289
+ const A = n.findIndex((w) => w.onChange == f);
290
+ A > -1 && n.splice(A, 1);
291
+ }, s = (f, A) => {
292
+ const w = {
294
293
  origin: f,
295
294
  changes: {
296
- created: w.created || [],
297
- updated: w.updated || [],
298
- deleted: w.deleted || []
295
+ created: A.created || [],
296
+ updated: A.updated || [],
297
+ deleted: A.deleted || []
299
298
  },
300
299
  state: [...e.values()]
301
300
  };
302
301
  n.forEach((B) => {
303
- Mt(B, A) && B.onChange(A);
302
+ Mt(B, w) && B.onChange(w);
304
303
  });
305
- }, a = (f, w = C.LOCAL) => {
304
+ }, a = (f, A = C.LOCAL) => {
306
305
  if (e.get(f.id))
307
306
  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(w, { created: [f] });
309
- }, r = (f, w) => {
310
- const A = typeof f == "string" ? w : f, B = typeof f == "string" ? f : f.id, S = e.get(B);
307
+ e.set(f.id, f), f.bodies.forEach((w) => t.set(w.id, f.id)), s(A, { created: [f] });
308
+ }, r = (f, A) => {
309
+ const w = typeof f == "string" ? A : f, B = typeof f == "string" ? f : f.id, S = e.get(B);
311
310
  if (S) {
312
- const R = ot(S, A);
313
- return B === A.id ? e.set(B, A) : (e.delete(B), e.set(A.id, A)), S.bodies.forEach((X) => t.delete(X.id)), A.bodies.forEach((X) => t.set(X.id, A.id)), R;
311
+ const R = ot(S, w);
312
+ return B === w.id ? e.set(B, w) : (e.delete(B), e.set(w.id, w)), S.bodies.forEach((X) => t.delete(X.id)), w.bodies.forEach((X) => t.set(X.id, w.id)), R;
314
313
  } else
315
314
  console.warn(`Cannot update annotation ${B} - does not exist`);
316
- }, d = (f, w = C.LOCAL, A = C.LOCAL) => {
317
- const B = Ct(w) ? A : w, S = r(f, w);
315
+ }, d = (f, A = C.LOCAL, w = C.LOCAL) => {
316
+ const B = Ct(A) ? w : A, S = r(f, A);
318
317
  S && s(B, { updated: [S] });
319
- }, u = (f, w = C.LOCAL) => {
320
- const A = f.reduce((B, S) => {
318
+ }, u = (f, A = C.LOCAL) => {
319
+ const w = f.reduce((B, S) => {
321
320
  const R = r(S);
322
321
  return R ? [...B, R] : B;
323
322
  }, []);
324
- A.length > 0 && s(w, { updated: A });
325
- }, b = (f, w = C.LOCAL) => {
326
- const A = e.get(f.annotation);
327
- if (A) {
323
+ w.length > 0 && s(A, { updated: w });
324
+ }, b = (f, A = C.LOCAL) => {
325
+ const w = e.get(f.annotation);
326
+ if (w) {
328
327
  const B = {
329
- ...A,
330
- bodies: [...A.bodies, f]
328
+ ...w,
329
+ bodies: [...w.bodies, f]
331
330
  };
332
- e.set(A.id, B), t.set(f.id, B.id), s(w, { updated: [{
333
- oldValue: A,
331
+ e.set(w.id, B), t.set(f.id, B.id), s(A, { updated: [{
332
+ oldValue: w,
334
333
  newValue: B,
335
334
  bodiesCreated: [f]
336
335
  }] });
337
336
  } else
338
337
  console.warn(`Attempt to add body to missing annotation: ${f.annotation}`);
339
338
  }, h = () => [...e.values()], v = (f = C.LOCAL) => {
340
- const w = [...e.values()];
341
- e.clear(), t.clear(), s(f, { deleted: w });
342
- }, m = (f, w = !0, A = C.LOCAL) => {
343
- if (w) {
339
+ const A = [...e.values()];
340
+ e.clear(), t.clear(), s(f, { deleted: A });
341
+ }, m = (f, A = !0, w = C.LOCAL) => {
342
+ if (A) {
344
343
  const B = [...e.values()];
345
344
  e.clear(), t.clear(), f.forEach((S) => {
346
345
  e.set(S.id, S), S.bodies.forEach((R) => t.set(R.id, S.id));
347
- }), s(A, { created: f, deleted: B });
346
+ }), s(w, { created: f, deleted: B });
348
347
  } else {
349
348
  const B = f.reduce((S, R) => {
350
349
  const X = e.get(R.id);
@@ -354,34 +353,34 @@ const Mt = (e, t) => {
354
353
  throw Error(`Bulk insert would overwrite the following annotations: ${B.map((S) => S.id).join(", ")}`);
355
354
  f.forEach((S) => {
356
355
  e.set(S.id, S), S.bodies.forEach((R) => t.set(R.id, S.id));
357
- }), s(A, { created: f });
356
+ }), s(w, { created: f });
358
357
  }
359
358
  }, x = (f) => {
360
- const w = typeof f == "string" ? f : f.id, A = e.get(w);
361
- if (A)
362
- return e.delete(w), A.bodies.forEach((B) => t.delete(B.id)), A;
363
- console.warn(`Attempt to delete missing annotation: ${w}`);
364
- }, M = (f, w = C.LOCAL) => {
365
- const A = x(f);
366
- A && s(w, { deleted: [A] });
367
- }, y = (f, w = C.LOCAL) => {
368
- const A = f.reduce((B, S) => {
359
+ const A = typeof f == "string" ? f : f.id, w = e.get(A);
360
+ if (w)
361
+ return e.delete(A), w.bodies.forEach((B) => t.delete(B.id)), w;
362
+ console.warn(`Attempt to delete missing annotation: ${A}`);
363
+ }, M = (f, A = C.LOCAL) => {
364
+ const w = x(f);
365
+ w && s(A, { deleted: [w] });
366
+ }, y = (f, A = C.LOCAL) => {
367
+ const w = f.reduce((B, S) => {
369
368
  const R = x(S);
370
369
  return R ? [...B, R] : B;
371
370
  }, []);
372
- A.length > 0 && s(w, { deleted: A });
373
- }, l = (f, w = C.LOCAL) => {
374
- const A = e.get(f.annotation);
375
- if (A) {
376
- const B = A.bodies.find((S) => S.id === f.id);
371
+ w.length > 0 && s(A, { deleted: w });
372
+ }, l = (f, A = C.LOCAL) => {
373
+ const w = e.get(f.annotation);
374
+ if (w) {
375
+ const B = w.bodies.find((S) => S.id === f.id);
377
376
  if (B) {
378
377
  t.delete(B.id);
379
378
  const S = {
380
- ...A,
381
- bodies: A.bodies.filter((R) => R.id !== f.id)
379
+ ...w,
380
+ bodies: w.bodies.filter((R) => R.id !== f.id)
382
381
  };
383
- e.set(A.id, S), s(w, { updated: [{
384
- oldValue: A,
382
+ e.set(w.id, S), s(A, { updated: [{
383
+ oldValue: w,
385
384
  newValue: S,
386
385
  bodiesDeleted: [B]
387
386
  }] });
@@ -390,54 +389,54 @@ const Mt = (e, t) => {
390
389
  } else
391
390
  console.warn(`Attempt to delete body from missing annotation ${f.annotation}`);
392
391
  }, c = (f) => {
393
- const w = e.get(f);
394
- return w ? { ...w } : void 0;
392
+ const A = e.get(f);
393
+ return A ? { ...A } : void 0;
395
394
  }, g = (f) => {
396
- const w = t.get(f);
397
- if (w) {
398
- const A = c(w).bodies.find((B) => B.id === f);
399
- if (A)
400
- return A;
395
+ const A = t.get(f);
396
+ if (A) {
397
+ const w = c(A).bodies.find((B) => B.id === f);
398
+ if (w)
399
+ return w;
401
400
  console.error(`Store integrity error: body ${f} in index, but not in annotation`);
402
401
  } else
403
402
  console.warn(`Attempt to retrieve missing body: ${f}`);
404
- }, p = (f, w) => {
405
- if (f.annotation !== w.annotation)
403
+ }, p = (f, A) => {
404
+ if (f.annotation !== A.annotation)
406
405
  throw "Annotation integrity violation: annotation ID must be the same when updating bodies";
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 ? w : R)
406
+ const w = e.get(f.annotation);
407
+ if (w) {
408
+ const B = w.bodies.find((R) => R.id === f.id), S = {
409
+ ...w,
410
+ bodies: w.bodies.map((R) => R.id === B.id ? A : R)
412
411
  };
413
- return e.set(A.id, S), B.id !== w.id && (t.delete(B.id), t.set(w.id, S.id)), {
414
- oldValue: A,
412
+ return e.set(w.id, S), B.id !== A.id && (t.delete(B.id), t.set(A.id, S.id)), {
413
+ oldValue: w,
415
414
  newValue: S,
416
- bodiesUpdated: [{ oldBody: B, newBody: w }]
415
+ bodiesUpdated: [{ oldBody: B, newBody: A }]
417
416
  };
418
417
  } else
419
418
  console.warn(`Attempt to add body to missing annotation ${f.annotation}`);
420
- }, E = (f, w, A = C.LOCAL) => {
421
- const B = p(f, w);
422
- B && s(A, { updated: [B] });
423
- }, T = (f, w = C.LOCAL) => {
424
- const A = f.map((B) => p({ id: B.id, annotation: B.annotation }, B)).filter(Boolean);
425
- s(w, { updated: A });
419
+ }, E = (f, A, w = C.LOCAL) => {
420
+ const B = p(f, A);
421
+ B && s(w, { updated: [B] });
422
+ }, T = (f, A = C.LOCAL) => {
423
+ const w = f.map((B) => p({ id: B.id, annotation: B.annotation }, B)).filter(Boolean);
424
+ s(A, { updated: w });
426
425
  }, L = (f) => {
427
- const w = e.get(f.annotation);
428
- if (w) {
429
- const A = {
430
- ...w,
426
+ const A = e.get(f.annotation);
427
+ if (A) {
428
+ const w = {
429
+ ...A,
431
430
  target: {
432
- ...w.target,
431
+ ...A.target,
433
432
  ...f
434
433
  }
435
434
  };
436
- return e.set(w.id, A), {
437
- oldValue: w,
438
- newValue: A,
435
+ return e.set(A.id, w), {
436
+ oldValue: A,
437
+ newValue: w,
439
438
  targetUpdated: {
440
- oldTarget: w.target,
439
+ oldTarget: A.target,
441
440
  newTarget: f
442
441
  }
443
442
  };
@@ -452,9 +451,9 @@ const Mt = (e, t) => {
452
451
  bulkDeleteAnnotation: y,
453
452
  bulkUpdateAnnotation: u,
454
453
  bulkUpdateBodies: T,
455
- bulkUpdateTargets: (f, w = C.LOCAL) => {
456
- const A = f.map((B) => L(B)).filter(Boolean);
457
- A.length > 0 && s(w, { updated: A });
454
+ bulkUpdateTargets: (f, A = C.LOCAL) => {
455
+ const w = f.map((B) => L(B)).filter(Boolean);
456
+ w.length > 0 && s(A, { updated: w });
458
457
  },
459
458
  clear: v,
460
459
  deleteAnnotation: M,
@@ -465,9 +464,9 @@ const Mt = (e, t) => {
465
464
  unobserve: i,
466
465
  updateAnnotation: d,
467
466
  updateBody: E,
468
- updateTarget: (f, w = C.LOCAL) => {
469
- const A = L(f);
470
- A && s(w, { updated: [A] });
467
+ updateTarget: (f, A = C.LOCAL) => {
468
+ const w = L(f);
469
+ w && s(A, { updated: [w] });
471
470
  }
472
471
  };
473
472
  };
@@ -1030,12 +1029,12 @@ const jt = (e, t) => {
1030
1029
  const n = new $t(), o = /* @__PURE__ */ new Map(), i = (l) => {
1031
1030
  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), E = Array.from(p.selector.range.getClientRects()), T = Qt(E);
1032
1031
  return T.map((L) => {
1033
- const { x: f, y: w, width: A, height: B } = L;
1032
+ const { x: f, y: A, width: w, height: B } = L;
1034
1033
  return {
1035
1034
  minX: f - c.x,
1036
- minY: w - c.y,
1037
- maxX: f - c.x + A,
1038
- maxY: w - c.y + B,
1035
+ minY: A - c.y,
1036
+ maxX: f - c.x + w,
1037
+ maxY: A - c.y + B,
1039
1038
  annotation: {
1040
1039
  id: l.annotation,
1041
1040
  rects: T
@@ -1098,7 +1097,7 @@ const jt = (e, t) => {
1098
1097
  const { range: t } = e.target.selector;
1099
1098
  return t instanceof Range && !t.collapsed;
1100
1099
  }, ne = (e, t) => {
1101
- const n = Rt(), o = ee(n, e), i = xt(n, t), s = wt(n), a = Ut(), r = (y, l = C.LOCAL) => {
1100
+ const n = Rt(), o = ee(n, e), i = xt(n, t), s = At(n), a = Ut(), r = (y, l = C.LOCAL) => {
1102
1101
  const c = y.target.selector.range instanceof Range ? y : { ...y, target: k(y.target, e) }, { range: g } = c.target.selector, p = g && !g.collapsed;
1103
1102
  return p && n.addAnnotation(c, l), p;
1104
1103
  }, d = (y, l = !0, c = C.LOCAL) => {
@@ -1124,7 +1123,7 @@ const jt = (e, t) => {
1124
1123
  const p = o.getDOMRectsForAnnotation(y);
1125
1124
  if (p.length > 0) {
1126
1125
  if (l && c) {
1127
- const E = p.find(({ top: T, right: L, bottom: f, left: w }) => l >= w - g && l <= L + g && c >= T - g && c <= f + g);
1126
+ const E = p.find(({ top: T, right: L, bottom: f, left: A }) => l >= A - g && l <= L + g && c >= T - g && c <= f + g);
1128
1127
  if (E)
1129
1128
  return E;
1130
1129
  }
@@ -1134,7 +1133,7 @@ const jt = (e, t) => {
1134
1133
  const E = o.getIntersectingRects(y, l, c, g).reduce((T, L) => ((T[L.annotation.id] = T[L.annotation.id] || []).push(L), T), {});
1135
1134
  return Object.entries(E).map(([T, L]) => ({
1136
1135
  annotation: n.getAnnotation(T),
1137
- rects: L.map(({ minX: f, minY: w, maxX: A, maxY: B }) => ({ x: f, y: w, width: A - f, height: B - w }))
1136
+ rects: L.map(({ minX: f, minY: A, maxX: w, maxY: B }) => ({ x: f, y: A, width: w - f, height: B - A }))
1138
1137
  }));
1139
1138
  }, M = () => o.recalculate();
1140
1139
  return n.observe(({ changes: y }) => {
@@ -1274,8 +1273,7 @@ const de = (e, t, n) => {
1274
1273
  }), {
1275
1274
  setUser: r
1276
1275
  };
1277
- };
1278
- const he = (e, t = {}) => {
1276
+ }, he = (e, t = {}) => {
1279
1277
  const n = ne(e, t.pointerAction), { selection: o, viewport: i } = n, s = n.store, a = Yt(s), r = Dt(
1280
1278
  n,
1281
1279
  a,