Skip to content

Commit

Permalink
✨ 允许拖入txt文件并每行创建节点
Browse files Browse the repository at this point in the history
  • Loading branch information
Littlefean committed Dec 9, 2024
1 parent 0a2cb9f commit bc9e348
Showing 1 changed file with 102 additions and 34 deletions.
136 changes: 102 additions & 34 deletions src/core/controller/concrete/ControllerDragFile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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;
};
Expand All @@ -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));
};
}

0 comments on commit bc9e348

Please sign in to comment.