@@ -6,6 +6,8 @@ import { ImageNode } from "@/core/stage/stageObject/entity/ImageNode";
66import { Rectangle } from "@graphif/shapes" ;
77import { toast } from "sonner" ;
88import { 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