Skip to content

Commit eaa2be3

Browse files
committed
feat: 增加导入svg图
1 parent f36e826 commit eaa2be3

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

app/src/core/service/GlobalMenu.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,27 @@ export function GlobalMenu() {
286286
<Images />
287287
导入PNG图片
288288
</Item>
289+
<Item
290+
disabled={!activeProject}
291+
onClick={async () => {
292+
const pathList = await open({
293+
title: "打开文件",
294+
directory: false,
295+
multiple: true,
296+
filters: [{ name: "*", extensions: ["svg"] }],
297+
});
298+
console.log(pathList);
299+
if (!pathList) {
300+
return;
301+
}
302+
for (const path of pathList) {
303+
DragFileIntoStageEngine.handleDropSvg(activeProject!, path);
304+
}
305+
}}
306+
>
307+
<Images />
308+
导入SVG图片
309+
</Item>
289310
</SubContent>
290311
</Sub>
291312

app/src/core/service/dataManageService/dragFileIntoStageEngine/dragFileIntoStageEngine.tsx

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { ImageNode } from "@/core/stage/stageObject/entity/ImageNode";
66
import { Rectangle } from "@graphif/shapes";
77
import { toast } from "sonner";
88
import { Random } from "@/core/algorithm/random";
9+
import { TextNode } from "@/core/stage/stageObject/entity/TextNode";
10+
import { SvgNode } from "@/core/stage/stageObject/entity/SvgNode";
911

1012
/**
1113
* 处理文件拖拽到舞台的引擎
@@ -20,15 +22,15 @@ export namespace DragFileIntoStageEngine {
2022
export async function handleDrop(project: Project, pathList: string[]) {
2123
try {
2224
for (const filePath of pathList) {
23-
// 检查文件是否为PNG格式
24-
if (!filePath.toLowerCase().endsWith(".png")) {
25-
console.warn(`不支持的文件格式: ${filePath}`);
26-
continue;
27-
}
28-
2925
const extName = filePath.split(".").pop()?.toLowerCase();
3026
if (extName === "png") {
3127
handleDropPng(project, filePath);
28+
} else if (extName === "txt") {
29+
handleDropTxt(project, filePath);
30+
} else if (extName === "svg") {
31+
handleDropSvg(project, filePath);
32+
} else {
33+
toast.error(`不支持的文件类型: 【${extName}】`);
3234
}
3335
}
3436
} catch (error) {
@@ -58,6 +60,29 @@ export namespace DragFileIntoStageEngine {
5860
});
5961

6062
project.stageManager.add(imageNode);
61-
return imageNode;
63+
}
64+
65+
export async function handleDropTxt(project: Project, filePath: string) {
66+
const fileData = await readFile(filePath);
67+
const content = new TextDecoder().decode(fileData);
68+
const textNode = new TextNode(project, {
69+
text: content,
70+
collisionBox: new CollisionBox([new Rectangle(project.camera.location.clone(), new Vector(300, 150))]),
71+
sizeAdjust: "manual",
72+
});
73+
74+
project.stageManager.add(textNode);
75+
}
76+
77+
export async function handleDropSvg(project: Project, filePath: string) {
78+
const fileData = await readFile(filePath);
79+
const content = new TextDecoder().decode(fileData);
80+
const svg = new DOMParser().parseFromString(content, "image/svg+xml");
81+
const item = new XMLSerializer().serializeToString(svg.documentElement);
82+
const attachmentId = project.addAttachment(new Blob([item], { type: "image/svg+xml" }));
83+
const entity = new SvgNode(project, {
84+
attachmentId,
85+
});
86+
project.stageManager.add(entity);
6287
}
6388
}

0 commit comments

Comments
 (0)