@annotorious/core 3.7.11 → 3.7.13

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