cs-label-tool 2.0.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/README.md +824 -0
  2. package/dist/components/annotator-element.d.ts +14 -0
  3. package/dist/components/annotator-element.d.ts.map +1 -0
  4. package/dist/components/define.d.ts +6 -0
  5. package/dist/components/define.d.ts.map +1 -0
  6. package/dist/components/styles.d.ts +2 -0
  7. package/dist/components/styles.d.ts.map +1 -0
  8. package/dist/core/annotator.d.ts +8 -0
  9. package/dist/core/annotator.d.ts.map +1 -0
  10. package/dist/core/commands.d.ts +34 -0
  11. package/dist/core/commands.d.ts.map +1 -0
  12. package/dist/core/events.d.ts +25 -0
  13. package/dist/core/events.d.ts.map +1 -0
  14. package/dist/core/immutability.d.ts +4 -0
  15. package/dist/core/immutability.d.ts.map +1 -0
  16. package/dist/core/state.d.ts +42 -0
  17. package/dist/core/state.d.ts.map +1 -0
  18. package/dist/core/types.d.ts +64 -0
  19. package/dist/core/types.d.ts.map +1 -0
  20. package/dist/cs-label-tool.d.ts +247 -0
  21. package/dist/cs-label-tool.d.ts.map +1 -0
  22. package/dist/geometry/matrix.d.ts +5 -0
  23. package/dist/geometry/matrix.d.ts.map +1 -0
  24. package/dist/geometry/polygon.d.ts +10 -0
  25. package/dist/geometry/polygon.d.ts.map +1 -0
  26. package/dist/geometry/rect.d.ts +4 -0
  27. package/dist/geometry/rect.d.ts.map +1 -0
  28. package/dist/geometry/types.d.ts +19 -0
  29. package/dist/geometry/types.d.ts.map +1 -0
  30. package/dist/image/image-commands.d.ts +14 -0
  31. package/dist/image/image-commands.d.ts.map +1 -0
  32. package/dist/image/standard-source.d.ts +3 -0
  33. package/dist/image/standard-source.d.ts.map +1 -0
  34. package/dist/image/types.d.ts +12 -0
  35. package/dist/image/types.d.ts.map +1 -0
  36. package/dist/index.d.ts +25 -0
  37. package/dist/index.d.ts.map +1 -0
  38. package/dist/index.js +2299 -0
  39. package/dist/index.js.map +1 -0
  40. package/dist/labels/labels.d.ts +6 -0
  41. package/dist/labels/labels.d.ts.map +1 -0
  42. package/dist/mask/rle.d.ts +13 -0
  43. package/dist/mask/rle.d.ts.map +1 -0
  44. package/dist/render/canvas-layers.d.ts +14 -0
  45. package/dist/render/canvas-layers.d.ts.map +1 -0
  46. package/dist/render/canvas-renderer.d.ts +10 -0
  47. package/dist/render/canvas-renderer.d.ts.map +1 -0
  48. package/dist/render/scheduler.d.ts +12 -0
  49. package/dist/render/scheduler.d.ts.map +1 -0
  50. package/dist/spatial/grid-index.d.ts +15 -0
  51. package/dist/spatial/grid-index.d.ts.map +1 -0
  52. package/dist/tools/api.d.ts +32 -0
  53. package/dist/tools/api.d.ts.map +1 -0
  54. package/dist/tools/brush-tool.d.ts +23 -0
  55. package/dist/tools/brush-tool.d.ts.map +1 -0
  56. package/dist/tools/controller.d.ts +9 -0
  57. package/dist/tools/controller.d.ts.map +1 -0
  58. package/dist/tools/eraser-tool.d.ts +8 -0
  59. package/dist/tools/eraser-tool.d.ts.map +1 -0
  60. package/dist/tools/index.d.ts +11 -0
  61. package/dist/tools/index.d.ts.map +1 -0
  62. package/dist/tools/polygon-tool.d.ts +33 -0
  63. package/dist/tools/polygon-tool.d.ts.map +1 -0
  64. package/dist/tools/rect-tool.d.ts +32 -0
  65. package/dist/tools/rect-tool.d.ts.map +1 -0
  66. package/dist/tools/registry.d.ts +4 -0
  67. package/dist/tools/registry.d.ts.map +1 -0
  68. package/dist/tools/select-tool.d.ts +15 -0
  69. package/dist/tools/select-tool.d.ts.map +1 -0
  70. package/dist/tools/types.d.ts +80 -0
  71. package/dist/tools/types.d.ts.map +1 -0
  72. package/dist/viewport/viewport.d.ts +20 -0
  73. package/dist/viewport/viewport.d.ts.map +1 -0
  74. package/package.json +38 -0
package/dist/index.js ADDED
@@ -0,0 +1,2299 @@
1
+ //#region src/spatial/grid-index.ts
2
+ function e(e) {
3
+ if (![
4
+ e.x,
5
+ e.y,
6
+ e.width,
7
+ e.height
8
+ ].every(Number.isFinite) || e.width < 0 || e.height < 0) throw RangeError("Spatial bounds must be finite and non-negative.");
9
+ }
10
+ function t(e, t) {
11
+ let n = Math.floor(t.x / e), r = Math.floor(t.y / e), i = Math.floor((t.x + t.width) / e), a = Math.floor((t.y + t.height) / e), o = [];
12
+ for (let e = r; e <= a; e += 1) for (let t = n; t <= i; t += 1) o.push(`${t}:${e}`);
13
+ return o;
14
+ }
15
+ function n(e, n) {
16
+ let r = e.items.get(n);
17
+ if (r !== void 0) for (let i of t(e.cellSize, r)) {
18
+ let t = e.cells.get(i);
19
+ t !== void 0 && (t.delete(n), t.size === 0 && e.cells.delete(i));
20
+ }
21
+ }
22
+ function r(e, n, r, i) {
23
+ for (let a of t(n, i)) {
24
+ let t = e.get(a) ?? /* @__PURE__ */ new Set();
25
+ t.add(r), e.set(a, t);
26
+ }
27
+ }
28
+ function i(e, t) {
29
+ return !(e.x + e.width < t.x || t.x + t.width < e.x || e.y + e.height < t.y || t.y + t.height < e.y);
30
+ }
31
+ function a(e) {
32
+ if (!Number.isFinite(e) || e <= 0) throw RangeError("Spatial cell size must be finite and positive.");
33
+ return {
34
+ cellSize: e,
35
+ cells: /* @__PURE__ */ new Map(),
36
+ items: /* @__PURE__ */ new Map(),
37
+ order: /* @__PURE__ */ new Map(),
38
+ nextOrder: 0
39
+ };
40
+ }
41
+ function o(t, n, i) {
42
+ return e(i), t.items.has(n) ? s(t, n, i) : (r(t.cells, t.cellSize, n, i), t.items.set(n, { ...i }), t.order.set(n, t.nextOrder), t.nextOrder += 1, t);
43
+ }
44
+ function s(t, i, a) {
45
+ return e(a), t.items.has(i) ? (n(t, i), r(t.cells, t.cellSize, i, a), t.items.set(i, { ...a }), t) : o(t, i, a);
46
+ }
47
+ function c(e, t) {
48
+ return e.items.has(t) ? (n(e, t), e.items.delete(t), e.order.delete(t), e) : e;
49
+ }
50
+ function l(e, t, n, r) {
51
+ return o(e, t, n), e.order.set(t, r), e;
52
+ }
53
+ function u(n, r) {
54
+ e(r);
55
+ let a = /* @__PURE__ */ new Set();
56
+ for (let e of t(n.cellSize, r)) for (let t of n.cells.get(e) ?? []) a.add(t);
57
+ return [...a].filter((e) => {
58
+ let t = n.items.get(e);
59
+ return t !== void 0 && i(t, r);
60
+ }).sort((e, t) => (n.order.get(e) ?? 0) - (n.order.get(t) ?? 0));
61
+ }
62
+ //#endregion
63
+ //#region src/geometry/polygon.ts
64
+ function d(e, t, n) {
65
+ let r = (e.y - t.y) * (n.x - t.x), i = (e.x - t.x) * (n.y - t.y), a = r - i, o = 2 ** -52 * 16 * Math.max(1, Math.abs(r) + Math.abs(i));
66
+ return Math.abs(a) > o ? !1 : e.x >= Math.min(t.x, n.x) && e.x <= Math.max(t.x, n.x) && e.y >= Math.min(t.y, n.y) && e.y <= Math.max(t.y, n.y);
67
+ }
68
+ function f(e, t, n) {
69
+ let r = (t.y - e.y) * (n.x - t.x), i = (t.x - e.x) * (n.y - t.y), a = r - i, o = 2 ** -52 * 16 * Math.max(1, Math.abs(r) + Math.abs(i));
70
+ return Math.abs(a) <= o ? 0 : a > 0 ? 1 : -1;
71
+ }
72
+ function p(e, t, n, r) {
73
+ let i = f(e, t, n), a = f(e, t, r), o = f(n, r, e), s = f(n, r, t);
74
+ return i !== 0 && a !== 0 && o !== 0 && s !== 0 ? i !== a && o !== s : i === 0 && d(n, e, t) || a === 0 && d(r, e, t) || o === 0 && d(e, n, r) || s === 0 && d(t, n, r);
75
+ }
76
+ function m(e, t) {
77
+ if (t.length < 3) return !1;
78
+ let n = !1;
79
+ for (let r = 0, i = t.length - 1; r < t.length; i = r, r += 1) {
80
+ let a = t[r], o = t[i];
81
+ if (!(a === void 0 || o === void 0)) {
82
+ if (d(e, o, a)) return !0;
83
+ a.y > e.y != o.y > e.y && e.x < (o.x - a.x) * (e.y - a.y) / (o.y - a.y) + a.x && (n = !n);
84
+ }
85
+ }
86
+ return n;
87
+ }
88
+ function h(e) {
89
+ if (e.some((e) => !Number.isFinite(e.x) || !Number.isFinite(e.y))) return {
90
+ valid: !1,
91
+ reason: "NON_FINITE_POINT"
92
+ };
93
+ if (new Set(e.map((e) => `${e.x}:${e.y}`)).size < 3) return {
94
+ valid: !1,
95
+ reason: "TOO_FEW_POINTS"
96
+ };
97
+ for (let t = 0; t < e.length; t += 1) {
98
+ let n = (t + 1) % e.length;
99
+ for (let r = t + 1; r < e.length; r += 1) {
100
+ let i = (r + 1) % e.length;
101
+ if (t === r || n === r || i === t) continue;
102
+ let a = e[t], o = e[n], s = e[r], c = e[i];
103
+ if (a !== void 0 && o !== void 0 && s !== void 0 && c !== void 0 && p(a, o, s, c)) return {
104
+ valid: !1,
105
+ reason: "SELF_INTERSECTION"
106
+ };
107
+ }
108
+ }
109
+ let t = e.reduce((t, n, r) => {
110
+ let i = e[(r + 1) % e.length];
111
+ return i === void 0 ? t : t + n.x * i.y - i.x * n.y;
112
+ }, 0);
113
+ return Math.abs(t) <= 2 ** -52 ? {
114
+ valid: !1,
115
+ reason: "ZERO_AREA"
116
+ } : { valid: !0 };
117
+ }
118
+ //#endregion
119
+ //#region src/core/events.ts
120
+ function ee(e, t) {
121
+ let n = Q(e), r = {
122
+ type: "error",
123
+ code: "SUBSCRIBER_ERROR",
124
+ error: t
125
+ };
126
+ for (let e of [...n.listeners.error]) try {
127
+ e(r);
128
+ } catch {}
129
+ }
130
+ function g(e, t) {
131
+ let n = Q(e), r = {
132
+ type: "change",
133
+ kind: t,
134
+ revision: n.revision
135
+ };
136
+ for (let t of [...n.listeners.change]) try {
137
+ t(r);
138
+ } catch (t) {
139
+ ee(e, t);
140
+ }
141
+ }
142
+ function _(e, t, n) {
143
+ let r = Q(e).listeners[t];
144
+ r.add(n);
145
+ let i = !0;
146
+ return () => {
147
+ i && (i = !1, r.delete(n));
148
+ };
149
+ }
150
+ //#endregion
151
+ //#region src/core/immutability.ts
152
+ function te(e, t = /* @__PURE__ */ new WeakSet()) {
153
+ if (typeof e != "object" || !e || t.has(e)) return e;
154
+ t.add(e);
155
+ for (let n of Object.values(e)) te(n, t);
156
+ return Object.freeze(e);
157
+ }
158
+ function ne(e) {
159
+ return e.type === "rect" ? Object.freeze({ ...e }) : e.type === "polygon" ? Object.freeze({
160
+ type: "polygon",
161
+ points: Object.freeze(e.points.map(([e, t]) => Object.freeze([e, t])))
162
+ }) : Object.freeze({
163
+ type: "mask",
164
+ width: e.width,
165
+ height: e.height,
166
+ rle: Object.freeze([...e.rle])
167
+ });
168
+ }
169
+ function v(e) {
170
+ let t = structuredClone(e.metadata);
171
+ return Object.freeze({
172
+ ...e,
173
+ geometry: ne(e.geometry),
174
+ metadata: te(t)
175
+ });
176
+ }
177
+ //#endregion
178
+ //#region src/core/types.ts
179
+ var re = Symbol("Annotator"), y = class extends Error {
180
+ code;
181
+ constructor(e, t) {
182
+ super(t), this.name = "AnnotatorError", this.code = e;
183
+ }
184
+ };
185
+ function ie() {
186
+ return Object.freeze({ [re]: !0 });
187
+ }
188
+ //#endregion
189
+ //#region src/core/commands.ts
190
+ function ae(e, t, n) {
191
+ let r = Q(e);
192
+ r.revision += 1, r.undoStack.push(n), r.undoStack.length > r.historyLimit && r.undoStack.shift(), r.redoStack = [], g(e, t);
193
+ }
194
+ function b(e, t, n, r) {
195
+ n(Q(e)), ae(e, t, {
196
+ undo: r,
197
+ redo: n
198
+ });
199
+ }
200
+ function x(e, t) {
201
+ if (!e.labels.some((e) => e.id === t)) throw new y("UNKNOWN_LABEL", `Unknown label: ${t}`);
202
+ }
203
+ function oe(e) {
204
+ let t = Date.now();
205
+ return {
206
+ id: globalThis.crypto.randomUUID(),
207
+ labelId: e,
208
+ source: "manual",
209
+ status: "accepted",
210
+ revision: 1,
211
+ createdAt: t,
212
+ updatedAt: t,
213
+ metadata: Object.freeze({})
214
+ };
215
+ }
216
+ function se(e) {
217
+ if (![
218
+ e.x,
219
+ e.y,
220
+ e.width,
221
+ e.height
222
+ ].every(Number.isFinite) || e.width <= 0 || e.height <= 0) throw new y("INVALID_GEOMETRY", "Rectangle coordinates must be finite and dimensions must be positive.");
223
+ }
224
+ function ce(e) {
225
+ if (e.type === "rect") {
226
+ se(e);
227
+ return;
228
+ }
229
+ if (e.type === "polygon") {
230
+ let t = h(e.points.map(([e, t]) => ({
231
+ x: e,
232
+ y: t
233
+ })));
234
+ if (!t.valid) throw new y("INVALID_GEOMETRY", `Invalid polygon: ${t.reason}`);
235
+ return;
236
+ }
237
+ if (e.type === "mask") {
238
+ if (e.width <= 0 || e.height <= 0) throw new y("INVALID_GEOMETRY", "Mask dimensions must be positive.");
239
+ return;
240
+ }
241
+ }
242
+ function le(e) {
243
+ if (e.type === "rect") return e;
244
+ if (e.type === "mask") return {
245
+ x: 0,
246
+ y: 0,
247
+ width: e.width,
248
+ height: e.height
249
+ };
250
+ let t = Infinity, n = Infinity, r = -Infinity, i = -Infinity;
251
+ for (let [a, o] of e.points) t = Math.min(t, a), n = Math.min(n, o), r = Math.max(r, a), i = Math.max(i, o);
252
+ return {
253
+ x: t,
254
+ y: n,
255
+ width: r - t,
256
+ height: i - n
257
+ };
258
+ }
259
+ function ue(e, t, n = e.annotations.length, r) {
260
+ e.annotations.splice(n, 0, t), e.annotationsById.set(t.id, t);
261
+ let i = le(t.geometry);
262
+ r === void 0 ? o(e.spatialIndex, t.id, i) : l(e.spatialIndex, t.id, i, r);
263
+ }
264
+ function S(e, t) {
265
+ e.selectedIds.includes(t) && (e.toolController?.cancel(), e.selectedIds = e.selectedIds.filter((e) => e !== t), e.interactionDraft = null);
266
+ let n = e.annotations.findIndex((e) => e.id === t);
267
+ n >= 0 && e.annotations.splice(n, 1), e.annotationsById.delete(t), c(e.spatialIndex, t);
268
+ }
269
+ function C(e, t) {
270
+ let n = Q(e);
271
+ x(n, t.labelId);
272
+ let r = {
273
+ type: "rect",
274
+ x: t.x,
275
+ y: t.y,
276
+ width: t.width,
277
+ height: t.height
278
+ };
279
+ se(r);
280
+ let i = v({
281
+ ...oe(t.labelId),
282
+ geometry: r
283
+ }), a = n.annotations.length;
284
+ return b(e, "annotation:add", (e) => ue(e, i, a), (e) => S(e, i.id)), i.id;
285
+ }
286
+ function de(e, t) {
287
+ let n = Q(e);
288
+ x(n, t.labelId);
289
+ let r = {
290
+ type: "polygon",
291
+ points: t.points.map((e) => [e.x, e.y])
292
+ };
293
+ ce(r);
294
+ let i = v({
295
+ ...oe(t.labelId),
296
+ geometry: r
297
+ }), a = n.annotations.length;
298
+ return b(e, "annotation:add", (e) => ue(e, i, a), (e) => S(e, i.id)), i.id;
299
+ }
300
+ function w(e, t) {
301
+ let n = Q(e);
302
+ x(n, t.labelId);
303
+ let r = {
304
+ type: "mask",
305
+ width: t.width,
306
+ height: t.height,
307
+ rle: t.rle
308
+ };
309
+ ce(r);
310
+ let i = v({
311
+ ...oe(t.labelId),
312
+ geometry: r
313
+ }), a = n.annotations.length;
314
+ return b(e, "annotation:add", (e) => ue(e, i, a), (e) => S(e, i.id)), i.id;
315
+ }
316
+ function T(e, t, n) {
317
+ ce(n);
318
+ let r = Q(e), i = r.annotationsById.get(t);
319
+ if (i === void 0) throw new y("ANNOTATION_NOT_FOUND", `Annotation not found: ${t}`);
320
+ if (i.geometry.type !== n.type) throw new y("INVALID_GEOMETRY", "Annotation geometry type cannot be changed.");
321
+ let a = v({
322
+ ...i,
323
+ geometry: n,
324
+ revision: i.revision + 1,
325
+ updatedAt: Date.now()
326
+ }), o = r.annotations.findIndex((e) => e.id === t), c = (e, n) => {
327
+ e.annotations[o] = n, e.annotationsById.set(t, n), s(e.spatialIndex, t, le(n.geometry));
328
+ };
329
+ b(e, "annotation:update", (e) => c(e, a), (e) => c(e, i));
330
+ }
331
+ function fe(e, t, n) {
332
+ let r = Q(e);
333
+ x(r, n);
334
+ let i = r.annotationsById.get(t);
335
+ if (i === void 0) throw new y("ANNOTATION_NOT_FOUND", `Annotation not found: ${t}`);
336
+ if (i.labelId === n) return;
337
+ let a = v({
338
+ ...i,
339
+ labelId: n,
340
+ revision: i.revision + 1,
341
+ updatedAt: Date.now()
342
+ }), o = r.annotations.findIndex((e) => e.id === t), s = (e, n) => {
343
+ e.annotations[o] = n, e.annotationsById.set(t, n);
344
+ };
345
+ b(e, "annotation:update", (e) => s(e, a), (e) => s(e, i));
346
+ }
347
+ function E(e, t) {
348
+ let n = Q(e), r = n.annotationsById.get(t);
349
+ if (r === void 0) return !1;
350
+ let i = n.annotations.findIndex((e) => e.id === t), a = n.spatialIndex.order.get(t);
351
+ if (a === void 0) throw Error(`Spatial index is missing annotation: ${t}`);
352
+ return b(e, "annotation:remove", (e) => S(e, t), (e) => ue(e, r, i, a)), !0;
353
+ }
354
+ function pe(e) {
355
+ return Q(e).undoStack.length > 0;
356
+ }
357
+ function me(e) {
358
+ return Q(e).redoStack.length > 0;
359
+ }
360
+ function he(e) {
361
+ let t = Q(e), n = t.undoStack.pop();
362
+ return n === void 0 ? !1 : (n.undo(t), t.redoStack.push(n), t.revision += 1, g(e, "history:undo"), !0);
363
+ }
364
+ function ge(e) {
365
+ let t = Q(e), n = t.redoStack.pop();
366
+ return n === void 0 ? !1 : (n.redo(t), t.undoStack.push(n), t.revision += 1, g(e, "history:redo"), !0);
367
+ }
368
+ function D(e, t) {
369
+ let n = Q(e);
370
+ return u(n.spatialIndex, t).map((e) => n.annotationsById.get(e)).filter((e) => e !== void 0);
371
+ }
372
+ //#endregion
373
+ //#region src/labels/labels.ts
374
+ function _e(e, t) {
375
+ let n = Q(e);
376
+ if (n.labels.some((e) => e.id === t.id)) throw new y("DUPLICATE_LABEL", `Label already exists: ${t.id}`);
377
+ let r = Object.freeze({ ...t }), i = n.labels.length, a = n.activeLabelId;
378
+ b(e, "label:add", (e) => {
379
+ e.labels.splice(i, 0, r), a === null && (e.activeLabelId = r.id);
380
+ }, (e) => {
381
+ e.labels.splice(i, 1), e.activeLabelId = a;
382
+ });
383
+ }
384
+ function ve(e, t) {
385
+ let n = Q(e);
386
+ if (!n.labels.some((e) => e.id === t)) throw new y("UNKNOWN_LABEL", `Unknown label: ${t}`);
387
+ let r = n.activeLabelId;
388
+ b(e, "label:activate", (e) => {
389
+ e.activeLabelId = t;
390
+ }, (e) => {
391
+ e.activeLabelId = r;
392
+ });
393
+ }
394
+ function O(e) {
395
+ return Q(e).activeLabelId;
396
+ }
397
+ function ye(e, t, n) {
398
+ let r = Q(e), i = r.labels.findIndex((e) => e.id === t);
399
+ if (i === -1) throw new y("UNKNOWN_LABEL", `Unknown label: ${t}`);
400
+ let a = r.labels[i], o = Object.freeze({
401
+ ...a,
402
+ ...n
403
+ });
404
+ b(e, "label:update", (e) => {
405
+ e.labels[i] = o;
406
+ }, (e) => {
407
+ e.labels[i] = a;
408
+ });
409
+ }
410
+ //#endregion
411
+ //#region src/geometry/matrix.ts
412
+ function be(e, t, n) {
413
+ if (![
414
+ e,
415
+ t,
416
+ n
417
+ ].every(Number.isFinite) || e <= 0) throw new y("INVALID_GEOMETRY", "Matrix scale must be positive and all values must be finite.");
418
+ return {
419
+ a: e,
420
+ b: 0,
421
+ c: 0,
422
+ d: e,
423
+ e: t,
424
+ f: n
425
+ };
426
+ }
427
+ function xe(e) {
428
+ let t = e.a * e.d - e.b * e.c;
429
+ if (!Number.isFinite(t) || t === 0) throw new y("INVALID_GEOMETRY", "Matrix is not invertible.");
430
+ return {
431
+ a: e.d / t,
432
+ b: -e.b / t,
433
+ c: -e.c / t,
434
+ d: e.a / t,
435
+ e: (e.c * e.f - e.d * e.e) / t,
436
+ f: (e.b * e.e - e.a * e.f) / t
437
+ };
438
+ }
439
+ function Se(e, t) {
440
+ return {
441
+ x: e.a * t.x + e.c * t.y + e.e,
442
+ y: e.b * t.x + e.d * t.y + e.f
443
+ };
444
+ }
445
+ //#endregion
446
+ //#region src/viewport/viewport.ts
447
+ function Ce(e, t) {
448
+ if (!Number.isFinite(e.width) || !Number.isFinite(e.height) || e.width <= 0 || e.height <= 0) throw new y("INVALID_GEOMETRY", `${t} dimensions must be finite and positive.`);
449
+ }
450
+ function we(e, t) {
451
+ if (!Number.isFinite(t) || t <= 0) throw new y("INVALID_GEOMETRY", "Viewport scale must be finite and positive.");
452
+ return Math.min(e.maxScale, Math.max(e.minScale, t));
453
+ }
454
+ function Te(e) {
455
+ Ce(e, "Viewport");
456
+ let t = e.minScale ?? .01, n = e.maxScale ?? 64;
457
+ if (t <= 0 || n < t) throw new y("INVALID_GEOMETRY", "Viewport scale limits must be positive and ordered.");
458
+ return {
459
+ width: e.width,
460
+ height: e.height,
461
+ scale: 1,
462
+ offsetX: 0,
463
+ offsetY: 0,
464
+ minScale: t,
465
+ maxScale: n
466
+ };
467
+ }
468
+ function Ee(e, t) {
469
+ return Se(be(e.scale, e.offsetX, e.offsetY), t);
470
+ }
471
+ function k(e, t) {
472
+ return Se(xe(be(e.scale, e.offsetX, e.offsetY)), t);
473
+ }
474
+ function De(e, t, n) {
475
+ let r = k(e, t), i = we(e, n);
476
+ return {
477
+ ...e,
478
+ scale: i,
479
+ offsetX: t.x - r.x * i,
480
+ offsetY: t.y - r.y * i
481
+ };
482
+ }
483
+ function Oe(e, t) {
484
+ return {
485
+ ...e,
486
+ offsetX: e.offsetX + t.x,
487
+ offsetY: e.offsetY + t.y
488
+ };
489
+ }
490
+ function ke(e, t) {
491
+ Ce(t, "Image");
492
+ let n = we(e, Math.min(e.width / t.width, e.height / t.height));
493
+ return {
494
+ ...e,
495
+ scale: n,
496
+ offsetX: (e.width - t.width * n) / 2,
497
+ offsetY: (e.height - t.height * n) / 2
498
+ };
499
+ }
500
+ //#endregion
501
+ //#region src/mask/rle.ts
502
+ function A(e) {
503
+ let t = [], n = 0, r = 0;
504
+ for (let i = 0; i < e.length; i += 1) {
505
+ let a = +((e[i] ?? 0) > 0);
506
+ a === n ? r += 1 : (t.push(r), n = a, r = 1);
507
+ }
508
+ return t.push(r), Object.freeze(t);
509
+ }
510
+ function j(e, t, n) {
511
+ let r = t * n, i = new Uint8Array(r), a = 0, o = 0;
512
+ for (let t of e) {
513
+ let e = Math.max(0, Math.floor(t));
514
+ if (o === 1 && i.fill(1, a, Math.min(r, a + e)), a += e, o = +(o === 0), a >= r) break;
515
+ }
516
+ return i;
517
+ }
518
+ function Ae(e, t) {
519
+ let n = Math.min(e.length, t.length);
520
+ for (let r = 0; r < n; r += 1) if ((e[r] ?? 0) > 0 && (t[r] ?? 0) > 0) return !0;
521
+ return !1;
522
+ }
523
+ function je(e, t) {
524
+ let n = Math.max(e.length, t.length), r = new Uint8Array(n);
525
+ for (let i = 0; i < n; i += 1) r[i] = +((e[i] ?? 0) > 0 || (t[i] ?? 0) > 0);
526
+ return r;
527
+ }
528
+ function Me(e, t) {
529
+ let n = e.length, r = new Uint8Array(n);
530
+ for (let i = 0; i < n; i += 1) r[i] = +((e[i] ?? 0) > 0 && (t[i] ?? 0) === 0);
531
+ return r;
532
+ }
533
+ function Ne(e) {
534
+ for (let t = 0; t < e.length; t += 1) if ((e[t] ?? 0) > 0) return !0;
535
+ return !1;
536
+ }
537
+ function Pe(e, t, n) {
538
+ let r = Math.max(0, Math.floor(t)), i = Math.max(0, Math.floor(n)), a = r * i, o = new Uint8Array(a), s = new Int32Array(a), c = [];
539
+ for (let t = 0; t < a; t += 1) {
540
+ if (o[t] === 1 || (e[t] ?? 0) === 0) continue;
541
+ let n = new Uint8Array(a), l = 0, u = 0;
542
+ for (s[u] = t, u += 1, o[t] = 1; l < u;) {
543
+ let t = s[l];
544
+ if (l += 1, t === void 0) continue;
545
+ n[t] = 1;
546
+ let a = t % r, c = Math.floor(t / r);
547
+ for (let t = -1; t <= 1; t += 1) for (let n = -1; n <= 1; n += 1) {
548
+ if (n === 0 && t === 0) continue;
549
+ let l = a + n, d = c + t;
550
+ if (l < 0 || d < 0 || l >= r || d >= i) continue;
551
+ let f = d * r + l;
552
+ o[f] === 1 || (e[f] ?? 0) === 0 || (o[f] = 1, s[u] = f, u += 1);
553
+ }
554
+ }
555
+ c.push(n);
556
+ }
557
+ return c;
558
+ }
559
+ function Fe(e, t, n) {
560
+ let r = Infinity, i = Infinity, a = -Infinity, o = -Infinity;
561
+ for (let s = 0; s < n; s += 1) for (let n = 0; n < t; n += 1) (e[s * t + n] ?? 0) !== 0 && (r = Math.min(r, n), i = Math.min(i, s), a = Math.max(a, n), o = Math.max(o, s));
562
+ return Number.isFinite(r) ? {
563
+ x: r,
564
+ y: i,
565
+ width: a - r + 1,
566
+ height: o - i + 1
567
+ } : null;
568
+ }
569
+ function Ie(e, t, n, r, i) {
570
+ let a = Math.round(r), o = Math.round(i), s = new Uint8Array(t * n);
571
+ for (let r = 0; r < n; r += 1) for (let i = 0; i < t; i += 1) {
572
+ if ((e[r * t + i] ?? 0) === 0) continue;
573
+ let c = i + a, l = r + o;
574
+ c >= 0 && l >= 0 && c < t && l < n && (s[l * t + c] = 1);
575
+ }
576
+ return s;
577
+ }
578
+ function Le(e, t, n, r, i) {
579
+ let a = Math.max(0, Math.ceil(i)), o = i * i;
580
+ for (let i = 0; i < r; i += 1) for (let s = 0; s < n; s += 1) if ((e[i * n + s] ?? 0) !== 0) for (let e = -a; e <= a; e += 1) for (let c = -a; c <= a; c += 1) {
581
+ if (c * c + e * e > o) continue;
582
+ let a = s + c, l = i + e;
583
+ if (a >= 0 && l >= 0 && a < n && l < r && (t[l * n + a] ?? 0) > 0) return !0;
584
+ }
585
+ return !1;
586
+ }
587
+ //#endregion
588
+ //#region src/render/canvas-layers.ts
589
+ var Re = [
590
+ "image",
591
+ "annotations",
592
+ "interaction",
593
+ "event"
594
+ ];
595
+ function ze(e) {
596
+ let t = e.style.position, n = e.style.overflow;
597
+ getComputedStyle(e).position === "static" && (e.style.position = "relative"), e.style.overflow = "hidden";
598
+ let r = Re.map((t, n) => {
599
+ let r = document.createElement("canvas");
600
+ return r.dataset.layer = t, r.style.position = "absolute", r.style.inset = "0", r.style.width = "100%", r.style.height = "100%", r.style.zIndex = String(n), r.style.pointerEvents = t === "event" ? "auto" : "none", e.append(r), [t, r];
601
+ }), i = Object.fromEntries(r);
602
+ return {
603
+ canvases: i,
604
+ resize() {
605
+ let t = e.getBoundingClientRect(), n = Math.max(1, Math.round(t.width)), r = Math.max(1, Math.round(t.height)), a = Math.max(1, window.devicePixelRatio || 1);
606
+ for (let e of Object.values(i)) e.width = Math.round(n * a), e.height = Math.round(r * a);
607
+ return {
608
+ width: n,
609
+ height: r,
610
+ dpr: a
611
+ };
612
+ },
613
+ destroy() {
614
+ for (let e of Object.values(i)) e.remove();
615
+ e.style.position = t, e.style.overflow = n;
616
+ }
617
+ };
618
+ }
619
+ //#endregion
620
+ //#region src/render/scheduler.ts
621
+ function Be(e) {
622
+ let t = /* @__PURE__ */ new Set(), n = null, r = !1, i = () => {
623
+ if (n = null, r || t.size === 0) return;
624
+ let i = new Set(t);
625
+ t.clear(), e.render(i);
626
+ };
627
+ return {
628
+ invalidate(a) {
629
+ r || (t.add(a), n === null && (n = e.requestFrame(i)));
630
+ },
631
+ destroy() {
632
+ r || (r = !0, t.clear(), n !== null && (e.cancelFrame(n), n = null));
633
+ }
634
+ };
635
+ }
636
+ //#endregion
637
+ //#region src/render/canvas-renderer.ts
638
+ function Ve(e, t) {
639
+ e.setTransform(1, 0, 0, 1, 0, 0), e.clearRect(0, 0, t.width, t.height);
640
+ }
641
+ function He(e, t, n, r) {
642
+ let i = document.createElement("canvas");
643
+ i.width = t.geometry.width, i.height = t.geometry.height;
644
+ let a = i.getContext("2d");
645
+ if (a === null) return;
646
+ let o = a.createImageData(t.geometry.width, t.geometry.height), s = j(t.geometry.rle, t.geometry.width, t.geometry.height), c = /^#?([0-9a-f]{6})$/i.exec(n)?.[1] ?? "2c9c21", l = Number.parseInt(c.slice(0, 2), 16), u = Number.parseInt(c.slice(2, 4), 16), d = Number.parseInt(c.slice(4, 6), 16);
647
+ for (let e = 0; e < s.length; e += 1) {
648
+ if (s[e] !== 1) continue;
649
+ let t = e * 4;
650
+ o.data[t] = l, o.data[t + 1] = u, o.data[t + 2] = d, o.data[t + 3] = 96;
651
+ }
652
+ a.putImageData(o, 0, 0);
653
+ let f = r?.points[0];
654
+ if (r !== void 0 && f !== void 0) {
655
+ a.globalCompositeOperation = "destination-out", a.beginPath(), a.moveTo(f.x, f.y);
656
+ for (let e of r.points.slice(1)) a.lineTo(e.x, e.y);
657
+ r.points.length === 1 && a.lineTo(f.x, f.y), a.lineWidth = r.size, a.lineCap = "round", a.lineJoin = "round", a.stroke(), a.globalCompositeOperation = "source-over";
658
+ }
659
+ e.drawImage(i, 0, 0);
660
+ }
661
+ function Ue(e) {
662
+ let t = Q(e), n = ze(t.container), r = n.resize(), i = () => {
663
+ let e = n.canvases.image, i = e.getContext("2d");
664
+ if (i === null || (Ve(i, e), t.image === null || t.viewport === null)) return;
665
+ let { scale: a, offsetX: o, offsetY: s } = t.viewport;
666
+ i.imageSmoothingEnabled = a < 1, i.imageSmoothingQuality = "high", i.setTransform(r.dpr * a, 0, 0, r.dpr * a, r.dpr * o, r.dpr * s), i.drawImage(t.image.source, 0, 0);
667
+ }, a = () => {
668
+ let i = n.canvases.annotations, a = i.getContext("2d");
669
+ if (a === null || (Ve(a, i), t.viewport === null)) return;
670
+ let o = k(t.viewport, {
671
+ x: 0,
672
+ y: 0
673
+ }), s = k(t.viewport, {
674
+ x: r.width,
675
+ y: r.height
676
+ }), c = D(e, {
677
+ x: Math.min(o.x, s.x),
678
+ y: Math.min(o.y, s.y),
679
+ width: Math.abs(s.x - o.x),
680
+ height: Math.abs(s.y - o.y)
681
+ }), { scale: l, offsetX: u, offsetY: d } = t.viewport;
682
+ a.imageSmoothingEnabled = !1, a.setTransform(r.dpr * l, 0, 0, r.dpr * l, r.dpr * u, r.dpr * d), a.lineWidth = 2 / l;
683
+ for (let e of c) {
684
+ if (t.interactionDraft?.type === "vector" && t.interactionDraft.annotationId === e.id && t.interactionDraft.geometry.type === "mask") continue;
685
+ let n = t.labels.find((t) => t.id === e.labelId);
686
+ a.strokeStyle = n?.color ?? "#2c9c21", a.fillStyle = n?.color ?? "#2c9c21", a.globalAlpha = 1;
687
+ let r = 0, i = 0;
688
+ if (e.geometry.type === "rect") {
689
+ let t = e;
690
+ r = t.geometry.x, i = t.geometry.y, a.globalAlpha = .16, a.fillRect(t.geometry.x, t.geometry.y, t.geometry.width, t.geometry.height), a.globalAlpha = 1, a.strokeRect(t.geometry.x, t.geometry.y, t.geometry.width, t.geometry.height);
691
+ } else if (e.geometry.type === "polygon") {
692
+ let t = e, n = t.geometry.points[0];
693
+ if (n === void 0) continue;
694
+ r = n[0], i = n[1], a.beginPath(), a.moveTo(n[0], n[1]);
695
+ for (let [e, n] of t.geometry.points.slice(1)) a.lineTo(e, n);
696
+ a.closePath(), a.globalAlpha = .16, a.fill(), a.globalAlpha = 1, a.stroke();
697
+ } else if (e.geometry.type === "mask") {
698
+ let o = e, s = Fe(j(o.geometry.rle, o.geometry.width, o.geometry.height), o.geometry.width, o.geometry.height);
699
+ r = s?.x ?? 0, i = s?.y ?? 0;
700
+ let c = t.interactionDraft?.type === "eraser" ? t.interactionDraft : void 0;
701
+ He(a, o, n?.color ?? "#2c9c21", c);
702
+ }
703
+ if (n?.name) {
704
+ a.font = `${14 / l}px sans-serif`, a.fillStyle = "#ffffff", a.strokeStyle = n.color, a.lineWidth = 3 / l, a.lineJoin = "round", a.globalAlpha = 1;
705
+ let e = a.measureText(n.name).width, t = 14 / l, o = 4 / l;
706
+ a.strokeRect(r - o, i - t - o, e + o * 2, t + o * 2), a.fillRect(r - o, i - t - o, e + o * 2, t + o * 2), a.fillStyle = n.color, a.fillText(n.name, r, i - o);
707
+ }
708
+ }
709
+ }, o = Be({
710
+ requestFrame: (e) => requestAnimationFrame(e),
711
+ cancelFrame: (e) => cancelAnimationFrame(e),
712
+ render(e) {
713
+ if (e.has("image") && i(), e.has("annotations") && a(), e.has("interaction")) {
714
+ let e = n.canvases.interaction, i = e.getContext("2d");
715
+ if (i !== null) {
716
+ Ve(i, e);
717
+ let n = t.interactionDraft;
718
+ if (n !== null && t.viewport !== null) {
719
+ let e = "labelId" in n ? t.labels.find((e) => e.id === n.labelId) : void 0, { scale: a, offsetX: o, offsetY: s } = t.viewport;
720
+ if (i.imageSmoothingEnabled = !1, i.setTransform(r.dpr * a, 0, 0, r.dpr * a, r.dpr * o, r.dpr * s), i.strokeStyle = e?.color ?? "#2c9c21", i.lineWidth = 2 / a, i.setLineDash([6 / a, 4 / a]), n.type === "vector" && n.geometry.type === "mask") {
721
+ let r = t.annotationsById.get(n.annotationId);
722
+ r?.geometry.type === "mask" && He(i, {
723
+ ...r,
724
+ geometry: n.geometry
725
+ }, e?.color ?? "#2c9c21");
726
+ } else if (n.type === "brush") {
727
+ i.strokeStyle = n.color;
728
+ let e = n.points[0];
729
+ if (e !== void 0) {
730
+ i.beginPath(), i.moveTo(e.x, e.y);
731
+ for (let e of n.points.slice(1)) i.lineTo(e.x, e.y);
732
+ n.points.length === 1 && i.lineTo(e.x, e.y), i.globalAlpha = .7, i.lineWidth = n.size, i.lineCap = "round", i.lineJoin = "round", i.setLineDash([]), i.stroke();
733
+ }
734
+ } else if (n.type === "rect") i.strokeRect(n.geometry.x, n.geometry.y, n.geometry.width, n.geometry.height);
735
+ else if (n.type !== "eraser") {
736
+ let e = n.type === "polygon" ? n.points.map((e) => [e.x, e.y]) : n.geometry.type === "polygon" ? n.geometry.points : [];
737
+ if (n.type === "vector" && n.geometry.type === "rect") i.strokeRect(n.geometry.x, n.geometry.y, n.geometry.width, n.geometry.height);
738
+ else if (e[0] !== void 0) {
739
+ i.beginPath(), i.moveTo(e[0][0], e[0][1]);
740
+ for (let t of e.slice(1)) i.lineTo(t[0], t[1]);
741
+ n.type === "vector" && i.closePath(), i.stroke();
742
+ }
743
+ }
744
+ }
745
+ if (t.viewport !== null) {
746
+ let { scale: e, offsetX: n, offsetY: a } = t.viewport;
747
+ i.setTransform(r.dpr * e, 0, 0, r.dpr * e, r.dpr * n, r.dpr * a);
748
+ let o = 8 / e;
749
+ i.setLineDash([]);
750
+ for (let n of t.selectedIds) {
751
+ let r = t.annotationsById.get(n);
752
+ if (r === void 0) continue;
753
+ let a = t.interactionDraft?.type === "vector" && t.interactionDraft.annotationId === n ? t.interactionDraft.geometry : r.geometry, s = a.type === "rect" ? [
754
+ [a.x, a.y],
755
+ [a.x + a.width / 2, a.y],
756
+ [a.x + a.width, a.y],
757
+ [a.x + a.width, a.y + a.height / 2],
758
+ [a.x + a.width, a.y + a.height],
759
+ [a.x + a.width / 2, a.y + a.height],
760
+ [a.x, a.y + a.height],
761
+ [a.x, a.y + a.height / 2]
762
+ ] : a.type === "polygon" ? a.points : [];
763
+ if (i.fillStyle = "#ffffff", i.strokeStyle = "#1677ff", i.globalAlpha = 1, a.type === "mask") {
764
+ let t = Fe(j(a.rle, a.width, a.height), a.width, a.height);
765
+ t !== null && (i.setLineDash([5 / e, 3 / e]), i.lineWidth = 2 / e, i.strokeRect(t.x, t.y, t.width, t.height), i.setLineDash([]));
766
+ }
767
+ for (let [e, t] of s) i.fillRect(e - o / 2, t - o / 2, o, o), i.strokeRect(e - o / 2, t - o / 2, o, o);
768
+ }
769
+ }
770
+ }
771
+ }
772
+ }
773
+ }), s = _(e, "change", (e) => {
774
+ e.kind !== "label:activate" && (o.invalidate("annotations"), o.invalidate("interaction"));
775
+ }), c = !1;
776
+ return {
777
+ eventCanvas: n.canvases.event,
778
+ invalidate: (e) => o.invalidate(e),
779
+ resize() {
780
+ c || (r = n.resize(), o.invalidate("image"), o.invalidate("annotations"), o.invalidate("interaction"));
781
+ },
782
+ destroy() {
783
+ c || (c = !0, s(), o.destroy(), n.destroy());
784
+ }
785
+ };
786
+ }
787
+ //#endregion
788
+ //#region src/image/image-commands.ts
789
+ function We(e) {
790
+ let t = e.getBoundingClientRect();
791
+ return {
792
+ width: Math.max(1, Math.round(t.width)),
793
+ height: Math.max(1, Math.round(t.height))
794
+ };
795
+ }
796
+ function M(e) {
797
+ let t = Q(e);
798
+ if (t.viewport === null) throw Error("An image must be loaded before using viewport commands.");
799
+ return {
800
+ state: t,
801
+ viewport: t.viewport
802
+ };
803
+ }
804
+ async function Ge(e, t) {
805
+ let n = Q(e), r = n.image !== null;
806
+ n.imageAbortController?.abort(), n.imageAbortController = null, n.image = null, n.viewport = null, n.toolController?.cancel(), n.interactionDraft = null, n.renderer?.invalidate("image"), n.renderer?.invalidate("annotations"), n.renderer?.invalidate("interaction"), n.renderer?.invalidate("interaction"), n.imageSource?.dispose(), n.imageSource = null, r && g(e, "image:clear");
807
+ let i = new AbortController();
808
+ n.imageAbortController = i, n.imageSource = t;
809
+ let a = await t.load(i.signal), o = Q(e);
810
+ o.imageSource !== t || i.signal.aborted || (o.image = a, o.viewport = ke(Te(We(o.container)), {
811
+ width: a.width,
812
+ height: a.height
813
+ }), o.renderer ??= Ue(e), o.renderer.resize(), o.renderer.invalidate("image"), o.renderer.invalidate("annotations"), g(e, "image:load"));
814
+ }
815
+ function N(e) {
816
+ return Q(e).image !== null;
817
+ }
818
+ function Ke(e) {
819
+ return M(e).viewport.scale;
820
+ }
821
+ function qe(e) {
822
+ let { state: t, viewport: n } = M(e), r = We(t.container);
823
+ t.viewport = {
824
+ ...n,
825
+ ...r
826
+ }, t.renderer?.resize();
827
+ }
828
+ function P(e) {
829
+ let { state: t, viewport: n } = M(e);
830
+ t.image !== null && (t.viewport = ke(n, t.image), t.renderer?.invalidate("image"), t.renderer?.invalidate("annotations"), t.renderer?.invalidate("interaction"));
831
+ }
832
+ function F(e, t, n) {
833
+ let { state: r, viewport: i } = M(e);
834
+ r.viewport = De(i, n ?? {
835
+ x: i.width / 2,
836
+ y: i.height / 2
837
+ }, t), r.renderer?.invalidate("image"), r.renderer?.invalidate("annotations"), r.renderer?.invalidate("interaction");
838
+ }
839
+ function I(e, t, n) {
840
+ let { viewport: r } = M(e);
841
+ F(e, r.scale * t, n);
842
+ }
843
+ function L(e, t) {
844
+ let { state: n, viewport: r } = M(e);
845
+ n.viewport = Oe(r, t), n.renderer?.invalidate("image"), n.renderer?.invalidate("annotations"), n.renderer?.invalidate("interaction");
846
+ }
847
+ function Je(e, t) {
848
+ let { state: n, viewport: r } = M(e), i = Ee(r, t), a = n.container.getBoundingClientRect();
849
+ return {
850
+ x: i.x + a.left,
851
+ y: i.y + a.top
852
+ };
853
+ }
854
+ function Ye(e, t) {
855
+ let { state: n, viewport: r } = M(e), i = n.container.getBoundingClientRect();
856
+ return k(r, {
857
+ x: t.x - i.left,
858
+ y: t.y - i.top
859
+ });
860
+ }
861
+ //#endregion
862
+ //#region src/tools/controller.ts
863
+ function Xe(e) {
864
+ return {
865
+ annotator: e,
866
+ setDraft(t) {
867
+ let n = Q(e);
868
+ n.interactionDraft = t, t.type === "eraser" && n.renderer?.invalidate("annotations"), n.renderer?.invalidate("interaction");
869
+ },
870
+ clearDraft() {
871
+ let t = Q(e), n = t.interactionDraft?.type === "eraser";
872
+ t.interactionDraft = null, n && t.renderer?.invalidate("annotations"), t.renderer?.invalidate("interaction");
873
+ }
874
+ };
875
+ }
876
+ function Ze(e, t, n) {
877
+ let r = Q(e), i = Ye(e, {
878
+ x: t.clientX,
879
+ y: t.clientY
880
+ }), a = r.image === null ? i : {
881
+ x: Math.min(r.image.width, Math.max(0, i.x)),
882
+ y: Math.min(r.image.height, Math.max(0, i.y))
883
+ };
884
+ return {
885
+ type: n,
886
+ pointerId: t.pointerId,
887
+ imagePoint: a,
888
+ buttons: t.buttons,
889
+ pressure: t.pressure,
890
+ detail: t.detail
891
+ };
892
+ }
893
+ function Qe(e, t, n) {
894
+ let r = Xe(e), i = null, a = !1, o = "default", s = !1, c = !1, l = null, u = {
895
+ x: 0,
896
+ y: 0
897
+ };
898
+ t.tabIndex = 0;
899
+ let d = (n) => {
900
+ if (n.button === 1 || n.button === 0 && (n.altKey || s)) {
901
+ n.preventDefault(), i?.cancel(r), l = n.pointerId, u = {
902
+ x: n.clientX,
903
+ y: n.clientY
904
+ }, t.setPointerCapture(n.pointerId), t.style.cursor = "grabbing";
905
+ return;
906
+ }
907
+ i === null || n.button !== 0 || (t.focus({ preventScroll: !0 }), t.setPointerCapture(n.pointerId), i.handle(Ze(e, n, "down"), r));
908
+ }, f = (t) => {
909
+ if (l === t.pointerId) {
910
+ let n = {
911
+ x: t.clientX,
912
+ y: t.clientY
913
+ };
914
+ L(e, {
915
+ x: n.x - u.x,
916
+ y: n.y - u.y
917
+ }), u = n;
918
+ return;
919
+ }
920
+ i?.handle(Ze(e, t, "move"), r);
921
+ }, p = (n) => {
922
+ if (l === n.pointerId) {
923
+ l = null, t.style.cursor = s ? "grab" : o, t.hasPointerCapture(n.pointerId) && t.releasePointerCapture(n.pointerId);
924
+ return;
925
+ }
926
+ i?.handle(Ze(e, n, "up"), r), t.hasPointerCapture(n.pointerId) && t.releasePointerCapture(n.pointerId);
927
+ }, m = () => {
928
+ l = null, t.style.cursor = s ? "grab" : o, i?.handle({ type: "cancel" }, r);
929
+ }, h = (e) => {
930
+ if (e.key === "Escape") {
931
+ i?.cancel(r);
932
+ return;
933
+ }
934
+ i?.handleKey?.(e, r);
935
+ }, ee = (e) => {
936
+ e.code !== "Space" || e.repeat || !c && document.activeElement !== t || (e.preventDefault(), s = !0, i?.cancel(r), t.style.cursor = l === null ? "grab" : "grabbing");
937
+ }, g = (e) => {
938
+ e.code !== "Space" || !s || (e.preventDefault(), s = !1, l === null && (t.style.cursor = o));
939
+ }, _ = (n) => {
940
+ n.preventDefault();
941
+ let r = t.getBoundingClientRect(), i = n.deltaMode === WheelEvent.DOM_DELTA_LINE ? 16 : n.deltaMode === WheelEvent.DOM_DELTA_PAGE ? r.height : 1;
942
+ I(e, Math.exp(-n.deltaY * i * .0015), {
943
+ x: n.clientX - r.left,
944
+ y: n.clientY - r.top
945
+ });
946
+ }, te = () => {
947
+ c = !0;
948
+ }, ne = () => {
949
+ c = !1;
950
+ };
951
+ return t.addEventListener("pointerdown", d), t.addEventListener("pointermove", f), t.addEventListener("pointerup", p), t.addEventListener("pointercancel", m), t.addEventListener("pointerenter", te), t.addEventListener("pointerleave", ne), t.addEventListener("wheel", _, { passive: !1 }), t.addEventListener("keydown", h), window.addEventListener("keydown", ee), window.addEventListener("keyup", g), {
952
+ activate(n) {
953
+ a || (i?.cancel(r), i = n, o = n.cursor, t.style.cursor = s ? "grab" : o, Q(e).activeToolId = n.id);
954
+ },
955
+ activateById(e) {
956
+ let t = n.get(e);
957
+ t !== void 0 && this.activate(t);
958
+ },
959
+ cancel() {
960
+ i?.cancel(r);
961
+ },
962
+ destroy() {
963
+ a || (a = !0, i?.cancel(r), i = null, t.removeEventListener("pointerdown", d), t.removeEventListener("pointermove", f), t.removeEventListener("pointerup", p), t.removeEventListener("pointercancel", m), t.removeEventListener("pointerenter", te), t.removeEventListener("pointerleave", ne), t.removeEventListener("wheel", _), t.removeEventListener("keydown", h), window.removeEventListener("keydown", ee), window.removeEventListener("keyup", g));
964
+ }
965
+ };
966
+ }
967
+ function $e(e) {
968
+ let t = Q(e);
969
+ if (t.renderer === null) throw Error("An image must be loaded before activating a drawing tool.");
970
+ return t.toolController ??= Qe(e, t.renderer.eventCanvas, Q(e).toolRegistry), t.toolController;
971
+ }
972
+ function R(e, t) {
973
+ $e(e).activate(t);
974
+ }
975
+ function z(e, t) {
976
+ $e(e).activateById(t);
977
+ }
978
+ function B(e) {
979
+ Q(e).toolController?.cancel();
980
+ }
981
+ function et(e) {
982
+ return Q(e).toolRegistry.list();
983
+ }
984
+ function tt(e, t) {
985
+ return Q(e).toolRegistry.listByCategory(t);
986
+ }
987
+ //#endregion
988
+ //#region src/tools/brush-tool.ts
989
+ function nt(e, t) {
990
+ let n = t ?? O(e);
991
+ if (n === null) throw new y("UNKNOWN_LABEL", "A label must be active before using the brush tool.");
992
+ return n;
993
+ }
994
+ function rt(e, t, n, r, i) {
995
+ let a = Math.max(0, Math.floor(r.x - i)), o = Math.min(t - 1, Math.ceil(r.x + i)), s = Math.max(0, Math.floor(r.y - i)), c = Math.min(n - 1, Math.ceil(r.y + i)), l = i * i;
996
+ for (let n = s; n <= c; n += 1) for (let i = a; i <= o; i += 1) {
997
+ let a = i + .5 - r.x, o = n + .5 - r.y;
998
+ a * a + o * o <= l && (e[n * t + i] = 1);
999
+ }
1000
+ }
1001
+ function it(e, t) {
1002
+ let n = [];
1003
+ for (let r = 0; r < e.length; r += 1) {
1004
+ let i = e[r], a = e[r - 1];
1005
+ if (i === void 0) continue;
1006
+ if (a === void 0) {
1007
+ n.push(i);
1008
+ continue;
1009
+ }
1010
+ let o = i.x - a.x, s = i.y - a.y, c = Math.max(1, Math.ceil(Math.hypot(o, s) / t));
1011
+ for (let e = 1; e <= c; e += 1) n.push({
1012
+ x: a.x + o * e / c,
1013
+ y: a.y + s * e / c
1014
+ });
1015
+ }
1016
+ return n;
1017
+ }
1018
+ function at(e) {
1019
+ let t = Math.max(1, Math.round(e.imageWidth)), n = Math.max(1, Math.round(e.imageHeight)), r = Math.max(1, e.brushSize) / 2, i = new Uint8Array(t * n);
1020
+ for (let a of it(e.points, Math.max(1, r / 2))) rt(i, t, n, a, r);
1021
+ return {
1022
+ type: "mask",
1023
+ width: t,
1024
+ height: n,
1025
+ rle: A(i)
1026
+ };
1027
+ }
1028
+ function ot(e, t, n) {
1029
+ let r = j(n.rle, n.width, n.height), i = D(e, {
1030
+ x: 0,
1031
+ y: 0,
1032
+ width: n.width,
1033
+ height: n.height
1034
+ }).filter((e) => e.labelId !== t || e.geometry.type !== "mask" || e.geometry.width !== n.width || e.geometry.height !== n.height ? !1 : Ae(j(e.geometry.rle, e.geometry.width, e.geometry.height), r));
1035
+ if (i.length === 0) {
1036
+ w(e, {
1037
+ labelId: t,
1038
+ ...n
1039
+ });
1040
+ return;
1041
+ }
1042
+ let a = r;
1043
+ for (let e of i) e.geometry.type === "mask" && (a = je(a, j(e.geometry.rle, e.geometry.width, e.geometry.height)));
1044
+ let [o, ...s] = i;
1045
+ if (!(o === void 0 || o.geometry.type !== "mask")) {
1046
+ T(e, o.id, {
1047
+ type: "mask",
1048
+ width: n.width,
1049
+ height: n.height,
1050
+ rle: A(a)
1051
+ });
1052
+ for (let t of s) E(e, t.id);
1053
+ }
1054
+ }
1055
+ function st(e = {}) {
1056
+ let t = {
1057
+ pointerId: null,
1058
+ points: []
1059
+ }, n = Math.max(1, e.size ?? 10);
1060
+ return {
1061
+ id: "brush",
1062
+ name: "涂抹",
1063
+ description: "在图像上绘制笔触",
1064
+ icon: "🖌️",
1065
+ cursor: "crosshair",
1066
+ category: "drawing",
1067
+ shortcuts: [{ key: "b" }],
1068
+ handle(r, i) {
1069
+ let a = Q(i.annotator);
1070
+ if (r.type === "cancel") {
1071
+ t = {
1072
+ pointerId: null,
1073
+ points: []
1074
+ }, i.clearDraft();
1075
+ return;
1076
+ }
1077
+ if (r.type === "down") {
1078
+ let o = nt(i.annotator, e.labelId), s = e.color ?? a.labels.find((e) => e.id === o)?.color ?? "#ff4d4f";
1079
+ t = {
1080
+ pointerId: r.pointerId,
1081
+ points: [r.imagePoint]
1082
+ }, i.setDraft({
1083
+ type: "brush",
1084
+ points: t.points,
1085
+ size: n,
1086
+ color: s,
1087
+ labelId: o
1088
+ });
1089
+ return;
1090
+ }
1091
+ if (r.type === "move") {
1092
+ if (t.pointerId !== r.pointerId || !(r.buttons & 1)) return;
1093
+ t = {
1094
+ pointerId: r.pointerId,
1095
+ points: [...t.points, r.imagePoint]
1096
+ };
1097
+ let o = nt(i.annotator, e.labelId), s = e.color ?? a.labels.find((e) => e.id === o)?.color ?? "#ff4d4f";
1098
+ i.setDraft({
1099
+ type: "brush",
1100
+ points: t.points,
1101
+ size: n,
1102
+ color: s,
1103
+ labelId: o
1104
+ });
1105
+ return;
1106
+ }
1107
+ if (r.type === "up") {
1108
+ if (t.pointerId !== r.pointerId) return;
1109
+ let o = [...t.points, r.imagePoint], s = a.image;
1110
+ if (o.length >= 2 && s !== null) {
1111
+ let t = nt(i.annotator, e.labelId);
1112
+ ot(i.annotator, t, at({
1113
+ imageWidth: s.width,
1114
+ imageHeight: s.height,
1115
+ brushSize: n,
1116
+ points: o
1117
+ }));
1118
+ }
1119
+ t = {
1120
+ pointerId: null,
1121
+ points: []
1122
+ }, i.clearDraft();
1123
+ }
1124
+ },
1125
+ cancel(e) {
1126
+ t = {
1127
+ pointerId: null,
1128
+ points: []
1129
+ }, e.clearDraft();
1130
+ }
1131
+ };
1132
+ }
1133
+ function V(e, t = {}) {
1134
+ R(e, st({
1135
+ labelId: nt(e, t.labelId),
1136
+ size: t.size ?? 10,
1137
+ color: t.color ?? "#ff4d4f"
1138
+ }));
1139
+ }
1140
+ //#endregion
1141
+ //#region src/geometry/rect.ts
1142
+ function ct(e) {
1143
+ if (!Number.isFinite(e.x) || !Number.isFinite(e.y)) throw new y("INVALID_GEOMETRY", "Point coordinates must be finite.");
1144
+ }
1145
+ function H(e, t) {
1146
+ ct(e), ct(t);
1147
+ let n = Math.min(e.x, t.x), r = Math.min(e.y, t.y);
1148
+ return {
1149
+ x: n,
1150
+ y: r,
1151
+ width: Math.max(e.x, t.x) - n,
1152
+ height: Math.max(e.y, t.y) - r
1153
+ };
1154
+ }
1155
+ function lt(e, t) {
1156
+ return ct(e), e.x >= t.x && e.x <= t.x + t.width && e.y >= t.y && e.y <= t.y + t.height;
1157
+ }
1158
+ //#endregion
1159
+ //#region src/tools/select-tool.ts
1160
+ function ut(e, t) {
1161
+ return {
1162
+ ...e,
1163
+ x: e.x + t.x,
1164
+ y: e.y + t.y
1165
+ };
1166
+ }
1167
+ function dt(e, t, n) {
1168
+ return {
1169
+ type: "polygon",
1170
+ points: e.points.map((e, r) => r === t ? [n.x, n.y] : e)
1171
+ };
1172
+ }
1173
+ function ft(e, t) {
1174
+ if (e.points.length <= 3) return null;
1175
+ let n = e.points.filter((e, n) => n !== t), r = {
1176
+ type: "polygon",
1177
+ points: n
1178
+ };
1179
+ return h(n.map(([e, t]) => ({
1180
+ x: e,
1181
+ y: t
1182
+ }))).valid ? r : null;
1183
+ }
1184
+ function pt(e, t, n) {
1185
+ let r = e.x, i = e.y, a = e.x + e.width, o = e.y + e.height, s = t.includes("west"), c = t.includes("east"), l = t.includes("north"), u = t.includes("south");
1186
+ return {
1187
+ type: "rect",
1188
+ ...H({
1189
+ x: s ? n.x : r,
1190
+ y: l ? n.y : i
1191
+ }, {
1192
+ x: c ? n.x : a,
1193
+ y: u ? n.y : o
1194
+ })
1195
+ };
1196
+ }
1197
+ function mt(e, t) {
1198
+ let n = e.x - t.x, r = e.y - t.y;
1199
+ return n * n + r * r;
1200
+ }
1201
+ function ht(e) {
1202
+ let t = e.x, n = e.y, r = e.x + e.width, i = e.y + e.height, a = (t + r) / 2, o = (n + i) / 2;
1203
+ return [
1204
+ ["north-west", {
1205
+ x: t,
1206
+ y: n
1207
+ }],
1208
+ ["north", {
1209
+ x: a,
1210
+ y: n
1211
+ }],
1212
+ ["north-east", {
1213
+ x: r,
1214
+ y: n
1215
+ }],
1216
+ ["east", {
1217
+ x: r,
1218
+ y: o
1219
+ }],
1220
+ ["south-east", {
1221
+ x: r,
1222
+ y: i
1223
+ }],
1224
+ ["south", {
1225
+ x: a,
1226
+ y: i
1227
+ }],
1228
+ ["south-west", {
1229
+ x: t,
1230
+ y: i
1231
+ }],
1232
+ ["west", {
1233
+ x: t,
1234
+ y: o
1235
+ }]
1236
+ ];
1237
+ }
1238
+ function gt(e, t, n) {
1239
+ let r = e.x, i = e.y, a = e.x + e.width, o = e.y + e.height, s = t.x >= r - n && t.x <= a + n, c = t.y >= i - n && t.y <= o + n, l = Math.abs(t.x - r) <= n && c, u = Math.abs(t.x - a) <= n && c, d = Math.abs(t.y - i) <= n && s, f = Math.abs(t.y - o) <= n && s;
1240
+ return l && d ? "north-west" : u && d ? "north-east" : u && f ? "south-east" : l && f ? "south-west" : l ? "west" : u ? "east" : d ? "north" : f ? "south" : null;
1241
+ }
1242
+ function _t(e, t) {
1243
+ if (e.geometry.type === "rect") return lt(t, e.geometry);
1244
+ if (e.geometry.type === "polygon") return m(t, e.geometry.points.map(([e, t]) => ({
1245
+ x: e,
1246
+ y: t
1247
+ })));
1248
+ if (e.geometry.type === "mask") {
1249
+ let n = Math.floor(t.x), r = Math.floor(t.y);
1250
+ return n < 0 || r < 0 || n >= e.geometry.width || r >= e.geometry.height ? !1 : j(e.geometry.rle, e.geometry.width, e.geometry.height)[r * e.geometry.width + n] === 1;
1251
+ }
1252
+ return !1;
1253
+ }
1254
+ function vt(e, t) {
1255
+ if (e.type === "rect") return ut(e, t);
1256
+ if (e.type === "polygon") return {
1257
+ type: "polygon",
1258
+ points: e.points.map(([e, n]) => [e + t.x, n + t.y])
1259
+ };
1260
+ let n = j(e.rle, e.width, e.height);
1261
+ return {
1262
+ type: "mask",
1263
+ width: e.width,
1264
+ height: e.height,
1265
+ rle: A(Ie(n, e.width, e.height, t.x, t.y))
1266
+ };
1267
+ }
1268
+ function yt(e, t, n, r) {
1269
+ let i = j(n.rle, n.width, n.height), a = D(e, {
1270
+ x: 0,
1271
+ y: 0,
1272
+ width: n.width,
1273
+ height: n.height
1274
+ }).filter((e) => e.id === t.id || e.labelId !== t.labelId || e.geometry.type !== "mask" || e.geometry.width !== n.width || e.geometry.height !== n.height ? !1 : Le(i, j(e.geometry.rle, e.geometry.width, e.geometry.height), n.width, n.height, r));
1275
+ for (let e of a) e.geometry.type === "mask" && (i = je(i, j(e.geometry.rle, e.geometry.width, e.geometry.height)));
1276
+ T(e, t.id, {
1277
+ ...n,
1278
+ rle: A(i)
1279
+ });
1280
+ for (let t of a) E(e, t.id);
1281
+ }
1282
+ function bt(e, t) {
1283
+ return JSON.stringify(e) === JSON.stringify(t);
1284
+ }
1285
+ function xt(e, t, n) {
1286
+ let r = n * n, i = e.points[t];
1287
+ return i !== void 0 && e.points.every(([e, n], a) => t === a || mt({
1288
+ x: i[0],
1289
+ y: i[1]
1290
+ }, {
1291
+ x: e,
1292
+ y: n
1293
+ }) >= r);
1294
+ }
1295
+ function St(e, t, n) {
1296
+ let r = n * n;
1297
+ if (e.geometry.type === "polygon") {
1298
+ let n = e.geometry.points.findIndex(([e, n]) => mt(t, {
1299
+ x: e,
1300
+ y: n
1301
+ }) <= r);
1302
+ return n < 0 ? null : {
1303
+ type: "polygon-vertex",
1304
+ index: n
1305
+ };
1306
+ }
1307
+ if (e.geometry.type === "rect") {
1308
+ let i = ht(e.geometry).find(([, e]) => mt(t, e) <= r), a = i === void 0 ? gt(e.geometry, t, n) : i[0];
1309
+ return a === null ? null : {
1310
+ type: "rect-handle",
1311
+ handle: a
1312
+ };
1313
+ }
1314
+ return null;
1315
+ }
1316
+ function Ct(e, t) {
1317
+ return e.mode.type === "polygon-vertex" ? e.annotation.geometry.type === "polygon" ? dt(e.annotation.geometry, e.mode.index, t) : e.annotation.geometry : e.mode.type === "rect-handle" ? e.annotation.geometry.type === "rect" ? pt(e.annotation.geometry, e.mode.handle, t) : e.annotation.geometry : vt(e.annotation.geometry, {
1318
+ x: t.x - e.start.x,
1319
+ y: t.y - e.start.y
1320
+ });
1321
+ }
1322
+ function U(e, t) {
1323
+ let n = Q(e);
1324
+ if (!n.annotationsById.has(t)) throw new y("ANNOTATION_NOT_FOUND", `Annotation not found: ${t}`);
1325
+ n.selectedIds = [t], n.renderer?.invalidate("interaction"), g(e, "selection:update");
1326
+ }
1327
+ function W(e) {
1328
+ let t = Q(e), n = t.selectedIds.length > 0;
1329
+ t.selectedIds = [], t.renderer?.invalidate("interaction"), n && g(e, "selection:update");
1330
+ }
1331
+ function G(e) {
1332
+ return [...Q(e).selectedIds];
1333
+ }
1334
+ function wt() {
1335
+ let e = { phase: "idle" }, t = null;
1336
+ return {
1337
+ id: "select",
1338
+ name: "选择",
1339
+ description: "选择、移动和编辑标注",
1340
+ icon: "☝️",
1341
+ cursor: "default",
1342
+ category: "selection",
1343
+ shortcuts: [{ key: "s" }],
1344
+ handle(n, r) {
1345
+ if (n.type === "cancel") {
1346
+ e = { phase: "idle" }, r.clearDraft();
1347
+ return;
1348
+ }
1349
+ if (n.type === "down") {
1350
+ let i = Q(r.annotator), a = 8 / (i.viewport?.scale ?? 1), o = i.selectedIds[0] === void 0 ? void 0 : i.annotationsById.get(i.selectedIds[0]), s = o, c = o === void 0 ? null : St(o, n.imagePoint, a), l = {
1351
+ x: n.imagePoint.x - a,
1352
+ y: n.imagePoint.y - a,
1353
+ width: a * 2,
1354
+ height: a * 2
1355
+ }, u = D(r.annotator, l).reverse().filter((e) => _t(e, n.imagePoint)), d = u.map((e) => e.id), f = o === void 0 ? -1 : d.indexOf(o.id), p = !1, m = 0;
1356
+ if (c === null) {
1357
+ let e = f >= 0 ? f : 0;
1358
+ s = u[e], p = f >= 0 && u.length > 1, m = u.length > 0 ? (e + 1) % u.length : 0, c = s === void 0 ? null : St(s, n.imagePoint, a) ?? { type: "move" };
1359
+ }
1360
+ if (s === void 0 || c === null) {
1361
+ t = null, W(r.annotator);
1362
+ return;
1363
+ }
1364
+ U(r.annotator, s.id), t = c.type === "polygon-vertex" ? {
1365
+ annotationId: s.id,
1366
+ index: c.index
1367
+ } : null, e = {
1368
+ phase: "dragging",
1369
+ pointerId: n.pointerId,
1370
+ annotation: s,
1371
+ start: n.imagePoint,
1372
+ mode: c,
1373
+ currentGeometry: s.geometry,
1374
+ moved: !1,
1375
+ clickTolerance: a,
1376
+ cycleOnClick: p,
1377
+ cycleIds: d,
1378
+ cycleNextIndex: m
1379
+ };
1380
+ return;
1381
+ }
1382
+ if (e.phase !== "dragging" || n.pointerId !== e.pointerId) return;
1383
+ let i = e.moved || mt(e.start, n.imagePoint) > e.clickTolerance * e.clickTolerance, a = Ct(e, n.imagePoint);
1384
+ if (e = {
1385
+ ...e,
1386
+ currentGeometry: a,
1387
+ moved: i
1388
+ }, n.type === "move") {
1389
+ r.setDraft({
1390
+ type: "vector",
1391
+ annotationId: e.annotation.id,
1392
+ geometry: a,
1393
+ labelId: e.annotation.labelId
1394
+ });
1395
+ return;
1396
+ }
1397
+ if (!e.moved && e.cycleOnClick && e.cycleIds.length > 1) {
1398
+ let t = e.cycleIds[e.cycleNextIndex % e.cycleIds.length];
1399
+ t !== void 0 && U(r.annotator, t), e = { phase: "idle" }, r.clearDraft();
1400
+ return;
1401
+ }
1402
+ let o = 1 / (Q(r.annotator).viewport?.scale ?? 1);
1403
+ (a.type === "rect" ? a.width >= o && a.height >= o : a.type !== "polygon" || h(a.points.map(([e, t]) => ({
1404
+ x: e,
1405
+ y: t
1406
+ }))).valid && (e.mode.type !== "polygon-vertex" || xt(a, e.mode.index, o))) && !bt(e.annotation.geometry, a) && (a.type === "mask" ? yt(r.annotator, e.annotation, a, e.clickTolerance) : T(r.annotator, e.annotation.id, a)), e = { phase: "idle" }, r.clearDraft();
1407
+ },
1408
+ handleKey(e, n) {
1409
+ let r = Q(n.annotator), i = r.selectedIds[0];
1410
+ if (e.key === "Delete" && i !== void 0) {
1411
+ e.preventDefault(), E(n.annotator, i), t = null, W(n.annotator);
1412
+ return;
1413
+ }
1414
+ if (e.key === "Backspace" && e.preventDefault(), e.key === "Backspace" && t !== null) {
1415
+ let e = r.annotationsById.get(t.annotationId);
1416
+ if (e?.geometry.type !== "polygon") return;
1417
+ let i = ft(e.geometry, t.index);
1418
+ i !== null && (T(n.annotator, e.id, i), t = null, n.clearDraft());
1419
+ }
1420
+ },
1421
+ cancel(n) {
1422
+ e = { phase: "idle" }, t = null, n.clearDraft();
1423
+ }
1424
+ };
1425
+ }
1426
+ function K(e) {
1427
+ R(e, wt());
1428
+ }
1429
+ //#endregion
1430
+ //#region src/tools/eraser-tool.ts
1431
+ function Tt(e, t, n) {
1432
+ let r = Q(e).image;
1433
+ if (r === null || t.length < 2) return;
1434
+ let i = at({
1435
+ imageWidth: r.width,
1436
+ imageHeight: r.height,
1437
+ brushSize: n,
1438
+ points: t
1439
+ }), a = j(i.rle, i.width, i.height), o = D(e, {
1440
+ x: 0,
1441
+ y: 0,
1442
+ width: i.width,
1443
+ height: i.height
1444
+ }).filter((e) => e.geometry.type === "mask" && e.geometry.width === i.width && e.geometry.height === i.height);
1445
+ for (let t of o) {
1446
+ if (t.geometry.type !== "mask") continue;
1447
+ let n = j(t.geometry.rle, t.geometry.width, t.geometry.height);
1448
+ if (!Ae(n, a)) continue;
1449
+ let r = Pe(Me(n, a), t.geometry.width, t.geometry.height), i = r[0];
1450
+ if (i !== void 0 && Ne(i)) {
1451
+ T(e, t.id, {
1452
+ type: "mask",
1453
+ width: t.geometry.width,
1454
+ height: t.geometry.height,
1455
+ rle: A(i)
1456
+ });
1457
+ for (let n of r.slice(1)) w(e, {
1458
+ labelId: t.labelId,
1459
+ width: t.geometry.width,
1460
+ height: t.geometry.height,
1461
+ rle: A(n)
1462
+ });
1463
+ } else E(e, t.id);
1464
+ }
1465
+ }
1466
+ function Et(e = {}) {
1467
+ let t = e.size ?? 10, n = {
1468
+ pointerId: null,
1469
+ points: []
1470
+ };
1471
+ return {
1472
+ id: "eraser",
1473
+ name: "橡皮擦",
1474
+ description: "擦除涂抹区域",
1475
+ icon: "🧹",
1476
+ cursor: "crosshair",
1477
+ category: "drawing",
1478
+ shortcuts: [{ key: "e" }],
1479
+ handle(e, r) {
1480
+ if (e.type === "cancel") {
1481
+ n = {
1482
+ pointerId: null,
1483
+ points: []
1484
+ }, r.clearDraft(), W(r.annotator);
1485
+ return;
1486
+ }
1487
+ if (e.type === "down") {
1488
+ n = {
1489
+ pointerId: e.pointerId,
1490
+ points: [e.imagePoint]
1491
+ }, r.setDraft({
1492
+ type: "eraser",
1493
+ points: n.points,
1494
+ size: t
1495
+ });
1496
+ return;
1497
+ }
1498
+ if (e.type === "move") {
1499
+ if (n.pointerId !== e.pointerId || !(e.buttons & 1)) return;
1500
+ n = {
1501
+ pointerId: e.pointerId,
1502
+ points: [...n.points, e.imagePoint]
1503
+ }, r.setDraft({
1504
+ type: "eraser",
1505
+ points: n.points,
1506
+ size: t
1507
+ });
1508
+ return;
1509
+ }
1510
+ if (e.type === "up") {
1511
+ if (n.pointerId !== e.pointerId) return;
1512
+ Tt(r.annotator, [...n.points, e.imagePoint], t), G(r.annotator).filter((e) => {
1513
+ let t = D(r.annotator, {
1514
+ x: 0,
1515
+ y: 0,
1516
+ width: 2 ** 53 - 1,
1517
+ height: 2 ** 53 - 1
1518
+ }).find((t) => t.id === e);
1519
+ return t === void 0 || t.geometry.type === "mask";
1520
+ }).length > 0 && W(r.annotator), n = {
1521
+ pointerId: null,
1522
+ points: []
1523
+ }, r.clearDraft();
1524
+ }
1525
+ },
1526
+ cancel(e) {
1527
+ n = {
1528
+ pointerId: null,
1529
+ points: []
1530
+ }, e.clearDraft(), W(e.annotator);
1531
+ }
1532
+ };
1533
+ }
1534
+ function q(e, t = {}) {
1535
+ R(e, Et(t));
1536
+ }
1537
+ //#endregion
1538
+ //#region src/tools/polygon-tool.ts
1539
+ function Dt() {
1540
+ return {
1541
+ points: [],
1542
+ preview: null
1543
+ };
1544
+ }
1545
+ function J(e, t) {
1546
+ if (t.type === "cancel") return { state: Dt() };
1547
+ if (t.type === "move") {
1548
+ let n = {
1549
+ ...e,
1550
+ preview: t.imagePoint
1551
+ };
1552
+ return {
1553
+ state: n,
1554
+ draft: [...n.points, t.imagePoint]
1555
+ };
1556
+ }
1557
+ if (t.type === "remove-last") {
1558
+ let t = {
1559
+ ...e,
1560
+ points: e.points.slice(0, -1)
1561
+ };
1562
+ return {
1563
+ state: t,
1564
+ draft: t.points
1565
+ };
1566
+ }
1567
+ if (t.type === "commit") return h(e.points).valid ? {
1568
+ state: Dt(),
1569
+ commit: [...e.points]
1570
+ } : { state: e };
1571
+ let n = e.points.at(-1);
1572
+ if (n !== void 0 && n.x === t.imagePoint.x && n.y === t.imagePoint.y) return { state: e };
1573
+ let r = {
1574
+ points: [...e.points, t.imagePoint],
1575
+ preview: t.imagePoint
1576
+ };
1577
+ return {
1578
+ state: r,
1579
+ draft: r.points
1580
+ };
1581
+ }
1582
+ function Ot(e, t) {
1583
+ let n = t ?? O(e);
1584
+ if (n === null) throw new y("UNKNOWN_LABEL", "A label must be active before drawing a polygon.");
1585
+ return n;
1586
+ }
1587
+ function kt(e) {
1588
+ let t = Dt(), n = (n, r) => {
1589
+ if (t = n.state, n.draft !== void 0) {
1590
+ let t = Ot(r.annotator, e.labelId);
1591
+ r.setDraft({
1592
+ type: "polygon",
1593
+ points: n.draft,
1594
+ labelId: t
1595
+ });
1596
+ } else t.points.length === 0 && r.clearDraft();
1597
+ if (n.commit !== void 0) {
1598
+ let t = Ot(r.annotator, e.labelId);
1599
+ de(r.annotator, {
1600
+ labelId: t,
1601
+ points: n.commit
1602
+ }), r.clearDraft();
1603
+ }
1604
+ }, r = (e) => {
1605
+ n(J(t, { type: "commit" }), e);
1606
+ };
1607
+ return {
1608
+ id: "polygon",
1609
+ name: "多边形",
1610
+ description: "绘制多边形标注",
1611
+ icon: "⬡",
1612
+ cursor: "crosshair",
1613
+ category: "drawing",
1614
+ shortcuts: [{ key: "p" }],
1615
+ handle(e, i) {
1616
+ if (e.type === "cancel") {
1617
+ n(J(t, { type: "cancel" }), i);
1618
+ return;
1619
+ }
1620
+ if (e.type === "move" && t.points.length > 0) {
1621
+ n(J(t, {
1622
+ type: "move",
1623
+ imagePoint: e.imagePoint
1624
+ }), i);
1625
+ return;
1626
+ }
1627
+ e.type === "down" && (n(J(t, {
1628
+ type: "point",
1629
+ imagePoint: e.imagePoint
1630
+ }), i), e.detail >= 2 && r(i));
1631
+ },
1632
+ handleKey(e, i) {
1633
+ e.key === "Enter" ? (e.preventDefault(), r(i)) : e.key === "Backspace" && (e.preventDefault(), n(J(t, { type: "remove-last" }), i));
1634
+ },
1635
+ cancel(e) {
1636
+ n(J(t, { type: "cancel" }), e);
1637
+ }
1638
+ };
1639
+ }
1640
+ function Y(e, t = {}) {
1641
+ R(e, kt({ labelId: Ot(e, t.labelId) }));
1642
+ }
1643
+ //#endregion
1644
+ //#region src/tools/rect-tool.ts
1645
+ function X() {
1646
+ return { phase: "idle" };
1647
+ }
1648
+ function At(e, t, n = 0) {
1649
+ if (t.type === "cancel") return { state: X() };
1650
+ if (t.type === "down") return e.phase === "idle" ? {
1651
+ state: {
1652
+ phase: "drawing",
1653
+ pointerId: t.pointerId,
1654
+ start: t.imagePoint,
1655
+ current: t.imagePoint
1656
+ },
1657
+ draft: H(t.imagePoint, t.imagePoint)
1658
+ } : { state: e };
1659
+ if (e.phase !== "drawing" || t.pointerId !== e.pointerId) return { state: e };
1660
+ let r = H(e.start, t.imagePoint);
1661
+ return t.type === "move" ? {
1662
+ state: {
1663
+ ...e,
1664
+ current: t.imagePoint
1665
+ },
1666
+ draft: r
1667
+ } : r.width < n || r.height < n ? { state: X() } : {
1668
+ state: X(),
1669
+ commit: r
1670
+ };
1671
+ }
1672
+ function jt(e, t) {
1673
+ let n = t ?? O(e);
1674
+ if (n === null) throw new y("UNKNOWN_LABEL", "A label must be active before drawing a rectangle.");
1675
+ return n;
1676
+ }
1677
+ function Mt(e) {
1678
+ let t = X();
1679
+ return {
1680
+ id: "rect",
1681
+ name: "矩形",
1682
+ description: "绘制矩形标注",
1683
+ icon: "⬜",
1684
+ cursor: "crosshair",
1685
+ category: "drawing",
1686
+ shortcuts: [{ key: "r" }],
1687
+ handle: (n, r) => {
1688
+ let i = At(t, n, e.minimumSize ?? 1);
1689
+ if (t = i.state, i.draft !== void 0) {
1690
+ let t = jt(r.annotator, e.labelId);
1691
+ r.setDraft({
1692
+ type: "rect",
1693
+ geometry: i.draft,
1694
+ labelId: t
1695
+ });
1696
+ } else r.clearDraft();
1697
+ if (i.commit !== void 0) {
1698
+ let t = jt(r.annotator, e.labelId);
1699
+ C(r.annotator, {
1700
+ labelId: t,
1701
+ ...i.commit
1702
+ });
1703
+ }
1704
+ },
1705
+ cancel(e) {
1706
+ t = At(t, { type: "cancel" }).state, e.clearDraft();
1707
+ }
1708
+ };
1709
+ }
1710
+ function Z(e, t = {}) {
1711
+ R(e, Mt({
1712
+ labelId: jt(e, t.labelId),
1713
+ ...t.minimumSize === void 0 ? {} : { minimumSize: t.minimumSize }
1714
+ }));
1715
+ }
1716
+ //#endregion
1717
+ //#region src/tools/registry.ts
1718
+ function Nt() {
1719
+ let e = /* @__PURE__ */ new Map();
1720
+ return {
1721
+ register(t) {
1722
+ e.set(t.id, t);
1723
+ },
1724
+ unregister(t) {
1725
+ e.delete(t);
1726
+ },
1727
+ get(t) {
1728
+ return e.get(t);
1729
+ },
1730
+ list() {
1731
+ return [...e.values()];
1732
+ },
1733
+ listByCategory(t) {
1734
+ return [...e.values()].filter((e) => e.category === t);
1735
+ }
1736
+ };
1737
+ }
1738
+ function Pt() {
1739
+ let e = Nt();
1740
+ return e.register(wt()), e.register(Mt({})), e.register(kt({})), e.register(st({})), e.register(Et()), e;
1741
+ }
1742
+ //#endregion
1743
+ //#region src/core/state.ts
1744
+ function Ft(e) {
1745
+ return {
1746
+ container: e.container,
1747
+ historyLimit: e.historyLimit ?? 100,
1748
+ destroyed: !1,
1749
+ revision: 0,
1750
+ annotations: [],
1751
+ labels: [],
1752
+ activeLabelId: null,
1753
+ spatialIndex: a(512),
1754
+ annotationsById: /* @__PURE__ */ new Map(),
1755
+ undoStack: [],
1756
+ redoStack: [],
1757
+ listeners: {
1758
+ change: /* @__PURE__ */ new Set(),
1759
+ error: /* @__PURE__ */ new Set()
1760
+ },
1761
+ imageSource: null,
1762
+ image: null,
1763
+ imageAbortController: null,
1764
+ renderer: null,
1765
+ viewport: null,
1766
+ toolController: null,
1767
+ activeToolId: null,
1768
+ interactionDraft: null,
1769
+ selectedIds: [],
1770
+ toolRegistry: Pt()
1771
+ };
1772
+ }
1773
+ //#endregion
1774
+ //#region src/core/annotator.ts
1775
+ var It = /* @__PURE__ */ new WeakMap();
1776
+ function Q(e) {
1777
+ let t = It.get(e);
1778
+ if (t === void 0 || t.destroyed) throw new y("ANNOTATOR_DESTROYED", "The annotator has been destroyed.");
1779
+ return t;
1780
+ }
1781
+ function Lt(e) {
1782
+ let t = ie();
1783
+ return It.set(t, Ft(e)), t;
1784
+ }
1785
+ function Rt(e) {
1786
+ let t = It.get(e);
1787
+ t === void 0 || t.destroyed || (t.toolController?.destroy(), t.imageAbortController?.abort(), t.renderer?.destroy(), t.imageSource?.dispose(), t.destroyed = !0, t.imageAbortController = null, t.renderer = null, t.imageSource = null, t.image = null, t.viewport = null, t.toolController = null, t.activeToolId = null, t.interactionDraft = null, t.selectedIds = [], t.listeners.change.clear(), t.listeners.error.clear());
1788
+ }
1789
+ function zt(e) {
1790
+ let t = Q(e);
1791
+ return Object.freeze({
1792
+ schemaVersion: 1,
1793
+ revision: t.revision,
1794
+ annotations: Object.freeze(t.annotations.map(v)),
1795
+ labels: Object.freeze(t.labels.map((e) => Object.freeze({ ...e })))
1796
+ });
1797
+ }
1798
+ //#endregion
1799
+ //#region src/image/standard-source.ts
1800
+ function $() {
1801
+ return new DOMException("Image loading was aborted.", "AbortError");
1802
+ }
1803
+ function Bt(e) {
1804
+ return typeof ImageBitmap < "u" && e instanceof ImageBitmap;
1805
+ }
1806
+ function Vt(e, t) {
1807
+ return new Promise((n, r) => {
1808
+ let i = new Image();
1809
+ i.crossOrigin = "anonymous";
1810
+ let a = () => {
1811
+ t.removeEventListener("abort", o), i.onload = null, i.onerror = null;
1812
+ }, o = () => {
1813
+ a(), i.src = "", r($());
1814
+ };
1815
+ i.onload = () => {
1816
+ a(), n(i);
1817
+ }, i.onerror = () => {
1818
+ a(), r(/* @__PURE__ */ Error("The source image could not be decoded."));
1819
+ }, t.addEventListener("abort", o, { once: !0 }), i.src = e;
1820
+ });
1821
+ }
1822
+ function Ht(e) {
1823
+ let t = e, n = null, r = null, i = null, a = null, o = !1;
1824
+ return {
1825
+ id: globalThis.crypto.randomUUID(),
1826
+ async load(e) {
1827
+ if (o) throw Error("The image source has been disposed.");
1828
+ if (e.aborted) throw $();
1829
+ if (t !== null && Bt(t)) {
1830
+ let e = t;
1831
+ t = null, i = createImageBitmap(e).then((e) => {
1832
+ if (o) throw e.close(), $();
1833
+ return r = e, e;
1834
+ });
1835
+ }
1836
+ if (i !== null) {
1837
+ let t = await i;
1838
+ if (o || e.aborted) throw $();
1839
+ return {
1840
+ source: t,
1841
+ width: t.width,
1842
+ height: t.height
1843
+ };
1844
+ }
1845
+ if (n !== null) return {
1846
+ source: n,
1847
+ width: n.naturalWidth,
1848
+ height: n.naturalHeight
1849
+ };
1850
+ if (t === null) throw Error("The image input is unavailable.");
1851
+ typeof t != "string" && (a = URL.createObjectURL(t));
1852
+ let s = await Vt(typeof t == "string" ? t : a, e);
1853
+ if (o) throw $();
1854
+ return n = s, t = null, {
1855
+ source: s,
1856
+ width: s.naturalWidth,
1857
+ height: s.naturalHeight
1858
+ };
1859
+ },
1860
+ dispose() {
1861
+ o || (o = !0, t = null, n = null, r?.close(), r = null, a !== null && (URL.revokeObjectURL(a), a = null));
1862
+ }
1863
+ };
1864
+ }
1865
+ //#endregion
1866
+ //#region src/tools/api.ts
1867
+ function Ut(e) {
1868
+ return Q(e).activeToolId;
1869
+ }
1870
+ function Wt(e) {
1871
+ let t = [...G(e)], n = 0;
1872
+ for (let r of t) E(e, r) && (n += 1);
1873
+ return W(e), n;
1874
+ }
1875
+ function Gt(e, t) {
1876
+ let n = [...G(e)];
1877
+ for (let r of n) fe(e, r, t);
1878
+ return n.length;
1879
+ }
1880
+ function Kt(e) {
1881
+ return Object.freeze({
1882
+ select() {
1883
+ K(e);
1884
+ },
1885
+ rect(t = {}) {
1886
+ Z(e, t);
1887
+ },
1888
+ polygon(t = {}) {
1889
+ Y(e, t);
1890
+ },
1891
+ brush(t = {}) {
1892
+ V(e, t);
1893
+ },
1894
+ eraser(t = {}) {
1895
+ q(e, t);
1896
+ },
1897
+ activate(t) {
1898
+ R(e, t);
1899
+ },
1900
+ activateById(t) {
1901
+ z(e, t);
1902
+ },
1903
+ activeId() {
1904
+ return Ut(e);
1905
+ },
1906
+ cancel() {
1907
+ B(e);
1908
+ },
1909
+ get(t) {
1910
+ return Q(e).toolRegistry.get(t);
1911
+ },
1912
+ list() {
1913
+ return Q(e).toolRegistry.list();
1914
+ },
1915
+ listByCategory(t) {
1916
+ return Q(e).toolRegistry.listByCategory(t);
1917
+ },
1918
+ register(t) {
1919
+ Q(e).toolRegistry.register(t);
1920
+ },
1921
+ unregister(t) {
1922
+ Q(e).toolRegistry.unregister(t);
1923
+ },
1924
+ selectAnnotation(t) {
1925
+ U(e, t);
1926
+ },
1927
+ clearSelection() {
1928
+ W(e);
1929
+ },
1930
+ selection() {
1931
+ return G(e);
1932
+ },
1933
+ deleteSelection() {
1934
+ return Wt(e);
1935
+ },
1936
+ setSelectionLabel(t) {
1937
+ return Gt(e, t);
1938
+ }
1939
+ });
1940
+ }
1941
+ //#endregion
1942
+ //#region src/tools/index.ts
1943
+ function qt(e, t) {
1944
+ Q(e).toolRegistry.register(t);
1945
+ }
1946
+ function Jt(e, t) {
1947
+ Q(e).toolRegistry.unregister(t);
1948
+ }
1949
+ function Yt(e, t) {
1950
+ return Q(e).toolRegistry.get(t);
1951
+ }
1952
+ function Xt(e) {
1953
+ return Q(e).toolRegistry.list();
1954
+ }
1955
+ function Zt(e, t) {
1956
+ return Q(e).toolRegistry.listByCategory(t);
1957
+ }
1958
+ //#endregion
1959
+ //#region src/components/styles.ts
1960
+ var Qt = "\n :host {\n --cs-annotator-background: #101318;\n --cs-annotator-border: #2a3039;\n --cs-annotator-control: #1a2029;\n --cs-annotator-control-active: #2563eb;\n --cs-annotator-foreground: #f8fafc;\n display: block;\n width: 100%;\n height: 100%;\n color: var(--cs-annotator-foreground);\n font: 14px/1.4 system-ui, sans-serif;\n }\n\n [part=\"shell\"] {\n display: grid;\n grid-template-rows: auto minmax(0, 1fr);\n width: 100%;\n height: 100%;\n overflow: hidden;\n background: var(--cs-annotator-background);\n }\n\n [part=\"toolbar\"] {\n display: flex;\n gap: 6px;\n align-items: center;\n padding: 8px;\n border-bottom: 1px solid var(--cs-annotator-border);\n }\n\n button {\n min-height: 32px;\n padding: 5px 10px;\n color: inherit;\n background: var(--cs-annotator-control);\n border: 1px solid var(--cs-annotator-border);\n border-radius: 6px;\n cursor: pointer;\n }\n\n button[aria-pressed=\"true\"] {\n background: var(--cs-annotator-control-active);\n border-color: #93c5fd;\n outline: 2px solid #ffffff;\n outline-offset: -4px;\n }\n\n button:focus-visible {\n outline: 2px solid #93c5fd;\n outline-offset: 2px;\n }\n\n button:disabled {\n cursor: not-allowed;\n opacity: 0.45;\n }\n\n [part=\"workspace\"] {\n display: grid;\n grid-template-columns: minmax(0, 1fr) 180px;\n min-height: 0;\n }\n\n [part=\"viewport\"] {\n position: relative;\n min-width: 0;\n min-height: 0;\n background: #090b0f;\n }\n\n [part=\"labels\"] {\n overflow: auto;\n padding: 8px;\n border-left: 1px solid var(--cs-annotator-border);\n }\n\n [part=\"labels\"] button {\n display: block;\n width: 100%;\n margin-bottom: 6px;\n text-align: left;\n }\n", $t = globalThis.HTMLElement ?? class {}, en = class extends $t {
1961
+ #e = null;
1962
+ #t = null;
1963
+ #n = null;
1964
+ #r = null;
1965
+ #i = /* @__PURE__ */ new Map();
1966
+ #a = null;
1967
+ connectedCallback() {
1968
+ if (this.shadowRoot !== null) return;
1969
+ let e = this.attachShadow({ mode: "open" });
1970
+ e.innerHTML = `
1971
+ <style>${Qt}</style>
1972
+ <div part="shell">
1973
+ <div part="toolbar" role="toolbar" aria-label="Annotation tools">
1974
+ <button type="button" data-action="select" aria-label="Select" aria-pressed="false">Select</button>
1975
+ <button type="button" data-action="rect" aria-label="Rectangle" aria-pressed="false">Rectangle</button>
1976
+ <button type="button" data-action="polygon" aria-label="Polygon" aria-pressed="false">Polygon</button>
1977
+ <button type="button" data-action="brush" aria-label="Brush" aria-pressed="false">Brush</button>
1978
+ <button type="button" data-action="eraser" aria-label="Eraser" aria-pressed="false">Eraser</button>
1979
+ <button type="button" data-action="zoom-in" aria-label="Zoom in">Zoom in</button>
1980
+ <button type="button" data-action="zoom-out" aria-label="Zoom out">Zoom out</button>
1981
+ <button type="button" data-action="fit" aria-label="Fit">Fit</button>
1982
+ <button type="button" data-action="undo" aria-label="Undo">Undo</button>
1983
+ <button type="button" data-action="redo" aria-label="Redo">Redo</button>
1984
+ </div>
1985
+ <div part="workspace">
1986
+ <div part="viewport" role="region" aria-label="Annotation viewport"></div>
1987
+ <div part="labels" role="group" aria-label="Labels"></div>
1988
+ </div>
1989
+ </div>
1990
+ `, this.#n = e.querySelector("[part=\"viewport\"]"), this.#r = e.querySelector("[part=\"labels\"]");
1991
+ for (let t of e.querySelectorAll("button[data-action]")) {
1992
+ let e = t.dataset.action;
1993
+ e !== void 0 && (this.#i.set(e, t), t.addEventListener("click", () => this.#o(e)));
1994
+ }
1995
+ this.#c();
1996
+ }
1997
+ disconnectedCallback() {
1998
+ this.destroy();
1999
+ }
2000
+ configure(e = {}) {
2001
+ if (this.connectedCallback(), this.#n === null) throw Error("Annotation viewport is unavailable.");
2002
+ this.destroy();
2003
+ let t = Lt({
2004
+ container: this.#n,
2005
+ ...e.historyLimit === void 0 ? {} : { historyLimit: e.historyLimit }
2006
+ });
2007
+ return this.#e = t, this.#t = _(t, "change", () => {
2008
+ this.#c(), this.#l();
2009
+ }), this.#c(), this.#l(), t;
2010
+ }
2011
+ get annotator() {
2012
+ return this.#e;
2013
+ }
2014
+ destroy() {
2015
+ this.#t?.(), this.#t = null, this.#e !== null && (Rt(this.#e), this.#e = null), this.#a = null, this.#c();
2016
+ }
2017
+ #o(e) {
2018
+ let t = this.#e;
2019
+ if (t !== null) try {
2020
+ e === "select" ? (K(t), this.#s("select")) : e === "rect" ? (Z(t), this.#s("rect")) : e === "polygon" ? (Y(t), this.#s("polygon")) : e === "brush" ? (V(t), this.#s("brush")) : e === "eraser" ? (q(t), this.#s("eraser")) : e === "fit" ? P(t) : e === "zoom-in" ? I(t, 1.25) : e === "zoom-out" ? I(t, .8) : e === "undo" ? he(t) : e === "redo" && ge(t), this.#c();
2021
+ } catch (e) {
2022
+ this.dispatchEvent(new CustomEvent("annotatorerror", {
2023
+ bubbles: !0,
2024
+ composed: !0,
2025
+ detail: e
2026
+ }));
2027
+ }
2028
+ }
2029
+ #s(e) {
2030
+ this.#a = e;
2031
+ for (let [t, n] of this.#i) [
2032
+ "select",
2033
+ "rect",
2034
+ "polygon",
2035
+ "brush",
2036
+ "eraser"
2037
+ ].includes(t) && n.setAttribute("aria-pressed", String(t === e));
2038
+ }
2039
+ #c() {
2040
+ let e = this.#e, t = e !== null, n = t && N(e);
2041
+ for (let [r, i] of this.#i) r === "undo" ? i.disabled = !t || !pe(e) : r === "redo" ? i.disabled = !t || !me(e) : i.disabled = !n;
2042
+ this.#s(this.#a ?? "");
2043
+ }
2044
+ #l() {
2045
+ if (this.#r === null || this.#e === null) return;
2046
+ let e = this.#e, t = O(e);
2047
+ this.#r.replaceChildren();
2048
+ for (let n of zt(e).labels) {
2049
+ let r = document.createElement("button");
2050
+ r.type = "button", r.textContent = n.name, r.style.borderLeft = `4px solid ${n.color}`, r.setAttribute("aria-pressed", String(n.id === t)), r.addEventListener("click", () => {
2051
+ ve(e, n.id), this.#l();
2052
+ }), this.#r.append(r);
2053
+ }
2054
+ }
2055
+ };
2056
+ //#endregion
2057
+ //#region src/components/define.ts
2058
+ function tn() {
2059
+ customElements.get("cs-annotator") === void 0 && customElements.define("cs-annotator", en);
2060
+ }
2061
+ function nn(e) {
2062
+ let t = typeof e == "string" ? document.querySelector(e) : e;
2063
+ if (t === null) throw Error(`Mount target not found: ${e}`);
2064
+ return t;
2065
+ }
2066
+ function rn(e, t = {}) {
2067
+ tn();
2068
+ let n = nn(e), r = document.createElement("cs-annotator");
2069
+ return n.append(r), r.configure(t);
2070
+ }
2071
+ function an(e) {
2072
+ let t = nn(e), n = t.matches("cs-annotator") ? t : t.querySelector("cs-annotator");
2073
+ n !== null && (n.destroy(), n.remove());
2074
+ }
2075
+ //#endregion
2076
+ //#region src/cs-label-tool.ts
2077
+ var on = {
2078
+ create: Lt,
2079
+ destroy: Rt,
2080
+ snapshot: zt,
2081
+ addRect: C,
2082
+ addPolygon: de,
2083
+ addMask: w,
2084
+ updateAnnotation: T,
2085
+ removeAnnotation: E,
2086
+ queryAnnotations: D,
2087
+ updateAnnotationLabel: fe,
2088
+ undo: he,
2089
+ redo: ge,
2090
+ canUndo: pe,
2091
+ canRedo: me,
2092
+ subscribe: _,
2093
+ addLabel: _e,
2094
+ getActiveLabel: O,
2095
+ setActiveLabel: ve,
2096
+ updateLabel: ye,
2097
+ setImage: Ge,
2098
+ fitToScreen: P,
2099
+ zoomTo: F,
2100
+ zoomBy: I,
2101
+ panBy: L,
2102
+ getZoom: Ke,
2103
+ hasImage: N,
2104
+ resizeViewport: qe,
2105
+ clientToImage: Ye,
2106
+ imageToClient: Je,
2107
+ createImageSource: Ht,
2108
+ useSelect: K,
2109
+ useRect: Z,
2110
+ usePolygon: Y,
2111
+ useBrush: V,
2112
+ useEraser: q,
2113
+ activateTool: R,
2114
+ activateToolById: z,
2115
+ registerTool: qt,
2116
+ unregisterTool: Jt,
2117
+ getTool: Yt,
2118
+ listTools: Xt,
2119
+ listToolsByCategory: Zt,
2120
+ cancelGesture: B,
2121
+ createToolApi: Kt,
2122
+ getActiveToolId: Ut,
2123
+ deleteSelectedAnnotations: Wt,
2124
+ updateSelectedAnnotationsLabel: Gt,
2125
+ selectAnnotation: U,
2126
+ clearSelection: W,
2127
+ getSelection: G,
2128
+ mount: rn,
2129
+ unmount: an,
2130
+ defineElements: tn,
2131
+ normalizeRect: H,
2132
+ pointInRect: lt,
2133
+ pointInPolygon: m,
2134
+ validatePolygon: h,
2135
+ createGridIndex: a,
2136
+ insertSpatialItem: o,
2137
+ updateSpatialItem: s,
2138
+ removeSpatialItem: c,
2139
+ querySpatialBounds: u,
2140
+ createViewport: Te,
2141
+ fitViewport: ke,
2142
+ imageToScreen: Ee,
2143
+ screenToImage: k,
2144
+ zoomAt: De,
2145
+ panViewport: Oe,
2146
+ createSelectTool: wt,
2147
+ createRectTool: Mt,
2148
+ createPolygonTool: kt,
2149
+ createBrushTool: st,
2150
+ createEraserTool: Et,
2151
+ moveRect: ut,
2152
+ resizeRect: pt,
2153
+ movePolygonVertex: dt,
2154
+ removePolygonVertex: ft,
2155
+ AnnotatorError: y
2156
+ };
2157
+ function sn(e) {
2158
+ return {
2159
+ annotator: e,
2160
+ tools: Kt(e),
2161
+ addRect(t) {
2162
+ return C(e, t);
2163
+ },
2164
+ addPolygon(t) {
2165
+ return de(e, t);
2166
+ },
2167
+ addMask(t) {
2168
+ return w(e, t);
2169
+ },
2170
+ updateAnnotation(t, n) {
2171
+ T(e, t, n);
2172
+ },
2173
+ updateAnnotationLabel(t, n) {
2174
+ fe(e, t, n);
2175
+ },
2176
+ removeAnnotation(t) {
2177
+ return E(e, t);
2178
+ },
2179
+ queryAnnotations(t) {
2180
+ return D(e, t);
2181
+ },
2182
+ undo() {
2183
+ return he(e);
2184
+ },
2185
+ redo() {
2186
+ return ge(e);
2187
+ },
2188
+ canUndo() {
2189
+ return pe(e);
2190
+ },
2191
+ canRedo() {
2192
+ return me(e);
2193
+ },
2194
+ addLabel(t) {
2195
+ _e(e, t);
2196
+ },
2197
+ getActiveLabel() {
2198
+ return O(e);
2199
+ },
2200
+ setActiveLabel(t) {
2201
+ ve(e, t);
2202
+ },
2203
+ updateLabel(t, n) {
2204
+ ye(e, t, n);
2205
+ },
2206
+ setImage(t) {
2207
+ return Ge(e, t);
2208
+ },
2209
+ fitToScreen() {
2210
+ P(e);
2211
+ },
2212
+ zoomTo(t, n) {
2213
+ F(e, t, n);
2214
+ },
2215
+ zoomBy(t) {
2216
+ I(e, t);
2217
+ },
2218
+ panBy(t) {
2219
+ L(e, t);
2220
+ },
2221
+ getZoom() {
2222
+ return Ke(e);
2223
+ },
2224
+ hasImage() {
2225
+ return N(e);
2226
+ },
2227
+ resizeViewport() {
2228
+ qe(e);
2229
+ },
2230
+ useSelect() {
2231
+ K(e);
2232
+ },
2233
+ useRect(t) {
2234
+ Z(e, t);
2235
+ },
2236
+ usePolygon(t) {
2237
+ Y(e, t);
2238
+ },
2239
+ useBrush(t) {
2240
+ V(e, t);
2241
+ },
2242
+ useEraser(t) {
2243
+ q(e, t);
2244
+ },
2245
+ activateTool(t) {
2246
+ R(e, t);
2247
+ },
2248
+ activateToolById(t) {
2249
+ z(e, t);
2250
+ },
2251
+ registerTool(t) {
2252
+ qt(e, t);
2253
+ },
2254
+ unregisterTool(t) {
2255
+ Jt(e, t);
2256
+ },
2257
+ getTool(t) {
2258
+ return Yt(e, t);
2259
+ },
2260
+ listTools() {
2261
+ return Xt(e);
2262
+ },
2263
+ listToolsByCategory(t) {
2264
+ return Zt(e, t);
2265
+ },
2266
+ cancelGesture() {
2267
+ B(e);
2268
+ },
2269
+ selectAnnotation(t) {
2270
+ U(e, t);
2271
+ },
2272
+ clearSelection() {
2273
+ W(e);
2274
+ },
2275
+ getSelection() {
2276
+ return G(e);
2277
+ },
2278
+ subscribe(t, n) {
2279
+ return _(e, t, n);
2280
+ },
2281
+ snapshot() {
2282
+ return zt(e);
2283
+ },
2284
+ destroy() {
2285
+ Rt(e);
2286
+ }
2287
+ };
2288
+ }
2289
+ function cn(e) {
2290
+ return sn(Lt(e));
2291
+ }
2292
+ function ln(e, t) {
2293
+ return sn(rn(e, t));
2294
+ }
2295
+ var un = on;
2296
+ //#endregion
2297
+ export { y as AnnotatorError, en as CSAnnotatorElement, R as activateTool, z as activateToolById, _e as addLabel, w as addMask, de as addPolygon, C as addRect, Le as binaryMasksWithinDistance, me as canRedo, pe as canUndo, B as cancelActiveGesture, W as clearSelection, Ye as clientToImage, cn as create, Lt as createAnnotator, at as createBrushMaskGeometry, st as createBrushTool, Pt as createDefaultToolRegistry, Et as createEraserTool, a as createGridIndex, kt as createPolygonTool, Dt as createPolygonToolState, Mt as createRectTool, X as createRectToolState, wt as createSelectTool, Ht as createStandardImageSource, Kt as createToolApi, Nt as createToolRegistry, Te as createViewport, un as csLabelTool, j as decodeBinaryMaskRle, tn as defineAnnotatorElements, Wt as deleteSelectedAnnotations, Rt as destroyAnnotator, A as encodeBinaryMaskRle, P as fitToScreen, ke as fitViewport, O as getActiveLabel, Ut as getActiveToolId, Fe as getBinaryMaskBounds, et as getRegisteredTools, tt as getRegisteredToolsByCategory, G as getSelection, zt as getSnapshot, Yt as getTool, Ke as getZoom, N as hasImage, Je as imageToClient, Ee as imageToScreen, o as insertSpatialItem, Xt as listTools, Zt as listToolsByCategory, ln as mount, rn as mountAnnotator, dt as movePolygonVertex, ut as moveRect, H as normalizeRect, L as panBy, Oe as panViewport, m as pointInPolygon, lt as pointInRect, D as queryAnnotations, u as querySpatialBounds, ge as redo, J as reducePolygonTool, At as reduceRectTool, qt as registerTool, E as removeAnnotation, ft as removePolygonVertex, c as removeSpatialItem, pt as resizeRect, qe as resizeViewport, k as screenToImage, U as selectAnnotation, ve as setActiveLabel, Ge as setImageSource, Pe as splitBinaryMaskComponents, _ as subscribe, Ie as translateBinaryMask, he as undo, an as unmountAnnotator, Jt as unregisterTool, T as updateAnnotation, fe as updateAnnotationLabel, ye as updateLabel, Gt as updateSelectedAnnotationsLabel, s as updateSpatialItem, V as useBrush, q as useEraser, Y as usePolygon, Z as useRect, K as useSelect, h as validatePolygon, De as zoomAt, I as zoomBy, F as zoomTo };
2298
+
2299
+ //# sourceMappingURL=index.js.map