poon-choi 0.0.2 → 0.0.4
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/.output/nitro.json +17 -0
- package/.output/public/assets/about-D4e6L2RX.js +1 -0
- package/.output/public/assets/index-B6P0kA2f.css +1 -0
- package/.output/public/assets/index-CdiJXXIG.js +11 -0
- package/.output/public/assets/routes-BgrRSMVP.js +7 -0
- package/.output/public/assets/styles-DqQrEVt_.css +1 -0
- package/.output/server/_chunks/ssr-renderer.mjs +23 -0
- package/.output/server/_libs/@tanstack/react-router+[...].mjs +15021 -0
- package/.output/server/_libs/clsx.mjs +15 -0
- package/.output/server/_libs/h3+rou3+srvx.mjs +1352 -0
- package/.output/server/_libs/tailwind-merge.mjs +3459 -0
- package/.output/server/_libs/tanstack__history.mjs +363 -0
- package/.output/server/_libs/ufo.mjs +63 -0
- package/.output/server/_runtime.mjs +34 -0
- package/.output/server/_ssr/about-C106R88A.mjs +26 -0
- package/.output/server/_ssr/empty-plugin-adapters-BiGySXWX.mjs +4 -0
- package/.output/server/_ssr/router-B7lhQtFL.mjs +258 -0
- package/.output/server/_ssr/routes-Cutxg__h.mjs +769 -0
- package/.output/server/_ssr/ssr.mjs +7304 -0
- package/.output/server/_ssr/start-CwhArFJN.mjs +3 -0
- package/.output/server/_tanstack-start-manifest_v-Dh5m4VsW.mjs +25 -0
- package/.output/server/index.mjs +311 -0
- package/LICENSE +21 -0
- package/README.md +10 -3
- package/README.zh.md +10 -3
- package/bin/{index.ts → cli.ts} +17 -2
- package/package.json +17 -49
- package/README.tanstack-start.md +0 -224
- package/src/components/DropStage.tsx +0 -64
- package/src/components/FitModeToggle.tsx +0 -34
- package/src/components/Footer.tsx +0 -44
- package/src/components/GapColorPicker.tsx +0 -53
- package/src/components/Header.tsx +0 -78
- package/src/components/PresetColorDropdown.tsx +0 -166
- package/src/components/ThemeToggle.tsx +0 -81
- package/src/components/ThumbnailStrip.tsx +0 -147
- package/src/components/VerticalSeparator.tsx +0 -5
- package/src/hooks/useToast.tsx +0 -23
- package/src/lib/config.ts +0 -48
- package/src/lib/draw.ts +0 -78
- package/src/lib/file.ts +0 -23
- package/src/lib/utils.ts +0 -7
- package/src/routeTree.gen.ts +0 -86
- package/src/router.tsx +0 -19
- package/src/routes/__root.tsx +0 -68
- package/src/routes/about.tsx +0 -23
- package/src/routes/index.css +0 -300
- package/src/routes/index.tsx +0 -511
- package/src/styles.css +0 -375
|
@@ -0,0 +1,769 @@
|
|
|
1
|
+
import { i as __toESM } from "../_runtime.mjs";
|
|
2
|
+
import { d as require_react, f as require_jsx_runtime } from "../_libs/@tanstack/react-router+[...].mjs";
|
|
3
|
+
import "./router-B7lhQtFL.mjs";
|
|
4
|
+
import { t as clsx } from "../_libs/clsx.mjs";
|
|
5
|
+
import { t as twMerge } from "../_libs/tailwind-merge.mjs";
|
|
6
|
+
// node_modules/.nitro/vite/services/ssr/assets/routes-Cutxg__h.js
|
|
7
|
+
var import_react = /* @__PURE__ */ __toESM(require_react());
|
|
8
|
+
var import_jsx_runtime = require_jsx_runtime();
|
|
9
|
+
/** biome-ignore-all lint/a11y/noStaticElementInteractions: <explanation> */
|
|
10
|
+
/** biome-ignore-all lint/a11y/useKeyWithClickEvents: <explanation> */
|
|
11
|
+
function DropStage({ items, children, onFiles }) {
|
|
12
|
+
const dragDepth = (0, import_react.useRef)(0);
|
|
13
|
+
const [isDragOver, setIsDragOver] = (0, import_react.useState)(false);
|
|
14
|
+
const handleDragEnter = (e) => {
|
|
15
|
+
if (e.dataTransfer && Array.from(e.dataTransfer.types).includes("Files")) {
|
|
16
|
+
e.preventDefault();
|
|
17
|
+
dragDepth.current += 1;
|
|
18
|
+
setIsDragOver(true);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
const handleDragover = (e) => {
|
|
22
|
+
if (e.dataTransfer && Array.from(e.dataTransfer.types).includes("Files")) {
|
|
23
|
+
e.preventDefault();
|
|
24
|
+
e.dataTransfer.dropEffect = "copy";
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
const handleDragLeave = (_e) => {
|
|
28
|
+
dragDepth.current = Math.max(0, dragDepth.current - 1);
|
|
29
|
+
if (dragDepth.current === 0) setIsDragOver(false);
|
|
30
|
+
};
|
|
31
|
+
const handleDrop = (e) => {
|
|
32
|
+
if (e.dataTransfer && Array.from(e.dataTransfer.types).includes("Files")) {
|
|
33
|
+
e.preventDefault();
|
|
34
|
+
dragDepth.current = 0;
|
|
35
|
+
setIsDragOver(false);
|
|
36
|
+
onFiles(e.dataTransfer.files);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
const className = [
|
|
40
|
+
"stage",
|
|
41
|
+
items.length > 0 && "has-image",
|
|
42
|
+
isDragOver && "dragover"
|
|
43
|
+
].filter(Boolean).join(" ");
|
|
44
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", {
|
|
45
|
+
className,
|
|
46
|
+
id: "stage",
|
|
47
|
+
onDragEnter: (event) => handleDragEnter(event),
|
|
48
|
+
onDragLeave: handleDragLeave,
|
|
49
|
+
onDragOver: handleDragover,
|
|
50
|
+
onDrop: handleDrop,
|
|
51
|
+
children
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* 受控的模式切换按钮。
|
|
56
|
+
* value: 'cover' | 'contain'
|
|
57
|
+
* onChange: (next: 'cover' | 'contain') => void
|
|
58
|
+
*/
|
|
59
|
+
function FitModeToggle({ value, onChange, className = "" }) {
|
|
60
|
+
const next = value === "cover" ? "contain" : "cover";
|
|
61
|
+
const label = value === "cover" ? "Cover" : "Contain";
|
|
62
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("button", {
|
|
63
|
+
type: "button",
|
|
64
|
+
onClick: () => onChange(next),
|
|
65
|
+
"aria-pressed": value === "contain",
|
|
66
|
+
className: "cursor-pointer px-4 py-2.5 rounded-lg text-sm font-medium bg-white text-slate-700 border border-slate-200 transition hover:bg-slate-50 " + className,
|
|
67
|
+
children: ["裁剪模式:", label]
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
function cn(...inputs) {
|
|
71
|
+
return twMerge(clsx(inputs));
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* color keyword to color value converter (eg. `'white'` -> `'#ffffff'`)
|
|
75
|
+
* @param c
|
|
76
|
+
* @returns
|
|
77
|
+
*/
|
|
78
|
+
var toColorValue = (c) => {
|
|
79
|
+
const ctx = document.createElement("canvas").getContext("2d");
|
|
80
|
+
if (!ctx) return c;
|
|
81
|
+
ctx.fillStyle = c;
|
|
82
|
+
return ctx.fillStyle;
|
|
83
|
+
};
|
|
84
|
+
function PresetColorDropdown({ onPick, presets: PRESETS, curColor }) {
|
|
85
|
+
const [open, setOpen] = (0, import_react.useState)(false);
|
|
86
|
+
const ref = (0, import_react.useRef)(null);
|
|
87
|
+
const [cycleIndex, setCycleIndex] = (0, import_react.useState)(0);
|
|
88
|
+
(0, import_react.useEffect)(() => {
|
|
89
|
+
if (!open) return;
|
|
90
|
+
const onDown = (e) => {
|
|
91
|
+
if (ref.current && !ref.current.contains(e.target)) setOpen(false);
|
|
92
|
+
};
|
|
93
|
+
document.addEventListener("mousedown", onDown);
|
|
94
|
+
return () => document.removeEventListener("mousedown", onDown);
|
|
95
|
+
}, [open]);
|
|
96
|
+
const handleCycle = () => {
|
|
97
|
+
const next = PRESETS[cycleIndex % PRESETS.length];
|
|
98
|
+
onPick(next);
|
|
99
|
+
setCycleIndex((i) => (i + 1) % PRESETS.length);
|
|
100
|
+
};
|
|
101
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
|
|
102
|
+
ref,
|
|
103
|
+
className: "relative flex gap-1",
|
|
104
|
+
children: [
|
|
105
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("button", {
|
|
106
|
+
type: "button",
|
|
107
|
+
onClick: () => setOpen((o) => !o),
|
|
108
|
+
className: "h-9 w-57 px-3 rounded-lg border border-slate-200 bg-white text-[13px]\r\n text-slate-600 flex justify-between items-center gap-2 hover:bg-slate-50",
|
|
109
|
+
children: [
|
|
110
|
+
"预设颜色 ",
|
|
111
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("code", {
|
|
112
|
+
className: "border-0!",
|
|
113
|
+
children: curColor
|
|
114
|
+
}),
|
|
115
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
|
|
116
|
+
className: "flex items-center gap-2 xms-auto",
|
|
117
|
+
children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(ColorPreviewerCube, { color: curColor }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", {
|
|
118
|
+
className: "text-slate-400",
|
|
119
|
+
children: "▾"
|
|
120
|
+
})]
|
|
121
|
+
})
|
|
122
|
+
]
|
|
123
|
+
}),
|
|
124
|
+
open && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
|
|
125
|
+
className: "absolute z-20 mt-10 min-w-[calc(100%-5rem)] max-h-64 overflow-auto rounded-lg border\r\n border-slate-200 bg-white shadow-lg p-1",
|
|
126
|
+
children: [!PRESETS.find((x) => isSameColor(x, curColor)) && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Option, {
|
|
127
|
+
className: "cursor-not-allowed",
|
|
128
|
+
color: curColor,
|
|
129
|
+
curColor,
|
|
130
|
+
renderColor: () => `${curColor} not in presets`
|
|
131
|
+
}), PRESETS.map((c) => {
|
|
132
|
+
const hex = toColorValue(c);
|
|
133
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Option, {
|
|
134
|
+
curColor,
|
|
135
|
+
color: c,
|
|
136
|
+
onClick: (color) => {
|
|
137
|
+
onPick(color);
|
|
138
|
+
setOpen(false);
|
|
139
|
+
}
|
|
140
|
+
}, hex);
|
|
141
|
+
})]
|
|
142
|
+
}),
|
|
143
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", {
|
|
144
|
+
type: "button",
|
|
145
|
+
onClick: handleCycle,
|
|
146
|
+
className: "h-9 px-3 rounded-lg text-[13px] font-medium bg-white text-slate-700\r\n border border-slate-200 transition hover:bg-slate-50",
|
|
147
|
+
children: "逐个设置"
|
|
148
|
+
})
|
|
149
|
+
]
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
function ColorPreviewerCube({ color }) {
|
|
153
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", {
|
|
154
|
+
className: "inline-block w-4 h-4 rounded border border-slate-300 shrink-0",
|
|
155
|
+
style: { background: color }
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
function Option({ color: c, onClick, curColor, renderColor = identity, className }) {
|
|
159
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("button", {
|
|
160
|
+
type: "button",
|
|
161
|
+
onClick: () => {
|
|
162
|
+
onClick?.(c);
|
|
163
|
+
},
|
|
164
|
+
className: cn(`cursor-pointer w-full flex items-center gap-2 px-2 py-1.5 rounded-md`, `text-[13px] text-slate-700 hover:bg-slate-100`, isSameColor(curColor, c) && "bg-slate-100", className),
|
|
165
|
+
children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(ColorPreviewerCube, { color: c }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("code", {
|
|
166
|
+
className: "font-mono border-0! whitespace-nowrap",
|
|
167
|
+
children: renderColor(c)
|
|
168
|
+
})]
|
|
169
|
+
}, c);
|
|
170
|
+
}
|
|
171
|
+
var isSameColor = (a, b) => {
|
|
172
|
+
return a === b || toColorValue(a) === toColorValue(b);
|
|
173
|
+
};
|
|
174
|
+
function identity(value) {
|
|
175
|
+
return value;
|
|
176
|
+
}
|
|
177
|
+
function GapColorPicker({ onChange, initialColor, presetColors }) {
|
|
178
|
+
const [value, setValue] = (0, import_react.useState)(initialColor);
|
|
179
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
|
|
180
|
+
className: "gap-color-wrap flex items-center gap-2 text-sm text-slate-600",
|
|
181
|
+
children: [
|
|
182
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("label", {
|
|
183
|
+
htmlFor: "gapColor",
|
|
184
|
+
className: "cursor-pointer",
|
|
185
|
+
children: "间隙颜色"
|
|
186
|
+
}),
|
|
187
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(PresetColorDropdown, {
|
|
188
|
+
presets: presetColors,
|
|
189
|
+
curColor: value,
|
|
190
|
+
onPick: (color) => {
|
|
191
|
+
setValue(color);
|
|
192
|
+
onChange(color);
|
|
193
|
+
}
|
|
194
|
+
}),
|
|
195
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
|
|
196
|
+
className: "flex items-center gap-2 ps-2 border border-gray-300 rounded-md",
|
|
197
|
+
style: {
|
|
198
|
+
outline: `1px solid ${value}`,
|
|
199
|
+
outlineOffset: "-2px"
|
|
200
|
+
},
|
|
201
|
+
children: ["🎨", /* @__PURE__ */ (0, import_jsx_runtime.jsx)("input", {
|
|
202
|
+
type: "color",
|
|
203
|
+
id: "gapColor",
|
|
204
|
+
value,
|
|
205
|
+
onChange: (e) => {
|
|
206
|
+
const gapColor = e.target.value;
|
|
207
|
+
setValue(gapColor);
|
|
208
|
+
onChange(gapColor);
|
|
209
|
+
},
|
|
210
|
+
className: "h-8 w-8 aspect-square bg-transparent cursor-pointer\r\n [&::-webkit-color-swatch-wrapper]:p-0\r\n [&::-webkit-color-swatch]:rounded-md [&::-webkit-color-swatch]:border-0\r\n "
|
|
211
|
+
})]
|
|
212
|
+
})
|
|
213
|
+
]
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
/** biome-ignore-all lint/a11y/noStaticElementInteractions: <explanation> */
|
|
217
|
+
function ThumbnailStrip({ items, maxImages = 9, onRemove, onMove, onAddMore }) {
|
|
218
|
+
const [dragSrc, setDragSrc] = (0, import_react.useState)(null);
|
|
219
|
+
const [dropTarget, setDropTarget] = (0, import_react.useState)(null);
|
|
220
|
+
const handleDragStart = (e, index) => {
|
|
221
|
+
setDragSrc(index);
|
|
222
|
+
e.dataTransfer.effectAllowed = "move";
|
|
223
|
+
try {
|
|
224
|
+
e.dataTransfer.setData("text/plain", String(index));
|
|
225
|
+
} catch {}
|
|
226
|
+
};
|
|
227
|
+
const handleDragEnd = () => {
|
|
228
|
+
setDragSrc(null);
|
|
229
|
+
setDropTarget(null);
|
|
230
|
+
};
|
|
231
|
+
const handleDragOver = (e, index) => {
|
|
232
|
+
e.preventDefault();
|
|
233
|
+
e.dataTransfer.dropEffect = "move";
|
|
234
|
+
if (dragSrc !== null && dragSrc !== index) setDropTarget(index);
|
|
235
|
+
};
|
|
236
|
+
const handleDragLeave = (index) => {
|
|
237
|
+
setDropTarget((cur) => cur === index ? null : cur);
|
|
238
|
+
};
|
|
239
|
+
const handleDrop = (e, index) => {
|
|
240
|
+
e.preventDefault();
|
|
241
|
+
setDropTarget(null);
|
|
242
|
+
if (dragSrc === null || dragSrc === index) return;
|
|
243
|
+
onMove(dragSrc, index);
|
|
244
|
+
setDragSrc(null);
|
|
245
|
+
};
|
|
246
|
+
const canAddMore = items.length > 0 && items.length < maxImages;
|
|
247
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
|
|
248
|
+
className: "strip",
|
|
249
|
+
children: [items.map((item, i) => {
|
|
250
|
+
const className = [
|
|
251
|
+
"thumb",
|
|
252
|
+
dragSrc === i && "dragging",
|
|
253
|
+
dropTarget === i && "drop-target"
|
|
254
|
+
].filter(Boolean).join(" ");
|
|
255
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
|
|
256
|
+
className,
|
|
257
|
+
draggable: true,
|
|
258
|
+
"data-index": i,
|
|
259
|
+
onDragStart: (e) => handleDragStart(e, i),
|
|
260
|
+
onDragEnd: handleDragEnd,
|
|
261
|
+
onDragOver: (e) => handleDragOver(e, i),
|
|
262
|
+
onDragLeave: () => handleDragLeave(i),
|
|
263
|
+
onDrop: (e) => handleDrop(e, i),
|
|
264
|
+
children: [
|
|
265
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("img", {
|
|
266
|
+
src: item.url,
|
|
267
|
+
alt: `图片 ${i + 1}`
|
|
268
|
+
}),
|
|
269
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", {
|
|
270
|
+
className: "idx",
|
|
271
|
+
children: i + 1
|
|
272
|
+
}),
|
|
273
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", {
|
|
274
|
+
type: "button",
|
|
275
|
+
className: "remove",
|
|
276
|
+
title: "移除",
|
|
277
|
+
onClick: (e) => {
|
|
278
|
+
e.stopPropagation();
|
|
279
|
+
onRemove(item.id);
|
|
280
|
+
},
|
|
281
|
+
children: "×"
|
|
282
|
+
}),
|
|
283
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
|
|
284
|
+
className: "move",
|
|
285
|
+
children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", {
|
|
286
|
+
type: "button",
|
|
287
|
+
title: "左移",
|
|
288
|
+
onClick: (e) => {
|
|
289
|
+
e.stopPropagation();
|
|
290
|
+
onMove(i, i - 1);
|
|
291
|
+
},
|
|
292
|
+
children: "←"
|
|
293
|
+
}), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", {
|
|
294
|
+
type: "button",
|
|
295
|
+
title: "右移",
|
|
296
|
+
onClick: (e) => {
|
|
297
|
+
e.stopPropagation();
|
|
298
|
+
onMove(i, i + 1);
|
|
299
|
+
},
|
|
300
|
+
children: "→"
|
|
301
|
+
})]
|
|
302
|
+
})
|
|
303
|
+
]
|
|
304
|
+
}, item.id);
|
|
305
|
+
}), canAddMore && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", {
|
|
306
|
+
type: "button",
|
|
307
|
+
className: "thumb add-more",
|
|
308
|
+
title: "继续添加图片",
|
|
309
|
+
onClick: onAddMore,
|
|
310
|
+
children: "+"
|
|
311
|
+
})]
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
function useToast() {
|
|
315
|
+
const containerRef = (0, import_react.useRef)(null);
|
|
316
|
+
const toast = (msg, ms = 2600) => {
|
|
317
|
+
const container = containerRef.current;
|
|
318
|
+
if (!container) return;
|
|
319
|
+
const el = document.createElement("div");
|
|
320
|
+
el.className = "toast";
|
|
321
|
+
el.textContent = msg;
|
|
322
|
+
container.appendChild(el);
|
|
323
|
+
setTimeout(() => {
|
|
324
|
+
el.style.transition = "opacity .25s";
|
|
325
|
+
el.style.opacity = "0";
|
|
326
|
+
setTimeout(() => el.remove(), 260);
|
|
327
|
+
}, ms);
|
|
328
|
+
};
|
|
329
|
+
return {
|
|
330
|
+
containerRef,
|
|
331
|
+
toast: (0, import_react.useCallback)(toast, [])
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
var drawImageToCell = (ctx, img, cell, scale, fitMode) => {
|
|
335
|
+
if (fitMode === "contain") drawContain(ctx, img, cell, scale);
|
|
336
|
+
else drawCover(ctx, img, cell, scale);
|
|
337
|
+
};
|
|
338
|
+
var drawContain = (ctx, img, cell, scale = 1) => {
|
|
339
|
+
const cx = cell.x * scale, cy = cell.y * scale;
|
|
340
|
+
const cw = cell.w * scale, ch = cell.h * scale;
|
|
341
|
+
const iw = img.naturalWidth || img.width;
|
|
342
|
+
const ih = img.naturalHeight || img.height;
|
|
343
|
+
if (!iw || !ih) return;
|
|
344
|
+
const cellRatio = cw / ch, imgRatio = iw / ih;
|
|
345
|
+
let dw, dh;
|
|
346
|
+
if (imgRatio > cellRatio) {
|
|
347
|
+
dw = cw;
|
|
348
|
+
dh = cw / imgRatio;
|
|
349
|
+
} else {
|
|
350
|
+
dh = ch;
|
|
351
|
+
dw = ch * imgRatio;
|
|
352
|
+
}
|
|
353
|
+
const dx = cx + (cw - dw) / 2;
|
|
354
|
+
const dy = cy + (ch - dh) / 2;
|
|
355
|
+
ctx.drawImage(img, 0, 0, iw, ih, dx, dy, dw, dh);
|
|
356
|
+
};
|
|
357
|
+
function drawCover(targetCtx, img, cell, scale = 1) {
|
|
358
|
+
const cx = cell.x * scale, cy = cell.y * scale;
|
|
359
|
+
const cw = cell.w * scale, ch = cell.h * scale;
|
|
360
|
+
const iw = img.naturalWidth || img.width;
|
|
361
|
+
const ih = img.naturalHeight || img.height;
|
|
362
|
+
if (!iw || !ih) return;
|
|
363
|
+
const cellRatio = cw / ch;
|
|
364
|
+
const imgRatio = iw / ih;
|
|
365
|
+
let sx, sy, sw, sh;
|
|
366
|
+
if (imgRatio > cellRatio) {
|
|
367
|
+
sh = ih;
|
|
368
|
+
sw = ih * cellRatio;
|
|
369
|
+
sx = (iw - sw) / 2;
|
|
370
|
+
sy = 0;
|
|
371
|
+
} else {
|
|
372
|
+
sw = iw;
|
|
373
|
+
sh = iw / cellRatio;
|
|
374
|
+
sx = 0;
|
|
375
|
+
sy = (ih - sh) / 2;
|
|
376
|
+
}
|
|
377
|
+
targetCtx.drawImage(img, sx, sy, sw, sh, cx, cy, cw, ch);
|
|
378
|
+
}
|
|
379
|
+
function isImageFile(file) {
|
|
380
|
+
return file && file.type && file.type.startsWith("image/");
|
|
381
|
+
}
|
|
382
|
+
async function loadImageFromFile(file) {
|
|
383
|
+
return new Promise((resolve, reject) => {
|
|
384
|
+
const url = URL.createObjectURL(file);
|
|
385
|
+
const img = new Image();
|
|
386
|
+
img.onload = () => resolve({
|
|
387
|
+
img,
|
|
388
|
+
url,
|
|
389
|
+
id: `${file.name}_${Math.random()}`
|
|
390
|
+
});
|
|
391
|
+
img.onerror = () => {
|
|
392
|
+
URL.revokeObjectURL(url);
|
|
393
|
+
reject(/* @__PURE__ */ new Error("decode failed"));
|
|
394
|
+
};
|
|
395
|
+
img.src = url;
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
function VerticalSeparator() {
|
|
399
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "vertical-separator w-0.5 bg-gray-300 inline-block h-[1.2em]" });
|
|
400
|
+
}
|
|
401
|
+
var SCALE = 2;
|
|
402
|
+
var config = {
|
|
403
|
+
COVER_W: 900 * SCALE,
|
|
404
|
+
COVER_H: 383 * SCALE,
|
|
405
|
+
JPEG_QUALITY: .92,
|
|
406
|
+
GAP: 5,
|
|
407
|
+
get GAP_COLOR() {
|
|
408
|
+
return config.PRESETS[0];
|
|
409
|
+
},
|
|
410
|
+
PRESETS: [
|
|
411
|
+
"white",
|
|
412
|
+
"yellowgreen",
|
|
413
|
+
"#6ff1a3",
|
|
414
|
+
"#7a7c88",
|
|
415
|
+
"#bfbfbf",
|
|
416
|
+
"#c1cbcc",
|
|
417
|
+
"#c6c9d8",
|
|
418
|
+
"#d6d5da",
|
|
419
|
+
"#f7d24f",
|
|
420
|
+
"#fda036",
|
|
421
|
+
"#c28f74",
|
|
422
|
+
"#a48157",
|
|
423
|
+
"#fac29f",
|
|
424
|
+
"#815f39"
|
|
425
|
+
]
|
|
426
|
+
};
|
|
427
|
+
/** biome-ignore-all lint/a11y/noSvgWithoutTitle: <explanation> */
|
|
428
|
+
/** biome-ignore-all lint/suspicious/useIterableCallbackReturn: <explanation> */
|
|
429
|
+
/** biome-ignore-all lint/a11y/noStaticElementInteractions: <explanation> */
|
|
430
|
+
/** biome-ignore-all lint/a11y/useKeyWithClickEvents: <explanation> */
|
|
431
|
+
/** biome-ignore-all lint/complexity/useOptionalChain: <explanation> */
|
|
432
|
+
/** biome-ignore-all lint/a11y/useButtonType: <explanation> */
|
|
433
|
+
var { COVER_W, COVER_H, GAP, GAP_COLOR: BG_COLOR, JPEG_QUALITY } = config;
|
|
434
|
+
function App() {
|
|
435
|
+
const THUMB_SIZE = 383;
|
|
436
|
+
const MAX_IMAGES = 6;
|
|
437
|
+
const [items, setItems] = (0, import_react.useState)([]);
|
|
438
|
+
const [gapColor, setGapColor] = (0, import_react.useState)(BG_COLOR);
|
|
439
|
+
const [fitMode, setFitMode] = (0, import_react.useState)("cover");
|
|
440
|
+
const previewRef = (0, import_react.useRef)(null);
|
|
441
|
+
const fileInputRef = (0, import_react.useRef)(null);
|
|
442
|
+
const { containerRef, toast } = useToast();
|
|
443
|
+
const composeOnto = (0, import_react.useCallback)((targetCtx, targetW, targetH) => {
|
|
444
|
+
const scale = targetW / COVER_W;
|
|
445
|
+
targetCtx.fillStyle = gapColor;
|
|
446
|
+
targetCtx.fillRect(0, 0, targetW, targetH);
|
|
447
|
+
if (items.length === 0) return;
|
|
448
|
+
const cells = getCells(items.length);
|
|
449
|
+
for (let i = 0; i < items.length; i++) drawImageToCell(targetCtx, items[i].img, cells[i], scale, fitMode);
|
|
450
|
+
}, [
|
|
451
|
+
items,
|
|
452
|
+
gapColor,
|
|
453
|
+
fitMode
|
|
454
|
+
]);
|
|
455
|
+
const handleMove = (from, to) => {
|
|
456
|
+
setItems((prev) => {
|
|
457
|
+
if (to < 0 || to >= prev.length) return prev;
|
|
458
|
+
const next = [...prev];
|
|
459
|
+
const [moved] = next.splice(from, 1);
|
|
460
|
+
next.splice(to, 0, moved);
|
|
461
|
+
return next;
|
|
462
|
+
});
|
|
463
|
+
};
|
|
464
|
+
const handleRemove = (id) => {
|
|
465
|
+
setItems((prev) => {
|
|
466
|
+
const target = prev.find((x) => x.id === id);
|
|
467
|
+
if (target) URL.revokeObjectURL(target.url);
|
|
468
|
+
return prev.filter((x) => x.id !== id);
|
|
469
|
+
});
|
|
470
|
+
};
|
|
471
|
+
function handleClearAllClick() {
|
|
472
|
+
items.forEach((it) => {
|
|
473
|
+
URL.revokeObjectURL(it.url);
|
|
474
|
+
});
|
|
475
|
+
setItems([]);
|
|
476
|
+
}
|
|
477
|
+
const addFiles = (0, import_react.useCallback)(async (files) => {
|
|
478
|
+
if (!files || files.length === 0) return;
|
|
479
|
+
const room = MAX_IMAGES - items.length;
|
|
480
|
+
if (room <= 0) {
|
|
481
|
+
toast(`最多 ${MAX_IMAGES} 张,已忽略 ${files.length} 张`);
|
|
482
|
+
return;
|
|
483
|
+
}
|
|
484
|
+
const imageFiles = files.filter(isImageFile);
|
|
485
|
+
const nonImageCount = files.length - imageFiles.length;
|
|
486
|
+
if (nonImageCount > 0) toast(`已跳过 ${nonImageCount} 个非图片文件`);
|
|
487
|
+
if (imageFiles.length === 0) return;
|
|
488
|
+
let toLoad = imageFiles;
|
|
489
|
+
let overflow = 0;
|
|
490
|
+
if (imageFiles.length > room) {
|
|
491
|
+
toLoad = imageFiles.slice(0, room);
|
|
492
|
+
overflow = imageFiles.length - room;
|
|
493
|
+
}
|
|
494
|
+
if (overflow > 0) toast(`最多 ${MAX_IMAGES} 张,已忽略 ${overflow} 张`);
|
|
495
|
+
const prevCursor = document.body.style.cursor;
|
|
496
|
+
document.body.style.cursor = "progress";
|
|
497
|
+
const loaded = [];
|
|
498
|
+
for (const f of toLoad) try {
|
|
499
|
+
loaded.push(await loadImageFromFile(f));
|
|
500
|
+
} catch (_) {
|
|
501
|
+
toast(`无法读取图片:${f.name || "未命名"}`);
|
|
502
|
+
}
|
|
503
|
+
document.body.style.cursor = prevCursor;
|
|
504
|
+
if (loaded.length === 0) return;
|
|
505
|
+
setItems((items) => items.concat(loaded));
|
|
506
|
+
}, [toast, items.length]);
|
|
507
|
+
function makeExportCanvas() {
|
|
508
|
+
const c = document.createElement("canvas");
|
|
509
|
+
c.width = COVER_W;
|
|
510
|
+
c.height = COVER_H;
|
|
511
|
+
const cx = c.getContext("2d");
|
|
512
|
+
if (!cx) throw new Error("no canvas context");
|
|
513
|
+
composeOnto(cx, COVER_W, COVER_H);
|
|
514
|
+
return c;
|
|
515
|
+
}
|
|
516
|
+
function makeThumbCanvas() {
|
|
517
|
+
const cover = makeExportCanvas();
|
|
518
|
+
const c = document.createElement("canvas");
|
|
519
|
+
c.width = THUMB_SIZE;
|
|
520
|
+
c.height = THUMB_SIZE;
|
|
521
|
+
const cx = c.getContext("2d");
|
|
522
|
+
if (!cx) throw new Error("no canvas context");
|
|
523
|
+
cx.fillStyle = gapColor;
|
|
524
|
+
cx.fillRect(0, 0, THUMB_SIZE, THUMB_SIZE);
|
|
525
|
+
const sx = (COVER_W - THUMB_SIZE) / 2;
|
|
526
|
+
const sy = (COVER_H - THUMB_SIZE) / 2;
|
|
527
|
+
cx.drawImage(cover, sx, sy, THUMB_SIZE, THUMB_SIZE, 0, 0, THUMB_SIZE, THUMB_SIZE);
|
|
528
|
+
return c;
|
|
529
|
+
}
|
|
530
|
+
function downloadCanvas(c, filename) {
|
|
531
|
+
console.log("JPEG_QUALITY:", JPEG_QUALITY);
|
|
532
|
+
c.toBlob((blob) => {
|
|
533
|
+
if (!blob) {
|
|
534
|
+
toast("导出失败");
|
|
535
|
+
return;
|
|
536
|
+
}
|
|
537
|
+
const a = document.createElement("a");
|
|
538
|
+
a.href = URL.createObjectURL(blob);
|
|
539
|
+
a.download = filename;
|
|
540
|
+
document.body.appendChild(a);
|
|
541
|
+
a.click();
|
|
542
|
+
a.remove();
|
|
543
|
+
setTimeout(() => URL.revokeObjectURL(a.href), 2e3);
|
|
544
|
+
}, "image/jpeg", JPEG_QUALITY);
|
|
545
|
+
}
|
|
546
|
+
const [fileInputValue, setFileInputValue] = (0, import_react.useState)("");
|
|
547
|
+
const handleDropzoneClick = () => fileInputRef.current.click();
|
|
548
|
+
const onFileInputChange = (e) => {
|
|
549
|
+
addFiles(Array.from(e.target.files || []));
|
|
550
|
+
setFileInputValue("");
|
|
551
|
+
};
|
|
552
|
+
(0, import_react.useEffect)(() => {
|
|
553
|
+
const handler = (e) => {
|
|
554
|
+
if (e.dataTransfer && Array.from(e.dataTransfer.types).includes("Files")) e.preventDefault();
|
|
555
|
+
};
|
|
556
|
+
const events = ["dragenter", "dragover"];
|
|
557
|
+
events.forEach((ev) => window.addEventListener(ev, handler));
|
|
558
|
+
return () => {
|
|
559
|
+
events.forEach((ev) => window.removeEventListener(ev, handler));
|
|
560
|
+
};
|
|
561
|
+
}, []);
|
|
562
|
+
(0, import_react.useEffect)(() => {
|
|
563
|
+
const handlePaste = (e) => {
|
|
564
|
+
const itemsInClip = e.clipboardData && e.clipboardData.items;
|
|
565
|
+
if (!itemsInClip) return;
|
|
566
|
+
const files = [];
|
|
567
|
+
for (const it of itemsInClip) if (it.kind === "file") {
|
|
568
|
+
const f = it.getAsFile();
|
|
569
|
+
if (f) files.push(f);
|
|
570
|
+
}
|
|
571
|
+
if (files.length) {
|
|
572
|
+
e.preventDefault();
|
|
573
|
+
addFiles(files);
|
|
574
|
+
}
|
|
575
|
+
};
|
|
576
|
+
window.addEventListener("paste", handlePaste);
|
|
577
|
+
return () => window.removeEventListener("paste", handlePaste);
|
|
578
|
+
}, [addFiles]);
|
|
579
|
+
const handleDlCoverClick = () => {
|
|
580
|
+
if (!items.length) return;
|
|
581
|
+
downloadCanvas(makeExportCanvas(), "wechat-cover-900x383.jpg");
|
|
582
|
+
};
|
|
583
|
+
const handleDlThumbClick = () => {
|
|
584
|
+
if (!items.length) return;
|
|
585
|
+
downloadCanvas(makeThumbCanvas(), "wechat-cover-thumbnail-383x383.jpg");
|
|
586
|
+
};
|
|
587
|
+
(0, import_react.useEffect)(() => {
|
|
588
|
+
const canvas = previewRef.current;
|
|
589
|
+
if (!canvas) return;
|
|
590
|
+
const ctx = canvas.getContext("2d");
|
|
591
|
+
if (!ctx) return;
|
|
592
|
+
const dpr = window.devicePixelRatio || 1;
|
|
593
|
+
const cssW = canvas.clientWidth || COVER_W;
|
|
594
|
+
const cssH = canvas.clientHeight || COVER_H;
|
|
595
|
+
canvas.width = Math.round(cssW * dpr);
|
|
596
|
+
canvas.height = Math.round(cssH * dpr);
|
|
597
|
+
ctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
598
|
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
599
|
+
composeOnto(ctx, canvas.width, canvas.height);
|
|
600
|
+
}, [composeOnto]);
|
|
601
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("main", {
|
|
602
|
+
className: "page-wrap px-4 pb-8 pt-14 1",
|
|
603
|
+
children: [/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
|
|
604
|
+
className: "wrap",
|
|
605
|
+
children: [
|
|
606
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("h1", { children: "封面拼接工具" }),
|
|
607
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("p", {
|
|
608
|
+
className: "sub",
|
|
609
|
+
children: [
|
|
610
|
+
"拖入 1–6 张图片,自动拼接为 ",
|
|
611
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("code", { children: "2.35:1" }),
|
|
612
|
+
" 封面(",
|
|
613
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("code", { children: [
|
|
614
|
+
COVER_W,
|
|
615
|
+
"×",
|
|
616
|
+
COVER_H
|
|
617
|
+
] }),
|
|
618
|
+
"),并生成 ",
|
|
619
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("code", { children: "1:1" }),
|
|
620
|
+
" 缩略图"
|
|
621
|
+
]
|
|
622
|
+
}),
|
|
623
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)(DropStage, {
|
|
624
|
+
items,
|
|
625
|
+
id: "stage",
|
|
626
|
+
onFiles: (xs) => addFiles(Array.from(xs)),
|
|
627
|
+
children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)("canvas", {
|
|
628
|
+
ref: previewRef,
|
|
629
|
+
id: "preview"
|
|
630
|
+
}), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
|
|
631
|
+
className: "dropzone",
|
|
632
|
+
id: "dropzone",
|
|
633
|
+
onClick: () => handleDropzoneClick(),
|
|
634
|
+
children: [
|
|
635
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("svg", {
|
|
636
|
+
viewBox: "0 0 24 24",
|
|
637
|
+
fill: "none",
|
|
638
|
+
stroke: "currentColor",
|
|
639
|
+
strokeWidth: "1.6",
|
|
640
|
+
children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)("path", {
|
|
641
|
+
d: "M12 16V4m0 0L8 8m4-4l4 4",
|
|
642
|
+
strokeLinecap: "round",
|
|
643
|
+
strokeLinejoin: "round"
|
|
644
|
+
}), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("path", {
|
|
645
|
+
d: "M4 16v2a2 2 0 002 2h12a2 2 0 002-2v-2",
|
|
646
|
+
strokeLinecap: "round"
|
|
647
|
+
})]
|
|
648
|
+
}),
|
|
649
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", {
|
|
650
|
+
className: "hint",
|
|
651
|
+
children: "拖入图片"
|
|
652
|
+
}),
|
|
653
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", {
|
|
654
|
+
className: "subhint",
|
|
655
|
+
children: "或点击选择 · 支持粘贴 · 最多 6 张"
|
|
656
|
+
})
|
|
657
|
+
]
|
|
658
|
+
})]
|
|
659
|
+
}),
|
|
660
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("input", {
|
|
661
|
+
type: "file",
|
|
662
|
+
id: "fileInput",
|
|
663
|
+
ref: fileInputRef,
|
|
664
|
+
onChange: (event) => onFileInputChange(event),
|
|
665
|
+
value: fileInputValue,
|
|
666
|
+
accept: "image/*",
|
|
667
|
+
multiple: true
|
|
668
|
+
}),
|
|
669
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(ThumbnailStrip, {
|
|
670
|
+
items,
|
|
671
|
+
maxImages: MAX_IMAGES,
|
|
672
|
+
onRemove: handleRemove,
|
|
673
|
+
onMove: handleMove,
|
|
674
|
+
onAddMore: () => fileInputRef.current?.click()
|
|
675
|
+
}),
|
|
676
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
|
|
677
|
+
className: "actions items-center",
|
|
678
|
+
children: [
|
|
679
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(FitModeToggle, {
|
|
680
|
+
value: fitMode,
|
|
681
|
+
onChange: setFitMode
|
|
682
|
+
}),
|
|
683
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(VerticalSeparator, {}),
|
|
684
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(GapColorPicker, {
|
|
685
|
+
presetColors: config.PRESETS,
|
|
686
|
+
initialColor: BG_COLOR,
|
|
687
|
+
onChange: setGapColor
|
|
688
|
+
}),
|
|
689
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(VerticalSeparator, {}),
|
|
690
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", {
|
|
691
|
+
className: "btn btn-primary",
|
|
692
|
+
id: "dlCover",
|
|
693
|
+
disabled: items.length === 0,
|
|
694
|
+
onClick: handleDlCoverClick,
|
|
695
|
+
children: "下载封面"
|
|
696
|
+
}),
|
|
697
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", {
|
|
698
|
+
className: "btn btn-secondary",
|
|
699
|
+
id: "dlThumb",
|
|
700
|
+
disabled: items.length === 0,
|
|
701
|
+
onClick: handleDlThumbClick,
|
|
702
|
+
children: "下载缩略图"
|
|
703
|
+
}),
|
|
704
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "spacer" }),
|
|
705
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", {
|
|
706
|
+
className: "btn btn-secondary",
|
|
707
|
+
id: "clearAll",
|
|
708
|
+
disabled: items.length === 0,
|
|
709
|
+
onClick: handleClearAllClick,
|
|
710
|
+
children: "清空"
|
|
711
|
+
})
|
|
712
|
+
]
|
|
713
|
+
})
|
|
714
|
+
]
|
|
715
|
+
}), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", {
|
|
716
|
+
id: "toasts",
|
|
717
|
+
ref: containerRef
|
|
718
|
+
})]
|
|
719
|
+
});
|
|
720
|
+
}
|
|
721
|
+
function getCells(n) {
|
|
722
|
+
const W = COVER_W, H = COVER_H;
|
|
723
|
+
let cols;
|
|
724
|
+
let rows;
|
|
725
|
+
let raw = [];
|
|
726
|
+
if (n <= 1) return [{
|
|
727
|
+
x: 0,
|
|
728
|
+
y: 0,
|
|
729
|
+
w: W,
|
|
730
|
+
h: H
|
|
731
|
+
}];
|
|
732
|
+
else if (n === 2) {
|
|
733
|
+
cols = 2;
|
|
734
|
+
rows = 1;
|
|
735
|
+
} else if (n === 3) {
|
|
736
|
+
cols = 3;
|
|
737
|
+
rows = 1;
|
|
738
|
+
} else if (n === 4) {
|
|
739
|
+
cols = 2;
|
|
740
|
+
rows = 2;
|
|
741
|
+
} else {
|
|
742
|
+
cols = 3;
|
|
743
|
+
rows = 2;
|
|
744
|
+
}
|
|
745
|
+
const cw = W / cols;
|
|
746
|
+
const ch = H / rows;
|
|
747
|
+
for (let r = 0; r < rows; r++) for (let c = 0; c < cols; c++) raw.push({
|
|
748
|
+
x: c * cw,
|
|
749
|
+
y: r * ch,
|
|
750
|
+
w: cw,
|
|
751
|
+
h: ch,
|
|
752
|
+
col: c,
|
|
753
|
+
row: r
|
|
754
|
+
});
|
|
755
|
+
if (n === 5) raw = raw.slice(0, 5);
|
|
756
|
+
return raw.map((cell) => {
|
|
757
|
+
const left = cell.col > 0 ? GAP / 2 : 0;
|
|
758
|
+
const right = cell.col < cols - 1 ? GAP / 2 : 0;
|
|
759
|
+
const top = cell.row > 0 ? GAP / 2 : 0;
|
|
760
|
+
const bottom = cell.row < rows - 1 ? GAP / 2 : 0;
|
|
761
|
+
return {
|
|
762
|
+
x: cell.x + left,
|
|
763
|
+
y: cell.y + top,
|
|
764
|
+
w: cell.w - left - right,
|
|
765
|
+
h: cell.h - top - bottom
|
|
766
|
+
};
|
|
767
|
+
});
|
|
768
|
+
}
|
|
769
|
+
export { App as component };
|