From bc9e348fa0251db1243a77b289f7d80ca7f31b54 Mon Sep 17 00:00:00 2001 From: littlefean <2028140990@qq.com> Date: Mon, 9 Dec 2024 14:46:45 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E5=85=81=E8=AE=B8=E6=8B=96?= =?UTF-8?q?=E5=85=A5txt=E6=96=87=E4=BB=B6=E5=B9=B6=E6=AF=8F=E8=A1=8C?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=E8=8A=82=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../concrete/ControllerDragFile.tsx | 136 +++++++++++++----- 1 file changed, 102 insertions(+), 34 deletions(-) diff --git a/src/core/controller/concrete/ControllerDragFile.tsx b/src/core/controller/concrete/ControllerDragFile.tsx index 45ebbcd..d72d772 100644 --- a/src/core/controller/concrete/ControllerDragFile.tsx +++ b/src/core/controller/concrete/ControllerDragFile.tsx @@ -6,7 +6,9 @@ import { Renderer } from "../../render/canvas2d/renderer"; import { Stage } from "../../stage/Stage"; import { StageLoader } from "../../stage/StageLoader"; import { StageManager } from "../../stage/stageManager/StageManager"; +import { TextNode } from "../../stageObject/entity/TextNode"; import { ControllerClassDragFile } from "../ControllerClassDragFile"; +import { v4 as uuidv4 } from "uuid"; // import { listen } from "@tauri-apps/api/event"; // listen("tauri://file-drop", (event) => { @@ -54,48 +56,25 @@ ControllerDragFile.dragOver = (event: DragEvent) => { ControllerDragFile.drop = (event: DragEvent) => { event.preventDefault(); event.stopPropagation(); - const mouseLocation = Renderer.transformView2World( + const mouseWorldLocation = Renderer.transformView2World( new Vector(event.clientX, event.clientY), ); const files = event.dataTransfer?.files; if (files && files.length > 0) { - const file = files[0]; // 获取第一个拖入的文件 - // 检查文件类型是否是json类型 - if (!file.type.includes("json")) { - console.error("文件类型错误:", file.type); - Stage.effects.push(new TextRiseEffect("文件类型错误:" + file.type)); - Stage.isDraggingFile = false; - Stage.effects.push(new ViewFlashEffect(Color.Red)); - return; - } - const reader = new FileReader(); - - reader.onload = (e) => { - const fileContent = e.target?.result; // 读取的文件内容 - - // 在这里处理读取到的内容 - const dataString = fileContent?.toString(); - if (dataString === undefined) { - console.error("文件内容为空"); - Stage.effects.push(new TextRiseEffect("文件内容为空")); + // const firstFile = files[0]; // 获取第一个拖入的文件 + let i = -1; + for (const file of files) { + i++; + if (file.type.includes("json")) { + dealJsonFileDrop(file, mouseWorldLocation); + } else if (file.type.includes("text")) { + dealTxtFileDrop(file, mouseWorldLocation); } else { - StageManager.addSerializedData( - StageLoader.validate(JSON.parse(dataString)), - mouseLocation, - ); - Stage.effects.push(new ViewFlashEffect(Color.White)); + dealUnknownFileDrop(file, mouseWorldLocation, i); } - }; - - reader.onerror = (e) => { - console.error("文件读取错误:", e); - Stage.effects.push(new TextRiseEffect("文件读取错误:" + e)); - Stage.effects.push(new ViewFlashEffect(Color.Red)); - }; - - reader.readAsText(file); // 以文本格式读取文件内容 + } } Stage.isDraggingFile = false; }; @@ -105,3 +84,92 @@ ControllerDragFile.dragLeave = (event: DragEvent) => { event.stopPropagation(); Stage.isDraggingFile = false; }; + +function dealUnknownFileDrop( + file: File, + mouseWorldLocation: Vector, + i: number, +) { + // 未知类型,按插入一个textNode判断 + const textNode = new TextNode({ + uuid: uuidv4(), + text: file.name, + location: [mouseWorldLocation.x, mouseWorldLocation.y], + size: [100, 100], + color: [0, 0, 0, 0], + }); + textNode.move( + new Vector( + -textNode.rectangle.size.x / 2, + -textNode.rectangle.size.y / 2 + i * 100, + ), + ); + StageManager.addTextNode(textNode); +} + +/** + * 处理json文件拖入窗口并松开的情况 + * @param file + * @param mouseWorldLocation + */ +function dealJsonFileDrop(file: File, mouseWorldLocation: Vector) { + const reader = new FileReader(); + reader.readAsText(file); // 以文本格式读取文件内容 + + reader.onload = (e) => { + const fileContent = e.target?.result; // 读取的文件内容 + + // 在这里处理读取到的内容 + const dataString = fileContent?.toString(); + if (dataString === undefined) { + console.error("文件内容为空"); + Stage.effects.push(new TextRiseEffect("文件内容为空")); + } else { + StageManager.addSerializedData( + StageLoader.validate(JSON.parse(dataString)), + mouseWorldLocation, + ); + Stage.effects.push(new ViewFlashEffect(Color.White)); + } + }; + + reader.onerror = (e) => { + console.error("文件读取错误:", e); + Stage.effects.push(new TextRiseEffect("文件读取错误:" + e)); + Stage.effects.push(new ViewFlashEffect(Color.Red)); + }; +} + +function dealTxtFileDrop(file: File, mouseWorldLocation: Vector) { + const reader = new FileReader(); + reader.readAsText(file); // 以文本格式读取文件内容 + reader.onload = (e) => { + const fileContent = e.target?.result; // 读取的文件内容 + + // 在这里处理读取到的内容 + const dataString = fileContent?.toString(); + if (dataString === undefined) { + console.error("文件内容为空"); + Stage.effects.push(new TextRiseEffect("文件内容为空")); + } else { + let yIndex = -1; + for (const line of dataString.split("\n")) { + yIndex++; + const textNode = new TextNode({ + uuid: uuidv4(), + text: line, + location: [mouseWorldLocation.x, mouseWorldLocation.y + yIndex * 100], + size: [100, 100], + color: [0, 0, 0, 0], + }); + StageManager.addTextNode(textNode); + } + Stage.effects.push(new ViewFlashEffect(Color.White)); + } + }; + reader.onerror = (e) => { + console.error("文件读取错误:", e); + Stage.effects.push(new TextRiseEffect("文件读取错误:" + e)); + Stage.effects.push(new ViewFlashEffect(Color.Red)); + }; +}