defaultIcon.ts
1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* 生成默认方法论步骤图标 PNG 文件
*/
export function buildDefaultIconSvg(stepOrder: number): string {
return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48"><circle cx="24" cy="24" r="22" fill="%232B6CB0"/><text x="24" y="30" text-anchor="middle" fill="white" font-size="18" font-weight="bold" font-family="system-ui">${stepOrder}</text></svg>`;
}
export function buildDefaultIconDataUrl(stepOrder: number): string {
const svgContent = buildDefaultIconSvg(stepOrder);
return `data:image/svg+xml,${encodeURIComponent(svgContent)}`;
}
export async function generateDefaultIconPngFile(stepOrder: number): Promise<File> {
const svgContent = buildDefaultIconSvg(stepOrder);
const svgDataUrl = `data:image/svg+xml,${encodeURIComponent(svgContent)}`;
const img = new Image();
img.src = svgDataUrl;
await new Promise<void>((resolve, reject) => {
img.onload = () => resolve();
img.onerror = () => reject(new Error('Failed to load SVG image'));
});
const canvas = document.createElement('canvas');
canvas.width = 48;
canvas.height = 48;
const ctx = canvas.getContext('2d');
if (!ctx) {
throw new Error('Canvas context not available');
}
ctx.drawImage(img, 0, 0, 48, 48);
const blob = await new Promise<Blob>((resolve, reject) => {
canvas.toBlob((blob) => {
if (blob) {
resolve(blob);
} else {
reject(new Error('Failed to convert canvas to PNG blob'));
}
}, 'image/png');
});
return new File([blob], `step-${stepOrder}-default-icon.png`, { type: 'image/png' });
}