fix: file drag and drop import cannot be used

This commit is contained in:
huzibaca 2024-11-26 03:07:25 +08:00
parent 9696c7cec0
commit e842ea745a
2 changed files with 45 additions and 25 deletions

View File

@ -16,6 +16,12 @@
"identifier": "fs:scope",
"allow": ["$APPDATA/**", "$RESOURCE/../**", "**"]
},
"fs:allow-app-read",
"fs:allow-app-read-recursive",
"fs:allow-appcache-read",
"fs:allow-appcache-read-recursive",
"fs:allow-appconfig-read",
"fs:allow-appconfig-read-recursive",
"core:window:allow-create",
"core:window:allow-center",
"core:window:allow-request-user-attention",

View File

@ -2,6 +2,7 @@ import useSWR, { mutate } from "swr";
import { useEffect, useMemo, useRef, useState } from "react";
import { useLockFn } from "ahooks";
import { Box, Button, IconButton, Stack, Divider, Grid2 } from "@mui/material";
import { FileDropEvent, getCurrent } from "@tauri-apps/plugin-window";
import {
DndContext,
closestCenter,
@ -25,7 +26,6 @@ import {
} from "@mui/icons-material";
import { useTranslation } from "react-i18next";
import {
getProfiles,
importProfile,
enhanceProfiles,
getRuntimeLogs,
@ -45,12 +45,14 @@ import { ProfileMore } from "@/components/profile/profile-more";
import { ProfileItem } from "@/components/profile/profile-item";
import { useProfiles } from "@/hooks/use-profiles";
import { ConfigViewer } from "@/components/setting/mods/config-viewer";
import { throttle } from "lodash-es";
import { add, throttle } from "lodash-es";
import { BaseStyledTextField } from "@/components/base/base-styled-text-field";
import { readTextFile } from "@tauri-apps/plugin-fs";
import { readText } from "@tauri-apps/plugin-clipboard-manager";
import { useLocation } from "react-router-dom";
import { useListen } from "@/hooks/use-listen";
import { listen } from "@tauri-apps/api/event";
import { TauriEvent } from "@tauri-apps/api/event";
const ProfilePage = () => {
const { t } = useTranslation();
@ -69,9 +71,14 @@ const ProfilePage = () => {
const { current } = location.state || {};
useEffect(() => {
const unlisten = addListener("tauri://file-drop", async (event) => {
const fileList = event.payload as string[];
for (let file of fileList) {
const handleFileDrop = async () => {
const unlisten = await addListener(
TauriEvent.DRAG_DROP,
async (event: any) => {
console.log("文件拖放事件:", event);
const paths = event.payload.paths;
for (let file of paths) {
if (!file.endsWith(".yaml") && !file.endsWith(".yml")) {
Notice.error(t("Only YAML Files Supported"));
continue;
@ -90,9 +97,16 @@ const ProfilePage = () => {
await createProfile(item, data);
await mutateProfiles();
}
});
},
);
return unlisten;
};
const unsubscribe = handleFileDrop();
return () => {
unlisten.then((fn) => fn());
unsubscribe.then((cleanup) => cleanup());
};
}, []);