@annotorious/core 3.2.3 → 3.3.0

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,5 +1,5 @@
1
1
  var M = Object.prototype.hasOwnProperty;
2
- function k(e, t) {
2
+ function N(e, t) {
3
3
  var n, o;
4
4
  if (e === t) return !0;
5
5
  if (e && t && (n = e.constructor) === t.constructor) {
@@ -7,52 +7,52 @@ function k(e, t) {
7
7
  if (n === RegExp) return e.toString() === t.toString();
8
8
  if (n === Array) {
9
9
  if ((o = e.length) === t.length)
10
- for (; o-- && k(e[o], t[o]); ) ;
10
+ for (; o-- && N(e[o], t[o]); ) ;
11
11
  return o === -1;
12
12
  }
13
13
  if (!n || typeof e == "object") {
14
14
  o = 0;
15
15
  for (n in e)
16
- if (M.call(e, n) && ++o && !M.call(t, n) || !(n in t) || !k(e[n], t[n])) return !1;
16
+ if (M.call(e, n) && ++o && !M.call(t, n) || !(n in t) || !N(e[n], t[n])) return !1;
17
17
  return Object.keys(t).length === o;
18
18
  }
19
19
  }
20
20
  return e !== e && t !== t;
21
21
  }
22
- function V() {
22
+ function _() {
23
23
  }
24
- function X(e, t) {
24
+ function F(e, t) {
25
25
  return e != e ? t == t : e !== t || e && typeof e == "object" || typeof e == "function";
26
26
  }
27
- const N = [];
28
- function j(e, t = V) {
27
+ const z = [];
28
+ function P(e, t = _) {
29
29
  let n;
30
30
  const o = /* @__PURE__ */ new Set();
31
31
  function s(b) {
32
- if (X(e, b) && (e = b, n)) {
33
- const w = !N.length;
34
- for (const m of o)
35
- m[1](), N.push(m, e);
36
- if (w) {
37
- for (let m = 0; m < N.length; m += 2)
38
- N[m][0](N[m + 1]);
39
- N.length = 0;
32
+ if (F(e, b) && (e = b, n)) {
33
+ const E = !z.length;
34
+ for (const h of o)
35
+ h[1](), z.push(h, e);
36
+ if (E) {
37
+ for (let h = 0; h < z.length; h += 2)
38
+ z[h][0](z[h + 1]);
39
+ z.length = 0;
40
40
  }
41
41
  }
42
42
  }
43
- function f(b) {
43
+ function p(b) {
44
44
  s(b(e));
45
45
  }
46
- function A(b, w = V) {
47
- const m = [b, w];
48
- return o.add(m), o.size === 1 && (n = t(s, f) || V), b(e), () => {
49
- o.delete(m), o.size === 0 && n && (n(), n = null);
46
+ function A(b, E = _) {
47
+ const h = [b, E];
48
+ return o.add(h), o.size === 1 && (n = t(s, p) || _), b(e), () => {
49
+ o.delete(h), o.size === 0 && n && (n(), n = null);
50
50
  };
51
51
  }
52
- return { set: s, update: f, subscribe: A };
52
+ return { set: s, update: p, subscribe: A };
53
53
  }
54
- const Ue = (e) => {
55
- const { subscribe: t, set: n } = j();
54
+ const Oe = (e) => {
55
+ const { subscribe: t, set: n } = P();
56
56
  let o;
57
57
  return t((s) => o = s), e.observe(({ changes: s }) => {
58
58
  if (o) {
@@ -68,59 +68,59 @@ const Ue = (e) => {
68
68
  set: n
69
69
  };
70
70
  };
71
- var Z = /* @__PURE__ */ ((e) => (e.EDIT = "EDIT", e.SELECT = "SELECT", e.NONE = "NONE", e))(Z || {});
72
- const z = { selected: [] }, Se = (e, t, n) => {
73
- const { subscribe: o, set: s } = j(z);
74
- let f = t, A = z;
75
- o((h) => A = h);
71
+ var K = /* @__PURE__ */ ((e) => (e.EDIT = "EDIT", e.SELECT = "SELECT", e.NONE = "NONE", e))(K || {});
72
+ const V = { selected: [] }, De = (e, t, n) => {
73
+ const { subscribe: o, set: s } = P(V);
74
+ let p = t, A = V;
75
+ o((m) => A = m);
76
76
  const b = () => {
77
- k(A, z) || s(z);
78
- }, w = () => {
79
- var h;
80
- return ((h = A.selected) == null ? void 0 : h.length) === 0;
81
- }, m = (h) => {
82
- if (w())
77
+ N(A, V) || s(V);
78
+ }, E = () => {
79
+ var m;
80
+ return ((m = A.selected) == null ? void 0 : m.length) === 0;
81
+ }, h = (m) => {
82
+ if (E())
83
83
  return !1;
84
- const C = typeof h == "string" ? h : h.id;
85
- return A.selected.some((D) => D.id === C);
86
- }, U = (h, C) => {
87
- let D;
88
- if (Array.isArray(h)) {
89
- if (D = h.map((i) => e.getAnnotation(i)).filter(Boolean), D.length < h.length) {
90
- console.warn("Invalid selection: " + h.filter((i) => !D.some((p) => p.id === i)));
84
+ const C = typeof m == "string" ? m : m.id;
85
+ return A.selected.some((T) => T.id === C);
86
+ }, S = (m, C) => {
87
+ let T;
88
+ if (Array.isArray(m)) {
89
+ if (T = m.map((d) => e.getAnnotation(d)).filter(Boolean), T.length < m.length) {
90
+ console.warn("Invalid selection: " + m.filter((d) => !T.some((g) => g.id === d)));
91
91
  return;
92
92
  }
93
93
  } else {
94
- const i = e.getAnnotation(h);
95
- if (!i) {
96
- console.warn("Invalid selection: " + h);
94
+ const d = e.getAnnotation(m);
95
+ if (!d) {
96
+ console.warn("Invalid selection: " + m);
97
97
  return;
98
98
  }
99
- D = [i];
99
+ T = [d];
100
100
  }
101
- const a = D.reduce((i, p) => {
102
- const v = W(p, f, n);
103
- return v === "EDIT" ? [...i, { id: p.id, editable: !0 }] : v === "SELECT" ? [...i, { id: p.id }] : i;
101
+ const c = T.reduce((d, g) => {
102
+ const v = W(g, p, n);
103
+ return v === "EDIT" ? [...d, { id: g.id, editable: !0 }] : v === "SELECT" ? [...d, { id: g.id }] : d;
104
104
  }, []);
105
- s({ selected: a, event: C });
106
- }, c = (h, C) => {
107
- const D = Array.isArray(h) ? h : [h], a = D.map((i) => e.getAnnotation(i)).filter((i) => !!i);
105
+ s({ selected: c, event: C });
106
+ }, l = (m, C) => {
107
+ const T = Array.isArray(m) ? m : [m], c = T.map((d) => e.getAnnotation(d)).filter((d) => !!d);
108
108
  s({
109
- selected: a.map((i) => {
110
- const p = C === void 0 ? W(i, f, n) === "EDIT" : C;
111
- return { id: i.id, editable: p };
109
+ selected: c.map((d) => {
110
+ const g = C === void 0 ? W(d, p, n) === "EDIT" : C;
111
+ return { id: d.id, editable: g };
112
112
  })
113
- }), a.length !== D.length && console.warn("Invalid selection", h);
114
- }, E = (h) => {
115
- if (w())
113
+ }), c.length !== T.length && console.warn("Invalid selection", m);
114
+ }, w = (m) => {
115
+ if (E())
116
116
  return !1;
117
117
  const { selected: C } = A;
118
- C.some(({ id: a }) => h.includes(a)) && s({ selected: C.filter(({ id: a }) => !h.includes(a)) });
119
- }, T = (h) => {
120
- f = h, c(A.selected.map(({ id: C }) => C));
118
+ C.some(({ id: c }) => m.includes(c)) && s({ selected: C.filter(({ id: c }) => !m.includes(c)) });
119
+ }, D = (m) => {
120
+ p = m, l(A.selected.map(({ id: C }) => C));
121
121
  };
122
122
  return e.observe(
123
- ({ changes: h }) => E((h.deleted || []).map((C) => C.id))
123
+ ({ changes: m }) => w((m.deleted || []).map((C) => C.id))
124
124
  ), {
125
125
  get event() {
126
126
  return A ? A.event : null;
@@ -129,15 +129,15 @@ const z = { selected: [] }, Se = (e, t, n) => {
129
129
  return A ? [...A.selected] : null;
130
130
  },
131
131
  get userSelectAction() {
132
- return f;
132
+ return p;
133
133
  },
134
134
  clear: b,
135
- isEmpty: w,
136
- isSelected: m,
137
- setSelected: c,
138
- setUserSelectAction: T,
135
+ isEmpty: E,
136
+ isSelected: h,
137
+ setSelected: l,
138
+ setUserSelectAction: D,
139
139
  subscribe: o,
140
- userSelect: U
140
+ userSelect: S
141
141
  };
142
142
  }, W = (e, t, n) => {
143
143
  const o = n ? n.serialize(e) : e;
@@ -145,38 +145,38 @@ const z = { selected: [] }, Se = (e, t, n) => {
145
145
  }, R = [];
146
146
  for (let e = 0; e < 256; ++e)
147
147
  R.push((e + 256).toString(16).slice(1));
148
- function F(e, t = 0) {
148
+ function ee(e, t = 0) {
149
149
  return (R[e[t + 0]] + R[e[t + 1]] + R[e[t + 2]] + R[e[t + 3]] + "-" + R[e[t + 4]] + R[e[t + 5]] + "-" + R[e[t + 6]] + R[e[t + 7]] + "-" + R[e[t + 8]] + R[e[t + 9]] + "-" + R[e[t + 10]] + R[e[t + 11]] + R[e[t + 12]] + R[e[t + 13]] + R[e[t + 14]] + R[e[t + 15]]).toLowerCase();
150
150
  }
151
- let _;
152
- const K = new Uint8Array(16);
153
- function ee() {
154
- if (!_) {
151
+ let Y;
152
+ const te = new Uint8Array(16);
153
+ function ne() {
154
+ if (!Y) {
155
155
  if (typeof crypto > "u" || !crypto.getRandomValues)
156
156
  throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");
157
- _ = crypto.getRandomValues.bind(crypto);
157
+ Y = crypto.getRandomValues.bind(crypto);
158
158
  }
159
- return _(K);
159
+ return Y(te);
160
160
  }
161
- const te = typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto), q = { randomUUID: te };
161
+ const oe = typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto), q = { randomUUID: oe };
162
162
  function G(e, t, n) {
163
163
  var s;
164
164
  if (q.randomUUID && !e)
165
165
  return q.randomUUID();
166
166
  e = e || {};
167
- const o = e.random ?? ((s = e.rng) == null ? void 0 : s.call(e)) ?? ee();
167
+ const o = e.random ?? ((s = e.rng) == null ? void 0 : s.call(e)) ?? ne();
168
168
  if (o.length < 16)
169
169
  throw new Error("Random bytes length must be >= 16");
170
- return o[6] = o[6] & 15 | 64, o[8] = o[8] & 63 | 128, F(o);
170
+ return o[6] = o[6] & 15 | 64, o[8] = o[8] & 63 | 128, ee(o);
171
171
  }
172
- const Te = (e) => {
173
- const { creator: t, updatedBy: n } = e.target, o = e.bodies.reduce((s, f) => [...s, f.creator, f.updatedBy].filter(Boolean), []);
172
+ const Be = (e) => {
173
+ const { creator: t, updatedBy: n } = e.target, o = e.bodies.reduce((s, p) => [...s, p.creator, p.updatedBy].filter(Boolean), []);
174
174
  return [
175
175
  t,
176
176
  n,
177
177
  ...o
178
178
  ].filter((s) => s);
179
- }, Y = (e) => {
179
+ }, j = (e) => {
180
180
  const t = (n) => {
181
181
  const o = { ...n };
182
182
  return n.created && typeof n.created == "string" && (o.created = new Date(n.created)), n.updated && typeof n.updated == "string" && (o.updated = new Date(n.updated)), o;
@@ -186,74 +186,74 @@ const Te = (e) => {
186
186
  bodies: (e.bodies || []).map(t),
187
187
  target: t(e.target)
188
188
  };
189
- }, De = (e, t, n, o) => ({
189
+ }, Re = (e, t, n, o) => ({
190
190
  id: G(),
191
191
  annotation: typeof e == "string" ? e : e.id,
192
192
  created: n || /* @__PURE__ */ new Date(),
193
193
  creator: o,
194
194
  ...t
195
- }), ne = (e, t) => {
195
+ }), se = (e, t) => {
196
196
  const n = new Set(e.bodies.map((o) => o.id));
197
197
  return t.bodies.filter((o) => !n.has(o.id));
198
- }, oe = (e, t) => {
198
+ }, ie = (e, t) => {
199
199
  const n = new Set(t.bodies.map((o) => o.id));
200
200
  return e.bodies.filter((o) => !n.has(o.id));
201
- }, se = (e, t) => t.bodies.map((n) => {
201
+ }, de = (e, t) => t.bodies.map((n) => {
202
202
  const o = e.bodies.find((s) => s.id === n.id);
203
- return { newBody: n, oldBody: o && !k(o, n) ? o : void 0 };
204
- }).filter(({ oldBody: n }) => n).map(({ oldBody: n, newBody: o }) => ({ oldBody: n, newBody: o })), ie = (e, t) => !k(e.target, t.target), J = (e, t) => {
205
- const n = ne(e, t), o = oe(e, t), s = se(e, t);
203
+ return { newBody: n, oldBody: o && !N(o, n) ? o : void 0 };
204
+ }).filter(({ oldBody: n }) => n).map(({ oldBody: n, newBody: o }) => ({ oldBody: n, newBody: o })), re = (e, t) => !N(e.target, t.target), J = (e, t) => {
205
+ const n = se(e, t), o = ie(e, t), s = de(e, t);
206
206
  return {
207
207
  oldValue: e,
208
208
  newValue: t,
209
209
  bodiesCreated: n.length > 0 ? n : void 0,
210
210
  bodiesDeleted: o.length > 0 ? o : void 0,
211
211
  bodiesUpdated: s.length > 0 ? s : void 0,
212
- targetUpdated: ie(e, t) ? { oldTarget: e.target, newTarget: t.target } : void 0
212
+ targetUpdated: re(e, t) ? { oldTarget: e.target, newTarget: t.target } : void 0
213
213
  };
214
214
  };
215
- var de = /* @__PURE__ */ ((e) => (e.BODY_ONLY = "BODY_ONLY", e.TARGET_ONLY = "TARGET_ONLY", e))(de || {}), O = /* @__PURE__ */ ((e) => (e.LOCAL = "LOCAL", e.REMOTE = "REMOTE", e.SILENT = "SILENT", e))(O || {});
216
- const re = (e, t) => {
217
- var f, A;
215
+ var ae = /* @__PURE__ */ ((e) => (e.BODY_ONLY = "BODY_ONLY", e.TARGET_ONLY = "TARGET_ONLY", e))(ae || {}), O = /* @__PURE__ */ ((e) => (e.LOCAL = "LOCAL", e.REMOTE = "REMOTE", e.SILENT = "SILENT", e))(O || {});
216
+ const ce = (e, t) => {
217
+ var p, A;
218
218
  const { changes: n, origin: o } = t;
219
219
  if (!(e.options.origin ? e.options.origin === o : o !== "SILENT"))
220
220
  return !1;
221
221
  if (e.options.ignore) {
222
- const { ignore: b } = e.options, w = (U) => U && U.length > 0;
223
- if (!(w(n.created) || w(n.deleted))) {
224
- const U = (f = n.updated) == null ? void 0 : f.some((E) => w(E.bodiesCreated) || w(E.bodiesDeleted) || w(E.bodiesUpdated)), c = (A = n.updated) == null ? void 0 : A.some((E) => E.targetUpdated);
225
- if (b === "BODY_ONLY" && U && !c || b === "TARGET_ONLY" && c && !U)
222
+ const { ignore: b } = e.options, E = (S) => S && S.length > 0;
223
+ if (!(E(n.created) || E(n.deleted))) {
224
+ const S = (p = n.updated) == null ? void 0 : p.some((w) => E(w.bodiesCreated) || E(w.bodiesDeleted) || E(w.bodiesUpdated)), l = (A = n.updated) == null ? void 0 : A.some((w) => w.targetUpdated);
225
+ if (b === "BODY_ONLY" && S && !l || b === "TARGET_ONLY" && l && !S)
226
226
  return !1;
227
227
  }
228
228
  }
229
229
  if (e.options.annotations) {
230
230
  const b = /* @__PURE__ */ new Set([
231
- ...(n.created || []).map((m) => m.id),
232
- ...(n.deleted || []).map((m) => m.id),
233
- ...(n.updated || []).map(({ oldValue: m }) => m.id)
231
+ ...(n.created || []).map((h) => h.id),
232
+ ...(n.deleted || []).map((h) => h.id),
233
+ ...(n.updated || []).map(({ oldValue: h }) => h.id)
234
234
  ]);
235
- return !!(Array.isArray(e.options.annotations) ? e.options.annotations : [e.options.annotations]).find((m) => b.has(m));
235
+ return !!(Array.isArray(e.options.annotations) ? e.options.annotations : [e.options.annotations]).find((h) => b.has(h));
236
236
  } else
237
237
  return !0;
238
- }, ae = (e, t) => {
239
- const n = new Set((e.created || []).map((c) => c.id)), o = new Set((e.updated || []).map(({ newValue: c }) => c.id)), s = new Set((t.created || []).map((c) => c.id)), f = new Set((t.deleted || []).map((c) => c.id)), A = new Set((t.updated || []).map(({ oldValue: c }) => c.id)), b = new Set((t.updated || []).filter(({ oldValue: c }) => n.has(c.id) || o.has(c.id)).map(({ oldValue: c }) => c.id)), w = [
240
- ...(e.created || []).filter((c) => !f.has(c.id)).map((c) => A.has(c.id) ? t.updated.find(({ oldValue: E }) => E.id === c.id).newValue : c),
238
+ }, le = (e, t) => {
239
+ const n = new Set((e.created || []).map((l) => l.id)), o = new Set((e.updated || []).map(({ newValue: l }) => l.id)), s = new Set((t.created || []).map((l) => l.id)), p = new Set((t.deleted || []).map((l) => l.id)), A = new Set((t.updated || []).map(({ oldValue: l }) => l.id)), b = new Set((t.updated || []).filter(({ oldValue: l }) => n.has(l.id) || o.has(l.id)).map(({ oldValue: l }) => l.id)), E = [
240
+ ...(e.created || []).filter((l) => !p.has(l.id)).map((l) => A.has(l.id) ? t.updated.find(({ oldValue: w }) => w.id === l.id).newValue : l),
241
241
  ...t.created || []
242
- ], m = [
243
- ...(e.deleted || []).filter((c) => !s.has(c.id)),
244
- ...(t.deleted || []).filter((c) => !n.has(c.id))
245
- ], U = [
246
- ...(e.updated || []).filter(({ newValue: c }) => !f.has(c.id)).map((c) => {
247
- const { oldValue: E, newValue: T } = c;
248
- if (A.has(T.id)) {
249
- const h = t.updated.find((C) => C.oldValue.id === T.id).newValue;
250
- return J(E, h);
242
+ ], h = [
243
+ ...(e.deleted || []).filter((l) => !s.has(l.id)),
244
+ ...(t.deleted || []).filter((l) => !n.has(l.id))
245
+ ], S = [
246
+ ...(e.updated || []).filter(({ newValue: l }) => !p.has(l.id)).map((l) => {
247
+ const { oldValue: w, newValue: D } = l;
248
+ if (A.has(D.id)) {
249
+ const m = t.updated.find((C) => C.oldValue.id === D.id).newValue;
250
+ return J(w, m);
251
251
  } else
252
- return c;
252
+ return l;
253
253
  }),
254
- ...(t.updated || []).filter(({ oldValue: c }) => !b.has(c.id))
254
+ ...(t.updated || []).filter(({ oldValue: l }) => !b.has(l.id))
255
255
  ];
256
- return { created: w, deleted: m, updated: U };
256
+ return { created: E, deleted: h, updated: S };
257
257
  }, $ = (e) => {
258
258
  const t = e.id === void 0 ? G() : e.id;
259
259
  return {
@@ -268,205 +268,214 @@ const re = (e, t) => {
268
268
  annotation: t
269
269
  }
270
270
  };
271
- }, ce = (e) => e.id !== void 0, Oe = () => {
272
- const e = /* @__PURE__ */ new Map(), t = /* @__PURE__ */ new Map(), n = [], o = (d, l = {}) => {
273
- n.push({ onChange: d, options: l });
274
- }, s = (d) => {
275
- const l = n.findIndex((u) => u.onChange == d);
276
- l > -1 && n.splice(l, 1);
277
- }, f = (d, l) => {
271
+ }, ue = (e) => e.id !== void 0, xe = () => {
272
+ const e = /* @__PURE__ */ new Map(), t = /* @__PURE__ */ new Map(), n = [], o = (i, a = {}) => {
273
+ n.push({ onChange: i, options: a });
274
+ }, s = (i) => {
275
+ const a = n.findIndex((u) => u.onChange == i);
276
+ a > -1 && n.splice(a, 1);
277
+ }, p = (i, a) => {
278
278
  const u = {
279
- origin: d,
279
+ origin: i,
280
280
  changes: {
281
- created: l.created || [],
282
- updated: l.updated || [],
283
- deleted: l.deleted || []
281
+ created: a.created || [],
282
+ updated: a.updated || [],
283
+ deleted: a.deleted || []
284
284
  },
285
285
  state: [...e.values()]
286
286
  };
287
- n.forEach((g) => {
288
- re(g, u) && g.onChange(u);
287
+ n.forEach((f) => {
288
+ ce(f, u) && f.onChange(u);
289
289
  });
290
- }, A = (d, l = O.LOCAL) => {
291
- if (d.id && e.get(d.id))
292
- throw Error(`Cannot add annotation ${d.id} - exists already`);
290
+ }, A = (i, a = O.LOCAL) => {
291
+ if (i.id && e.get(i.id))
292
+ throw Error(`Cannot add annotation ${i.id} - exists already`);
293
293
  {
294
- const g = $(d);
295
- e.set(g.id, g), g.bodies.forEach((S) => t.set(S.id, g.id)), f(l, { created: [g] });
294
+ const f = $(i);
295
+ e.set(f.id, f), f.bodies.forEach((L) => t.set(L.id, f.id)), p(a, { created: [f] });
296
296
  }
297
- }, b = (d, l) => {
298
- const u = $(typeof d == "string" ? l : d), g = typeof d == "string" ? d : d.id, S = g && e.get(g);
299
- if (S) {
300
- const y = J(S, u);
301
- return g === u.id ? e.set(g, u) : (e.delete(g), e.set(u.id, u)), S.bodies.forEach((x) => t.delete(x.id)), u.bodies.forEach((x) => t.set(x.id, u.id)), y;
297
+ }, b = (i, a) => {
298
+ const u = $(typeof i == "string" ? a : i), f = typeof i == "string" ? i : i.id, L = f && e.get(f);
299
+ if (L) {
300
+ const y = J(L, u);
301
+ return f === u.id ? e.set(f, u) : (e.delete(f), e.set(u.id, u)), L.bodies.forEach((B) => t.delete(B.id)), u.bodies.forEach((B) => t.set(B.id, u.id)), y;
302
302
  } else
303
- console.warn(`Cannot update annotation ${g} - does not exist`);
304
- }, w = (d, l = O.LOCAL, u = O.LOCAL) => {
305
- const g = ce(l) ? u : l, S = b(d, l);
306
- S && f(g, { updated: [S] });
307
- }, m = (d, l = O.LOCAL) => {
308
- const u = d.reduce((g, S) => {
309
- const y = b(S);
310
- return y ? [...g, y] : g;
303
+ console.warn(`Cannot update annotation ${f} - does not exist`);
304
+ }, E = (i, a = O.LOCAL, u = O.LOCAL) => {
305
+ const f = ue(a) ? u : a, L = b(i, a);
306
+ L && p(f, { updated: [L] });
307
+ }, h = (i, a = O.LOCAL) => {
308
+ !!e.get(i.id) ? E(i) : A(i);
309
+ }, S = (i, a = O.LOCAL) => {
310
+ const u = i.reduce((f, L) => {
311
+ const y = b(L);
312
+ return y ? [...f, y] : f;
311
313
  }, []);
312
- u.length > 0 && f(l, { updated: u });
313
- }, U = (d, l = O.LOCAL) => {
314
- const u = e.get(d.annotation);
314
+ u.length > 0 && p(a, { updated: u });
315
+ }, l = (i, a = O.LOCAL) => {
316
+ const u = i.map($), { toAdd: f, toUpdate: L } = u.reduce((B, I) => e.get(I.id) ? { ...B, toUpdate: [...B.toUpdate, I] } : { ...B, toAdd: [...B.toAdd, I] }, { toAdd: [], toUpdate: [] }), y = L.map((B) => b(B, a)).filter(Boolean);
317
+ f.forEach((B) => {
318
+ e.set(B.id, B), B.bodies.forEach((I) => t.set(I.id, B.id));
319
+ }), p(a, { created: f, updated: y });
320
+ }, w = (i, a = O.LOCAL) => {
321
+ const u = e.get(i.annotation);
315
322
  if (u) {
316
- const g = {
323
+ const f = {
317
324
  ...u,
318
- bodies: [...u.bodies, d]
325
+ bodies: [...u.bodies, i]
319
326
  };
320
- e.set(u.id, g), t.set(d.id, g.id), f(l, { updated: [{
327
+ e.set(u.id, f), t.set(i.id, f.id), p(a, { updated: [{
321
328
  oldValue: u,
322
- newValue: g,
323
- bodiesCreated: [d]
329
+ newValue: f,
330
+ bodiesCreated: [i]
324
331
  }] });
325
332
  } else
326
- console.warn(`Attempt to add body to missing annotation: ${d.annotation}`);
327
- }, c = () => [...e.values()], E = (d = O.LOCAL) => {
328
- const l = [...e.values()];
329
- e.clear(), t.clear(), f(d, { deleted: l });
330
- }, T = (d, l = !0, u = O.LOCAL) => {
331
- const g = d.map($);
332
- if (l) {
333
- const S = [...e.values()];
334
- e.clear(), t.clear(), g.forEach((y) => {
335
- e.set(y.id, y), y.bodies.forEach((x) => t.set(x.id, y.id));
336
- }), f(u, { created: g, deleted: S });
333
+ console.warn(`Attempt to add body to missing annotation: ${i.annotation}`);
334
+ }, D = () => [...e.values()], m = (i = O.LOCAL) => {
335
+ const a = [...e.values()];
336
+ e.clear(), t.clear(), p(i, { deleted: a });
337
+ }, C = (i, a = !0, u = O.LOCAL) => {
338
+ const f = i.map($);
339
+ if (a) {
340
+ const L = [...e.values()];
341
+ e.clear(), t.clear(), f.forEach((y) => {
342
+ e.set(y.id, y), y.bodies.forEach((B) => t.set(B.id, y.id));
343
+ }), p(u, { created: f, deleted: L });
337
344
  } else {
338
- const S = d.reduce((y, x) => {
339
- const H = x.id && e.get(x.id);
340
- return H ? [...y, H] : y;
345
+ const L = i.reduce((y, B) => {
346
+ const I = B.id && e.get(B.id);
347
+ return I ? [...y, I] : y;
341
348
  }, []);
342
- if (S.length > 0)
343
- throw Error(`Bulk insert would overwrite the following annotations: ${S.map((y) => y.id).join(", ")}`);
344
- g.forEach((y) => {
345
- e.set(y.id, y), y.bodies.forEach((x) => t.set(x.id, y.id));
346
- }), f(u, { created: g });
349
+ if (L.length > 0)
350
+ throw Error(`Bulk insert would overwrite the following annotations: ${L.map((y) => y.id).join(", ")}`);
351
+ f.forEach((y) => {
352
+ e.set(y.id, y), y.bodies.forEach((B) => t.set(B.id, y.id));
353
+ }), p(u, { created: f });
347
354
  }
348
- }, h = (d) => {
349
- const l = typeof d == "string" ? d : d.id, u = e.get(l);
355
+ }, T = (i) => {
356
+ const a = typeof i == "string" ? i : i.id, u = e.get(a);
350
357
  if (u)
351
- return e.delete(l), u.bodies.forEach((g) => t.delete(g.id)), u;
352
- console.warn(`Attempt to delete missing annotation: ${l}`);
353
- }, C = (d, l = O.LOCAL) => {
354
- const u = h(d);
355
- u && f(l, { deleted: [u] });
356
- }, D = (d, l = O.LOCAL) => {
357
- const u = d.reduce((g, S) => {
358
- const y = h(S);
359
- return y ? [...g, y] : g;
358
+ return e.delete(a), u.bodies.forEach((f) => t.delete(f.id)), u;
359
+ console.warn(`Attempt to delete missing annotation: ${a}`);
360
+ }, c = (i, a = O.LOCAL) => {
361
+ const u = T(i);
362
+ u && p(a, { deleted: [u] });
363
+ }, d = (i, a = O.LOCAL) => {
364
+ const u = i.reduce((f, L) => {
365
+ const y = T(L);
366
+ return y ? [...f, y] : f;
360
367
  }, []);
361
- u.length > 0 && f(l, { deleted: u });
362
- }, a = (d) => {
363
- const l = e.get(d.annotation);
364
- if (l) {
365
- const u = l.bodies.find((g) => g.id === d.id);
368
+ u.length > 0 && p(a, { deleted: u });
369
+ }, g = (i) => {
370
+ const a = e.get(i.annotation);
371
+ if (a) {
372
+ const u = a.bodies.find((f) => f.id === i.id);
366
373
  if (u) {
367
374
  t.delete(u.id);
368
- const g = {
369
- ...l,
370
- bodies: l.bodies.filter((y) => y.id !== d.id)
375
+ const f = {
376
+ ...a,
377
+ bodies: a.bodies.filter((y) => y.id !== i.id)
371
378
  };
372
- return e.set(l.id, g), {
373
- oldValue: l,
374
- newValue: g,
379
+ return e.set(a.id, f), {
380
+ oldValue: a,
381
+ newValue: f,
375
382
  bodiesDeleted: [u]
376
383
  };
377
384
  } else
378
- console.warn(`Attempt to delete missing body ${d.id} from annotation ${d.annotation}`);
385
+ console.warn(`Attempt to delete missing body ${i.id} from annotation ${i.annotation}`);
379
386
  } else
380
- console.warn(`Attempt to delete body from missing annotation ${d.annotation}`);
381
- }, i = (d, l = O.LOCAL) => {
382
- const u = a(d);
383
- u && f(l, { updated: [u] });
384
- }, p = (d, l = O.LOCAL) => {
385
- const u = d.map((g) => a(g)).filter(Boolean);
386
- u.length > 0 && f(l, { updated: u });
387
- }, v = (d) => {
388
- const l = e.get(d);
389
- return l ? { ...l } : void 0;
390
- }, r = (d) => {
391
- const l = t.get(d);
392
- if (l) {
393
- const g = v(l).bodies.find((S) => S.id === d);
394
- if (g)
395
- return g;
396
- console.error(`Store integrity error: body ${d} in index, but not in annotation`);
387
+ console.warn(`Attempt to delete body from missing annotation ${i.annotation}`);
388
+ }, v = (i, a = O.LOCAL) => {
389
+ const u = g(i);
390
+ u && p(a, { updated: [u] });
391
+ }, r = (i, a = O.LOCAL) => {
392
+ const u = i.map((f) => g(f)).filter(Boolean);
393
+ u.length > 0 && p(a, { updated: u });
394
+ }, U = (i) => {
395
+ const a = e.get(i);
396
+ return a ? { ...a } : void 0;
397
+ }, x = (i) => {
398
+ const a = t.get(i);
399
+ if (a) {
400
+ const f = U(a).bodies.find((L) => L.id === i);
401
+ if (f)
402
+ return f;
403
+ console.error(`Store integrity error: body ${i} in index, but not in annotation`);
397
404
  } else
398
- console.warn(`Attempt to retrieve missing body: ${d}`);
399
- }, L = (d, l) => {
400
- if (d.annotation !== l.annotation)
405
+ console.warn(`Attempt to retrieve missing body: ${i}`);
406
+ }, k = (i, a) => {
407
+ if (i.annotation !== a.annotation)
401
408
  throw "Annotation integrity violation: annotation ID must be the same when updating bodies";
402
- const u = e.get(d.annotation);
409
+ const u = e.get(i.annotation);
403
410
  if (u) {
404
- const g = u.bodies.find((y) => y.id === d.id), S = {
411
+ const f = u.bodies.find((y) => y.id === i.id), L = {
405
412
  ...u,
406
- bodies: u.bodies.map((y) => y.id === g.id ? l : y)
413
+ bodies: u.bodies.map((y) => y.id === f.id ? a : y)
407
414
  };
408
- return e.set(u.id, S), g.id !== l.id && (t.delete(g.id), t.set(l.id, S.id)), {
415
+ return e.set(u.id, L), f.id !== a.id && (t.delete(f.id), t.set(a.id, L.id)), {
409
416
  oldValue: u,
410
- newValue: S,
411
- bodiesUpdated: [{ oldBody: g, newBody: l }]
417
+ newValue: L,
418
+ bodiesUpdated: [{ oldBody: f, newBody: a }]
412
419
  };
413
420
  } else
414
- console.warn(`Attempt to add body to missing annotation ${d.annotation}`);
415
- }, B = (d, l, u = O.LOCAL) => {
416
- const g = L(d, l);
417
- g && f(u, { updated: [g] });
418
- }, I = (d, l = O.LOCAL) => {
419
- const u = d.map((g) => L({ id: g.id, annotation: g.annotation }, g)).filter(Boolean);
420
- f(l, { updated: u });
421
- }, P = (d) => {
422
- const l = e.get(d.annotation);
423
- if (l) {
421
+ console.warn(`Attempt to add body to missing annotation ${i.annotation}`);
422
+ }, X = (i, a, u = O.LOCAL) => {
423
+ const f = k(i, a);
424
+ f && p(u, { updated: [f] });
425
+ }, Z = (i, a = O.LOCAL) => {
426
+ const u = i.map((f) => k({ id: f.id, annotation: f.annotation }, f)).filter(Boolean);
427
+ p(a, { updated: u });
428
+ }, H = (i) => {
429
+ const a = e.get(i.annotation);
430
+ if (a) {
424
431
  const u = {
425
- ...l,
432
+ ...a,
426
433
  target: {
427
- ...l.target,
428
- ...d
434
+ ...a.target,
435
+ ...i
429
436
  }
430
437
  };
431
- return e.set(l.id, u), {
432
- oldValue: l,
438
+ return e.set(a.id, u), {
439
+ oldValue: a,
433
440
  newValue: u,
434
441
  targetUpdated: {
435
- oldTarget: l.target,
436
- newTarget: d
442
+ oldTarget: a.target,
443
+ newTarget: i
437
444
  }
438
445
  };
439
446
  } else
440
- console.warn(`Attempt to update target on missing annotation: ${d.annotation}`);
447
+ console.warn(`Attempt to update target on missing annotation: ${i.annotation}`);
441
448
  };
442
449
  return {
443
450
  addAnnotation: A,
444
- addBody: U,
445
- all: c,
446
- bulkAddAnnotation: T,
447
- bulkDeleteAnnotation: D,
448
- bulkDeleteBodies: p,
449
- bulkUpdateAnnotation: m,
450
- bulkUpdateBodies: I,
451
- bulkUpdateTargets: (d, l = O.LOCAL) => {
452
- const u = d.map((g) => P(g)).filter(Boolean);
453
- u.length > 0 && f(l, { updated: u });
451
+ addBody: w,
452
+ all: D,
453
+ bulkAddAnnotations: C,
454
+ bulkDeleteAnnotations: d,
455
+ bulkDeleteBodies: r,
456
+ bulkUpdateAnnotations: S,
457
+ bulkUpdateBodies: Z,
458
+ bulkUpdateTargets: (i, a = O.LOCAL) => {
459
+ const u = i.map((f) => H(f)).filter(Boolean);
460
+ u.length > 0 && p(a, { updated: u });
454
461
  },
455
- clear: E,
456
- deleteAnnotation: C,
457
- deleteBody: i,
458
- getAnnotation: v,
459
- getBody: r,
462
+ bulkUpsertAnnotations: l,
463
+ clear: m,
464
+ deleteAnnotation: c,
465
+ deleteBody: v,
466
+ getAnnotation: U,
467
+ getBody: x,
460
468
  observe: o,
461
469
  unobserve: s,
462
- updateAnnotation: w,
463
- updateBody: B,
464
- updateTarget: (d, l = O.LOCAL) => {
465
- const u = P(d);
466
- u && f(l, { updated: [u] });
467
- }
470
+ updateAnnotation: E,
471
+ updateBody: X,
472
+ updateTarget: (i, a = O.LOCAL) => {
473
+ const u = H(i);
474
+ u && p(a, { updated: [u] });
475
+ },
476
+ upsertAnnotation: h
468
477
  };
469
- }, Re = (e) => ({
478
+ }, ke = (e) => ({
470
479
  ...e,
471
480
  subscribe: (n) => {
472
481
  const o = (s) => n(s.state);
@@ -487,135 +496,135 @@ let Q = () => ({
487
496
  };
488
497
  }
489
498
  });
490
- const le = 250, Be = (e, t) => {
499
+ const fe = 250, Ie = (e, t) => {
491
500
  const n = Q(), o = (t == null ? void 0 : t.changes) || [];
492
- let s = t ? t.pointer : -1, f = !1, A = 0;
501
+ let s = t ? t.pointer : -1, p = !1, A = 0;
493
502
  const b = (r) => {
494
- if (!f) {
495
- const { changes: L } = r, B = performance.now();
496
- if (B - A > le)
497
- o.splice(s + 1), o.push(L), s = o.length - 1;
503
+ if (!p) {
504
+ const { changes: U } = r, x = performance.now();
505
+ if (x - A > fe)
506
+ o.splice(s + 1), o.push(U), s = o.length - 1;
498
507
  else {
499
- const I = o.length - 1;
500
- o[I] = ae(o[I], L);
508
+ const k = o.length - 1;
509
+ o[k] = le(o[k], U);
501
510
  }
502
- A = B;
511
+ A = x;
503
512
  }
504
- f = !1;
513
+ p = !1;
505
514
  };
506
515
  e.observe(b, { origin: O.LOCAL });
507
- const w = (r) => r && r.length > 0 && e.bulkDeleteAnnotation(r), m = (r) => r && r.length > 0 && e.bulkAddAnnotation(r, !1), U = (r) => r && r.length > 0 && e.bulkUpdateAnnotation(r.map(({ oldValue: L }) => L)), c = (r) => r && r.length > 0 && e.bulkUpdateAnnotation(r.map(({ newValue: L }) => L)), E = (r) => r && r.length > 0 && e.bulkAddAnnotation(r, !1), T = (r) => r && r.length > 0 && e.bulkDeleteAnnotation(r);
516
+ const E = (r) => r && r.length > 0 && e.bulkDeleteAnnotations(r), h = (r) => r && r.length > 0 && e.bulkAddAnnotations(r, !1), S = (r) => r && r.length > 0 && e.bulkUpdateAnnotations(r.map(({ oldValue: U }) => U)), l = (r) => r && r.length > 0 && e.bulkUpdateAnnotations(r.map(({ newValue: U }) => U)), w = (r) => r && r.length > 0 && e.bulkAddAnnotations(r, !1), D = (r) => r && r.length > 0 && e.bulkDeleteAnnotations(r);
508
517
  return {
509
518
  canRedo: () => o.length - 1 > s,
510
519
  canUndo: () => s > -1,
511
520
  destroy: () => e.unobserve(b),
512
521
  getHistory: () => ({ changes: [...o], pointer: s }),
513
- on: (r, L) => n.on(r, L),
522
+ on: (r, U) => n.on(r, U),
514
523
  redo: () => {
515
524
  if (o.length - 1 > s) {
516
- f = !0;
517
- const { created: r, updated: L, deleted: B } = o[s + 1];
518
- m(r), c(L), T(B), n.emit("redo", o[s + 1]), s += 1;
525
+ p = !0;
526
+ const { created: r, updated: U, deleted: x } = o[s + 1];
527
+ h(r), l(U), D(x), n.emit("redo", o[s + 1]), s += 1;
519
528
  }
520
529
  },
521
530
  undo: () => {
522
531
  if (s > -1) {
523
- f = !0;
524
- const { created: r, updated: L, deleted: B } = o[s];
525
- w(r), U(L), E(B), n.emit("undo", o[s]), s -= 1;
532
+ p = !0;
533
+ const { created: r, updated: U, deleted: x } = o[s];
534
+ E(r), S(U), w(x), n.emit("undo", o[s]), s -= 1;
526
535
  }
527
536
  }
528
537
  };
529
- }, xe = () => {
530
- const { subscribe: e, set: t } = j([]);
538
+ }, Ne = () => {
539
+ const { subscribe: e, set: t } = P([]);
531
540
  return {
532
541
  subscribe: e,
533
542
  set: t
534
543
  };
535
- }, Ie = (e, t, n, o) => {
536
- const { hover: s, selection: f, store: A, viewport: b } = e, w = /* @__PURE__ */ new Map();
537
- let m = [], U, c;
538
- const E = (a, i) => {
539
- w.has(a) ? w.get(a).push(i) : w.set(a, [i]);
540
- }, T = (a, i) => {
541
- const p = w.get(a);
542
- if (p) {
543
- const v = p.indexOf(i);
544
- v !== -1 && p.splice(v, 1);
544
+ }, ze = (e, t, n, o) => {
545
+ const { hover: s, selection: p, store: A, viewport: b } = e, E = /* @__PURE__ */ new Map();
546
+ let h = [], S, l;
547
+ const w = (c, d) => {
548
+ E.has(c) ? E.get(c).push(d) : E.set(c, [d]);
549
+ }, D = (c, d) => {
550
+ const g = E.get(c);
551
+ if (g) {
552
+ const v = g.indexOf(d);
553
+ v !== -1 && g.splice(v, 1);
545
554
  }
546
- }, h = (a, i, p) => {
547
- w.has(a) && setTimeout(() => {
548
- w.get(a).forEach((v) => {
555
+ }, m = (c, d, g) => {
556
+ E.has(c) && setTimeout(() => {
557
+ E.get(c).forEach((v) => {
549
558
  if (n) {
550
- const r = Array.isArray(i) ? i.map((B) => n.serialize(B)) : n.serialize(i), L = p ? p instanceof PointerEvent ? p : n.serialize(p) : void 0;
551
- v(r, L);
559
+ const r = Array.isArray(d) ? d.map((x) => n.serialize(x)) : n.serialize(d), U = g ? g instanceof PointerEvent ? g : n.serialize(g) : void 0;
560
+ v(r, U);
552
561
  } else
553
- v(i, p);
562
+ v(d, g);
554
563
  });
555
564
  }, 1);
556
565
  }, C = () => {
557
- const { selected: a } = f, i = (a || []).map(({ id: p }) => A.getAnnotation(p));
558
- i.forEach((p) => {
559
- const v = m.find((r) => r.id === p.id);
560
- (!v || !k(v, p)) && h("updateAnnotation", p, v);
561
- }), m = m.map((p) => {
562
- const v = i.find(({ id: r }) => r === p.id);
563
- return v || p;
566
+ const { selected: c } = p, d = (c || []).map(({ id: g }) => A.getAnnotation(g));
567
+ d.forEach((g) => {
568
+ const v = h.find((r) => r.id === g.id);
569
+ (!v || !N(v, g)) && m("updateAnnotation", g, v);
570
+ }), h = h.map((g) => {
571
+ const v = d.find(({ id: r }) => r === g.id);
572
+ return v || g;
564
573
  });
565
574
  };
566
- f.subscribe(({ selected: a }) => {
567
- if (!(m.length === 0 && a.length === 0)) {
568
- if (m.length === 0 && a.length > 0)
569
- m = a.map(({ id: i }) => A.getAnnotation(i));
570
- else if (m.length > 0 && a.length === 0)
571
- m.forEach((i) => {
572
- const p = A.getAnnotation(i.id);
573
- p && !k(p, i) && h("updateAnnotation", p, i);
574
- }), m = [];
575
+ p.subscribe(({ selected: c }) => {
576
+ if (!(h.length === 0 && c.length === 0)) {
577
+ if (h.length === 0 && c.length > 0)
578
+ h = c.map(({ id: d }) => A.getAnnotation(d));
579
+ else if (h.length > 0 && c.length === 0)
580
+ h.forEach((d) => {
581
+ const g = A.getAnnotation(d.id);
582
+ g && !N(g, d) && m("updateAnnotation", g, d);
583
+ }), h = [];
575
584
  else {
576
- const i = new Set(m.map((r) => r.id)), p = new Set(a.map(({ id: r }) => r));
577
- m.filter((r) => !p.has(r.id)).forEach((r) => {
578
- const L = A.getAnnotation(r.id);
579
- L && !k(L, r) && h("updateAnnotation", L, r);
580
- }), m = [
585
+ const d = new Set(h.map((r) => r.id)), g = new Set(c.map(({ id: r }) => r));
586
+ h.filter((r) => !g.has(r.id)).forEach((r) => {
587
+ const U = A.getAnnotation(r.id);
588
+ U && !N(U, r) && m("updateAnnotation", U, r);
589
+ }), h = [
581
590
  // Remove annotations that were deselected
582
- ...m.filter((r) => p.has(r.id)),
591
+ ...h.filter((r) => g.has(r.id)),
583
592
  // Add editable annotations that were selected
584
- ...a.filter(({ id: r }) => !i.has(r)).map(({ id: r }) => A.getAnnotation(r))
593
+ ...c.filter(({ id: r }) => !d.has(r)).map(({ id: r }) => A.getAnnotation(r))
585
594
  ];
586
595
  }
587
- h("selectionChanged", m);
596
+ m("selectionChanged", h);
588
597
  }
589
- }), s.subscribe((a) => {
590
- !U && a ? h("mouseEnterAnnotation", A.getAnnotation(a)) : U && !a ? h("mouseLeaveAnnotation", A.getAnnotation(U)) : U && a && (h("mouseLeaveAnnotation", A.getAnnotation(U)), h("mouseEnterAnnotation", A.getAnnotation(a))), U = a;
591
- }), b == null || b.subscribe((a) => h("viewportIntersect", a.map((i) => A.getAnnotation(i)))), A.observe((a) => {
592
- o && (c && clearTimeout(c), c = setTimeout(C, 1e3));
593
- const { created: i, deleted: p } = a.changes;
594
- (i || []).forEach((r) => h("createAnnotation", r)), (p || []).forEach((r) => h("deleteAnnotation", r)), (a.changes.updated || []).filter((r) => [
598
+ }), s.subscribe((c) => {
599
+ !S && c ? m("mouseEnterAnnotation", A.getAnnotation(c)) : S && !c ? m("mouseLeaveAnnotation", A.getAnnotation(S)) : S && c && (m("mouseLeaveAnnotation", A.getAnnotation(S)), m("mouseEnterAnnotation", A.getAnnotation(c))), S = c;
600
+ }), b == null || b.subscribe((c) => m("viewportIntersect", c.map((d) => A.getAnnotation(d)))), A.observe((c) => {
601
+ o && (l && clearTimeout(l), l = setTimeout(C, 1e3));
602
+ const { created: d, deleted: g } = c.changes;
603
+ (d || []).forEach((r) => m("createAnnotation", r)), (g || []).forEach((r) => m("deleteAnnotation", r)), (c.changes.updated || []).filter((r) => [
595
604
  ...r.bodiesCreated || [],
596
605
  ...r.bodiesDeleted || [],
597
606
  ...r.bodiesUpdated || []
598
- ].length > 0).forEach(({ oldValue: r, newValue: L }) => {
599
- const B = m.find((I) => I.id === r.id) || r;
600
- m = m.map((I) => I.id === r.id ? L : I), h("updateAnnotation", L, B);
607
+ ].length > 0).forEach(({ oldValue: r, newValue: U }) => {
608
+ const x = h.find((k) => k.id === r.id) || r;
609
+ h = h.map((k) => k.id === r.id ? U : k), m("updateAnnotation", U, x);
601
610
  });
602
- }, { origin: O.LOCAL }), A.observe((a) => {
603
- if (m) {
604
- const i = new Set(m.map((v) => v.id)), p = (a.changes.updated || []).filter(({ newValue: v }) => i.has(v.id)).map(({ newValue: v }) => v);
605
- p.length > 0 && (m = m.map((v) => {
606
- const r = p.find((L) => L.id === v.id);
611
+ }, { origin: O.LOCAL }), A.observe((c) => {
612
+ if (h) {
613
+ const d = new Set(h.map((v) => v.id)), g = (c.changes.updated || []).filter(({ newValue: v }) => d.has(v.id)).map(({ newValue: v }) => v);
614
+ g.length > 0 && (h = h.map((v) => {
615
+ const r = g.find((U) => U.id === v.id);
607
616
  return r || v;
608
617
  }));
609
618
  }
610
619
  }, { origin: O.REMOTE });
611
- const D = (a) => (i) => {
612
- const { updated: p } = i;
613
- a ? (p || []).forEach((v) => h("updateAnnotation", v.oldValue, v.newValue)) : (p || []).forEach((v) => h("updateAnnotation", v.newValue, v.oldValue));
620
+ const T = (c) => (d) => {
621
+ const { updated: g } = d;
622
+ c ? (g || []).forEach((v) => m("updateAnnotation", v.oldValue, v.newValue)) : (g || []).forEach((v) => m("updateAnnotation", v.newValue, v.oldValue));
614
623
  };
615
- return t.on("undo", D(!0)), t.on("redo", D(!1)), { on: E, off: T, emit: h };
616
- }, ke = (e) => (t) => t.map((n) => e.serialize(n)), ue = (e) => (t) => t.reduce((n, o) => {
617
- const { parsed: s, error: f } = e.parse(o);
618
- return f ? {
624
+ return t.on("undo", T(!0)), t.on("redo", T(!1)), { on: w, off: D, emit: m };
625
+ }, $e = (e) => (t) => t.map((n) => e.serialize(n)), pe = (e) => (t) => t.reduce((n, o) => {
626
+ const { parsed: s, error: p } = e.parse(o);
627
+ return p ? {
619
628
  parsed: n.parsed,
620
629
  failed: [...n.failed, o]
621
630
  } : s ? {
@@ -624,124 +633,124 @@ const le = 250, Be = (e, t) => {
624
633
  } : {
625
634
  ...n
626
635
  };
627
- }, { parsed: [], failed: [] }), Ne = (e, t, n) => {
628
- const { store: o, selection: s } = e, f = (a) => {
636
+ }, { parsed: [], failed: [] }), Ve = (e, t, n) => {
637
+ const { store: o, selection: s } = e, p = (c) => {
629
638
  if (n) {
630
- const { parsed: i, error: p } = n.parse(a);
631
- i ? o.addAnnotation(i, O.REMOTE) : console.error(p);
639
+ const { parsed: d, error: g } = n.parse(c);
640
+ d ? o.addAnnotation(d, O.REMOTE) : console.error(g);
632
641
  } else
633
- o.addAnnotation(Y(a), O.REMOTE);
634
- }, A = () => s.clear(), b = () => o.clear(), w = (a) => {
635
- const i = o.getAnnotation(a);
636
- return n && i ? n.serialize(i) : i;
637
- }, m = () => n ? o.all().map(n.serialize) : o.all(), U = () => {
638
- var p;
639
- const i = (((p = s.selected) == null ? void 0 : p.map((v) => v.id)) || []).map((v) => o.getAnnotation(v)).filter(Boolean);
640
- return n ? i.map(n.serialize) : i;
641
- }, c = (a, i = !0) => fetch(a).then((p) => p.json()).then((p) => (T(p, i), p)), E = (a) => {
642
- if (typeof a == "string") {
643
- const i = o.getAnnotation(a);
644
- if (o.deleteAnnotation(a), i)
645
- return n ? n.serialize(i) : i;
642
+ o.addAnnotation(j(c), O.REMOTE);
643
+ }, A = () => s.clear(), b = () => o.clear(), E = (c) => {
644
+ const d = o.getAnnotation(c);
645
+ return n && d ? n.serialize(d) : d;
646
+ }, h = () => n ? o.all().map(n.serialize) : o.all(), S = () => {
647
+ var g;
648
+ const d = (((g = s.selected) == null ? void 0 : g.map((v) => v.id)) || []).map((v) => o.getAnnotation(v)).filter(Boolean);
649
+ return n ? d.map(n.serialize) : d;
650
+ }, l = (c, d = !0) => fetch(c).then((g) => g.json()).then((g) => (D(g, d), g)), w = (c) => {
651
+ if (typeof c == "string") {
652
+ const d = o.getAnnotation(c);
653
+ if (o.deleteAnnotation(c), d)
654
+ return n ? n.serialize(d) : d;
646
655
  } else {
647
- const i = n ? n.parse(a).parsed : a;
648
- if (i)
649
- return o.deleteAnnotation(i), a;
656
+ const d = n ? n.parse(c).parsed : c;
657
+ if (d)
658
+ return o.deleteAnnotation(d), c;
650
659
  }
651
- }, T = (a, i = !0) => {
660
+ }, D = (c, d = !0) => {
652
661
  if (n) {
653
- const p = n.parseAll || ue(n), { parsed: v, failed: r } = p(a);
654
- r.length > 0 && console.warn(`Discarded ${r.length} invalid annotations`, r), o.bulkAddAnnotation(v, i, O.REMOTE);
662
+ const g = n.parseAll || pe(n), { parsed: v, failed: r } = g(c);
663
+ r.length > 0 && console.warn(`Discarded ${r.length} invalid annotations`, r), o.bulkAddAnnotations(v, d, O.REMOTE);
655
664
  } else
656
- o.bulkAddAnnotation(a.map(Y), i, O.REMOTE);
657
- }, h = (a, i) => {
658
- a ? s.setSelected(a, i) : s.clear();
659
- }, C = (a) => {
660
- s.clear(), s.setUserSelectAction(a);
661
- }, D = (a) => {
665
+ o.bulkAddAnnotations(c.map(j), d, O.REMOTE);
666
+ }, m = (c, d) => {
667
+ c ? s.setSelected(c, d) : s.clear();
668
+ }, C = (c) => {
669
+ s.clear(), s.setUserSelectAction(c);
670
+ }, T = (c) => {
662
671
  if (n) {
663
- const i = n.parse(a).parsed, p = n.serialize(o.getAnnotation(i.id));
664
- return o.updateAnnotation(i), p;
672
+ const d = n.parse(c).parsed, g = n.serialize(o.getAnnotation(d.id));
673
+ return o.updateAnnotation(d), g;
665
674
  } else {
666
- const i = o.getAnnotation(a.id);
667
- return o.updateAnnotation(Y(a)), i;
675
+ const d = o.getAnnotation(c.id);
676
+ return o.updateAnnotation(j(c)), d;
668
677
  }
669
678
  };
670
679
  return {
671
- addAnnotation: f,
680
+ addAnnotation: p,
672
681
  cancelSelected: A,
673
682
  canRedo: t.canRedo,
674
683
  canUndo: t.canUndo,
675
684
  clearAnnotations: b,
676
- getAnnotationById: w,
677
- getAnnotations: m,
685
+ getAnnotationById: E,
686
+ getAnnotations: h,
678
687
  getHistory: t.getHistory,
679
- getSelected: U,
680
- loadAnnotations: c,
688
+ getSelected: S,
689
+ loadAnnotations: l,
681
690
  redo: t.redo,
682
- removeAnnotation: E,
683
- setAnnotations: T,
684
- setSelected: h,
691
+ removeAnnotation: w,
692
+ setAnnotations: D,
693
+ setSelected: m,
685
694
  setUserSelectAction: C,
686
695
  undo: t.undo,
687
- updateAnnotation: D
696
+ updateAnnotation: T
688
697
  };
689
- }, ze = (e, t, n) => typeof t == "function" ? t(e, n) : t, $e = (e, t) => typeof e != "function" && typeof t != "function" ? {
698
+ }, _e = (e, t, n) => typeof t == "function" ? t(e, n) : t, Ye = (e, t) => typeof e != "function" && typeof t != "function" ? {
690
699
  ...e || {},
691
700
  ...t || {}
692
701
  } : (n, o) => {
693
- const s = typeof e == "function" ? e(n, o) : e, f = typeof t == "function" ? t(n, o) : t;
702
+ const s = typeof e == "function" ? e(n, o) : e, p = typeof t == "function" ? t(n, o) : t;
694
703
  return {
695
704
  ...s || {},
696
- ...f || {}
705
+ ...p || {}
697
706
  };
698
- }, fe = "useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";
699
- let pe = (e) => crypto.getRandomValues(new Uint8Array(e)), ge = (e, t, n) => {
707
+ }, ge = "useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";
708
+ let he = (e) => crypto.getRandomValues(new Uint8Array(e)), me = (e, t, n) => {
700
709
  let o = (2 << Math.log2(e.length - 1)) - 1, s = -~(1.6 * o * t / e.length);
701
- return (f = t) => {
710
+ return (p = t) => {
702
711
  let A = "";
703
712
  for (; ; ) {
704
- let b = n(s), w = s | 0;
705
- for (; w--; )
706
- if (A += e[b[w] & o] || "", A.length >= f) return A;
713
+ let b = n(s), E = s | 0;
714
+ for (; E--; )
715
+ if (A += e[b[E] & o] || "", A.length >= p) return A;
707
716
  }
708
717
  };
709
- }, he = (e, t = 21) => ge(e, t | 0, pe), me = (e = 21) => {
718
+ }, Ae = (e, t = 21) => me(e, t | 0, he), be = (e = 21) => {
710
719
  let t = "", n = crypto.getRandomValues(new Uint8Array(e |= 0));
711
720
  for (; e--; )
712
- t += fe[n[e] & 63];
721
+ t += ge[n[e] & 63];
713
722
  return t;
714
723
  };
715
- const Ve = () => ({ isGuest: !0, id: he("1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_", 20)() }), Ae = (e) => {
724
+ const je = () => ({ isGuest: !0, id: Ae("1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_", 20)() }), ve = (e) => {
716
725
  const t = JSON.stringify(e);
717
726
  let n = 0;
718
727
  for (let o = 0, s = t.length; o < s; o++) {
719
- let f = t.charCodeAt(o);
720
- n = (n << 5) - n + f, n |= 0;
728
+ let p = t.charCodeAt(o);
729
+ n = (n << 5) - n + p, n |= 0;
721
730
  }
722
731
  return `${n}`;
723
- }, be = (e) => e ? typeof e == "object" ? { ...e } : e : void 0, _e = (e, t) => (Array.isArray(e) ? e : [e]).map((n) => {
724
- const { id: o, type: s, purpose: f, value: A, created: b, modified: w, creator: m, ...U } = n;
732
+ }, Ee = (e) => e ? typeof e == "object" ? { ...e } : e : void 0, Pe = (e, t) => (Array.isArray(e) ? e : [e]).map((n) => {
733
+ const { id: o, type: s, purpose: p, value: A, created: b, modified: E, creator: h, ...S } = n;
725
734
  return {
726
- id: o || `temp-${Ae(n)}`,
735
+ id: o || `temp-${ve(n)}`,
727
736
  annotation: t,
728
737
  type: s,
729
- purpose: f,
738
+ purpose: p,
730
739
  value: A,
731
- creator: be(m),
740
+ creator: Ee(h),
732
741
  created: b ? new Date(b) : void 0,
733
- updated: w ? new Date(w) : void 0,
734
- ...U
742
+ updated: E ? new Date(E) : void 0,
743
+ ...S
735
744
  };
736
- }), Ye = (e) => e.map((t) => {
745
+ }), He = (e) => e.map((t) => {
737
746
  var b;
738
- const { annotation: n, created: o, updated: s, ...f } = t, A = {
739
- ...f,
747
+ const { annotation: n, created: o, updated: s, ...p } = t, A = {
748
+ ...p,
740
749
  created: o == null ? void 0 : o.toISOString(),
741
750
  modified: s == null ? void 0 : s.toISOString()
742
751
  };
743
752
  return (b = A.id) != null && b.startsWith("temp-") && delete A.id, A;
744
- }), ve = [
753
+ }), we = [
745
754
  "#ff7c00",
746
755
  // orange
747
756
  "#1ac938",
@@ -760,101 +769,101 @@ const Ve = () => ({ isGuest: !0, id: he("1234567890abcdefghijklmnopqrstuvwxyzABC
760
769
  // cyan
761
770
  "#023eff"
762
771
  // blue
763
- ], Ee = () => {
764
- const e = [...ve];
772
+ ], Ce = () => {
773
+ const e = [...we];
765
774
  return { assignRandomColor: () => {
766
775
  const o = Math.floor(Math.random() * e.length), s = e[o];
767
776
  return e.splice(o, 1), s;
768
777
  }, releaseColor: (o) => e.push(o) };
769
- }, we = () => {
770
- const e = Ee();
778
+ }, ye = () => {
779
+ const e = Ce();
771
780
  return { addUser: (o, s) => {
772
- const f = e.assignRandomColor();
781
+ const p = e.assignRandomColor();
773
782
  return {
774
783
  label: s.name || s.id,
775
784
  avatar: s.avatar,
776
- color: f
785
+ color: p
777
786
  };
778
787
  }, removeUser: (o) => e.releaseColor(o.appearance.color) };
779
- }, Ce = (e, t) => e.every((n) => e.includes(n)) && t.every((n) => e.includes(n)), je = me(), Pe = (e = we()) => {
780
- const t = Q(), n = /* @__PURE__ */ new Map(), o = /* @__PURE__ */ new Map(), s = (c, E) => {
781
- if (n.has(c)) {
782
- console.warn("Attempt to add user that is already present", c, E);
788
+ }, Le = (e, t) => e.every((n) => e.includes(n)) && t.every((n) => e.includes(n)), Me = be(), We = (e = ye()) => {
789
+ const t = Q(), n = /* @__PURE__ */ new Map(), o = /* @__PURE__ */ new Map(), s = (l, w) => {
790
+ if (n.has(l)) {
791
+ console.warn("Attempt to add user that is already present", l, w);
783
792
  return;
784
793
  }
785
- const T = e.addUser(c, E);
786
- n.set(c, {
787
- ...E,
788
- presenceKey: c,
789
- appearance: T
794
+ const D = e.addUser(l, w);
795
+ n.set(l, {
796
+ ...w,
797
+ presenceKey: l,
798
+ appearance: D
790
799
  });
791
- }, f = (c) => {
792
- const E = n.get(c);
793
- if (!E) {
794
- console.warn("Attempt to remove user that is not present", c);
800
+ }, p = (l) => {
801
+ const w = n.get(l);
802
+ if (!w) {
803
+ console.warn("Attempt to remove user that is not present", l);
795
804
  return;
796
805
  }
797
- e.removeUser(E), n.delete(c);
798
- }, A = (c) => {
799
- const E = new Set(c.map((C) => C.presenceKey)), T = c.filter(({ presenceKey: C }) => !n.has(C)), h = Array.from(n.values()).filter((C) => !E.has(C.presenceKey));
800
- T.forEach(({ presenceKey: C, user: D }) => s(C, D)), h.forEach((C) => {
801
- const { presenceKey: D } = C;
802
- o.has(D) && t.emit("selectionChange", C, null), f(D);
803
- }), (T.length > 0 || h.length > 0) && t.emit("presence", m());
804
- }, b = (c, E) => {
805
- const T = n.get(c);
806
- if (!T) {
806
+ e.removeUser(w), n.delete(l);
807
+ }, A = (l) => {
808
+ const w = new Set(l.map((C) => C.presenceKey)), D = l.filter(({ presenceKey: C }) => !n.has(C)), m = Array.from(n.values()).filter((C) => !w.has(C.presenceKey));
809
+ D.forEach(({ presenceKey: C, user: T }) => s(C, T)), m.forEach((C) => {
810
+ const { presenceKey: T } = C;
811
+ o.has(T) && t.emit("selectionChange", C, null), p(T);
812
+ }), (D.length > 0 || m.length > 0) && t.emit("presence", h());
813
+ }, b = (l, w) => {
814
+ const D = n.get(l);
815
+ if (!D) {
807
816
  console.warn("Activity notification from user that is not present");
808
817
  return;
809
818
  }
810
- const h = o.get(c);
811
- (!h || !Ce(h, E)) && (o.set(c, E), t.emit("selectionChange", T, E));
812
- }, w = (c, E) => {
813
- const T = n.get(c);
814
- if (!T) {
815
- console.warn("Selection change for user that is not present", c);
819
+ const m = o.get(l);
820
+ (!m || !Le(m, w)) && (o.set(l, w), t.emit("selectionChange", D, w));
821
+ }, E = (l, w) => {
822
+ const D = n.get(l);
823
+ if (!D) {
824
+ console.warn("Selection change for user that is not present", l);
816
825
  return;
817
826
  }
818
- E ? o.set(c, E) : o.delete(c), t.emit("selectionChange", T, E);
819
- }, m = () => [...Array.from(n.values())];
827
+ w ? o.set(l, w) : o.delete(l), t.emit("selectionChange", D, w);
828
+ }, h = () => [...Array.from(n.values())];
820
829
  return {
821
- getPresentUsers: m,
830
+ getPresentUsers: h,
822
831
  notifyActivity: b,
823
- on: (c, E) => t.on(c, E),
832
+ on: (l, w) => t.on(l, w),
824
833
  syncUsers: A,
825
- updateSelection: w
834
+ updateSelection: E
826
835
  };
827
836
  };
828
837
  export {
829
- de as Ignore,
838
+ ae as Ignore,
830
839
  O as Origin,
831
- je as PRESENCE_KEY,
832
- Z as UserSelectAction,
833
- $e as chainStyles,
834
- ze as computeStyle,
835
- Ve as createAnonymousGuest,
836
- Ne as createBaseAnnotator,
837
- De as createBody,
838
- we as createDefaultAppearanceProvider,
839
- Ue as createHoverState,
840
- Ie as createLifecycleObserver,
841
- Pe as createPresenceState,
842
- Se as createSelectionState,
843
- Oe as createStore,
844
- Be as createUndoStack,
845
- xe as createViewportState,
846
- Ee as defaultColorProvider,
840
+ Me as PRESENCE_KEY,
841
+ K as UserSelectAction,
842
+ Ye as chainStyles,
843
+ _e as computeStyle,
844
+ je as createAnonymousGuest,
845
+ Ve as createBaseAnnotator,
846
+ Re as createBody,
847
+ ye as createDefaultAppearanceProvider,
848
+ Oe as createHoverState,
849
+ ze as createLifecycleObserver,
850
+ We as createPresenceState,
851
+ De as createSelectionState,
852
+ xe as createStore,
853
+ Ie as createUndoStack,
854
+ Ne as createViewportState,
855
+ Ce as defaultColorProvider,
847
856
  J as diffAnnotations,
848
- Te as getContributors,
849
- ae as mergeChanges,
857
+ Be as getContributors,
858
+ le as mergeChanges,
850
859
  W as onUserSelect,
851
- ue as parseAll,
852
- _e as parseW3CBodies,
853
- be as parseW3CUser,
854
- Y as reviveDates,
855
- ke as serializeAll,
856
- Ye as serializeW3CBodies,
857
- re as shouldNotify,
858
- Re as toSvelteStore
860
+ pe as parseAll,
861
+ Pe as parseW3CBodies,
862
+ Ee as parseW3CUser,
863
+ j as reviveDates,
864
+ $e as serializeAll,
865
+ He as serializeW3CBodies,
866
+ ce as shouldNotify,
867
+ ke as toSvelteStore
859
868
  };
860
869
  //# sourceMappingURL=annotorious-core.es.js.map