electron-snip 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/CHANGELOG.md +74 -0
  2. package/LICENSE +21 -0
  3. package/README.md +252 -2
  4. package/dist/annotations/annotations.cjs +1745 -0
  5. package/dist/annotations/annotations.cjs.map +1 -0
  6. package/dist/annotations/annotations.css +159 -0
  7. package/dist/annotations/annotations.css.map +1 -0
  8. package/dist/annotations/annotations.d.cts +340 -0
  9. package/dist/annotations/annotations.d.ts +340 -0
  10. package/dist/annotations/annotations.js +1722 -0
  11. package/dist/annotations/annotations.js.map +1 -0
  12. package/dist/index.cjs +1822 -0
  13. package/dist/index.cjs.map +1 -0
  14. package/dist/index.d.cts +187 -0
  15. package/dist/index.d.ts +187 -0
  16. package/dist/index.js +1783 -0
  17. package/dist/index.js.map +1 -0
  18. package/dist/injector-5YZ2B5KA.js +117 -0
  19. package/dist/injector-5YZ2B5KA.js.map +1 -0
  20. package/dist/koffi-TKWNTTDX.js +434 -0
  21. package/dist/koffi-TKWNTTDX.js.map +1 -0
  22. package/dist/ocr/ocr.cjs +145 -0
  23. package/dist/ocr/ocr.cjs.map +1 -0
  24. package/dist/ocr/ocr.d.cts +3 -0
  25. package/dist/ocr/ocr.d.ts +3 -0
  26. package/dist/ocr/ocr.js +110 -0
  27. package/dist/ocr/ocr.js.map +1 -0
  28. package/dist/preload.cjs +112 -0
  29. package/dist/preload.cjs.map +1 -0
  30. package/dist/record/mp4.cjs +346 -0
  31. package/dist/record/mp4.cjs.map +1 -0
  32. package/dist/record/mp4.d.cts +28 -0
  33. package/dist/record/mp4.d.ts +28 -0
  34. package/dist/record/mp4.js +323 -0
  35. package/dist/record/mp4.js.map +1 -0
  36. package/dist/record/record.cjs +764 -0
  37. package/dist/record/record.cjs.map +1 -0
  38. package/dist/record/record.d.cts +111 -0
  39. package/dist/record/record.d.ts +111 -0
  40. package/dist/record/record.js +723 -0
  41. package/dist/record/record.js.map +1 -0
  42. package/dist/renderer/overlay.css +478 -0
  43. package/dist/renderer/overlay.css.map +1 -0
  44. package/dist/renderer/overlay.html +62 -0
  45. package/dist/renderer/overlay.js +2551 -0
  46. package/dist/renderer/overlay.js.map +1 -0
  47. package/dist/renderer/pin.css +283 -0
  48. package/dist/renderer/pin.css.map +1 -0
  49. package/dist/renderer/pin.html +23 -0
  50. package/dist/renderer/pin.js +1941 -0
  51. package/dist/renderer/pin.js.map +1 -0
  52. package/dist/renderer/record.css +80 -0
  53. package/dist/renderer/record.html +26 -0
  54. package/dist/renderer/record.js +730 -0
  55. package/dist/renderer/record.js.map +1 -0
  56. package/dist/renderer/scroll.css +155 -0
  57. package/dist/renderer/scroll.html +41 -0
  58. package/dist/renderer/scroll.js +669 -0
  59. package/dist/renderer/scroll.js.map +1 -0
  60. package/dist/scroll/chunk-QKAC7M7X.js +118 -0
  61. package/dist/scroll/chunk-QKAC7M7X.js.map +1 -0
  62. package/dist/scroll/injector-3L4F6RIJ.js +11 -0
  63. package/dist/scroll/injector-3L4F6RIJ.js.map +1 -0
  64. package/dist/scroll/scroll.cjs +779 -0
  65. package/dist/scroll/scroll.cjs.map +1 -0
  66. package/dist/scroll/scroll.d.cts +72 -0
  67. package/dist/scroll/scroll.d.ts +72 -0
  68. package/dist/scroll/scroll.js +616 -0
  69. package/dist/scroll/scroll.js.map +1 -0
  70. package/dist/utils/utils.cjs +62 -0
  71. package/dist/utils/utils.cjs.map +1 -0
  72. package/dist/utils/utils.d.cts +61 -0
  73. package/dist/utils/utils.d.ts +61 -0
  74. package/dist/utils/utils.js +39 -0
  75. package/dist/utils/utils.js.map +1 -0
  76. package/package.json +172 -4
@@ -0,0 +1,1745 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/annotations/index.ts
21
+ var annotations_exports = {};
22
+ __export(annotations_exports, {
23
+ Editor: () => Editor,
24
+ createToolbar: () => createToolbar,
25
+ drawShape: () => drawShape,
26
+ toolUsesWidth: () => toolUsesWidth
27
+ });
28
+ module.exports = __toCommonJS(annotations_exports);
29
+
30
+ // src/shared/selection.ts
31
+ var HANDLE_IDS = ["nw", "n", "ne", "e", "se", "s", "sw", "w"];
32
+ var HANDLE_HIT_DIP = 12;
33
+ var MIN_EDGE = 1;
34
+ function handlePoint(rect, id) {
35
+ const { x, y, width, height } = rect;
36
+ const cx = x + width / 2;
37
+ const cy = y + height / 2;
38
+ switch (id) {
39
+ case "nw":
40
+ return { x, y };
41
+ case "n":
42
+ return { x: cx, y };
43
+ case "ne":
44
+ return { x: x + width, y };
45
+ case "e":
46
+ return { x: x + width, y: cy };
47
+ case "se":
48
+ return { x: x + width, y: y + height };
49
+ case "s":
50
+ return { x: cx, y: y + height };
51
+ case "sw":
52
+ return { x, y: y + height };
53
+ case "w":
54
+ return { x, y: cy };
55
+ }
56
+ }
57
+ function resizeRect(origin, handle, cursor) {
58
+ let left = origin.x;
59
+ let top = origin.y;
60
+ let right = origin.x + origin.width;
61
+ let bottom = origin.y + origin.height;
62
+ if (handle.includes("w")) left = Math.min(cursor.x, right - MIN_EDGE);
63
+ if (handle.includes("e")) right = Math.max(cursor.x, left + MIN_EDGE);
64
+ if (handle.includes("n")) top = Math.min(cursor.y, bottom - MIN_EDGE);
65
+ if (handle.includes("s")) bottom = Math.max(cursor.y, top + MIN_EDGE);
66
+ return { x: left, y: top, width: right - left, height: bottom - top };
67
+ }
68
+ var HANDLE_CURSOR = {
69
+ nw: "nwse-resize",
70
+ se: "nwse-resize",
71
+ ne: "nesw-resize",
72
+ sw: "nesw-resize",
73
+ n: "ns-resize",
74
+ s: "ns-resize",
75
+ e: "ew-resize",
76
+ w: "ew-resize"
77
+ };
78
+
79
+ // src/annotations/shapes.ts
80
+ var scratch = /* @__PURE__ */ new Map();
81
+ function getScratch(key, w, h) {
82
+ let c = scratch.get(key);
83
+ if (!c) {
84
+ c = document.createElement("canvas");
85
+ scratch.set(key, c);
86
+ }
87
+ if (c.width !== w) c.width = w;
88
+ if (c.height !== h) c.height = h;
89
+ return c;
90
+ }
91
+ function scratchCtx(c) {
92
+ return c.getContext("2d", { willReadFrequently: true });
93
+ }
94
+ function normRect(x, y, w, h) {
95
+ return {
96
+ x: w < 0 ? x + w : x,
97
+ y: h < 0 ? y + h : y,
98
+ w: Math.abs(w),
99
+ h: Math.abs(h)
100
+ };
101
+ }
102
+ function viewportIn(ctx) {
103
+ const t = ctx.getTransform();
104
+ const kx = Math.abs(t.a) || 1;
105
+ const ky = Math.abs(t.d) || 1;
106
+ return {
107
+ x0: -t.e / kx,
108
+ y0: -t.f / ky,
109
+ x1: (ctx.canvas.width - t.e) / kx,
110
+ y1: (ctx.canvas.height - t.f) / ky
111
+ };
112
+ }
113
+ function drawShape(ctx, shape) {
114
+ ctx.save();
115
+ ctx.lineCap = "round";
116
+ ctx.lineJoin = "round";
117
+ switch (shape.type) {
118
+ case "arrow": {
119
+ const { x1, y1, x2, y2, color, width, head } = shape;
120
+ const dx = x2 - x1;
121
+ const dy = y2 - y1;
122
+ const len = Math.hypot(dx, dy);
123
+ if (len < 0.5) break;
124
+ const ux = dx / len;
125
+ const uy = dy / len;
126
+ const nx = -uy;
127
+ const ny = ux;
128
+ const shaftMax = Math.max(2, width * 2);
129
+ const headBase = Math.max(6, width * 5);
130
+ const headLen = Math.min(Math.max(width * 6, 26), len * 0.85);
131
+ const shaftLen = len - headLen;
132
+ const halfAt = (t) => {
133
+ const p = Math.min(t, shaftLen) / Math.max(shaftLen, 1);
134
+ return shaftMax / 2 * p * p;
135
+ };
136
+ const edge = (t, side) => ({
137
+ x: x1 + ux * t + nx * halfAt(t) * side,
138
+ y: y1 + uy * t + ny * halfAt(t) * side
139
+ });
140
+ const tip = edge(0, 1);
141
+ const shaft1 = edge(shaftLen, 1);
142
+ const shaft2 = edge(shaftLen, -1);
143
+ const rootX = x1 + ux * shaftLen;
144
+ const rootY = y1 + uy * shaftLen;
145
+ const wing1 = { x: rootX + nx * (headBase / 2), y: rootY + ny * (headBase / 2) };
146
+ const wing2 = { x: rootX - nx * (headBase / 2), y: rootY - ny * (headBase / 2) };
147
+ ctx.beginPath();
148
+ ctx.moveTo(tip.x, tip.y);
149
+ ctx.lineTo(shaft1.x, shaft1.y);
150
+ ctx.lineTo(wing1.x, wing1.y);
151
+ ctx.lineTo(x2, y2);
152
+ ctx.lineTo(wing2.x, wing2.y);
153
+ ctx.lineTo(shaft2.x, shaft2.y);
154
+ ctx.closePath();
155
+ ctx.fillStyle = color;
156
+ ctx.strokeStyle = color;
157
+ ctx.lineJoin = "round";
158
+ if (head === "open") {
159
+ ctx.lineWidth = Math.max(1, width * 0.8);
160
+ ctx.stroke();
161
+ } else {
162
+ ctx.fill();
163
+ }
164
+ break;
165
+ }
166
+ case "line": {
167
+ const { x1, y1, x2, y2, color, width } = shape;
168
+ ctx.strokeStyle = color;
169
+ ctx.lineWidth = width;
170
+ ctx.beginPath();
171
+ ctx.moveTo(x1, y1);
172
+ ctx.lineTo(x2, y2);
173
+ ctx.stroke();
174
+ break;
175
+ }
176
+ case "rect": {
177
+ const r = normRect(shape.x, shape.y, shape.w, shape.h);
178
+ ctx.strokeStyle = shape.color;
179
+ ctx.lineWidth = shape.width;
180
+ ctx.strokeRect(r.x, r.y, r.w, r.h);
181
+ break;
182
+ }
183
+ case "ellipse": {
184
+ const r = normRect(shape.x, shape.y, shape.w, shape.h);
185
+ ctx.strokeStyle = shape.color;
186
+ ctx.lineWidth = shape.width;
187
+ ctx.beginPath();
188
+ ctx.ellipse(r.x + r.w / 2, r.y + r.h / 2, Math.max(r.w / 2, 0.5), Math.max(r.h / 2, 0.5), 0, 0, Math.PI * 2);
189
+ ctx.stroke();
190
+ break;
191
+ }
192
+ case "pen": {
193
+ const pts = shape.points;
194
+ if (pts.length === 0) break;
195
+ ctx.strokeStyle = shape.color;
196
+ ctx.lineWidth = shape.width;
197
+ ctx.beginPath();
198
+ ctx.moveTo(pts[0].x, pts[0].y);
199
+ if (pts.length === 1) {
200
+ ctx.lineTo(pts[0].x + 0.01, pts[0].y);
201
+ } else {
202
+ for (let i = 1; i < pts.length; i++) ctx.lineTo(pts[i].x, pts[i].y);
203
+ }
204
+ ctx.stroke();
205
+ break;
206
+ }
207
+ case "mosaic":
208
+ if (shape.mode === "region") {
209
+ mosaic(ctx, shape.x, shape.y, shape.w, shape.h);
210
+ } else if (shape.mode === "brush") {
211
+ mosaicBrush(ctx, shape.points, shape.radius);
212
+ } else {
213
+ blurRegion(ctx, shape.x, shape.y, shape.w, shape.h);
214
+ }
215
+ break;
216
+ case "counter": {
217
+ const r = 14;
218
+ ctx.beginPath();
219
+ ctx.arc(shape.x, shape.y, r, 0, Math.PI * 2);
220
+ ctx.fillStyle = shape.color;
221
+ ctx.fill();
222
+ ctx.lineWidth = 2;
223
+ ctx.strokeStyle = "rgba(255, 255, 255, 0.9)";
224
+ ctx.stroke();
225
+ ctx.fillStyle = "#ffffff";
226
+ ctx.font = 'bold 16px "Segoe UI", "Microsoft YaHei", sans-serif';
227
+ ctx.textAlign = "center";
228
+ ctx.textBaseline = "middle";
229
+ ctx.fillText(String(shape.n), shape.x, shape.y + 1);
230
+ break;
231
+ }
232
+ case "text": {
233
+ ctx.fillStyle = shape.color;
234
+ ctx.textBaseline = "top";
235
+ ctx.font = textFont(shape.size);
236
+ const lineHeight = textLineHeight(shape.size);
237
+ wrapText(ctx, shape.text, shape.size, shape.maxWidth).forEach((line, i) => {
238
+ ctx.fillText(line, shape.x, shape.y + i * lineHeight);
239
+ });
240
+ break;
241
+ }
242
+ }
243
+ ctx.restore();
244
+ }
245
+ function textFont(size) {
246
+ return `${size}px "Segoe UI", "Microsoft YaHei", sans-serif`;
247
+ }
248
+ function textLineHeight(size) {
249
+ return size * 1.3;
250
+ }
251
+ function wrapText(ctx, text, size, maxWidth) {
252
+ const paragraphs = text.split("\n");
253
+ if (!maxWidth || maxWidth <= 0) return paragraphs;
254
+ ctx.save();
255
+ ctx.font = textFont(size);
256
+ const out = [];
257
+ for (const para of paragraphs) {
258
+ let line = "";
259
+ for (const ch of para) {
260
+ const next = line + ch;
261
+ if (line && ctx.measureText(next).width > maxWidth) {
262
+ out.push(line);
263
+ line = ch;
264
+ } else {
265
+ line = next;
266
+ }
267
+ }
268
+ out.push(line);
269
+ }
270
+ ctx.restore();
271
+ return out;
272
+ }
273
+ function measureTextBlock(ctx, text, size, maxWidth) {
274
+ const lines = wrapText(ctx, text, size, maxWidth);
275
+ ctx.save();
276
+ ctx.font = textFont(size);
277
+ ctx.textBaseline = "top";
278
+ let width = 0;
279
+ for (const line of lines) {
280
+ width = Math.max(width, ctx.measureText(line).width);
281
+ }
282
+ ctx.restore();
283
+ return { width, height: lines.length * textLineHeight(size) };
284
+ }
285
+ function pixelatedRegion(ctx, rx, ry, rw, rh, blockTarget) {
286
+ const cols = Math.max(1, Math.round(rw / blockTarget));
287
+ const rows = Math.max(1, Math.round(rh / blockTarget));
288
+ const t = ctx.getTransform();
289
+ const kx = t.a || 1;
290
+ const ky = t.d || 1;
291
+ const sx = t.e + rx * kx;
292
+ const sy = t.f + ry * ky;
293
+ const snap = getScratch("snap", rw, rh);
294
+ const sctx = scratchCtx(snap);
295
+ if (!sctx) return null;
296
+ sctx.setTransform(1, 0, 0, 1, 0, 0);
297
+ sctx.clearRect(0, 0, rw, rh);
298
+ sctx.drawImage(ctx.canvas, sx, sy, rw * kx, rh * ky, 0, 0, rw, rh);
299
+ const tmp = getScratch("tmp", cols, rows);
300
+ const tctx = scratchCtx(tmp);
301
+ if (!tctx) return null;
302
+ tctx.setTransform(1, 0, 0, 1, 0, 0);
303
+ tctx.clearRect(0, 0, cols, rows);
304
+ tctx.imageSmoothingEnabled = true;
305
+ tctx.imageSmoothingQuality = "high";
306
+ tctx.drawImage(snap, 0, 0, rw, rh, 0, 0, cols, rows);
307
+ const out = getScratch("pix", rw, rh);
308
+ const octx = scratchCtx(out);
309
+ if (!octx) return null;
310
+ octx.setTransform(1, 0, 0, 1, 0, 0);
311
+ octx.clearRect(0, 0, rw, rh);
312
+ octx.imageSmoothingEnabled = false;
313
+ octx.drawImage(tmp, 0, 0, cols, rows, 0, 0, rw, rh);
314
+ return out;
315
+ }
316
+ function blurRegion(ctx, x, y, w, h) {
317
+ const r = normRect(x, y, w, h);
318
+ if (r.w < 2 || r.h < 2) return;
319
+ const radius = Math.min(40, Math.max(8, Math.round(Math.min(r.w, r.h) / 8)));
320
+ const pad = radius * 2;
321
+ const t = ctx.getTransform();
322
+ const kx = t.a || 1;
323
+ const ky = t.d || 1;
324
+ const sx = Math.max(t.e, Math.round((r.x - pad) * kx + t.e));
325
+ const sy = Math.max(t.f, Math.round((r.y - pad) * ky + t.f));
326
+ const sw = Math.min(ctx.canvas.width - sx, Math.round((r.w + pad * 2) * kx));
327
+ const sh = Math.min(ctx.canvas.height - sy, Math.round((r.h + pad * 2) * ky));
328
+ if (sw < 1 || sh < 1) return;
329
+ const snap = getScratch("blursnap", sw, sh);
330
+ const sctx = scratchCtx(snap);
331
+ if (!sctx) return;
332
+ sctx.setTransform(1, 0, 0, 1, 0, 0);
333
+ sctx.clearRect(0, 0, sw, sh);
334
+ sctx.drawImage(ctx.canvas, sx, sy, sw, sh, 0, 0, sw, sh);
335
+ ctx.save();
336
+ ctx.beginPath();
337
+ ctx.rect(r.x, r.y, r.w, r.h);
338
+ ctx.clip();
339
+ ctx.filter = `blur(${radius}px)`;
340
+ ctx.imageSmoothingEnabled = true;
341
+ ctx.drawImage(snap, 0, 0, sw, sh, (sx - t.e) / kx, (sy - t.f) / ky, sw / kx, sh / ky);
342
+ ctx.filter = "none";
343
+ ctx.restore();
344
+ }
345
+ function mosaic(ctx, x, y, w, h) {
346
+ const vp = viewportIn(ctx);
347
+ const n = normRect(x, y, w, h);
348
+ const rx = Math.max(Math.round(n.x), Math.ceil(vp.x0));
349
+ const ry = Math.max(Math.round(n.y), Math.ceil(vp.y0));
350
+ const rw = Math.min(Math.round(n.x + n.w), Math.floor(vp.x1)) - rx;
351
+ const rh = Math.min(Math.round(n.y + n.h), Math.floor(vp.y1)) - ry;
352
+ if (rw < 1 || rh < 1) return;
353
+ const blockTarget = Math.min(40, Math.max(6, Math.round(Math.min(rw, rh) / 14)));
354
+ const pix = pixelatedRegion(ctx, rx, ry, rw, rh, blockTarget);
355
+ if (!pix) return;
356
+ ctx.imageSmoothingEnabled = false;
357
+ ctx.drawImage(pix, 0, 0, rw, rh, rx, ry, rw, rh);
358
+ ctx.imageSmoothingEnabled = true;
359
+ }
360
+ function mosaicBrush(ctx, points, radius) {
361
+ if (points.length === 0) return;
362
+ const r = Math.max(2, Math.round(radius));
363
+ let minX = Infinity;
364
+ let minY = Infinity;
365
+ let maxX = -Infinity;
366
+ let maxY = -Infinity;
367
+ for (const p of points) {
368
+ if (p.x - r < minX) minX = p.x - r;
369
+ if (p.y - r < minY) minY = p.y - r;
370
+ if (p.x + r > maxX) maxX = p.x + r;
371
+ if (p.y + r > maxY) maxY = p.y + r;
372
+ }
373
+ const vp = viewportIn(ctx);
374
+ const rx = Math.max(Math.floor(minX), Math.ceil(vp.x0));
375
+ const ry = Math.max(Math.floor(minY), Math.ceil(vp.y0));
376
+ const rw = Math.min(Math.ceil(maxX), Math.floor(vp.x1)) - rx;
377
+ const rh = Math.min(Math.ceil(maxY), Math.floor(vp.y1)) - ry;
378
+ if (rw < 1 || rh < 1) return;
379
+ const pix = pixelatedRegion(ctx, rx, ry, rw, rh, Math.max(4, r));
380
+ if (!pix) return;
381
+ ctx.imageSmoothingEnabled = false;
382
+ for (const p of points) {
383
+ ctx.save();
384
+ ctx.beginPath();
385
+ ctx.arc(p.x, p.y, r, 0, Math.PI * 2);
386
+ ctx.clip();
387
+ ctx.drawImage(pix, 0, 0, rw, rh, rx, ry, rw, rh);
388
+ ctx.restore();
389
+ }
390
+ ctx.imageSmoothingEnabled = true;
391
+ }
392
+ function drawDraftRect(ctx, x, y, w, h) {
393
+ const r = normRect(x, y, w, h);
394
+ ctx.save();
395
+ ctx.fillStyle = "rgba(255, 255, 255, 0.28)";
396
+ ctx.strokeStyle = "#ffffff";
397
+ ctx.lineWidth = 1;
398
+ ctx.fillRect(r.x, r.y, r.w, r.h);
399
+ ctx.strokeRect(r.x + 0.5, r.y + 0.5, Math.max(0, r.w - 1), Math.max(0, r.h - 1));
400
+ ctx.restore();
401
+ }
402
+ function cloneShape(shape) {
403
+ if (shape.type === "pen") {
404
+ return { ...shape, points: shape.points.map((p) => ({ ...p })) };
405
+ }
406
+ return { ...shape };
407
+ }
408
+ function shapeBBox(shape, ctx) {
409
+ if (shape.type === "text") {
410
+ const m = measureTextBlock(ctx, shape.text, shape.size, shape.maxWidth);
411
+ return { x: shape.x, y: shape.y, width: m.width, height: m.height };
412
+ }
413
+ if (shape.type === "counter") {
414
+ return { x: shape.x - 14, y: shape.y - 14, width: 28, height: 28 };
415
+ }
416
+ if (shape.type === "line") {
417
+ return {
418
+ x: Math.min(shape.x1, shape.x2),
419
+ y: Math.min(shape.y1, shape.y2),
420
+ width: Math.abs(shape.x2 - shape.x1),
421
+ height: Math.abs(shape.y2 - shape.y1)
422
+ };
423
+ }
424
+ if (shape.type === "pen") {
425
+ let minX = Infinity;
426
+ let minY = Infinity;
427
+ let maxX = -Infinity;
428
+ let maxY = -Infinity;
429
+ for (const p of shape.points) {
430
+ if (p.x < minX) minX = p.x;
431
+ if (p.y < minY) minY = p.y;
432
+ if (p.x > maxX) maxX = p.x;
433
+ if (p.y > maxY) maxY = p.y;
434
+ }
435
+ if (!Number.isFinite(minX)) return { x: 0, y: 0, width: 0, height: 0 };
436
+ return { x: minX, y: minY, width: maxX - minX, height: maxY - minY };
437
+ }
438
+ if (shape.type === "arrow") {
439
+ return {
440
+ x: Math.min(shape.x1, shape.x2),
441
+ y: Math.min(shape.y1, shape.y2),
442
+ width: Math.abs(shape.x2 - shape.x1),
443
+ height: Math.abs(shape.y2 - shape.y1)
444
+ };
445
+ }
446
+ if (shape.type === "mosaic") {
447
+ if (shape.mode === "region" || shape.mode === "blur") {
448
+ const r2 = normRect(shape.x, shape.y, shape.w, shape.h);
449
+ return { x: r2.x, y: r2.y, width: r2.w, height: r2.h };
450
+ }
451
+ let minX = Infinity;
452
+ let minY = Infinity;
453
+ let maxX = -Infinity;
454
+ let maxY = -Infinity;
455
+ for (const p of shape.points) {
456
+ if (p.x < minX) minX = p.x;
457
+ if (p.y < minY) minY = p.y;
458
+ if (p.x > maxX) maxX = p.x;
459
+ if (p.y > maxY) maxY = p.y;
460
+ }
461
+ if (!Number.isFinite(minX)) return { x: 0, y: 0, width: 0, height: 0 };
462
+ const rr = shape.radius;
463
+ return {
464
+ x: minX - rr,
465
+ y: minY - rr,
466
+ width: maxX - minX + rr * 2,
467
+ height: maxY - minY + rr * 2
468
+ };
469
+ }
470
+ const r = normRect(shape.x, shape.y, shape.w, shape.h);
471
+ return { x: r.x, y: r.y, width: r.w, height: r.h };
472
+ }
473
+ function canResize(shape) {
474
+ return shape.type === "rect" || shape.type === "ellipse" || shape.type === "arrow" || shape.type === "line";
475
+ }
476
+ function shapeHandlePoints(shape, ctx) {
477
+ if (shape.type === "arrow") {
478
+ return [
479
+ { id: "p1", x: shape.x1, y: shape.y1 },
480
+ { id: "p2", x: shape.x2, y: shape.y2 }
481
+ ];
482
+ }
483
+ if (shape.type === "line") {
484
+ return [
485
+ { id: "p1", x: shape.x1, y: shape.y1 },
486
+ { id: "p2", x: shape.x2, y: shape.y2 }
487
+ ];
488
+ }
489
+ if (!canResize(shape)) return [];
490
+ const box = shapeBBox(shape, ctx);
491
+ return HANDLE_IDS.map((id) => ({ id, ...handlePoint(box, id) }));
492
+ }
493
+ function applyShapeHandle(origin, handle, cursor, ctx) {
494
+ if (origin.type === "arrow" || origin.type === "line") {
495
+ return handle === "p1" ? { ...origin, x1: cursor.x, y1: cursor.y } : { ...origin, x2: cursor.x, y2: cursor.y };
496
+ }
497
+ const box = shapeBBox(origin, ctx);
498
+ const next = resizeRect(box, handle, cursor);
499
+ if (origin.type === "rect" || origin.type === "ellipse") {
500
+ return { ...origin, x: next.x, y: next.y, w: next.width, h: next.height };
501
+ }
502
+ return cloneShape(origin);
503
+ }
504
+ function isShapeHit(canvas, shape, p, tol) {
505
+ if (shape.type === "mosaic") {
506
+ if (shape.mode !== "brush") {
507
+ const r = normRect(shape.x, shape.y, shape.w, shape.h);
508
+ return p.x >= r.x && p.x <= r.x + r.w && p.y >= r.y && p.y <= r.y + r.h;
509
+ }
510
+ const rr = shape.radius + tol;
511
+ return shape.points.some((q) => Math.hypot(q.x - p.x, q.y - p.y) <= rr);
512
+ }
513
+ const x0 = Math.max(0, Math.floor(p.x - tol));
514
+ const y0 = Math.max(0, Math.floor(p.y - tol));
515
+ const x1 = Math.min(canvas.width, Math.ceil(p.x + tol) + 1);
516
+ const y1 = Math.min(canvas.height, Math.ceil(p.y + tol) + 1);
517
+ const w = x1 - x0;
518
+ const h = y1 - y0;
519
+ if (w <= 0 || h <= 0) return false;
520
+ const hit = getScratch("hit", w, h);
521
+ const hctx = scratchCtx(hit);
522
+ if (!hctx) return false;
523
+ hctx.setTransform(1, 0, 0, 1, 0, 0);
524
+ hctx.clearRect(0, 0, w, h);
525
+ hctx.setTransform(1, 0, 0, 1, -x0, -y0);
526
+ drawShape(hctx, shape);
527
+ hctx.setTransform(1, 0, 0, 1, 0, 0);
528
+ const { data } = hctx.getImageData(0, 0, w, h);
529
+ for (let i = 3; i < data.length; i += 4) {
530
+ if (data[i] !== 0) return true;
531
+ }
532
+ return false;
533
+ }
534
+ function drawSelection(ctx, shape, unit) {
535
+ const box = shapeBBox(shape, ctx);
536
+ ctx.save();
537
+ ctx.lineWidth = 1;
538
+ if (shape.type === "arrow" || shape.type === "line") {
539
+ ctx.fillStyle = "#ffffff";
540
+ ctx.strokeStyle = "rgba(0, 0, 0, 0.85)";
541
+ for (const p of [
542
+ { x: shape.x1, y: shape.y1 },
543
+ { x: shape.x2, y: shape.y2 }
544
+ ]) {
545
+ ctx.beginPath();
546
+ ctx.arc(p.x, p.y, Math.max(3, unit * 0.6), 0, Math.PI * 2);
547
+ ctx.fill();
548
+ ctx.stroke();
549
+ }
550
+ } else if (canResize(shape)) {
551
+ ctx.fillStyle = "#ffffff";
552
+ ctx.strokeStyle = "rgba(0, 0, 0, 0.85)";
553
+ for (const id of HANDLE_IDS) {
554
+ const hp = handlePoint(box, id);
555
+ ctx.fillRect(hp.x - unit / 2, hp.y - unit / 2, unit, unit);
556
+ ctx.strokeRect(hp.x - unit / 2 + 0.5, hp.y - unit / 2 + 0.5, unit - 1, unit - 1);
557
+ }
558
+ } else {
559
+ ctx.setLineDash([4, 4]);
560
+ ctx.strokeStyle = "rgba(47, 111, 237, 0.95)";
561
+ ctx.strokeRect(box.x - 0.5, box.y - 0.5, box.width + 1, box.height + 1);
562
+ ctx.setLineDash([]);
563
+ }
564
+ ctx.restore();
565
+ }
566
+ function translateShape(shape, dx, dy) {
567
+ if (shape.type === "arrow" || shape.type === "line") {
568
+ return {
569
+ ...shape,
570
+ x1: shape.x1 + dx,
571
+ y1: shape.y1 + dy,
572
+ x2: shape.x2 + dx,
573
+ y2: shape.y2 + dy
574
+ };
575
+ }
576
+ if (shape.type === "pen") {
577
+ return {
578
+ ...shape,
579
+ points: shape.points.map((p) => ({ x: p.x + dx, y: p.y + dy }))
580
+ };
581
+ }
582
+ if (shape.type === "mosaic") {
583
+ if (shape.mode !== "brush") {
584
+ return { ...shape, x: shape.x + dx, y: shape.y + dy };
585
+ }
586
+ return {
587
+ ...shape,
588
+ points: shape.points.map((p) => ({ x: p.x + dx, y: p.y + dy }))
589
+ };
590
+ }
591
+ return { ...shape, x: shape.x + dx, y: shape.y + dy };
592
+ }
593
+ function withColor(shape, color) {
594
+ if (shape.type === "mosaic") return shape;
595
+ return { ...shape, color };
596
+ }
597
+ function drawDraftPath(ctx, points, radius) {
598
+ if (points.length === 0) return;
599
+ ctx.save();
600
+ ctx.strokeStyle = "rgba(255, 255, 255, 0.28)";
601
+ ctx.lineWidth = Math.max(4, radius * 2);
602
+ ctx.lineCap = "round";
603
+ ctx.lineJoin = "round";
604
+ ctx.beginPath();
605
+ ctx.moveTo(points[0].x, points[0].y);
606
+ for (let i = 1; i < points.length; i++) {
607
+ ctx.lineTo(points[i].x, points[i].y);
608
+ }
609
+ if (points.length === 1) ctx.lineTo(points[0].x + 0.01, points[0].y);
610
+ ctx.stroke();
611
+ ctx.restore();
612
+ }
613
+
614
+ // src/annotations/tools.ts
615
+ var ROW2_BY_TOOL = {
616
+ hand: [],
617
+ arrow: ["head", "width", "color"],
618
+ line: ["width", "color"],
619
+ rect: ["width", "color"],
620
+ ellipse: ["width", "color"],
621
+ pen: ["width", "color"],
622
+ marker: ["width", "color"],
623
+ text: ["size", "color"],
624
+ mosaic: ["mode"],
625
+ counter: ["color"]
626
+ };
627
+ function toolUsesWidth(tool, mosaicMode) {
628
+ return (ROW2_BY_TOOL[tool] ?? []).includes("width") || tool === "mosaic" && mosaicMode === "brush";
629
+ }
630
+
631
+ // src/annotations/editor.ts
632
+ var COLORS = ["#ff3b30", "#ff9500", "#ffcc00", "#34c759", "#2f6fed", "#af52de", "#ffffff", "#000000"];
633
+ var WIDTH_MIN = 1;
634
+ var WIDTH_MAX = 20;
635
+ var ARROW_HEADS = ["solid", "open"];
636
+ function squareExtent(dx, dy) {
637
+ const side = Math.max(Math.abs(dx), Math.abs(dy));
638
+ return [dx < 0 ? -side : side, dy < 0 ? -side : side];
639
+ }
640
+ var FONT_SIZES = [16, 24, 32, 48];
641
+ var MOSAIC_MODES = ["region", "brush"];
642
+ var Editor = class {
643
+ canvas;
644
+ toolIds = [
645
+ "hand",
646
+ "arrow",
647
+ "line",
648
+ "rect",
649
+ "ellipse",
650
+ "pen",
651
+ "marker",
652
+ "mosaic",
653
+ "counter",
654
+ "text"
655
+ ];
656
+ ctx;
657
+ textEditor;
658
+ opts;
659
+ /** 已提交的标注(源数据,唯一真相) */
660
+ shapes = [];
661
+ /** 撤销栈 / 重做栈:存整份快照。加、删、清空、改色、移动共用一套历史,
662
+ * 不用为每种操作各写一遍反向命令。 */
663
+ past = [];
664
+ future = [];
665
+ draft = null;
666
+ selected = null;
667
+ /** 正在拖动的图形副本;落盘前 `shapes` 里仍是原对象 */
668
+ editing = null;
669
+ editingOf = null;
670
+ /** 结束当前手势的收尾函数(在 attach 里绑定 pointerup 的同一个闭包) */
671
+ endGesture = () => {
672
+ };
673
+ editOrigin = null;
674
+ editStart = null;
675
+ editHandle = null;
676
+ current = "hand";
677
+ color = COLORS[0];
678
+ width = 4;
679
+ arrowHead = "solid";
680
+ fontSize = FONT_SIZES[0];
681
+ mosaicMode = "region";
682
+ penPoints = [];
683
+ mode = null;
684
+ /** 图像像素 / 屏幕像素,命中阈值与手柄尺寸按它放大 */
685
+ pxScale = 1;
686
+ /** 当前活跃的文字输入框提交函数;同时只允许一个 */
687
+ closeActiveText = null;
688
+ /** 已 setPointerCapture 的指针;null = 未捕获 */
689
+ capturedPointer = null;
690
+ onToolChange;
691
+ /**
692
+ * 工具属性(颜色 / 线宽 / 箭头头部 / 字号 / 马赛克模式)变化。录制控制条用:
693
+ * 它的工具条挂在另一块屏的窗口里,属性变了要下发给标注层。
694
+ */
695
+ onAttrsChange;
696
+ /** 每次撤销 / 重做 / 新增 / 删除 / 清空 / 改色 / 落盘调整后触发 */
697
+ onHistoryChange;
698
+ /**
699
+ * 拖画中的草稿变化(含结束时的 null)。遮罩用它把草稿实时投影到
700
+ * 其余屏 —— 否则标注画过屏幕边界时,另一块屏上永远看不到。
701
+ */
702
+ onDraftChange;
703
+ /**
704
+ * 拖动 / 缩放**既有**标注的在途状态:开始时给它在已提交列表里的下标,
705
+ * 结束时给 null。宿主据此先广播「去掉它」的快照、再把拖动副本当草稿
706
+ * 投影 —— 否则其余屏在拖动期间会旧位、新位叠着画两份。
707
+ */
708
+ onInFlightChange;
709
+ constructor(canvas, textEditor, opts) {
710
+ this.canvas = canvas;
711
+ this.textEditor = textEditor;
712
+ this.opts = opts;
713
+ const ctx = canvas.getContext("2d");
714
+ if (!ctx) throw new Error("\u65E0\u6CD5\u521B\u5EFA canvas 2d \u4E0A\u4E0B\u6587");
715
+ this.ctx = ctx;
716
+ this.attach();
717
+ }
718
+ /** 结束进行中的手势(画图 / 拖动 / 调整)。宿主把按下交给自己处理时调用,避免状态残留。 */
719
+ resetGesture() {
720
+ this.endGesture();
721
+ }
722
+ /** 当前已提交标注的快照(深拷贝),遮罩每次历史变化回传给主进程用。 */
723
+ getShapes() {
724
+ return this.snapshot();
725
+ }
726
+ /** 直接把标注层画进宿主的画布(遮罩用:它自己负责底图 / 遮罩 / 选框)。 */
727
+ renderInto(ctx) {
728
+ for (const shape of this.shapes) {
729
+ if (shape === this.editingOf) continue;
730
+ drawShape(ctx, shape);
731
+ }
732
+ if (this.editing) drawShape(ctx, this.editing);
733
+ if (this.draft) {
734
+ if (this.draft.type === "mosaic") {
735
+ if (this.draft.mode === "brush") {
736
+ drawDraftPath(ctx, this.draft.points, this.draft.radius);
737
+ } else {
738
+ drawDraftRect(ctx, this.draft.x, this.draft.y, this.draft.w, this.draft.h);
739
+ }
740
+ } else if (this.draft.type !== "text") {
741
+ drawShape(ctx, this.draft);
742
+ }
743
+ }
744
+ const focus = this.editing ?? this.selected;
745
+ if (focus) drawSelection(ctx, focus, HANDLE_SIZE_UNIT * this.pxScale);
746
+ }
747
+ get currentTool() {
748
+ return this.current;
749
+ }
750
+ get colorValue() {
751
+ return this.color;
752
+ }
753
+ get widthValue() {
754
+ return this.width;
755
+ }
756
+ get arrowHeads() {
757
+ return ARROW_HEADS;
758
+ }
759
+ get arrowHeadValue() {
760
+ return this.arrowHead;
761
+ }
762
+ get fontSizes() {
763
+ return FONT_SIZES;
764
+ }
765
+ get fontSizeValue() {
766
+ return this.fontSize;
767
+ }
768
+ get mosaicModes() {
769
+ return MOSAIC_MODES;
770
+ }
771
+ get mosaicModeValue() {
772
+ return this.mosaicMode;
773
+ }
774
+ get canUndo() {
775
+ return this.past.length > 0;
776
+ }
777
+ get canRedo() {
778
+ return this.future.length > 0;
779
+ }
780
+ get hasSelection() {
781
+ return this.selected !== null;
782
+ }
783
+ // ------------------------------------------------------------ 历史
784
+ snapshot() {
785
+ return this.shapes.map(cloneShape);
786
+ }
787
+ /** **变更之前**调用:把当前状态压进撤销栈并清空重做栈。 */
788
+ beginChange() {
789
+ this.past.push(this.snapshot());
790
+ this.future = [];
791
+ }
792
+ commit(shape) {
793
+ this.beginChange();
794
+ this.shapes.push(shape);
795
+ this.onHistoryChange?.();
796
+ }
797
+ undo() {
798
+ const prev = this.past.pop();
799
+ if (!prev) return;
800
+ this.future.push(this.snapshot());
801
+ this.shapes = prev;
802
+ this.selected = null;
803
+ this.onHistoryChange?.();
804
+ this.render();
805
+ }
806
+ redo() {
807
+ const next = this.future.pop();
808
+ if (!next) return;
809
+ this.past.push(this.snapshot());
810
+ this.shapes = next;
811
+ this.selected = null;
812
+ this.onHistoryChange?.();
813
+ this.render();
814
+ }
815
+ /** 清空也走历史:Ctrl+Z 一次性全拿回来 */
816
+ clear() {
817
+ if (this.shapes.length === 0) return;
818
+ this.beginChange();
819
+ this.shapes = [];
820
+ this.selected = null;
821
+ if (this.draft) this.onDraftChange?.(null);
822
+ this.draft = null;
823
+ this.penPoints = [];
824
+ this.onHistoryChange?.();
825
+ this.render();
826
+ }
827
+ /** 删除当前选中的那条标注(可撤销)。 */
828
+ deleteSelected() {
829
+ const target = this.selected;
830
+ if (!target) return;
831
+ this.beginChange();
832
+ this.shapes = this.shapes.filter((s) => s !== target);
833
+ this.selected = null;
834
+ this.onHistoryChange?.();
835
+ this.render();
836
+ console.log(`[editor] \u5220\u9664\u6807\u6CE8 \u5269\u4F59=${this.shapes.length}`);
837
+ }
838
+ // ------------------------------------------------------------ 选中
839
+ select(shape) {
840
+ if (this.selected === shape) return;
841
+ this.selected = shape;
842
+ this.render();
843
+ }
844
+ hitTolerance() {
845
+ return Math.max(6, HANDLE_HIT_DIP * 0.75 * this.pxScale);
846
+ }
847
+ /** 从最上层往下找光标落在哪条标注上。 */
848
+ hitTest(p) {
849
+ const tol = this.hitTolerance();
850
+ for (let i = this.shapes.length - 1; i >= 0; i--) {
851
+ const shape = this.shapes[i];
852
+ if (shape === this.editingOf) continue;
853
+ if (isShapeHit(this.canvas, shape, p, tol)) return shape;
854
+ }
855
+ return null;
856
+ }
857
+ hitSelectedHandle(p) {
858
+ const target = this.editing ?? this.selected;
859
+ if (!target) return null;
860
+ const tol = HANDLE_HIT_DIP * this.pxScale;
861
+ for (const h of shapeHandlePoints(target, this.ctx)) {
862
+ if (Math.hypot(h.x - p.x, h.y - p.y) <= tol) return h.id;
863
+ }
864
+ return null;
865
+ }
866
+ // ------------------------------------------------------------ 坐标
867
+ toImage(event) {
868
+ const rect = this.canvas.getBoundingClientRect();
869
+ const sx = this.opts.coords === "css" || rect.width <= 0 ? 1 : this.canvas.width / rect.width;
870
+ const sy = this.opts.coords === "css" || rect.height <= 0 ? 1 : this.canvas.height / rect.height;
871
+ this.pxScale = sx;
872
+ return {
873
+ x: (event.clientX - rect.left) * sx,
874
+ y: (event.clientY - rect.top) * sy
875
+ };
876
+ }
877
+ // ------------------------------------------------------------ 渲染
878
+ render() {
879
+ this.opts.repaint();
880
+ }
881
+ // ------------------------------------------------------------ 工具 / 属性
882
+ setTool(tool) {
883
+ this.current = tool;
884
+ this.resetInteraction();
885
+ this.selected = null;
886
+ this.updateCursor();
887
+ this.onToolChange?.(tool);
888
+ this.render();
889
+ }
890
+ setColor(color) {
891
+ this.color = color;
892
+ const target = this.selected;
893
+ if (target && target.type !== "mosaic") {
894
+ const idx = this.shapes.indexOf(target);
895
+ const next = withColor(target, color);
896
+ if (idx >= 0 && next !== target) {
897
+ this.beginChange();
898
+ this.shapes[idx] = next;
899
+ this.selected = next;
900
+ this.onHistoryChange?.();
901
+ console.log(`[editor] \u6539\u8272 index=${idx} color=${color}`);
902
+ }
903
+ }
904
+ this.onAttrsChange?.();
905
+ this.render();
906
+ }
907
+ /** 换箭头头部样式;选中箭头时同步改这一条(与 setColor 同语义) */
908
+ setArrowHead(head) {
909
+ this.arrowHead = head;
910
+ const target = this.selected;
911
+ if (target && target.type === "arrow" && target.head !== head) {
912
+ const idx = this.shapes.indexOf(target);
913
+ if (idx >= 0) {
914
+ this.beginChange();
915
+ const next = { ...target, head };
916
+ this.shapes[idx] = next;
917
+ this.selected = next;
918
+ this.onHistoryChange?.();
919
+ console.log(`[editor] \u6362\u7BAD\u5934\u5934\u90E8 index=${idx} head=${head}`);
920
+ }
921
+ }
922
+ this.onAttrsChange?.();
923
+ this.render();
924
+ }
925
+ /** 换字号;选中文字时同步改这一条 */
926
+ setFontSize(size) {
927
+ this.fontSize = size;
928
+ const target = this.selected;
929
+ if (target && target.type === "text" && target.size !== size) {
930
+ const idx = this.shapes.indexOf(target);
931
+ if (idx >= 0) {
932
+ this.beginChange();
933
+ const next = { ...target, size };
934
+ this.shapes[idx] = next;
935
+ this.selected = next;
936
+ this.onHistoryChange?.();
937
+ console.log(`[editor] \u6539\u5B57\u53F7 index=${idx} size=${size}`);
938
+ }
939
+ }
940
+ this.onAttrsChange?.();
941
+ this.render();
942
+ }
943
+ /** 切换马赛克绘制形式 —— 纯工具设置,不进历史 */
944
+ setMosaicMode(mode) {
945
+ if (this.mosaicMode === mode) return;
946
+ this.mosaicMode = mode;
947
+ this.onAttrsChange?.();
948
+ this.render();
949
+ }
950
+ setWidth(width) {
951
+ this.width = width;
952
+ this.onAttrsChange?.();
953
+ }
954
+ updateCursor() {
955
+ this.canvas.style.cursor = "crosshair";
956
+ }
957
+ /** 只针对**已选中**那条:手柄给调整光标、图形本体给移动光标。 */
958
+ hoverCursor(event) {
959
+ const override = this.opts.cursorOverride?.(event);
960
+ if (override) {
961
+ this.canvas.style.cursor = override;
962
+ return;
963
+ }
964
+ if (!this.selected) {
965
+ this.updateCursor();
966
+ return;
967
+ }
968
+ const p = this.toImage(event);
969
+ const handle = this.hitSelectedHandle(p);
970
+ if (handle) {
971
+ this.canvas.style.cursor = handle === "p1" || handle === "p2" ? "crosshair" : HANDLE_CURSOR[handle];
972
+ return;
973
+ }
974
+ if (isShapeHit(this.canvas, this.selected, p, this.hitTolerance())) {
975
+ this.canvas.style.cursor = "move";
976
+ return;
977
+ }
978
+ this.updateCursor();
979
+ }
980
+ resetInteraction() {
981
+ if (this.draft) this.onDraftChange?.(null);
982
+ this.mode = null;
983
+ this.draft = null;
984
+ this.penPoints = [];
985
+ this.editing = null;
986
+ this.editingOf = null;
987
+ this.editOrigin = null;
988
+ this.editStart = null;
989
+ this.editHandle = null;
990
+ }
991
+ /**
992
+ * 按下后捕获指针:多屏截图时每块屏是**独立窗口**,不捕获的话指针一移进
993
+ * 邻屏窗口,本窗口就再也收不到 pointermove / pointerup —— 草稿停在屏幕
994
+ * 边界,松手落空。捕获后 OS 会把整次拖动的鼠标消息都路由给本窗口,
995
+ * 坐标出界(负值或超出窗口宽高)也照常送达。
996
+ */
997
+ capturePointer(event) {
998
+ try {
999
+ this.canvas.setPointerCapture(event.pointerId);
1000
+ this.capturedPointer = event.pointerId;
1001
+ } catch {
1002
+ this.capturedPointer = null;
1003
+ }
1004
+ }
1005
+ releasePointer() {
1006
+ if (this.capturedPointer === null) return;
1007
+ try {
1008
+ this.canvas.releasePointerCapture(this.capturedPointer);
1009
+ } catch {
1010
+ }
1011
+ this.capturedPointer = null;
1012
+ }
1013
+ /** 拖动 / 缩放开始:把在途标注的下标交给宿主(遮罩据此投影到其余屏)。 */
1014
+ beginInFlight(shape) {
1015
+ const idx = this.shapes.indexOf(shape);
1016
+ if (idx >= 0) this.onInFlightChange?.(idx);
1017
+ }
1018
+ // ------------------------------------------------------------ 指针
1019
+ attach() {
1020
+ const { canvas } = this;
1021
+ canvas.addEventListener("pointerdown", (event) => {
1022
+ if (event.button !== 0) return;
1023
+ if (this.mode) this.endGesture();
1024
+ const p = this.toImage(event);
1025
+ const handle = this.selected ? this.hitSelectedHandle(p) : null;
1026
+ if (handle && this.selected) {
1027
+ this.mode = "handle";
1028
+ this.capturePointer(event);
1029
+ this.editingOf = this.selected;
1030
+ this.editOrigin = cloneShape(this.selected);
1031
+ this.editStart = p;
1032
+ this.editHandle = handle;
1033
+ this.editing = cloneShape(this.selected);
1034
+ this.beginInFlight(this.selected);
1035
+ return;
1036
+ }
1037
+ const hit = this.hitTest(p);
1038
+ if (hit) {
1039
+ this.select(hit);
1040
+ this.mode = "move";
1041
+ this.capturePointer(event);
1042
+ this.editingOf = hit;
1043
+ this.editOrigin = cloneShape(hit);
1044
+ this.editStart = p;
1045
+ this.editHandle = null;
1046
+ this.editing = cloneShape(hit);
1047
+ this.beginInFlight(hit);
1048
+ this.render();
1049
+ return;
1050
+ }
1051
+ this.select(null);
1052
+ if (this.current === "text") {
1053
+ event.preventDefault();
1054
+ const hitText = this.hitTest(p);
1055
+ if (hitText && hitText.type === "text") {
1056
+ this.select(hitText);
1057
+ this.openTextEditor(event, hitText);
1058
+ return;
1059
+ }
1060
+ this.openTextEditor(event);
1061
+ return;
1062
+ }
1063
+ if (this.current === "counter") {
1064
+ this.commit({
1065
+ type: "counter",
1066
+ x: p.x,
1067
+ y: p.y,
1068
+ n: this.shapes.filter((s) => s.type === "counter").length + 1,
1069
+ color: this.color
1070
+ });
1071
+ this.render();
1072
+ return;
1073
+ }
1074
+ this.mode = "draw";
1075
+ this.capturePointer(event);
1076
+ this.penPoints = [p];
1077
+ if (this.current === "arrow") {
1078
+ this.draft = {
1079
+ type: "arrow",
1080
+ x1: p.x,
1081
+ y1: p.y,
1082
+ x2: p.x,
1083
+ y2: p.y,
1084
+ color: this.color,
1085
+ width: this.width,
1086
+ head: this.arrowHead
1087
+ };
1088
+ } else if (this.current === "line") {
1089
+ this.draft = {
1090
+ type: "line",
1091
+ x1: p.x,
1092
+ y1: p.y,
1093
+ x2: p.x,
1094
+ y2: p.y,
1095
+ color: this.color,
1096
+ width: this.width
1097
+ };
1098
+ } else if (this.current === "pen") {
1099
+ this.draft = {
1100
+ type: "pen",
1101
+ points: [p],
1102
+ color: this.color,
1103
+ width: this.width
1104
+ };
1105
+ } else if (this.current === "marker") {
1106
+ this.draft = {
1107
+ type: "pen",
1108
+ points: [p],
1109
+ color: `${this.color}59`,
1110
+ width: Math.max(12, this.width * 3)
1111
+ };
1112
+ } else if (this.current === "mosaic") {
1113
+ this.draft = this.mosaicMode === "brush" ? {
1114
+ type: "mosaic",
1115
+ mode: "brush",
1116
+ points: [p],
1117
+ radius: Math.max(2, this.width)
1118
+ } : {
1119
+ // 选区 / 高斯模糊:都是拖矩形
1120
+ type: "mosaic",
1121
+ mode: this.mosaicMode,
1122
+ x: p.x,
1123
+ y: p.y,
1124
+ w: 0,
1125
+ h: 0
1126
+ };
1127
+ } else {
1128
+ this.draft = {
1129
+ type: this.current === "rect" ? "rect" : "ellipse",
1130
+ x: p.x,
1131
+ y: p.y,
1132
+ w: 0,
1133
+ h: 0,
1134
+ color: this.color,
1135
+ width: this.width
1136
+ };
1137
+ }
1138
+ this.render();
1139
+ });
1140
+ window.addEventListener("pointermove", (event) => {
1141
+ if (!this.mode) {
1142
+ this.hoverCursor(event);
1143
+ return;
1144
+ }
1145
+ if (event.buttons === 0) {
1146
+ this.endGesture();
1147
+ this.hoverCursor(event);
1148
+ return;
1149
+ }
1150
+ const p = this.toImage(event);
1151
+ if (this.mode === "move") {
1152
+ if (!this.editing || !this.editOrigin || !this.editStart) return;
1153
+ this.editing = translateShape(this.editOrigin, p.x - this.editStart.x, p.y - this.editStart.y);
1154
+ this.render();
1155
+ this.onDraftChange?.(this.editing);
1156
+ return;
1157
+ }
1158
+ if (this.mode === "handle") {
1159
+ if (!this.editing || !this.editOrigin || !this.editHandle) return;
1160
+ this.editing = applyShapeHandle(this.editOrigin, this.editHandle, p, this.ctx);
1161
+ this.render();
1162
+ this.onDraftChange?.(this.editing);
1163
+ return;
1164
+ }
1165
+ if (!this.draft) return;
1166
+ if (this.draft.type === "arrow" || this.draft.type === "line") {
1167
+ this.draft.x2 = p.x;
1168
+ this.draft.y2 = p.y;
1169
+ } else if (this.draft.type === "pen") {
1170
+ this.penPoints.push(p);
1171
+ this.draft.points = event.shiftKey ? [this.penPoints[0], p] : [...this.penPoints];
1172
+ } else if (this.draft.type === "rect" || this.draft.type === "ellipse") {
1173
+ const dx = p.x - this.draft.x;
1174
+ const dy = p.y - this.draft.y;
1175
+ const [w, h] = event.shiftKey ? squareExtent(dx, dy) : [dx, dy];
1176
+ this.draft.w = w;
1177
+ this.draft.h = h;
1178
+ } else if (this.draft.type === "mosaic") {
1179
+ if (this.draft.mode === "brush") {
1180
+ this.penPoints.push(p);
1181
+ this.draft.points = [...this.penPoints];
1182
+ } else {
1183
+ const start = this.penPoints[0];
1184
+ this.draft.w = p.x - start.x;
1185
+ this.draft.h = p.y - start.y;
1186
+ }
1187
+ }
1188
+ this.render();
1189
+ this.onDraftChange?.(this.draft);
1190
+ });
1191
+ const finish = () => {
1192
+ const mode = this.mode;
1193
+ if (!mode) return;
1194
+ this.releasePointer();
1195
+ if (mode === "draw") {
1196
+ this.mode = null;
1197
+ const finished = this.draft;
1198
+ this.draft = null;
1199
+ this.penPoints = [];
1200
+ this.onDraftChange?.(null);
1201
+ if (finished && !this.tooSmall(finished)) {
1202
+ this.commit(finished);
1203
+ console.log(`[editor] \u65B0\u589E\u6807\u6CE8 type=${finished.type} \u603B\u6570=${this.shapes.length}`);
1204
+ }
1205
+ this.render();
1206
+ return;
1207
+ }
1208
+ this.mode = null;
1209
+ const changed = this.editing && this.editOrigin && JSON.stringify(this.editing) !== JSON.stringify(this.editOrigin);
1210
+ this.onDraftChange?.(null);
1211
+ this.onInFlightChange?.(null);
1212
+ if (changed && this.editingOf && this.editing) {
1213
+ this.beginChange();
1214
+ const idx = this.shapes.indexOf(this.editingOf);
1215
+ if (idx >= 0) this.shapes[idx] = this.editing;
1216
+ console.log(`[editor] \u8C03\u6574\u6807\u6CE8 index=${idx} type=${this.editing.type}`);
1217
+ this.onHistoryChange?.();
1218
+ }
1219
+ this.selected = null;
1220
+ this.editing = null;
1221
+ this.editingOf = null;
1222
+ this.editOrigin = null;
1223
+ this.editStart = null;
1224
+ this.editHandle = null;
1225
+ this.render();
1226
+ };
1227
+ window.addEventListener("pointerup", finish);
1228
+ window.addEventListener("pointercancel", finish);
1229
+ window.addEventListener("blur", finish);
1230
+ this.endGesture = finish;
1231
+ canvas.addEventListener("wheel", (event) => this.onWheel(event), { passive: false });
1232
+ }
1233
+ /** 滚轮调线宽的步长累计:离散滚轮一格 ±100,触板的连续小步长攒够 100 走一步 */
1234
+ wheelAccum = 0;
1235
+ /**
1236
+ * 滚轮调线宽:吃线宽的工具上(与工具条滑杆同一份 `toolUsesWidth` 判定),
1237
+ * 向上滚加粗、向下滚变细,实时经 onAttrsChange 回填滑杆。
1238
+ *
1239
+ * 接管时 preventDefault + stopPropagation —— 贴图编辑模式的缩放 / 透明度
1240
+ * 挂在 window 层,canvas 先于 window 触发,拦在到达之前。**两类事件放行**:
1241
+ * 不吃线宽的工具(含手型),以及带 Ctrl/Cmd 修饰的滚轮 —— 后者在贴图上
1242
+ * 是既定的「调透明度」交互,不能被线宽劫持。
1243
+ */
1244
+ onWheel(event) {
1245
+ if (event.ctrlKey || event.metaKey) return;
1246
+ if (!toolUsesWidth(this.current, this.mosaicMode)) return;
1247
+ event.preventDefault();
1248
+ event.stopPropagation();
1249
+ this.wheelAccum += event.deltaY;
1250
+ while (Math.abs(this.wheelAccum) >= 100) {
1251
+ const up = this.wheelAccum < 0;
1252
+ const step = up ? 1 : -1;
1253
+ this.wheelAccum += up ? 100 : -100;
1254
+ const next = Math.min(this.widthMax, Math.max(this.widthMin, this.width + step));
1255
+ if (next === this.width) {
1256
+ this.wheelAccum = 0;
1257
+ break;
1258
+ }
1259
+ this.setWidth(next);
1260
+ }
1261
+ }
1262
+ tooSmall(shape) {
1263
+ if (shape.type === "arrow") {
1264
+ return Math.hypot(shape.x2 - shape.x1, shape.y2 - shape.y1) < 6;
1265
+ }
1266
+ if (shape.type === "pen") return shape.points.length < 2;
1267
+ if (shape.type === "mosaic") {
1268
+ if (shape.mode === "brush") return shape.points.length < 3;
1269
+ return Math.abs(shape.w) < 6 || Math.abs(shape.h) < 6;
1270
+ }
1271
+ if (shape.type === "rect" || shape.type === "ellipse") {
1272
+ return Math.abs(shape.w) < 3 || Math.abs(shape.h) < 3;
1273
+ }
1274
+ return false;
1275
+ }
1276
+ // ------------------------------------------------------------ 文字
1277
+ /**
1278
+ * 打开文字输入框。
1279
+ *
1280
+ * 两个坑:
1281
+ * 1. focus() 必须延到下一个宏任务,否则浏览器的默认焦点行为会立刻把
1282
+ * 焦点抢回 body,输入框刚 focus 就 blur,立刻触发一次空提交;
1283
+ * 2. 同一时刻只允许一个输入框 —— 再次点击画布时先把上一个提交掉,
1284
+ * 否则会堆叠监听器、旧文本被 value='' 清掉。
1285
+ */
1286
+ openTextEditor(event, existing) {
1287
+ console.log(`[editor] \u6253\u5F00\u6587\u5B57\u8F93\u5165\u6846 client=${Math.round(event.clientX)},${Math.round(event.clientY)}`);
1288
+ this.closeActiveText?.();
1289
+ const ta = this.textEditor;
1290
+ const fontSize = existing ? existing.size : this.fontSize;
1291
+ const scale = this.textScale();
1292
+ ta.value = existing?.text ?? "";
1293
+ ta.hidden = false;
1294
+ const boxW = Math.max(60, fontSize * scale * 4);
1295
+ const boxH = fontSize * scale * 1.4;
1296
+ const left = Math.min(event.clientX, Math.max(0, window.innerWidth - boxW));
1297
+ const top = Math.min(event.clientY, Math.max(0, window.innerHeight - boxH));
1298
+ ta.style.left = `${left}px`;
1299
+ ta.style.top = `${top}px`;
1300
+ ta.style.fontSize = `${fontSize * scale}px`;
1301
+ ta.style.color = existing ? existing.color : this.color;
1302
+ ta.style.minWidth = `${boxW}px`;
1303
+ ta.style.minHeight = `${boxH}px`;
1304
+ const origin = existing ? { x: existing.x, y: existing.y } : this.toImage({ clientX: left, clientY: top });
1305
+ let ended = false;
1306
+ let focused = false;
1307
+ const end = (save) => {
1308
+ if (ended) return;
1309
+ ended = true;
1310
+ ta.hidden = true;
1311
+ if (save) {
1312
+ const text = ta.value.trim();
1313
+ console.log(`[editor] \u6587\u5B57\u63D0\u4EA4 \u957F\u5EA6=${text.length}`);
1314
+ if (text) {
1315
+ const maxWidth = ta.clientWidth / scale;
1316
+ const at = this.clampTextOrigin(origin, text, fontSize, maxWidth);
1317
+ if (existing) {
1318
+ const idx = this.shapes.indexOf(existing);
1319
+ if (idx >= 0) {
1320
+ this.beginChange();
1321
+ this.shapes[idx] = {
1322
+ ...existing,
1323
+ x: at.x,
1324
+ y: at.y,
1325
+ text,
1326
+ size: fontSize,
1327
+ maxWidth
1328
+ };
1329
+ this.onHistoryChange?.();
1330
+ this.render();
1331
+ }
1332
+ } else {
1333
+ this.commit({
1334
+ type: "text",
1335
+ x: at.x,
1336
+ y: at.y,
1337
+ text,
1338
+ color: this.color,
1339
+ size: fontSize,
1340
+ maxWidth
1341
+ });
1342
+ console.log(`[editor] \u65B0\u589E\u6807\u6CE8 type=text \u603B\u6570=${this.shapes.length}`);
1343
+ this.render();
1344
+ }
1345
+ }
1346
+ } else {
1347
+ console.log("[editor] \u6587\u5B57\u53D6\u6D88");
1348
+ }
1349
+ ta.removeEventListener("blur", onBlur);
1350
+ ta.removeEventListener("keydown", onKey);
1351
+ if (this.closeActiveText === handler) this.closeActiveText = null;
1352
+ this.opts.onTextEdit?.(false);
1353
+ };
1354
+ const handler = () => end(true);
1355
+ const onBlur = () => {
1356
+ if (focused) end(true);
1357
+ };
1358
+ const onKey = (e) => {
1359
+ e.stopPropagation();
1360
+ if (e.key === "Escape") {
1361
+ e.preventDefault();
1362
+ end(false);
1363
+ } else if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
1364
+ e.preventDefault();
1365
+ end(true);
1366
+ }
1367
+ };
1368
+ ta.addEventListener("blur", onBlur);
1369
+ ta.addEventListener("keydown", onKey);
1370
+ this.closeActiveText = handler;
1371
+ this.opts.onTextEdit?.(true);
1372
+ setTimeout(() => {
1373
+ ta.focus();
1374
+ focused = true;
1375
+ }, 0);
1376
+ }
1377
+ /**
1378
+ * 把文字锚点夹进画布:点在右下角又打了很多字的话,不夹紧
1379
+ * 导出时右侧 / 下侧会被画布裁掉。测量用绘制同一套字体与行高。
1380
+ */
1381
+ /** 文本框 CSS px → 标注坐标的比例(钉图是图像像素空间,遮罩是 DIP 空间 1:1)。 */
1382
+ textScale() {
1383
+ const rect = this.canvas.getBoundingClientRect();
1384
+ return this.opts.coords === "css" || rect.width <= 0 ? 1 : rect.width / this.canvas.width;
1385
+ }
1386
+ clampTextOrigin(origin, text, size, maxWidth) {
1387
+ const block = measureTextBlock(this.ctx, text, size, maxWidth);
1388
+ const rect = this.canvas.getBoundingClientRect();
1389
+ const maxX = this.opts.coords === "css" ? rect.width : this.canvas.width;
1390
+ const maxY = this.opts.coords === "css" ? rect.height : this.canvas.height;
1391
+ return {
1392
+ x: Math.min(Math.max(origin.x, 0), Math.max(0, maxX - block.width)),
1393
+ y: Math.min(Math.max(origin.y, 0), Math.max(0, maxY - block.height))
1394
+ };
1395
+ }
1396
+ // ------------------------------------------------------------ 工具属性
1397
+ get colors() {
1398
+ return COLORS;
1399
+ }
1400
+ get widthMin() {
1401
+ return WIDTH_MIN;
1402
+ }
1403
+ get widthMax() {
1404
+ return WIDTH_MAX;
1405
+ }
1406
+ };
1407
+ var HANDLE_SIZE_UNIT = 8;
1408
+
1409
+ // node_modules/lucide-static/icons/hand.svg
1410
+ var hand_default = '<!-- @license lucide-static v1.48.0 - ISC -->\n<svg\n class="lucide lucide-hand"\n xmlns="http://www.w3.org/2000/svg"\n width="24"\n height="24"\n viewBox="0 0 24 24"\n fill="none"\n stroke="currentColor"\n stroke-width="2"\n stroke-linecap="round"\n stroke-linejoin="round"\n>\n <path d="M18 11V6a2 2 0 0 0-2-2a2 2 0 0 0-2 2" />\n <path d="M14 10V4a2 2 0 0 0-2-2a2 2 0 0 0-2 2v2" />\n <path d="M10 10.5V6a2 2 0 0 0-2-2a2 2 0 0 0-2 2v8" />\n <path d="M18 8a2 2 0 1 1 4 0v6a8 8 0 0 1-8 8h-2c-2.8 0-4.5-.86-5.99-2.34l-3.6-3.6a2 2 0 0 1 2.83-2.82L7 15" />\n</svg>\n';
1411
+
1412
+ // node_modules/lucide-static/icons/arrow-up-right.svg
1413
+ var arrow_up_right_default = '<!-- @license lucide-static v1.48.0 - ISC -->\n<svg\n class="lucide lucide-arrow-up-right"\n xmlns="http://www.w3.org/2000/svg"\n width="24"\n height="24"\n viewBox="0 0 24 24"\n fill="none"\n stroke="currentColor"\n stroke-width="2"\n stroke-linecap="round"\n stroke-linejoin="round"\n>\n <path d="M7 7h10v10" />\n <path d="M7 17 17 7" />\n</svg>\n';
1414
+
1415
+ // node_modules/lucide-static/icons/square.svg
1416
+ var square_default = '<!-- @license lucide-static v1.48.0 - ISC -->\n<svg\n class="lucide lucide-square"\n xmlns="http://www.w3.org/2000/svg"\n width="24"\n height="24"\n viewBox="0 0 24 24"\n fill="none"\n stroke="currentColor"\n stroke-width="2"\n stroke-linecap="round"\n stroke-linejoin="round"\n>\n <rect width="18" height="18" x="3" y="3" rx="2" />\n</svg>\n';
1417
+
1418
+ // node_modules/lucide-static/icons/circle.svg
1419
+ var circle_default = '<!-- @license lucide-static v1.48.0 - ISC -->\n<svg\n class="lucide lucide-circle"\n xmlns="http://www.w3.org/2000/svg"\n width="24"\n height="24"\n viewBox="0 0 24 24"\n fill="none"\n stroke="currentColor"\n stroke-width="2"\n stroke-linecap="round"\n stroke-linejoin="round"\n>\n <circle cx="12" cy="12" r="10" />\n</svg>\n';
1420
+
1421
+ // node_modules/lucide-static/icons/pencil.svg
1422
+ var pencil_default = '<!-- @license lucide-static v1.48.0 - ISC -->\n<svg\n class="lucide lucide-pencil"\n xmlns="http://www.w3.org/2000/svg"\n width="24"\n height="24"\n viewBox="0 0 24 24"\n fill="none"\n stroke="currentColor"\n stroke-width="2"\n stroke-linecap="round"\n stroke-linejoin="round"\n>\n <path d="M21.174 6.812a1 1 0 0 0-3.986-3.987L3.842 16.174a2 2 0 0 0-.5.83l-1.321 4.352a.5.5 0 0 0 .623.622l4.353-1.32a2 2 0 0 0 .83-.497z" />\n <path d="m15 5 4 4" />\n</svg>\n';
1423
+
1424
+ // node_modules/lucide-static/icons/grid-3x3.svg
1425
+ var grid_3x3_default = '<!-- @license lucide-static v1.48.0 - ISC -->\n<svg\n class="lucide lucide-grid-3x3"\n xmlns="http://www.w3.org/2000/svg"\n width="24"\n height="24"\n viewBox="0 0 24 24"\n fill="none"\n stroke="currentColor"\n stroke-width="2"\n stroke-linecap="round"\n stroke-linejoin="round"\n>\n <rect width="18" height="18" x="3" y="3" rx="2" />\n <path d="M3 9h18" />\n <path d="M3 15h18" />\n <path d="M9 3v18" />\n <path d="M15 3v18" />\n</svg>\n';
1426
+
1427
+ // node_modules/lucide-static/icons/type.svg
1428
+ var type_default = '<!-- @license lucide-static v1.48.0 - ISC -->\n<svg\n class="lucide lucide-type"\n xmlns="http://www.w3.org/2000/svg"\n width="24"\n height="24"\n viewBox="0 0 24 24"\n fill="none"\n stroke="currentColor"\n stroke-width="2"\n stroke-linecap="round"\n stroke-linejoin="round"\n>\n <path d="M12 4v16" />\n <path d="M4 7V5a1 1 0 0 1 1-1h14a1 1 0 0 1 1 1v2" />\n <path d="M9 20h6" />\n</svg>\n';
1429
+
1430
+ // node_modules/lucide-static/icons/undo-2.svg
1431
+ var undo_2_default = '<!-- @license lucide-static v1.48.0 - ISC -->\n<svg\n class="lucide lucide-undo-2"\n xmlns="http://www.w3.org/2000/svg"\n width="24"\n height="24"\n viewBox="0 0 24 24"\n fill="none"\n stroke="currentColor"\n stroke-width="2"\n stroke-linecap="round"\n stroke-linejoin="round"\n>\n <path d="M9 14 4 9l5-5" />\n <path d="M4 9h10.5a5.5 5.5 0 0 1 5.5 5.5a5.5 5.5 0 0 1-5.5 5.5H11" />\n</svg>\n';
1432
+
1433
+ // node_modules/lucide-static/icons/redo-2.svg
1434
+ var redo_2_default = '<!-- @license lucide-static v1.48.0 - ISC -->\n<svg\n class="lucide lucide-redo-2"\n xmlns="http://www.w3.org/2000/svg"\n width="24"\n height="24"\n viewBox="0 0 24 24"\n fill="none"\n stroke="currentColor"\n stroke-width="2"\n stroke-linecap="round"\n stroke-linejoin="round"\n>\n <path d="m15 14 5-5-5-5" />\n <path d="M20 9H9.5A5.5 5.5 0 0 0 4 14.5A5.5 5.5 0 0 0 9.5 20H13" />\n</svg>\n';
1435
+
1436
+ // node_modules/lucide-static/icons/trash-2.svg
1437
+ var trash_2_default = '<!-- @license lucide-static v1.48.0 - ISC -->\n<svg\n class="lucide lucide-trash-2"\n xmlns="http://www.w3.org/2000/svg"\n width="24"\n height="24"\n viewBox="0 0 24 24"\n fill="none"\n stroke="currentColor"\n stroke-width="2"\n stroke-linecap="round"\n stroke-linejoin="round"\n>\n <path d="M10 11v6" />\n <path d="M14 11v6" />\n <path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6" />\n <path d="M3 6h18" />\n <path d="M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2" />\n</svg>\n';
1438
+
1439
+ // node_modules/lucide-static/icons/copy.svg
1440
+ var copy_default = '<!-- @license lucide-static v1.48.0 - ISC -->\n<svg\n class="lucide lucide-copy"\n xmlns="http://www.w3.org/2000/svg"\n width="24"\n height="24"\n viewBox="0 0 24 24"\n fill="none"\n stroke="currentColor"\n stroke-width="2"\n stroke-linecap="round"\n stroke-linejoin="round"\n>\n <rect width="14" height="14" x="8" y="8" rx="2" ry="2" />\n <path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2" />\n</svg>\n';
1441
+
1442
+ // node_modules/lucide-static/icons/save.svg
1443
+ var save_default = '<!-- @license lucide-static v1.48.0 - ISC -->\n<svg\n class="lucide lucide-save"\n xmlns="http://www.w3.org/2000/svg"\n width="24"\n height="24"\n viewBox="0 0 24 24"\n fill="none"\n stroke="currentColor"\n stroke-width="2"\n stroke-linecap="round"\n stroke-linejoin="round"\n>\n <path d="M15.2 3a2 2 0 0 1 1.4.6l3.8 3.8a2 2 0 0 1 .6 1.4V19a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2z" />\n <path d="M17 21v-7a1 1 0 0 0-1-1H8a1 1 0 0 0-1 1v7" />\n <path d="M7 3v4a1 1 0 0 0 1 1h7" />\n</svg>\n';
1444
+
1445
+ // node_modules/lucide-static/icons/x.svg
1446
+ var x_default = '<!-- @license lucide-static v1.48.0 - ISC -->\n<svg\n class="lucide lucide-x"\n xmlns="http://www.w3.org/2000/svg"\n width="24"\n height="24"\n viewBox="0 0 24 24"\n fill="none"\n stroke="currentColor"\n stroke-width="2"\n stroke-linecap="round"\n stroke-linejoin="round"\n>\n <path d="M18 6 6 18" />\n <path d="m6 6 12 12" />\n</svg>\n';
1447
+
1448
+ // node_modules/lucide-static/icons/check.svg
1449
+ var check_default = '<!-- @license lucide-static v1.48.0 - ISC -->\n<svg\n class="lucide lucide-check"\n xmlns="http://www.w3.org/2000/svg"\n width="24"\n height="24"\n viewBox="0 0 24 24"\n fill="none"\n stroke="currentColor"\n stroke-width="2"\n stroke-linecap="round"\n stroke-linejoin="round"\n>\n <path d="M20 6 9 17l-5-5" />\n</svg>\n';
1450
+
1451
+ // node_modules/lucide-static/icons/pin.svg
1452
+ var pin_default = '<!-- @license lucide-static v1.48.0 - ISC -->\n<svg\n class="lucide lucide-pin"\n xmlns="http://www.w3.org/2000/svg"\n width="24"\n height="24"\n viewBox="0 0 24 24"\n fill="none"\n stroke="currentColor"\n stroke-width="2"\n stroke-linecap="round"\n stroke-linejoin="round"\n>\n <path d="M12 17v5" />\n <path d="M9 10.76a2 2 0 0 1-1.11 1.79l-1.78.9A2 2 0 0 0 5 15.24V16a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1v-.76a2 2 0 0 0-1.11-1.79l-1.78-.9A2 2 0 0 1 15 10.76V7a1 1 0 0 1 1-1 2 2 0 0 0 0-4H8a2 2 0 0 0 0 4 1 1 0 0 1 1 1z" />\n</svg>\n';
1453
+
1454
+ // node_modules/lucide-static/icons/square-dashed.svg
1455
+ var square_dashed_default = '<!-- @license lucide-static v1.48.0 - ISC -->\n<svg\n class="lucide lucide-square-dashed"\n xmlns="http://www.w3.org/2000/svg"\n width="24"\n height="24"\n viewBox="0 0 24 24"\n fill="none"\n stroke="currentColor"\n stroke-width="2"\n stroke-linecap="round"\n stroke-linejoin="round"\n>\n <path d="M5 3a2 2 0 0 0-2 2" />\n <path d="M19 3a2 2 0 0 1 2 2" />\n <path d="M21 19a2 2 0 0 1-2 2" />\n <path d="M5 21a2 2 0 0 1-2-2" />\n <path d="M9 3h1" />\n <path d="M9 21h1" />\n <path d="M14 3h1" />\n <path d="M14 21h1" />\n <path d="M3 9v1" />\n <path d="M21 9v1" />\n <path d="M3 14v1" />\n <path d="M21 14v1" />\n</svg>\n';
1456
+
1457
+ // node_modules/lucide-static/icons/brush.svg
1458
+ var brush_default = '<!-- @license lucide-static v1.48.0 - ISC -->\n<svg\n class="lucide lucide-brush"\n xmlns="http://www.w3.org/2000/svg"\n width="24"\n height="24"\n viewBox="0 0 24 24"\n fill="none"\n stroke="currentColor"\n stroke-width="2"\n stroke-linecap="round"\n stroke-linejoin="round"\n>\n <path d="m11 10 3 3" />\n <path d="M6.5 21A3.5 3.5 0 1 0 3 17.5a2.62 2.62 0 0 1-.708 1.792A1 1 0 0 0 3 21z" />\n <path d="M9.969 17.031 21.378 5.624a1 1 0 0 0-3.002-3.002L6.967 14.031" />\n</svg>\n';
1459
+
1460
+ // node_modules/lucide-static/icons/slash.svg
1461
+ var slash_default = '<!-- @license lucide-static v1.48.0 - ISC -->\n<svg\n class="lucide lucide-slash"\n xmlns="http://www.w3.org/2000/svg"\n width="24"\n height="24"\n viewBox="0 0 24 24"\n fill="none"\n stroke="currentColor"\n stroke-width="2"\n stroke-linecap="round"\n stroke-linejoin="round"\n>\n <path d="M22 2 2 22" />\n</svg>\n';
1462
+
1463
+ // node_modules/lucide-static/icons/highlighter.svg
1464
+ var highlighter_default = '<!-- @license lucide-static v1.48.0 - ISC -->\n<svg\n class="lucide lucide-highlighter"\n xmlns="http://www.w3.org/2000/svg"\n width="24"\n height="24"\n viewBox="0 0 24 24"\n fill="none"\n stroke="currentColor"\n stroke-width="2"\n stroke-linecap="round"\n stroke-linejoin="round"\n>\n <path d="m9 11-6 6v3h9l3-3" />\n <path d="m22 12-4.6 4.6a2 2 0 0 1-2.8 0l-5.2-5.2a2 2 0 0 1 0-2.8L14 4" />\n</svg>\n';
1465
+
1466
+ // node_modules/lucide-static/icons/list-ordered.svg
1467
+ var list_ordered_default = '<!-- @license lucide-static v1.48.0 - ISC -->\n<svg\n class="lucide lucide-list-ordered"\n xmlns="http://www.w3.org/2000/svg"\n width="24"\n height="24"\n viewBox="0 0 24 24"\n fill="none"\n stroke="currentColor"\n stroke-width="2"\n stroke-linecap="round"\n stroke-linejoin="round"\n>\n <path d="M11 5h10" />\n <path d="M11 12h10" />\n <path d="M11 19h10" />\n <path d="M4 4h1v5" />\n <path d="M4 9h2" />\n <path d="M6.5 20H3.4c0-1 2.6-1.925 2.6-3.5a1.5 1.5 0 0 0-2.6-1.02" />\n</svg>\n';
1468
+
1469
+ // node_modules/lucide-static/icons/droplets.svg
1470
+ var droplets_default = '<!-- @license lucide-static v1.48.0 - ISC -->\n<svg\n class="lucide lucide-droplets"\n xmlns="http://www.w3.org/2000/svg"\n width="24"\n height="24"\n viewBox="0 0 24 24"\n fill="none"\n stroke="currentColor"\n stroke-width="2"\n stroke-linecap="round"\n stroke-linejoin="round"\n>\n <path d="M7 16.3c2.2 0 4-1.83 4-4.05 0-1.16-.57-2.26-1.71-3.19S7.29 6.75 7 5.3c-.29 1.45-1.14 2.84-2.29 3.76S3 11.1 3 12.25c0 2.22 1.8 4.05 4 4.05z" />\n <path d="M12.56 6.6A10.97 10.97 0 0 0 14 3.02c.5 2.5 2 4.9 4 6.5s3 3.5 3 5.5a6.98 6.98 0 0 1-11.91 4.97" />\n</svg>\n';
1471
+
1472
+ // src/annotations/toolbar.ts
1473
+ var TOOL_LABELS = {
1474
+ hand: "\u79FB\u52A8",
1475
+ arrow: "\u7BAD\u5934",
1476
+ line: "\u76F4\u7EBF",
1477
+ rect: "\u77E9\u5F62",
1478
+ ellipse: "\u692D\u5706",
1479
+ pen: "\u753B\u7B14",
1480
+ marker: "\u8367\u5149\u7B14",
1481
+ mosaic: "\u9A6C\u8D5B\u514B",
1482
+ counter: "\u5E8F\u53F7",
1483
+ text: "\u6587\u5B57"
1484
+ };
1485
+ var TOOL_ICONS = {
1486
+ hand: hand_default,
1487
+ arrow: arrow_up_right_default,
1488
+ line: slash_default,
1489
+ rect: square_default,
1490
+ ellipse: circle_default,
1491
+ pen: pencil_default,
1492
+ marker: highlighter_default,
1493
+ mosaic: grid_3x3_default,
1494
+ counter: list_ordered_default,
1495
+ text: type_default
1496
+ };
1497
+ var ACTION_ICONS = {
1498
+ copy: copy_default,
1499
+ save: save_default,
1500
+ cancel: x_default,
1501
+ confirm: check_default,
1502
+ pin: pin_default
1503
+ };
1504
+ var ARROW_HEAD_LABELS = {
1505
+ solid: "\u5B9E\u5FC3\u7BAD\u5934",
1506
+ open: "\u7A7A\u5FC3\u7BAD\u5934"
1507
+ };
1508
+ var ARROW_HEAD_ICONS = {
1509
+ solid: `<svg viewBox="0 0 24 24" width="16" height="16" aria-hidden="true"><path d="M4.5 19.5 14 10" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round"/><path d="M11 4.5h8.5V13z" fill="currentColor"/></svg>`,
1510
+ open: `<svg viewBox="0 0 24 24" width="16" height="16" aria-hidden="true"><path d="M4.5 19.5 14 10" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round"/><path d="M11 4.5h8.5V13" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round"/></svg>`
1511
+ };
1512
+ var MOSAIC_MODE_LABELS = {
1513
+ region: "\u9009\u533A",
1514
+ brush: "\u6D82\u62B9",
1515
+ blur: "\u6A21\u7CCA"
1516
+ };
1517
+ var MOSAIC_MODE_ICONS = {
1518
+ region: square_dashed_default,
1519
+ brush: brush_default,
1520
+ blur: droplets_default
1521
+ };
1522
+ function createToolbar(container, editor, opts = {}) {
1523
+ container.replaceChildren();
1524
+ const tools = group();
1525
+ for (const id of opts.tools ?? editor.toolIds) {
1526
+ const btn = iconButton(TOOL_ICONS[id], TOOL_LABELS[id]);
1527
+ btn.dataset.tool = id;
1528
+ btn.classList.toggle("is-active", id === editor.currentTool);
1529
+ btn.addEventListener("click", () => {
1530
+ editor.setTool(id);
1531
+ syncActive();
1532
+ });
1533
+ tools.append(btn);
1534
+ }
1535
+ const colors = group();
1536
+ for (const c of editor.colors) {
1537
+ const btn = button("");
1538
+ btn.className = "swatch";
1539
+ btn.style.background = c;
1540
+ btn.dataset.color = c;
1541
+ btn.dataset.tip = c;
1542
+ btn.setAttribute("aria-label", c);
1543
+ if (c === "#ffffff") btn.classList.add("is-light");
1544
+ btn.addEventListener("click", () => {
1545
+ editor.setColor(c);
1546
+ syncActive();
1547
+ });
1548
+ colors.append(btn);
1549
+ }
1550
+ const widths = group();
1551
+ const widthRange = document.createElement("input");
1552
+ widthRange.type = "range";
1553
+ widthRange.min = String(editor.widthMin);
1554
+ widthRange.max = String(editor.widthMax);
1555
+ widthRange.step = "1";
1556
+ widthRange.title = `\u7EBF\u5BBD ${editor.widthValue}`;
1557
+ const widthVal = document.createElement("span");
1558
+ widthVal.className = "width-val";
1559
+ widthRange.addEventListener("input", () => {
1560
+ const v = Number(widthRange.value);
1561
+ editor.setWidth(v);
1562
+ widthVal.textContent = String(v);
1563
+ widthRange.title = `\u7EBF\u5BBD ${v}`;
1564
+ });
1565
+ widths.append(widthRange, widthVal);
1566
+ const heads = group();
1567
+ for (const h of editor.arrowHeads) {
1568
+ const btn = iconButton(ARROW_HEAD_ICONS[h], ARROW_HEAD_LABELS[h]);
1569
+ btn.dataset.head = h;
1570
+ btn.addEventListener("click", () => {
1571
+ editor.setArrowHead(h);
1572
+ syncActive();
1573
+ });
1574
+ heads.append(btn);
1575
+ }
1576
+ const sizes = group();
1577
+ const sizeSelect = document.createElement("select");
1578
+ sizeSelect.title = "\u5B57\u53F7";
1579
+ for (const n of editor.fontSizes) {
1580
+ const opt = document.createElement("option");
1581
+ opt.value = String(n);
1582
+ opt.textContent = String(n);
1583
+ sizeSelect.append(opt);
1584
+ }
1585
+ sizeSelect.addEventListener("change", () => {
1586
+ editor.setFontSize(Number(sizeSelect.value));
1587
+ });
1588
+ sizes.append(sizeSelect);
1589
+ const modes = group();
1590
+ for (const m of editor.mosaicModes) {
1591
+ const btn = iconButton(MOSAIC_MODE_ICONS[m], MOSAIC_MODE_LABELS[m]);
1592
+ btn.dataset.mode = m;
1593
+ btn.classList.toggle("is-active", m === editor.mosaicModeValue);
1594
+ btn.addEventListener("click", () => {
1595
+ editor.setMosaicMode(m);
1596
+ syncActive();
1597
+ });
1598
+ modes.append(btn);
1599
+ }
1600
+ const history = group();
1601
+ const undo = iconButton(undo_2_default, "\u64A4\u9500");
1602
+ undo.addEventListener("click", () => editor.undo());
1603
+ const redo = iconButton(redo_2_default, "\u91CD\u505A");
1604
+ redo.addEventListener("click", () => editor.redo());
1605
+ const clear = iconButton(trash_2_default, "\u6E05\u7A7A");
1606
+ clear.addEventListener("click", () => editor.clear());
1607
+ history.append(undo, redo, clear);
1608
+ const actions = group();
1609
+ const byId = /* @__PURE__ */ new Map();
1610
+ const extras = opts.actions ?? [];
1611
+ for (const spec of extras) {
1612
+ const icon = spec.icon ?? ACTION_ICONS[spec.id];
1613
+ const btn = icon ? iconButton(icon, spec.label) : button(spec.label);
1614
+ btn.dataset.action = spec.id;
1615
+ if (!icon) btn.title = spec.label;
1616
+ if (spec.danger) btn.classList.add("is-danger");
1617
+ if (spec.primary) btn.classList.add("is-primary");
1618
+ if (spec.active) btn.classList.add("is-active");
1619
+ btn.addEventListener("click", () => void spec.run(btn));
1620
+ actions.append(btn);
1621
+ byId.set(spec.id, btn);
1622
+ }
1623
+ const mainRow = rowEl();
1624
+ const mainGroups = [tools, history];
1625
+ if (extras.length > 0) mainGroups.push(actions);
1626
+ mainRow.append(...mainGroups);
1627
+ const propRow = rowEl();
1628
+ propRow.append(heads, sizes, modes, widths, colors);
1629
+ const row2Groups = /* @__PURE__ */ new Map([
1630
+ ["head", heads],
1631
+ ["size", sizes],
1632
+ ["mode", modes],
1633
+ ["width", widths],
1634
+ ["color", colors]
1635
+ ]);
1636
+ const row2Order = [heads, sizes, modes, widths, colors];
1637
+ container.append(mainRow, propRow);
1638
+ addDividers(mainGroups);
1639
+ function syncActive() {
1640
+ const attr = (name, current) => {
1641
+ container.querySelectorAll(`[data-${name}]`).forEach((b) => b.classList.toggle("is-active", b.dataset[name] === current));
1642
+ };
1643
+ attr("tool", editor.currentTool);
1644
+ attr("color", editor.colorValue);
1645
+ attr("mode", editor.mosaicModeValue);
1646
+ attr("head", editor.arrowHeadValue);
1647
+ sizeSelect.value = String(editor.fontSizeValue);
1648
+ widthRange.value = String(editor.widthValue);
1649
+ widthVal.textContent = String(editor.widthValue);
1650
+ syncRow2();
1651
+ }
1652
+ function syncRow2() {
1653
+ const show = new Set(ROW2_BY_TOOL[editor.currentTool] ?? []);
1654
+ if (toolUsesWidth(editor.currentTool, editor.mosaicModeValue)) {
1655
+ show.add("width");
1656
+ }
1657
+ row2Groups.forEach((g, key) => {
1658
+ g.hidden = !show.has(key);
1659
+ });
1660
+ propRow.hidden = show.size === 0;
1661
+ syncRow2Dividers();
1662
+ }
1663
+ function syncRow2Dividers() {
1664
+ row2Order.forEach((g) => {
1665
+ g.firstElementChild?.classList.remove("is-group-start");
1666
+ });
1667
+ let first = true;
1668
+ for (const g of row2Order) {
1669
+ if (g.hidden) continue;
1670
+ if (first) {
1671
+ first = false;
1672
+ continue;
1673
+ }
1674
+ g.firstElementChild?.classList.add("is-group-start");
1675
+ }
1676
+ }
1677
+ syncActive();
1678
+ const syncHistory = () => {
1679
+ undo.disabled = !editor.canUndo;
1680
+ redo.disabled = !editor.canRedo;
1681
+ };
1682
+ editor.onHistoryChange = syncHistory;
1683
+ syncHistory();
1684
+ editor.onAttrsChange = syncActive;
1685
+ window.addEventListener("keydown", (event) => {
1686
+ if (event.target instanceof HTMLTextAreaElement) return;
1687
+ const mod = event.ctrlKey || event.metaKey;
1688
+ const key = event.key.toLowerCase();
1689
+ if (mod && key === "z") {
1690
+ event.preventDefault();
1691
+ if (event.shiftKey) editor.redo();
1692
+ else editor.undo();
1693
+ } else if (mod && key === "y") {
1694
+ event.preventDefault();
1695
+ editor.redo();
1696
+ } else if (mod && key === "c" && byId.has("copy")) {
1697
+ event.preventDefault();
1698
+ byId.get("copy")?.click();
1699
+ } else if (mod && key === "s" && byId.has("save")) {
1700
+ event.preventDefault();
1701
+ byId.get("save")?.click();
1702
+ } else if (event.key === "Delete" || event.key === "Backspace") {
1703
+ event.preventDefault();
1704
+ editor.deleteSelected();
1705
+ } else if (event.key === "Escape") {
1706
+ const target = byId.get("close") ?? byId.get("cancel");
1707
+ if (target) {
1708
+ event.preventDefault();
1709
+ target.click();
1710
+ }
1711
+ }
1712
+ });
1713
+ return { undo, redo, clear };
1714
+ }
1715
+ function group() {
1716
+ const el = document.createElement("div");
1717
+ el.className = "group";
1718
+ return el;
1719
+ }
1720
+ function rowEl() {
1721
+ const el = document.createElement("div");
1722
+ el.className = "toolbar-row";
1723
+ return el;
1724
+ }
1725
+ function addDividers(groups) {
1726
+ groups.forEach((g, i) => {
1727
+ if (i > 0) g.firstElementChild?.classList.add("is-group-start");
1728
+ });
1729
+ }
1730
+ function button(text) {
1731
+ const el = document.createElement("button");
1732
+ el.type = "button";
1733
+ el.textContent = text;
1734
+ el.style.setProperty("-webkit-app-region", "no-drag");
1735
+ el.style.setProperty("app-region", "no-drag");
1736
+ return el;
1737
+ }
1738
+ function iconButton(svg, label) {
1739
+ const el = button("");
1740
+ el.innerHTML = svg;
1741
+ el.dataset.tip = label;
1742
+ el.setAttribute("aria-label", label);
1743
+ return el;
1744
+ }
1745
+ //# sourceMappingURL=annotations.cjs.map