react-os-shell 2.9.3 → 3.0.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.
- package/dist/Browser-3SDJFEZW.js +7 -0
- package/dist/{Browser-23ULR34J.js.map → Browser-3SDJFEZW.js.map} +1 -1
- package/dist/{Calculator-RI2AXFNQ.js → Calculator-LGYXEZTY.js} +4 -4
- package/dist/{Calculator-RI2AXFNQ.js.map → Calculator-LGYXEZTY.js.map} +1 -1
- package/dist/{CurrencyConverter-TVSLZLYL.js → CurrencyConverter-JI5ECSQA.js} +4 -4
- package/dist/{CurrencyConverter-TVSLZLYL.js.map → CurrencyConverter-JI5ECSQA.js.map} +1 -1
- package/dist/{Documents-BGCOIW5E.js → Documents-HCHGAFPB.js} +4 -4
- package/dist/{Documents-BGCOIW5E.js.map → Documents-HCHGAFPB.js.map} +1 -1
- package/dist/Files-FYWUNLBY.js +12 -0
- package/dist/{Files-X7ERMLDM.js.map → Files-FYWUNLBY.js.map} +1 -1
- package/dist/{Notepad-OTOYAF4L.js → Notepad-WRS7EMCK.js} +4 -4
- package/dist/{Notepad-OTOYAF4L.js.map → Notepad-WRS7EMCK.js.map} +1 -1
- package/dist/{PomodoroTimer-FZP7SKPY.js → PomodoroTimer-5WZ37PLI.js} +5 -5
- package/dist/{PomodoroTimer-FZP7SKPY.js.map → PomodoroTimer-5WZ37PLI.js.map} +1 -1
- package/dist/Preview-HOLQVFSU.js +8 -0
- package/dist/{Preview-XSX4HVK6.js.map → Preview-HOLQVFSU.js.map} +1 -1
- package/dist/Spreadsheet-MRUQ4LJR.js +7 -0
- package/dist/{Spreadsheet-4FOA6E6K.js.map → Spreadsheet-MRUQ4LJR.js.map} +1 -1
- package/dist/{Stock-36BKWEUO.js → Stock-ERDFTUOZ.js} +4 -4
- package/dist/{Stock-36BKWEUO.js.map → Stock-ERDFTUOZ.js.map} +1 -1
- package/dist/{Weather-QUHBOFVA.js → Weather-6WBY4ANL.js} +4 -4
- package/dist/{Weather-QUHBOFVA.js.map → Weather-6WBY4ANL.js.map} +1 -1
- package/dist/{WorldClock-2UY5R4L3.js → WorldClock-ZOLBVSJC.js} +4 -4
- package/dist/{WorldClock-2UY5R4L3.js.map → WorldClock-ZOLBVSJC.js.map} +1 -1
- package/dist/apps/index.js +19 -20
- package/dist/apps/index.js.map +1 -1
- package/dist/{chunk-BJS5UCR6.js → chunk-6OWYD4O6.js} +20 -7
- package/dist/chunk-6OWYD4O6.js.map +1 -0
- package/dist/{chunk-MIFS2574.js → chunk-6VMTAVUD.js} +3 -3
- package/dist/{chunk-MIFS2574.js.map → chunk-6VMTAVUD.js.map} +1 -1
- package/dist/{chunk-DVXRDQEN.js → chunk-D7QQTFM3.js} +4 -4
- package/dist/{chunk-DVXRDQEN.js.map → chunk-D7QQTFM3.js.map} +1 -1
- package/dist/{chunk-Y2L2DBL6.js → chunk-QS2MEW6R.js} +5 -5
- package/dist/{chunk-Y2L2DBL6.js.map → chunk-QS2MEW6R.js.map} +1 -1
- package/dist/{chunk-HKBUFA4V.js → chunk-SKNOGIDC.js} +997 -7
- package/dist/chunk-SKNOGIDC.js.map +1 -0
- package/dist/{chunk-HMV37F2X.js → chunk-X4MB43G2.js} +4 -4
- package/dist/{chunk-HMV37F2X.js.map → chunk-X4MB43G2.js.map} +1 -1
- package/dist/{chunk-T2P7CZ3S.js → chunk-XRBSB7FX.js} +3 -3
- package/dist/{chunk-T2P7CZ3S.js.map → chunk-XRBSB7FX.js.map} +1 -1
- package/dist/{chunk-YWANIOU6.js → chunk-XRCABX52.js} +4 -4
- package/dist/{chunk-YWANIOU6.js.map → chunk-XRCABX52.js.map} +1 -1
- package/dist/{chunk-UWNETCYO.js → chunk-XVCEFF6M.js} +4 -4
- package/dist/{chunk-UWNETCYO.js.map → chunk-XVCEFF6M.js.map} +1 -1
- package/dist/index.d.ts +1 -125
- package/dist/index.js +17 -512
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/dist/Browser-23ULR34J.js +0 -7
- package/dist/Files-X7ERMLDM.js +0 -13
- package/dist/ImageAnnotator-55NPWJ2R.js +0 -4
- package/dist/ImageAnnotator-55NPWJ2R.js.map +0 -1
- package/dist/Preview-XSX4HVK6.js +0 -9
- package/dist/Spreadsheet-4FOA6E6K.js +0 -7
- package/dist/chunk-BJS5UCR6.js.map +0 -1
- package/dist/chunk-HKBUFA4V.js.map +0 -1
- package/dist/chunk-NUPYEVU4.js +0 -999
- package/dist/chunk-NUPYEVU4.js.map +0 -1
package/dist/chunk-NUPYEVU4.js
DELETED
|
@@ -1,999 +0,0 @@
|
|
|
1
|
-
import { toast_default } from './chunk-VENYVK3L.js';
|
|
2
|
-
import { forwardRef, useRef, useState, useMemo, useEffect, useImperativeHandle } from 'react';
|
|
3
|
-
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
|
|
4
|
-
|
|
5
|
-
var COLORS = ["#ef4444", "#f97316", "#eab308", "#22c55e", "#3b82f6", "#8b5cf6", "#000000", "#ffffff"];
|
|
6
|
-
var FONTS = [
|
|
7
|
-
{ id: "system", label: "System", css: "-apple-system, system-ui, sans-serif" },
|
|
8
|
-
{ id: "serif", label: "Serif", css: 'Georgia, "Times New Roman", serif' },
|
|
9
|
-
{ id: "mono", label: "Mono", css: 'ui-monospace, "SF Mono", Menlo, monospace' },
|
|
10
|
-
{ id: "cursive", label: "Cursive", css: '"Brush Script MT", "Comic Sans MS", cursive' }
|
|
11
|
-
];
|
|
12
|
-
var STROKE_DEFAULT = 4;
|
|
13
|
-
var TEXT_SIZE_DEFAULT = 24;
|
|
14
|
-
var RECT_RADIUS_DEFAULT = 12;
|
|
15
|
-
var MOSAIC_BLOCK = 12;
|
|
16
|
-
var ZOOM_MIN = 0.25;
|
|
17
|
-
var ZOOM_MAX = 4;
|
|
18
|
-
var ZOOM_STEP = 0.25;
|
|
19
|
-
var newId = () => Math.random().toString(36).slice(2, 9);
|
|
20
|
-
var ImageAnnotator = forwardRef(function ImageAnnotator2({ src, filename, onApply, onCancel }, ref) {
|
|
21
|
-
const wrapRef = useRef(null);
|
|
22
|
-
const canvasRef = useRef(null);
|
|
23
|
-
const svgRef = useRef(null);
|
|
24
|
-
const imageRef = useRef(null);
|
|
25
|
-
const [tool, setTool] = useState("select");
|
|
26
|
-
const [color, setColor] = useState(COLORS[0]);
|
|
27
|
-
const [stroke, setStroke] = useState(STROKE_DEFAULT);
|
|
28
|
-
const [textSize, setTextSize] = useState(TEXT_SIZE_DEFAULT);
|
|
29
|
-
const [textFont, setTextFont] = useState(FONTS[0].id);
|
|
30
|
-
const [textBold, setTextBold] = useState(true);
|
|
31
|
-
const [textItalic, setTextItalic] = useState(false);
|
|
32
|
-
const [textUnderline, setTextUnderline] = useState(false);
|
|
33
|
-
const [rectRadius, setRectRadius] = useState(RECT_RADIUS_DEFAULT);
|
|
34
|
-
const [annotations, setAnnotations] = useState([]);
|
|
35
|
-
const [selectedId, setSelectedId] = useState(null);
|
|
36
|
-
const [imageSize, setImageSize] = useState(null);
|
|
37
|
-
const [fitSize, setFitSize] = useState(null);
|
|
38
|
-
const [zoom, setZoom] = useState(1);
|
|
39
|
-
const [preview, setPreview] = useState(null);
|
|
40
|
-
const [pendingText, setPendingText] = useState(null);
|
|
41
|
-
const [pendingCrop, setPendingCrop] = useState(null);
|
|
42
|
-
const dragRef = useRef(null);
|
|
43
|
-
const [isDragging, setIsDragging] = useState(false);
|
|
44
|
-
const displaySize = useMemo(() => {
|
|
45
|
-
if (!fitSize) return null;
|
|
46
|
-
return { w: fitSize.w * zoom, h: fitSize.h * zoom };
|
|
47
|
-
}, [fitSize, zoom]);
|
|
48
|
-
const scale = displaySize && imageSize ? displaySize.w / imageSize.w : 1;
|
|
49
|
-
const selected = useMemo(
|
|
50
|
-
() => annotations.find((a) => a.id === selectedId) ?? null,
|
|
51
|
-
[annotations, selectedId]
|
|
52
|
-
);
|
|
53
|
-
useEffect(() => {
|
|
54
|
-
if (tool === "select" && selected) {
|
|
55
|
-
if ("color" in selected) setColor(selected.color);
|
|
56
|
-
if ("stroke" in selected) setStroke(selected.stroke);
|
|
57
|
-
if (selected.type === "rect") setRectRadius(selected.radius);
|
|
58
|
-
if (selected.type === "text") {
|
|
59
|
-
setTextSize(selected.size);
|
|
60
|
-
setTextFont(selected.font);
|
|
61
|
-
setTextBold(selected.bold);
|
|
62
|
-
setTextItalic(selected.italic);
|
|
63
|
-
setTextUnderline(selected.underline);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
}, [selectedId]);
|
|
67
|
-
useEffect(() => {
|
|
68
|
-
const img = new Image();
|
|
69
|
-
img.crossOrigin = "anonymous";
|
|
70
|
-
img.onload = () => {
|
|
71
|
-
imageRef.current = img;
|
|
72
|
-
setImageSize({ w: img.naturalWidth, h: img.naturalHeight });
|
|
73
|
-
setAnnotations([]);
|
|
74
|
-
setSelectedId(null);
|
|
75
|
-
};
|
|
76
|
-
img.onerror = () => toast_default.error("Failed to load image");
|
|
77
|
-
img.src = src;
|
|
78
|
-
}, [src]);
|
|
79
|
-
useEffect(() => {
|
|
80
|
-
if (!imageSize) return;
|
|
81
|
-
const update = () => {
|
|
82
|
-
const wrap = wrapRef.current;
|
|
83
|
-
if (!wrap) return;
|
|
84
|
-
const r = wrap.getBoundingClientRect();
|
|
85
|
-
const availW = Math.max(0, r.width - 32);
|
|
86
|
-
const availH = Math.max(0, r.height - 32);
|
|
87
|
-
if (availW === 0 || availH === 0) return;
|
|
88
|
-
const ratio = imageSize.w / imageSize.h;
|
|
89
|
-
let w = imageSize.w, h = imageSize.h;
|
|
90
|
-
if (w > availW) {
|
|
91
|
-
w = availW;
|
|
92
|
-
h = w / ratio;
|
|
93
|
-
}
|
|
94
|
-
if (h > availH) {
|
|
95
|
-
h = availH;
|
|
96
|
-
w = h * ratio;
|
|
97
|
-
}
|
|
98
|
-
setFitSize({ w, h });
|
|
99
|
-
};
|
|
100
|
-
update();
|
|
101
|
-
const ro = new ResizeObserver(update);
|
|
102
|
-
if (wrapRef.current) ro.observe(wrapRef.current);
|
|
103
|
-
return () => ro.disconnect();
|
|
104
|
-
}, [imageSize]);
|
|
105
|
-
const mosaicAnnos = useMemo(
|
|
106
|
-
() => annotations.filter((a) => a.type === "mosaic"),
|
|
107
|
-
[annotations]
|
|
108
|
-
);
|
|
109
|
-
useEffect(() => {
|
|
110
|
-
const img = imageRef.current;
|
|
111
|
-
const c = canvasRef.current;
|
|
112
|
-
if (!img || !c || !imageSize) return;
|
|
113
|
-
c.width = imageSize.w;
|
|
114
|
-
c.height = imageSize.h;
|
|
115
|
-
const ctx = c.getContext("2d");
|
|
116
|
-
ctx.drawImage(img, 0, 0);
|
|
117
|
-
for (const m of mosaicAnnos) applyMosaic(ctx, m);
|
|
118
|
-
}, [imageSize, mosaicAnnos, fitSize]);
|
|
119
|
-
const evToImage = (e) => {
|
|
120
|
-
const svg = svgRef.current;
|
|
121
|
-
if (!svg) return { x: 0, y: 0 };
|
|
122
|
-
const pt = svg.createSVGPoint();
|
|
123
|
-
pt.x = e.clientX;
|
|
124
|
-
pt.y = e.clientY;
|
|
125
|
-
const ctm = svg.getScreenCTM();
|
|
126
|
-
if (!ctm) return { x: 0, y: 0 };
|
|
127
|
-
const t = pt.matrixTransform(ctm.inverse());
|
|
128
|
-
return { x: t.x, y: t.y };
|
|
129
|
-
};
|
|
130
|
-
const beginDrag = (drag) => {
|
|
131
|
-
dragRef.current = drag;
|
|
132
|
-
setIsDragging(true);
|
|
133
|
-
};
|
|
134
|
-
useEffect(() => {
|
|
135
|
-
if (!isDragging) return;
|
|
136
|
-
const onMove = (ev) => {
|
|
137
|
-
const drag = dragRef.current;
|
|
138
|
-
if (!drag) return;
|
|
139
|
-
const p = evToImage(ev);
|
|
140
|
-
if (drag.kind === "draw") {
|
|
141
|
-
setPreview(makeShape(tool, drag.start, p, color, stroke, rectRadius));
|
|
142
|
-
} else if (drag.kind === "pen") {
|
|
143
|
-
const last = drag.points[drag.points.length - 1];
|
|
144
|
-
if (Math.abs(p.x - last.x) > 1 || Math.abs(p.y - last.y) > 1) {
|
|
145
|
-
drag.points.push(p);
|
|
146
|
-
setPreview({ id: "", type: "draw", points: drag.points.slice(), color, stroke });
|
|
147
|
-
}
|
|
148
|
-
} else if (drag.kind === "crop") {
|
|
149
|
-
setPendingCrop(normalizeRect(drag.start, p));
|
|
150
|
-
} else if (drag.kind === "move") {
|
|
151
|
-
const dx = p.x - drag.start.x;
|
|
152
|
-
const dy = p.y - drag.start.y;
|
|
153
|
-
setAnnotations((prev) => prev.map((a) => a.id === drag.id ? translate(drag.original, dx, dy) : a));
|
|
154
|
-
} else if (drag.kind === "resize") {
|
|
155
|
-
setAnnotations((prev) => prev.map((a) => a.id === drag.id ? resize(drag.original, drag.corner, p) : a));
|
|
156
|
-
}
|
|
157
|
-
};
|
|
158
|
-
const onUp = () => {
|
|
159
|
-
const drag = dragRef.current;
|
|
160
|
-
dragRef.current = null;
|
|
161
|
-
setIsDragging(false);
|
|
162
|
-
if (drag?.kind === "draw") {
|
|
163
|
-
setPreview((p) => {
|
|
164
|
-
if (!p || isTrivial(p)) return null;
|
|
165
|
-
const anno = { ...p, id: newId() };
|
|
166
|
-
setAnnotations((prev) => [...prev, anno]);
|
|
167
|
-
setSelectedId(anno.id);
|
|
168
|
-
setTool("select");
|
|
169
|
-
return null;
|
|
170
|
-
});
|
|
171
|
-
} else if (drag?.kind === "pen") {
|
|
172
|
-
if (drag.points.length < 2) {
|
|
173
|
-
setPreview(null);
|
|
174
|
-
return;
|
|
175
|
-
}
|
|
176
|
-
const anno = { id: newId(), type: "draw", points: drag.points, color, stroke };
|
|
177
|
-
setAnnotations((prev) => [...prev, anno]);
|
|
178
|
-
setSelectedId(anno.id);
|
|
179
|
-
setTool("select");
|
|
180
|
-
setPreview(null);
|
|
181
|
-
}
|
|
182
|
-
};
|
|
183
|
-
window.addEventListener("pointermove", onMove);
|
|
184
|
-
window.addEventListener("pointerup", onUp);
|
|
185
|
-
window.addEventListener("pointercancel", onUp);
|
|
186
|
-
return () => {
|
|
187
|
-
window.removeEventListener("pointermove", onMove);
|
|
188
|
-
window.removeEventListener("pointerup", onUp);
|
|
189
|
-
window.removeEventListener("pointercancel", onUp);
|
|
190
|
-
};
|
|
191
|
-
}, [isDragging, tool, color, stroke, rectRadius]);
|
|
192
|
-
const handleAnnoPointerDown = (e, anno) => {
|
|
193
|
-
if (tool !== "select") return;
|
|
194
|
-
e.stopPropagation();
|
|
195
|
-
setSelectedId(anno.id);
|
|
196
|
-
beginDrag({ kind: "move", id: anno.id, start: evToImage(e), original: anno });
|
|
197
|
-
};
|
|
198
|
-
const handleHandlePointerDown = (e, anno, corner) => {
|
|
199
|
-
e.stopPropagation();
|
|
200
|
-
beginDrag({ kind: "resize", id: anno.id, corner, start: evToImage(e), original: anno });
|
|
201
|
-
};
|
|
202
|
-
const handleSvgPointerDown = (e) => {
|
|
203
|
-
if (tool === "select") {
|
|
204
|
-
setSelectedId(null);
|
|
205
|
-
return;
|
|
206
|
-
}
|
|
207
|
-
if (tool === "text") {
|
|
208
|
-
const p = evToImage(e);
|
|
209
|
-
setPendingText({ x: p.x, y: p.y, value: "" });
|
|
210
|
-
return;
|
|
211
|
-
}
|
|
212
|
-
const start = evToImage(e);
|
|
213
|
-
if (tool === "crop") {
|
|
214
|
-
beginDrag({ kind: "crop", start });
|
|
215
|
-
return;
|
|
216
|
-
}
|
|
217
|
-
if (tool === "draw") {
|
|
218
|
-
beginDrag({ kind: "pen", points: [start] });
|
|
219
|
-
setPreview({ id: "", type: "draw", points: [start], color, stroke });
|
|
220
|
-
return;
|
|
221
|
-
}
|
|
222
|
-
setPreview(makeShape(tool, start, start, color, stroke, rectRadius));
|
|
223
|
-
beginDrag({ kind: "draw", start });
|
|
224
|
-
};
|
|
225
|
-
const handleAnnoDoubleClick = (anno) => {
|
|
226
|
-
if (anno.type !== "text") return;
|
|
227
|
-
setPendingText({ x: anno.x, y: anno.y, value: anno.text, editingId: anno.id });
|
|
228
|
-
};
|
|
229
|
-
useEffect(() => {
|
|
230
|
-
const onKey = (ev) => {
|
|
231
|
-
if (document.activeElement && /^(INPUT|TEXTAREA)$/.test(document.activeElement.tagName)) return;
|
|
232
|
-
if ((ev.metaKey || ev.ctrlKey) && (ev.key === "z" || ev.key === "Z") && !ev.shiftKey) {
|
|
233
|
-
ev.preventDefault();
|
|
234
|
-
setAnnotations((prev) => prev.slice(0, -1));
|
|
235
|
-
setSelectedId(null);
|
|
236
|
-
return;
|
|
237
|
-
}
|
|
238
|
-
if (!selectedId) return;
|
|
239
|
-
if (ev.key === "Delete" || ev.key === "Backspace") {
|
|
240
|
-
ev.preventDefault();
|
|
241
|
-
setAnnotations((prev) => prev.filter((a) => a.id !== selectedId));
|
|
242
|
-
setSelectedId(null);
|
|
243
|
-
} else if (ev.key === "Escape") {
|
|
244
|
-
setSelectedId(null);
|
|
245
|
-
}
|
|
246
|
-
};
|
|
247
|
-
window.addEventListener("keydown", onKey);
|
|
248
|
-
return () => window.removeEventListener("keydown", onKey);
|
|
249
|
-
}, [selectedId]);
|
|
250
|
-
const setColorAndApply = (c) => {
|
|
251
|
-
setColor(c);
|
|
252
|
-
if (!selectedId) return;
|
|
253
|
-
setAnnotations((prev) => prev.map((a) => {
|
|
254
|
-
if (a.id !== selectedId) return a;
|
|
255
|
-
if (a.type === "mosaic") return a;
|
|
256
|
-
return { ...a, color: c };
|
|
257
|
-
}));
|
|
258
|
-
};
|
|
259
|
-
const setStrokeAndApply = (s) => {
|
|
260
|
-
setStroke(s);
|
|
261
|
-
if (!selectedId) return;
|
|
262
|
-
setAnnotations((prev) => prev.map((a) => {
|
|
263
|
-
if (a.id !== selectedId) return a;
|
|
264
|
-
if (a.type === "rect" || a.type === "circle" || a.type === "arrow" || a.type === "draw") {
|
|
265
|
-
return { ...a, stroke: s };
|
|
266
|
-
}
|
|
267
|
-
return a;
|
|
268
|
-
}));
|
|
269
|
-
};
|
|
270
|
-
const setRectRadiusAndApply = (r) => {
|
|
271
|
-
setRectRadius(r);
|
|
272
|
-
if (!selectedId) return;
|
|
273
|
-
setAnnotations((prev) => prev.map(
|
|
274
|
-
(a) => a.id === selectedId && a.type === "rect" ? { ...a, radius: r } : a
|
|
275
|
-
));
|
|
276
|
-
};
|
|
277
|
-
const setTextSizeAndApply = (s) => {
|
|
278
|
-
setTextSize(s);
|
|
279
|
-
if (!selectedId) return;
|
|
280
|
-
setAnnotations((prev) => prev.map(
|
|
281
|
-
(a) => a.id === selectedId && a.type === "text" ? { ...a, size: s } : a
|
|
282
|
-
));
|
|
283
|
-
};
|
|
284
|
-
const setTextFontAndApply = (f) => {
|
|
285
|
-
setTextFont(f);
|
|
286
|
-
if (!selectedId) return;
|
|
287
|
-
setAnnotations((prev) => prev.map(
|
|
288
|
-
(a) => a.id === selectedId && a.type === "text" ? { ...a, font: f } : a
|
|
289
|
-
));
|
|
290
|
-
};
|
|
291
|
-
const toggleTextStyleAndApply = (which) => {
|
|
292
|
-
const next = !{ bold: textBold, italic: textItalic, underline: textUnderline }[which];
|
|
293
|
-
if (which === "bold") setTextBold(next);
|
|
294
|
-
if (which === "italic") setTextItalic(next);
|
|
295
|
-
if (which === "underline") setTextUnderline(next);
|
|
296
|
-
if (!selectedId) return;
|
|
297
|
-
setAnnotations((prev) => prev.map(
|
|
298
|
-
(a) => a.id === selectedId && a.type === "text" ? { ...a, [which]: next } : a
|
|
299
|
-
));
|
|
300
|
-
};
|
|
301
|
-
const commitText = () => {
|
|
302
|
-
if (!pendingText) return;
|
|
303
|
-
const value = pendingText.value;
|
|
304
|
-
if (!value.trim()) {
|
|
305
|
-
if (pendingText.editingId) {
|
|
306
|
-
setAnnotations((prev) => prev.filter((a) => a.id !== pendingText.editingId));
|
|
307
|
-
}
|
|
308
|
-
setPendingText(null);
|
|
309
|
-
return;
|
|
310
|
-
}
|
|
311
|
-
if (pendingText.editingId) {
|
|
312
|
-
setAnnotations((prev) => prev.map(
|
|
313
|
-
(a) => a.id === pendingText.editingId && a.type === "text" ? { ...a, text: value } : a
|
|
314
|
-
));
|
|
315
|
-
} else {
|
|
316
|
-
const anno = {
|
|
317
|
-
id: newId(),
|
|
318
|
-
type: "text",
|
|
319
|
-
x: pendingText.x,
|
|
320
|
-
y: pendingText.y,
|
|
321
|
-
text: value,
|
|
322
|
-
color,
|
|
323
|
-
size: textSize,
|
|
324
|
-
font: textFont,
|
|
325
|
-
bold: textBold,
|
|
326
|
-
italic: textItalic,
|
|
327
|
-
underline: textUnderline
|
|
328
|
-
};
|
|
329
|
-
setAnnotations((prev) => [...prev, anno]);
|
|
330
|
-
setSelectedId(anno.id);
|
|
331
|
-
setTool("select");
|
|
332
|
-
}
|
|
333
|
-
setPendingText(null);
|
|
334
|
-
};
|
|
335
|
-
const applyCrop = () => {
|
|
336
|
-
if (!pendingCrop || !imageRef.current || !imageSize) return;
|
|
337
|
-
const r = pendingCrop;
|
|
338
|
-
const tmp = document.createElement("canvas");
|
|
339
|
-
tmp.width = Math.round(r.w);
|
|
340
|
-
tmp.height = Math.round(r.h);
|
|
341
|
-
const tctx = tmp.getContext("2d");
|
|
342
|
-
const sourceCanvas = document.createElement("canvas");
|
|
343
|
-
sourceCanvas.width = imageSize.w;
|
|
344
|
-
sourceCanvas.height = imageSize.h;
|
|
345
|
-
const sctx = sourceCanvas.getContext("2d");
|
|
346
|
-
sctx.drawImage(imageRef.current, 0, 0);
|
|
347
|
-
for (const m of mosaicAnnos) applyMosaic(sctx, m);
|
|
348
|
-
tctx.drawImage(sourceCanvas, r.x, r.y, r.w, r.h, 0, 0, r.w, r.h);
|
|
349
|
-
const newImg = new Image();
|
|
350
|
-
newImg.onload = () => {
|
|
351
|
-
imageRef.current = newImg;
|
|
352
|
-
setImageSize({ w: newImg.naturalWidth, h: newImg.naturalHeight });
|
|
353
|
-
setAnnotations(
|
|
354
|
-
(prev) => prev.filter((a) => a.type !== "mosaic").map((a) => translate(a, -r.x, -r.y)).filter((a) => withinBounds(a, newImg.naturalWidth, newImg.naturalHeight))
|
|
355
|
-
);
|
|
356
|
-
setPendingCrop(null);
|
|
357
|
-
};
|
|
358
|
-
newImg.src = tmp.toDataURL("image/png");
|
|
359
|
-
};
|
|
360
|
-
const cancelCrop = () => setPendingCrop(null);
|
|
361
|
-
const undoLast = () => {
|
|
362
|
-
setAnnotations((prev) => prev.slice(0, -1));
|
|
363
|
-
setSelectedId(null);
|
|
364
|
-
};
|
|
365
|
-
const compositeToCanvas = async () => {
|
|
366
|
-
const c = canvasRef.current;
|
|
367
|
-
const svg = svgRef.current;
|
|
368
|
-
if (!c || !svg || !imageSize) return null;
|
|
369
|
-
const out = document.createElement("canvas");
|
|
370
|
-
out.width = imageSize.w;
|
|
371
|
-
out.height = imageSize.h;
|
|
372
|
-
const octx = out.getContext("2d");
|
|
373
|
-
octx.drawImage(c, 0, 0);
|
|
374
|
-
const clone = svg.cloneNode(true);
|
|
375
|
-
clone.querySelectorAll("[data-chrome]").forEach((n) => n.remove());
|
|
376
|
-
const xml = new XMLSerializer().serializeToString(clone);
|
|
377
|
-
const svgBlob = new Blob([xml], { type: "image/svg+xml;charset=utf-8" });
|
|
378
|
-
const svgUrl = URL.createObjectURL(svgBlob);
|
|
379
|
-
const svgImg = new Image();
|
|
380
|
-
await new Promise((resolve) => {
|
|
381
|
-
svgImg.onload = () => resolve();
|
|
382
|
-
svgImg.onerror = () => resolve();
|
|
383
|
-
svgImg.src = svgUrl;
|
|
384
|
-
});
|
|
385
|
-
octx.drawImage(svgImg, 0, 0, imageSize.w, imageSize.h);
|
|
386
|
-
URL.revokeObjectURL(svgUrl);
|
|
387
|
-
return out;
|
|
388
|
-
};
|
|
389
|
-
useImperativeHandle(ref, () => ({
|
|
390
|
-
save: async () => {
|
|
391
|
-
const out = await compositeToCanvas();
|
|
392
|
-
if (!out) return;
|
|
393
|
-
out.toBlob((blob) => {
|
|
394
|
-
if (!blob) {
|
|
395
|
-
toast_default.error("Failed to export");
|
|
396
|
-
return;
|
|
397
|
-
}
|
|
398
|
-
const url = URL.createObjectURL(blob);
|
|
399
|
-
const a = document.createElement("a");
|
|
400
|
-
a.href = url;
|
|
401
|
-
const base = filename.replace(/\.[^.]+$/, "");
|
|
402
|
-
a.download = `${base}-annotated.png`;
|
|
403
|
-
a.click();
|
|
404
|
-
URL.revokeObjectURL(url);
|
|
405
|
-
}, "image/png");
|
|
406
|
-
},
|
|
407
|
-
copy: async () => {
|
|
408
|
-
if (!("clipboard" in navigator) || typeof ClipboardItem === "undefined") {
|
|
409
|
-
toast_default.error("Clipboard images not supported in this browser");
|
|
410
|
-
return;
|
|
411
|
-
}
|
|
412
|
-
const out = await compositeToCanvas();
|
|
413
|
-
if (!out) return;
|
|
414
|
-
out.toBlob(async (blob) => {
|
|
415
|
-
if (!blob) {
|
|
416
|
-
toast_default.error("Failed to copy");
|
|
417
|
-
return;
|
|
418
|
-
}
|
|
419
|
-
try {
|
|
420
|
-
await navigator.clipboard.write([new ClipboardItem({ "image/png": blob })]);
|
|
421
|
-
toast_default.success("Copied to clipboard");
|
|
422
|
-
} catch {
|
|
423
|
-
toast_default.error("Copy failed (clipboard permission?)");
|
|
424
|
-
}
|
|
425
|
-
}, "image/png");
|
|
426
|
-
}
|
|
427
|
-
}), [imageSize, filename]);
|
|
428
|
-
const tools = useMemo(() => [
|
|
429
|
-
{ id: "select", label: "Select / Move", icon: /* @__PURE__ */ jsx("svg", { className: "h-4 w-4", fill: "none", viewBox: "0 0 24 24", stroke: "currentColor", strokeWidth: 1.8, children: /* @__PURE__ */ jsx("path", { strokeLinecap: "round", strokeLinejoin: "round", d: "M5 3l7 17 2-7 7-2L5 3z" }) }) },
|
|
430
|
-
{ id: "draw", label: "Pen / Draw", icon: /* @__PURE__ */ jsx("svg", { className: "h-4 w-4", fill: "none", viewBox: "0 0 24 24", stroke: "currentColor", strokeWidth: 1.8, children: /* @__PURE__ */ jsx("path", { strokeLinecap: "round", strokeLinejoin: "round", d: "M16.862 4.487l1.687-1.688a1.875 1.875 0 112.652 2.652L6.832 19.82a4.5 4.5 0 01-1.897 1.13l-2.685.8.8-2.685a4.5 4.5 0 011.13-1.897L16.863 4.487z" }) }) },
|
|
431
|
-
{ id: "rect", label: "Rectangle", icon: /* @__PURE__ */ jsx("svg", { className: "h-4 w-4", fill: "none", viewBox: "0 0 24 24", stroke: "currentColor", strokeWidth: 1.8, children: /* @__PURE__ */ jsx("rect", { x: "4", y: "6", width: "16", height: "12", rx: "3" }) }) },
|
|
432
|
-
{ id: "circle", label: "Ellipse", icon: /* @__PURE__ */ jsx("svg", { className: "h-4 w-4", fill: "none", viewBox: "0 0 24 24", stroke: "currentColor", strokeWidth: 1.8, children: /* @__PURE__ */ jsx("ellipse", { cx: "12", cy: "12", rx: "8", ry: "6" }) }) },
|
|
433
|
-
{ id: "arrow", label: "Arrow", icon: /* @__PURE__ */ jsx("svg", { className: "h-4 w-4", fill: "none", viewBox: "0 0 24 24", stroke: "currentColor", strokeWidth: 1.8, children: /* @__PURE__ */ jsx("path", { strokeLinecap: "round", strokeLinejoin: "round", d: "M5 19L19 5m0 0h-7m7 0v7" }) }) },
|
|
434
|
-
{ id: "mosaic", label: "Mosaic", icon: /* @__PURE__ */ jsx("svg", { className: "h-4 w-4", fill: "none", viewBox: "0 0 24 24", stroke: "currentColor", strokeWidth: 1.8, children: /* @__PURE__ */ jsx("path", { strokeLinecap: "round", strokeLinejoin: "round", d: "M4 4h6v6H4zM14 4h6v6h-6zM4 14h6v6H4zM14 14h6v6h-6z" }) }) },
|
|
435
|
-
{ id: "text", label: "Text", icon: /* @__PURE__ */ jsx("svg", { className: "h-4 w-4", fill: "none", viewBox: "0 0 24 24", stroke: "currentColor", strokeWidth: 1.8, children: /* @__PURE__ */ jsx("path", { strokeLinecap: "round", strokeLinejoin: "round", d: "M4 6h16M12 6v14M9 20h6" }) }) },
|
|
436
|
-
{ id: "crop", label: "Crop", icon: /* @__PURE__ */ jsx("svg", { className: "h-4 w-4", fill: "none", viewBox: "0 0 24 24", stroke: "currentColor", strokeWidth: 1.8, children: /* @__PURE__ */ jsx("path", { strokeLinecap: "round", strokeLinejoin: "round", d: "M6 2v15a1 1 0 001 1h15M2 6h15a1 1 0 011 1v15" }) }) }
|
|
437
|
-
], []);
|
|
438
|
-
const btnClass = (active) => `p-1.5 rounded transition-colors ${active ? "bg-blue-500 text-white" : "text-gray-700 hover:bg-gray-200"}`;
|
|
439
|
-
const ctxType = selected?.type ?? (tool === "select" ? null : tool);
|
|
440
|
-
const showStrokeControl = ctxType === "rect" || ctxType === "circle" || ctxType === "arrow" || ctxType === "draw";
|
|
441
|
-
const showRectRadius = ctxType === "rect";
|
|
442
|
-
const showTextControls = ctxType === "text";
|
|
443
|
-
return /* @__PURE__ */ jsxs("div", { className: "flex flex-col h-full bg-gray-100", children: [
|
|
444
|
-
/* @__PURE__ */ jsxs("div", { className: "flex items-center gap-1.5 px-3 py-2 border-b border-gray-200 bg-white shrink-0 flex-wrap text-[12px]", children: [
|
|
445
|
-
tools.map((t) => /* @__PURE__ */ jsx(
|
|
446
|
-
"button",
|
|
447
|
-
{
|
|
448
|
-
onClick: () => {
|
|
449
|
-
setTool(t.id);
|
|
450
|
-
setSelectedId(null);
|
|
451
|
-
},
|
|
452
|
-
title: t.label,
|
|
453
|
-
className: btnClass(tool === t.id),
|
|
454
|
-
children: t.icon
|
|
455
|
-
},
|
|
456
|
-
t.id
|
|
457
|
-
)),
|
|
458
|
-
/* @__PURE__ */ jsx("div", { className: "h-5 w-px bg-gray-300 mx-1" }),
|
|
459
|
-
/* @__PURE__ */ jsx("div", { className: "flex items-center gap-1", children: COLORS.map((c) => /* @__PURE__ */ jsx(
|
|
460
|
-
"button",
|
|
461
|
-
{
|
|
462
|
-
onClick: () => setColorAndApply(c),
|
|
463
|
-
title: c,
|
|
464
|
-
className: `h-5 w-5 rounded-full border ${color === c ? "ring-2 ring-blue-500 ring-offset-1" : "border-gray-300"}`,
|
|
465
|
-
style: { background: c }
|
|
466
|
-
},
|
|
467
|
-
c
|
|
468
|
-
)) }),
|
|
469
|
-
showStrokeControl && /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
470
|
-
/* @__PURE__ */ jsx("div", { className: "h-5 w-px bg-gray-300 mx-1" }),
|
|
471
|
-
/* @__PURE__ */ jsxs("label", { className: "flex items-center gap-1.5 text-gray-600", children: [
|
|
472
|
-
/* @__PURE__ */ jsx("span", { children: "Weight" }),
|
|
473
|
-
/* @__PURE__ */ jsx("input", { type: "range", min: 1, max: 20, step: 1, value: stroke, onChange: (e) => setStrokeAndApply(Number(e.target.value)), className: "w-16 accent-blue-500" }),
|
|
474
|
-
/* @__PURE__ */ jsx("span", { className: "tabular-nums w-5 text-right", children: stroke })
|
|
475
|
-
] })
|
|
476
|
-
] }),
|
|
477
|
-
showRectRadius && /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
478
|
-
/* @__PURE__ */ jsx("div", { className: "h-5 w-px bg-gray-300 mx-1" }),
|
|
479
|
-
/* @__PURE__ */ jsxs("label", { className: "flex items-center gap-1.5 text-gray-600", children: [
|
|
480
|
-
/* @__PURE__ */ jsx("span", { children: "Radius" }),
|
|
481
|
-
/* @__PURE__ */ jsx("input", { type: "range", min: 0, max: 48, step: 1, value: rectRadius, onChange: (e) => setRectRadiusAndApply(Number(e.target.value)), className: "w-16 accent-blue-500" }),
|
|
482
|
-
/* @__PURE__ */ jsx("span", { className: "tabular-nums w-6 text-right", children: rectRadius })
|
|
483
|
-
] })
|
|
484
|
-
] }),
|
|
485
|
-
showTextControls && /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
486
|
-
/* @__PURE__ */ jsx("div", { className: "h-5 w-px bg-gray-300 mx-1" }),
|
|
487
|
-
/* @__PURE__ */ jsx("select", { value: textFont, onChange: (e) => setTextFontAndApply(e.target.value), className: "text-xs border border-gray-300 rounded px-1 py-0.5 bg-white", children: FONTS.map((f) => /* @__PURE__ */ jsx("option", { value: f.id, children: f.label }, f.id)) }),
|
|
488
|
-
/* @__PURE__ */ jsx("button", { onClick: () => toggleTextStyleAndApply("bold"), className: btnClass(textBold), title: "Bold", children: /* @__PURE__ */ jsx("span", { className: "font-bold", children: "B" }) }),
|
|
489
|
-
/* @__PURE__ */ jsx("button", { onClick: () => toggleTextStyleAndApply("italic"), className: btnClass(textItalic), title: "Italic", children: /* @__PURE__ */ jsx("span", { className: "italic", children: "I" }) }),
|
|
490
|
-
/* @__PURE__ */ jsx("button", { onClick: () => toggleTextStyleAndApply("underline"), className: btnClass(textUnderline), title: "Underline", children: /* @__PURE__ */ jsx("span", { className: "underline", children: "U" }) }),
|
|
491
|
-
/* @__PURE__ */ jsxs("label", { className: "flex items-center gap-1.5 text-gray-600", children: [
|
|
492
|
-
/* @__PURE__ */ jsx("span", { children: "Size" }),
|
|
493
|
-
/* @__PURE__ */ jsx("input", { type: "range", min: 10, max: 96, step: 1, value: textSize, onChange: (e) => setTextSizeAndApply(Number(e.target.value)), className: "w-16 accent-blue-500" }),
|
|
494
|
-
/* @__PURE__ */ jsx("span", { className: "tabular-nums w-7 text-right", children: textSize })
|
|
495
|
-
] })
|
|
496
|
-
] }),
|
|
497
|
-
/* @__PURE__ */ jsx("div", { className: "h-5 w-px bg-gray-300 mx-1" }),
|
|
498
|
-
/* @__PURE__ */ jsx("button", { onClick: () => setZoom((z) => Math.max(ZOOM_MIN, Math.round((z - ZOOM_STEP) * 100) / 100)), className: "px-2 py-1 rounded hover:bg-gray-200 text-gray-700", title: "Zoom out", children: "\u2212" }),
|
|
499
|
-
/* @__PURE__ */ jsxs("span", { className: "text-gray-600 tabular-nums w-10 text-center", children: [
|
|
500
|
-
Math.round(zoom * 100),
|
|
501
|
-
"%"
|
|
502
|
-
] }),
|
|
503
|
-
/* @__PURE__ */ jsx("button", { onClick: () => setZoom((z) => Math.min(ZOOM_MAX, Math.round((z + ZOOM_STEP) * 100) / 100)), className: "px-2 py-1 rounded hover:bg-gray-200 text-gray-700", title: "Zoom in", children: "+" }),
|
|
504
|
-
/* @__PURE__ */ jsx("button", { onClick: () => setZoom(1), className: "px-2 py-1 rounded hover:bg-gray-200 text-gray-700", title: "Fit to area", children: "Fit" }),
|
|
505
|
-
/* @__PURE__ */ jsx("div", { className: "h-5 w-px bg-gray-300 mx-1" }),
|
|
506
|
-
/* @__PURE__ */ jsx("button", { onClick: undoLast, disabled: annotations.length === 0, className: "px-2 py-1 rounded hover:bg-gray-200 disabled:opacity-30 text-gray-700", children: "Undo" }),
|
|
507
|
-
/* @__PURE__ */ jsxs("div", { className: "ml-auto flex items-center gap-2", children: [
|
|
508
|
-
pendingCrop && /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
509
|
-
/* @__PURE__ */ jsx("button", { onClick: applyCrop, className: "px-2 py-1 rounded bg-blue-500 text-white hover:bg-blue-600", children: "Apply Crop" }),
|
|
510
|
-
/* @__PURE__ */ jsx("button", { onClick: cancelCrop, className: "px-2 py-1 rounded hover:bg-gray-200 text-gray-700", children: "Cancel" })
|
|
511
|
-
] }),
|
|
512
|
-
onApply && !pendingCrop && /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
513
|
-
onCancel && /* @__PURE__ */ jsx("button", { onClick: onCancel, className: "px-3 py-1 rounded hover:bg-gray-200 text-gray-700 text-xs font-medium", children: "Cancel" }),
|
|
514
|
-
/* @__PURE__ */ jsx(
|
|
515
|
-
"button",
|
|
516
|
-
{
|
|
517
|
-
onClick: async () => {
|
|
518
|
-
const out = await compositeToCanvas();
|
|
519
|
-
if (!out) {
|
|
520
|
-
toast_default.error("Failed to export");
|
|
521
|
-
return;
|
|
522
|
-
}
|
|
523
|
-
out.toBlob((blob) => {
|
|
524
|
-
if (!blob) {
|
|
525
|
-
toast_default.error("Failed to export");
|
|
526
|
-
return;
|
|
527
|
-
}
|
|
528
|
-
onApply(blob);
|
|
529
|
-
}, "image/png");
|
|
530
|
-
},
|
|
531
|
-
className: "px-3 py-1 rounded bg-blue-500 text-white hover:bg-blue-600 text-xs font-medium",
|
|
532
|
-
children: "Apply"
|
|
533
|
-
}
|
|
534
|
-
)
|
|
535
|
-
] })
|
|
536
|
-
] })
|
|
537
|
-
] }),
|
|
538
|
-
/* @__PURE__ */ jsx("div", { ref: wrapRef, className: "flex-1 overflow-auto bg-gray-200 flex items-center justify-center p-4 relative", children: displaySize && imageSize && /* @__PURE__ */ jsxs(
|
|
539
|
-
"div",
|
|
540
|
-
{
|
|
541
|
-
className: "relative shadow-lg rounded overflow-hidden bg-white shrink-0",
|
|
542
|
-
style: { width: displaySize.w, height: displaySize.h },
|
|
543
|
-
children: [
|
|
544
|
-
/* @__PURE__ */ jsx("canvas", { ref: canvasRef, style: { position: "absolute", inset: 0, width: "100%", height: "100%", display: "block" } }),
|
|
545
|
-
/* @__PURE__ */ jsxs(
|
|
546
|
-
"svg",
|
|
547
|
-
{
|
|
548
|
-
ref: svgRef,
|
|
549
|
-
viewBox: `0 0 ${imageSize.w} ${imageSize.h}`,
|
|
550
|
-
preserveAspectRatio: "none",
|
|
551
|
-
style: {
|
|
552
|
-
position: "absolute",
|
|
553
|
-
inset: 0,
|
|
554
|
-
width: "100%",
|
|
555
|
-
height: "100%",
|
|
556
|
-
touchAction: "none",
|
|
557
|
-
cursor: tool === "select" ? "default" : tool === "text" ? "text" : "crosshair"
|
|
558
|
-
},
|
|
559
|
-
onPointerDown: handleSvgPointerDown,
|
|
560
|
-
children: [
|
|
561
|
-
annotations.map((a) => /* @__PURE__ */ jsx(
|
|
562
|
-
AnnotationView,
|
|
563
|
-
{
|
|
564
|
-
anno: a,
|
|
565
|
-
selected: selectedId === a.id,
|
|
566
|
-
zoom,
|
|
567
|
-
onPointerDown: (e) => handleAnnoPointerDown(e, a),
|
|
568
|
-
onDoubleClick: () => handleAnnoDoubleClick(a),
|
|
569
|
-
onHandlePointerDown: (e, corner) => handleHandlePointerDown(e, a, corner)
|
|
570
|
-
},
|
|
571
|
-
a.id
|
|
572
|
-
)),
|
|
573
|
-
preview && /* @__PURE__ */ jsx(AnnotationView, { anno: { ...preview, id: "__preview" }, preview: true }),
|
|
574
|
-
pendingCrop && /* @__PURE__ */ jsx(CropOverlay, { rect: pendingCrop, imageSize })
|
|
575
|
-
]
|
|
576
|
-
}
|
|
577
|
-
),
|
|
578
|
-
pendingText && /* @__PURE__ */ jsx(
|
|
579
|
-
PendingTextEditor,
|
|
580
|
-
{
|
|
581
|
-
pendingText,
|
|
582
|
-
color,
|
|
583
|
-
size: textSize,
|
|
584
|
-
font: FONTS.find((f) => f.id === textFont)?.css ?? FONTS[0].css,
|
|
585
|
-
bold: textBold,
|
|
586
|
-
italic: textItalic,
|
|
587
|
-
underline: textUnderline,
|
|
588
|
-
scale,
|
|
589
|
-
onChange: (value) => setPendingText({ ...pendingText, value }),
|
|
590
|
-
onCommit: commitText,
|
|
591
|
-
onCancel: () => setPendingText(null)
|
|
592
|
-
}
|
|
593
|
-
)
|
|
594
|
-
]
|
|
595
|
-
}
|
|
596
|
-
) }),
|
|
597
|
-
/* @__PURE__ */ jsx("div", { className: "px-3 py-1.5 border-t border-gray-200 bg-white text-[11px] text-gray-500 shrink-0", children: tool === "select" ? selectedId ? "Drag to move. Drag a corner to resize. Delete / Backspace removes. Click outside to deselect." : "Tap a shape to select. Double-click text to edit." : tool === "text" ? "Click to drop a text label." : tool === "crop" ? "Drag a rectangle to set the crop region." : tool === "draw" ? "Drag to draw freehand." : "Drag on the image to draw." })
|
|
598
|
-
] });
|
|
599
|
-
});
|
|
600
|
-
var ImageAnnotator_default = ImageAnnotator;
|
|
601
|
-
function AnnotationView({ anno, selected, preview, zoom = 1, onPointerDown, onDoubleClick, onHandlePointerDown }) {
|
|
602
|
-
const dim = boundingBox(anno);
|
|
603
|
-
const interactive = onPointerDown ? { onPointerDown, pointerEvents: "all", style: { cursor: "move" } } : {};
|
|
604
|
-
const dblc = onDoubleClick ? { onDoubleClick } : {};
|
|
605
|
-
let body;
|
|
606
|
-
if (anno.type === "rect") {
|
|
607
|
-
body = /* @__PURE__ */ jsx(
|
|
608
|
-
"rect",
|
|
609
|
-
{
|
|
610
|
-
x: anno.x,
|
|
611
|
-
y: anno.y,
|
|
612
|
-
width: anno.w,
|
|
613
|
-
height: anno.h,
|
|
614
|
-
rx: anno.radius,
|
|
615
|
-
ry: anno.radius,
|
|
616
|
-
fill: "none",
|
|
617
|
-
stroke: anno.color,
|
|
618
|
-
strokeWidth: anno.stroke,
|
|
619
|
-
strokeLinecap: "round",
|
|
620
|
-
strokeLinejoin: "round",
|
|
621
|
-
...interactive
|
|
622
|
-
}
|
|
623
|
-
);
|
|
624
|
-
} else if (anno.type === "circle") {
|
|
625
|
-
body = /* @__PURE__ */ jsx(
|
|
626
|
-
"ellipse",
|
|
627
|
-
{
|
|
628
|
-
cx: anno.x + anno.w / 2,
|
|
629
|
-
cy: anno.y + anno.h / 2,
|
|
630
|
-
rx: anno.w / 2,
|
|
631
|
-
ry: anno.h / 2,
|
|
632
|
-
fill: "none",
|
|
633
|
-
stroke: anno.color,
|
|
634
|
-
strokeWidth: anno.stroke,
|
|
635
|
-
...interactive
|
|
636
|
-
}
|
|
637
|
-
);
|
|
638
|
-
} else if (anno.type === "arrow") {
|
|
639
|
-
body = /* @__PURE__ */ jsx(ArrowShape, { anno, interactive });
|
|
640
|
-
} else if (anno.type === "draw") {
|
|
641
|
-
body = /* @__PURE__ */ jsx(
|
|
642
|
-
"path",
|
|
643
|
-
{
|
|
644
|
-
d: pointsToPath(anno.points),
|
|
645
|
-
fill: "none",
|
|
646
|
-
stroke: anno.color,
|
|
647
|
-
strokeWidth: anno.stroke,
|
|
648
|
-
strokeLinecap: "round",
|
|
649
|
-
strokeLinejoin: "round",
|
|
650
|
-
...interactive
|
|
651
|
-
}
|
|
652
|
-
);
|
|
653
|
-
} else if (anno.type === "mosaic") {
|
|
654
|
-
body = /* @__PURE__ */ jsx(
|
|
655
|
-
"rect",
|
|
656
|
-
{
|
|
657
|
-
x: anno.x,
|
|
658
|
-
y: anno.y,
|
|
659
|
-
width: anno.w,
|
|
660
|
-
height: anno.h,
|
|
661
|
-
fill: "rgba(0,0,0,0.001)",
|
|
662
|
-
stroke: selected ? "#3b82f6" : "none",
|
|
663
|
-
strokeWidth: selected ? 2 : 0,
|
|
664
|
-
strokeDasharray: selected ? "6,4" : void 0,
|
|
665
|
-
...interactive
|
|
666
|
-
}
|
|
667
|
-
);
|
|
668
|
-
} else {
|
|
669
|
-
const font = FONTS.find((f) => f.id === anno.font)?.css ?? FONTS[0].css;
|
|
670
|
-
body = /* @__PURE__ */ jsx(
|
|
671
|
-
"text",
|
|
672
|
-
{
|
|
673
|
-
x: anno.x,
|
|
674
|
-
y: anno.y + anno.size,
|
|
675
|
-
fill: anno.color,
|
|
676
|
-
fontSize: anno.size,
|
|
677
|
-
fontWeight: anno.bold ? 700 : 400,
|
|
678
|
-
fontStyle: anno.italic ? "italic" : "normal",
|
|
679
|
-
textDecoration: anno.underline ? "underline" : void 0,
|
|
680
|
-
fontFamily: font,
|
|
681
|
-
style: { userSelect: "none" },
|
|
682
|
-
...interactive,
|
|
683
|
-
...dblc,
|
|
684
|
-
children: anno.text.split("\n").map((line, i) => /* @__PURE__ */ jsx("tspan", { x: anno.x, dy: i === 0 ? 0 : anno.size * 1.2, children: line }, i))
|
|
685
|
-
}
|
|
686
|
-
);
|
|
687
|
-
}
|
|
688
|
-
return /* @__PURE__ */ jsxs("g", { children: [
|
|
689
|
-
body,
|
|
690
|
-
selected && !preview && /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
691
|
-
/* @__PURE__ */ jsx(
|
|
692
|
-
"rect",
|
|
693
|
-
{
|
|
694
|
-
"data-chrome": "selection",
|
|
695
|
-
x: dim.x - 4,
|
|
696
|
-
y: dim.y - 4,
|
|
697
|
-
width: dim.w + 8,
|
|
698
|
-
height: dim.h + 8,
|
|
699
|
-
fill: "none",
|
|
700
|
-
stroke: "#3b82f6",
|
|
701
|
-
strokeWidth: 2 / zoom,
|
|
702
|
-
strokeDasharray: `${6 / zoom},${4 / zoom}`,
|
|
703
|
-
pointerEvents: "none"
|
|
704
|
-
}
|
|
705
|
-
),
|
|
706
|
-
onHandlePointerDown && /* @__PURE__ */ jsx(ResizeHandles, { anno, zoom, onHandlePointerDown })
|
|
707
|
-
] })
|
|
708
|
-
] });
|
|
709
|
-
}
|
|
710
|
-
function ResizeHandles({
|
|
711
|
-
anno,
|
|
712
|
-
zoom,
|
|
713
|
-
onHandlePointerDown
|
|
714
|
-
}) {
|
|
715
|
-
const r = 6 / zoom;
|
|
716
|
-
const sw = 1.5 / zoom;
|
|
717
|
-
const handleProps = (cursor) => ({
|
|
718
|
-
fill: "#fff",
|
|
719
|
-
stroke: "#3b82f6",
|
|
720
|
-
strokeWidth: sw,
|
|
721
|
-
pointerEvents: "all",
|
|
722
|
-
style: { cursor },
|
|
723
|
-
"data-chrome": "handle"
|
|
724
|
-
});
|
|
725
|
-
if (anno.type === "arrow") {
|
|
726
|
-
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
727
|
-
/* @__PURE__ */ jsx("circle", { cx: anno.x1, cy: anno.y1, r, ...handleProps("grab"), onPointerDown: (e) => onHandlePointerDown(e, "start") }),
|
|
728
|
-
/* @__PURE__ */ jsx("circle", { cx: anno.x2, cy: anno.y2, r, ...handleProps("grab"), onPointerDown: (e) => onHandlePointerDown(e, "end") })
|
|
729
|
-
] });
|
|
730
|
-
}
|
|
731
|
-
if (anno.type === "text" || anno.type === "draw") {
|
|
732
|
-
return null;
|
|
733
|
-
}
|
|
734
|
-
const x = anno.x, y = anno.y, w = anno.w, h = anno.h;
|
|
735
|
-
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
736
|
-
/* @__PURE__ */ jsx("circle", { cx: x, cy: y, r, ...handleProps("nwse-resize"), onPointerDown: (e) => onHandlePointerDown(e, "nw") }),
|
|
737
|
-
/* @__PURE__ */ jsx("circle", { cx: x + w, cy: y, r, ...handleProps("nesw-resize"), onPointerDown: (e) => onHandlePointerDown(e, "ne") }),
|
|
738
|
-
/* @__PURE__ */ jsx("circle", { cx: x + w, cy: y + h, r, ...handleProps("nwse-resize"), onPointerDown: (e) => onHandlePointerDown(e, "se") }),
|
|
739
|
-
/* @__PURE__ */ jsx("circle", { cx: x, cy: y + h, r, ...handleProps("nesw-resize"), onPointerDown: (e) => onHandlePointerDown(e, "sw") })
|
|
740
|
-
] });
|
|
741
|
-
}
|
|
742
|
-
function ArrowShape({ anno, interactive }) {
|
|
743
|
-
const headLen = Math.max(12, anno.stroke * 4);
|
|
744
|
-
const angle = Math.atan2(anno.y2 - anno.y1, anno.x2 - anno.x1);
|
|
745
|
-
const a1 = angle + Math.PI - Math.PI / 7;
|
|
746
|
-
const a2 = angle + Math.PI + Math.PI / 7;
|
|
747
|
-
const head = `M${anno.x2},${anno.y2} L${anno.x2 + headLen * Math.cos(a1)},${anno.y2 + headLen * Math.sin(a1)} L${anno.x2 + headLen * Math.cos(a2)},${anno.y2 + headLen * Math.sin(a2)} Z`;
|
|
748
|
-
return /* @__PURE__ */ jsxs("g", { ...interactive, children: [
|
|
749
|
-
/* @__PURE__ */ jsx(
|
|
750
|
-
"line",
|
|
751
|
-
{
|
|
752
|
-
x1: anno.x1,
|
|
753
|
-
y1: anno.y1,
|
|
754
|
-
x2: anno.x2,
|
|
755
|
-
y2: anno.y2,
|
|
756
|
-
stroke: anno.color,
|
|
757
|
-
strokeWidth: anno.stroke,
|
|
758
|
-
strokeLinecap: "round"
|
|
759
|
-
}
|
|
760
|
-
),
|
|
761
|
-
/* @__PURE__ */ jsx("path", { d: head, fill: anno.color })
|
|
762
|
-
] });
|
|
763
|
-
}
|
|
764
|
-
function CropOverlay({ rect, imageSize }) {
|
|
765
|
-
return /* @__PURE__ */ jsxs("g", { pointerEvents: "none", children: [
|
|
766
|
-
/* @__PURE__ */ jsx(
|
|
767
|
-
"path",
|
|
768
|
-
{
|
|
769
|
-
d: `M0,0 H${imageSize.w} V${imageSize.h} H0 Z M${rect.x},${rect.y} V${rect.y + rect.h} H${rect.x + rect.w} V${rect.y} Z`,
|
|
770
|
-
fill: "rgba(0,0,0,0.45)",
|
|
771
|
-
fillRule: "evenodd"
|
|
772
|
-
}
|
|
773
|
-
),
|
|
774
|
-
/* @__PURE__ */ jsx(
|
|
775
|
-
"rect",
|
|
776
|
-
{
|
|
777
|
-
x: rect.x,
|
|
778
|
-
y: rect.y,
|
|
779
|
-
width: rect.w,
|
|
780
|
-
height: rect.h,
|
|
781
|
-
fill: "none",
|
|
782
|
-
stroke: "#fff",
|
|
783
|
-
strokeWidth: 2,
|
|
784
|
-
strokeDasharray: "8,6"
|
|
785
|
-
}
|
|
786
|
-
)
|
|
787
|
-
] });
|
|
788
|
-
}
|
|
789
|
-
function PendingTextEditor({
|
|
790
|
-
pendingText,
|
|
791
|
-
color,
|
|
792
|
-
size,
|
|
793
|
-
font,
|
|
794
|
-
bold,
|
|
795
|
-
italic,
|
|
796
|
-
underline,
|
|
797
|
-
scale,
|
|
798
|
-
onChange,
|
|
799
|
-
onCommit,
|
|
800
|
-
onCancel
|
|
801
|
-
}) {
|
|
802
|
-
const ref = useRef(null);
|
|
803
|
-
useEffect(() => {
|
|
804
|
-
const id = requestAnimationFrame(() => {
|
|
805
|
-
ref.current?.focus();
|
|
806
|
-
ref.current?.select?.();
|
|
807
|
-
});
|
|
808
|
-
return () => cancelAnimationFrame(id);
|
|
809
|
-
}, []);
|
|
810
|
-
return /* @__PURE__ */ jsx(
|
|
811
|
-
"div",
|
|
812
|
-
{
|
|
813
|
-
style: {
|
|
814
|
-
position: "absolute",
|
|
815
|
-
left: `${pendingText.x * scale}px`,
|
|
816
|
-
top: `${pendingText.y * scale}px`,
|
|
817
|
-
transform: "translateY(-2px)",
|
|
818
|
-
zIndex: 5
|
|
819
|
-
},
|
|
820
|
-
onPointerDown: (e) => e.stopPropagation(),
|
|
821
|
-
children: /* @__PURE__ */ jsx(
|
|
822
|
-
"textarea",
|
|
823
|
-
{
|
|
824
|
-
ref,
|
|
825
|
-
value: pendingText.value,
|
|
826
|
-
onChange: (e) => onChange(e.target.value),
|
|
827
|
-
onBlur: onCommit,
|
|
828
|
-
onKeyDown: (e) => {
|
|
829
|
-
if (e.key === "Escape") {
|
|
830
|
-
e.preventDefault();
|
|
831
|
-
onCancel();
|
|
832
|
-
} else if (e.key === "Enter" && !e.shiftKey) {
|
|
833
|
-
e.preventDefault();
|
|
834
|
-
onCommit();
|
|
835
|
-
}
|
|
836
|
-
},
|
|
837
|
-
placeholder: "Type then Enter\u2026",
|
|
838
|
-
rows: 1,
|
|
839
|
-
className: "bg-white/95 border border-blue-400 rounded px-1 py-0.5 outline-none resize-none shadow-md",
|
|
840
|
-
style: {
|
|
841
|
-
color,
|
|
842
|
-
fontSize: `${size * scale}px`,
|
|
843
|
-
fontFamily: font,
|
|
844
|
-
fontWeight: bold ? 700 : 400,
|
|
845
|
-
fontStyle: italic ? "italic" : "normal",
|
|
846
|
-
textDecoration: underline ? "underline" : void 0,
|
|
847
|
-
minWidth: 80
|
|
848
|
-
}
|
|
849
|
-
}
|
|
850
|
-
)
|
|
851
|
-
}
|
|
852
|
-
);
|
|
853
|
-
}
|
|
854
|
-
function makeShape(tool, start, end, color, stroke, rectRadius) {
|
|
855
|
-
if (tool === "rect") {
|
|
856
|
-
const r = normalizeRect(start, end);
|
|
857
|
-
return { id: "", type: "rect", x: r.x, y: r.y, w: r.w, h: r.h, color, stroke, radius: rectRadius };
|
|
858
|
-
}
|
|
859
|
-
if (tool === "circle") {
|
|
860
|
-
const r = normalizeRect(start, end);
|
|
861
|
-
return { id: "", type: "circle", x: r.x, y: r.y, w: r.w, h: r.h, color, stroke };
|
|
862
|
-
}
|
|
863
|
-
if (tool === "arrow") {
|
|
864
|
-
return { id: "", type: "arrow", x1: start.x, y1: start.y, x2: end.x, y2: end.y, color, stroke };
|
|
865
|
-
}
|
|
866
|
-
if (tool === "mosaic") {
|
|
867
|
-
const r = normalizeRect(start, end);
|
|
868
|
-
return { id: "", type: "mosaic", x: r.x, y: r.y, w: r.w, h: r.h };
|
|
869
|
-
}
|
|
870
|
-
return null;
|
|
871
|
-
}
|
|
872
|
-
function isTrivial(a) {
|
|
873
|
-
if (a.type === "arrow") return Math.abs(a.x2 - a.x1) < 4 && Math.abs(a.y2 - a.y1) < 4;
|
|
874
|
-
if (a.type === "draw") return a.points.length < 2;
|
|
875
|
-
if ("w" in a && "h" in a) return a.w < 4 || a.h < 4;
|
|
876
|
-
return false;
|
|
877
|
-
}
|
|
878
|
-
function translate(a, dx, dy) {
|
|
879
|
-
if (a.type === "arrow") return { ...a, x1: a.x1 + dx, y1: a.y1 + dy, x2: a.x2 + dx, y2: a.y2 + dy };
|
|
880
|
-
if (a.type === "draw") return { ...a, points: a.points.map((p) => ({ x: p.x + dx, y: p.y + dy })) };
|
|
881
|
-
if (a.type === "text") return { ...a, x: a.x + dx, y: a.y + dy };
|
|
882
|
-
return { ...a, x: a.x + dx, y: a.y + dy };
|
|
883
|
-
}
|
|
884
|
-
function resize(original, corner, p) {
|
|
885
|
-
if (original.type === "arrow") {
|
|
886
|
-
if (corner === "start") return { ...original, x1: p.x, y1: p.y };
|
|
887
|
-
if (corner === "end") return { ...original, x2: p.x, y2: p.y };
|
|
888
|
-
return original;
|
|
889
|
-
}
|
|
890
|
-
if (original.type === "text" || original.type === "draw") return original;
|
|
891
|
-
const left = original.x;
|
|
892
|
-
const right = original.x + original.w;
|
|
893
|
-
const top = original.y;
|
|
894
|
-
const bottom = original.y + original.h;
|
|
895
|
-
let x1 = left, y1 = top, x2 = right, y2 = bottom;
|
|
896
|
-
if (corner === "nw") {
|
|
897
|
-
x1 = p.x;
|
|
898
|
-
y1 = p.y;
|
|
899
|
-
}
|
|
900
|
-
if (corner === "ne") {
|
|
901
|
-
x2 = p.x;
|
|
902
|
-
y1 = p.y;
|
|
903
|
-
}
|
|
904
|
-
if (corner === "se") {
|
|
905
|
-
x2 = p.x;
|
|
906
|
-
y2 = p.y;
|
|
907
|
-
}
|
|
908
|
-
if (corner === "sw") {
|
|
909
|
-
x1 = p.x;
|
|
910
|
-
y2 = p.y;
|
|
911
|
-
}
|
|
912
|
-
const r = normalizeRect({ x: x1, y: y1 }, { x: x2, y: y2 });
|
|
913
|
-
return { ...original, x: r.x, y: r.y, w: r.w, h: r.h };
|
|
914
|
-
}
|
|
915
|
-
function boundingBox(a) {
|
|
916
|
-
if (a.type === "arrow") {
|
|
917
|
-
const x = Math.min(a.x1, a.x2);
|
|
918
|
-
const y = Math.min(a.y1, a.y2);
|
|
919
|
-
return { x, y, w: Math.abs(a.x2 - a.x1), h: Math.abs(a.y2 - a.y1) };
|
|
920
|
-
}
|
|
921
|
-
if (a.type === "draw") {
|
|
922
|
-
if (a.points.length === 0) return { x: 0, y: 0, w: 0, h: 0 };
|
|
923
|
-
let minX = a.points[0].x, maxX = a.points[0].x, minY = a.points[0].y, maxY = a.points[0].y;
|
|
924
|
-
for (const p of a.points) {
|
|
925
|
-
if (p.x < minX) minX = p.x;
|
|
926
|
-
if (p.x > maxX) maxX = p.x;
|
|
927
|
-
if (p.y < minY) minY = p.y;
|
|
928
|
-
if (p.y > maxY) maxY = p.y;
|
|
929
|
-
}
|
|
930
|
-
return { x: minX, y: minY, w: maxX - minX, h: maxY - minY };
|
|
931
|
-
}
|
|
932
|
-
if (a.type === "text") {
|
|
933
|
-
const lines = a.text.split("\n");
|
|
934
|
-
const longest = lines.reduce((m, l) => Math.max(m, l.length), 0);
|
|
935
|
-
return { x: a.x, y: a.y, w: longest * a.size * 0.55, h: lines.length * a.size * 1.2 };
|
|
936
|
-
}
|
|
937
|
-
return { x: a.x, y: a.y, w: a.w, h: a.h };
|
|
938
|
-
}
|
|
939
|
-
function normalizeRect(a, b) {
|
|
940
|
-
const x = Math.min(a.x, b.x);
|
|
941
|
-
const y = Math.min(a.y, b.y);
|
|
942
|
-
const w = Math.abs(a.x - b.x);
|
|
943
|
-
const h = Math.abs(a.y - b.y);
|
|
944
|
-
return { x, y, w, h };
|
|
945
|
-
}
|
|
946
|
-
function withinBounds(a, w, h) {
|
|
947
|
-
const bb = boundingBox(a);
|
|
948
|
-
return bb.x + bb.w > 0 && bb.y + bb.h > 0 && bb.x < w && bb.y < h;
|
|
949
|
-
}
|
|
950
|
-
function pointsToPath(points) {
|
|
951
|
-
if (points.length === 0) return "";
|
|
952
|
-
let d = `M ${points[0].x} ${points[0].y}`;
|
|
953
|
-
for (let i = 1; i < points.length; i++) d += ` L ${points[i].x} ${points[i].y}`;
|
|
954
|
-
return d;
|
|
955
|
-
}
|
|
956
|
-
function applyMosaic(ctx, rect) {
|
|
957
|
-
const x = Math.round(Math.max(0, rect.x));
|
|
958
|
-
const y = Math.round(Math.max(0, rect.y));
|
|
959
|
-
const w = Math.round(Math.min(ctx.canvas.width - x, rect.w));
|
|
960
|
-
const h = Math.round(Math.min(ctx.canvas.height - y, rect.h));
|
|
961
|
-
if (w < 2 || h < 2) return;
|
|
962
|
-
const data = ctx.getImageData(x, y, w, h);
|
|
963
|
-
const block = MOSAIC_BLOCK;
|
|
964
|
-
for (let by = 0; by < h; by += block) {
|
|
965
|
-
for (let bx = 0; bx < w; bx += block) {
|
|
966
|
-
let r = 0, g = 0, b = 0, a = 0, n = 0;
|
|
967
|
-
const blockW = Math.min(block, w - bx);
|
|
968
|
-
const blockH = Math.min(block, h - by);
|
|
969
|
-
for (let yy = 0; yy < blockH; yy++) {
|
|
970
|
-
for (let xx = 0; xx < blockW; xx++) {
|
|
971
|
-
const i = ((by + yy) * w + (bx + xx)) * 4;
|
|
972
|
-
r += data.data[i];
|
|
973
|
-
g += data.data[i + 1];
|
|
974
|
-
b += data.data[i + 2];
|
|
975
|
-
a += data.data[i + 3];
|
|
976
|
-
n++;
|
|
977
|
-
}
|
|
978
|
-
}
|
|
979
|
-
r = Math.round(r / n);
|
|
980
|
-
g = Math.round(g / n);
|
|
981
|
-
b = Math.round(b / n);
|
|
982
|
-
a = Math.round(a / n);
|
|
983
|
-
for (let yy = 0; yy < blockH; yy++) {
|
|
984
|
-
for (let xx = 0; xx < blockW; xx++) {
|
|
985
|
-
const i = ((by + yy) * w + (bx + xx)) * 4;
|
|
986
|
-
data.data[i] = r;
|
|
987
|
-
data.data[i + 1] = g;
|
|
988
|
-
data.data[i + 2] = b;
|
|
989
|
-
data.data[i + 3] = a;
|
|
990
|
-
}
|
|
991
|
-
}
|
|
992
|
-
}
|
|
993
|
-
}
|
|
994
|
-
ctx.putImageData(data, x, y);
|
|
995
|
-
}
|
|
996
|
-
|
|
997
|
-
export { ImageAnnotator_default };
|
|
998
|
-
//# sourceMappingURL=chunk-NUPYEVU4.js.map
|
|
999
|
-
//# sourceMappingURL=chunk-NUPYEVU4.js.map
|