fix: file drag and drop import cannot be used

This commit is contained in:
huzibaca 2024-11-26 03:07:25 +08:00
parent 184b588f20
commit 17a8dfb58a
No known key found for this signature in database
GPG Key ID: D4364EE4851DC302
2 changed files with 45 additions and 25 deletions

View File

@ -16,6 +16,12 @@
"identifier": "fs:scope", "identifier": "fs:scope",
"allow": ["$APPDATA/**", "$RESOURCE/../**", "**"] "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-create",
"core:window:allow-center", "core:window:allow-center",
"core:window:allow-request-user-attention", "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 { useEffect, useMemo, useRef, useState } from "react";
import { useLockFn } from "ahooks"; import { useLockFn } from "ahooks";
import { Box, Button, IconButton, Stack, Divider, Grid2 } from "@mui/material"; import { Box, Button, IconButton, Stack, Divider, Grid2 } from "@mui/material";
import { FileDropEvent, getCurrent } from "@tauri-apps/plugin-window";
import { import {
DndContext, DndContext,
closestCenter, closestCenter,
@ -25,7 +26,6 @@ import {
} from "@mui/icons-material"; } from "@mui/icons-material";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { import {
getProfiles,
importProfile, importProfile,
enhanceProfiles, enhanceProfiles,
getRuntimeLogs, getRuntimeLogs,
@ -45,12 +45,14 @@ import { ProfileMore } from "@/components/profile/profile-more";
import { ProfileItem } from "@/components/profile/profile-item"; import { ProfileItem } from "@/components/profile/profile-item";
import { useProfiles } from "@/hooks/use-profiles"; import { useProfiles } from "@/hooks/use-profiles";
import { ConfigViewer } from "@/components/setting/mods/config-viewer"; 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 { BaseStyledTextField } from "@/components/base/base-styled-text-field";
import { readTextFile } from "@tauri-apps/plugin-fs"; import { readTextFile } from "@tauri-apps/plugin-fs";
import { readText } from "@tauri-apps/plugin-clipboard-manager"; import { readText } from "@tauri-apps/plugin-clipboard-manager";
import { useLocation } from "react-router-dom"; import { useLocation } from "react-router-dom";
import { useListen } from "@/hooks/use-listen"; import { useListen } from "@/hooks/use-listen";
import { listen } from "@tauri-apps/api/event";
import { TauriEvent } from "@tauri-apps/api/event";
const ProfilePage = () => { const ProfilePage = () => {
const { t } = useTranslation(); const { t } = useTranslation();
@ -69,30 +71,42 @@ const ProfilePage = () => {
const { current } = location.state || {}; const { current } = location.state || {};
useEffect(() => { useEffect(() => {
const unlisten = addListener("tauri://file-drop", async (event) => { const handleFileDrop = async () => {
const fileList = event.payload as string[]; const unlisten = await addListener(
for (let file of fileList) { TauriEvent.DRAG_DROP,
if (!file.endsWith(".yaml") && !file.endsWith(".yml")) { async (event: any) => {
Notice.error(t("Only YAML Files Supported")); console.log("文件拖放事件:", event);
continue; const paths = event.payload.paths;
}
const item = { for (let file of paths) {
type: "local", if (!file.endsWith(".yaml") && !file.endsWith(".yml")) {
name: file.split(/\/|\\/).pop() ?? "New Profile", Notice.error(t("Only YAML Files Supported"));
desc: "", continue;
url: "", }
option: { const item = {
with_proxy: false, type: "local",
self_proxy: false, name: file.split(/\/|\\/).pop() ?? "New Profile",
}, desc: "",
} as IProfileItem; url: "",
let data = await readTextFile(file); option: {
await createProfile(item, data); with_proxy: false,
await mutateProfiles(); self_proxy: false,
} },
}); } as IProfileItem;
let data = await readTextFile(file);
await createProfile(item, data);
await mutateProfiles();
}
},
);
return unlisten;
};
const unsubscribe = handleFileDrop();
return () => { return () => {
unlisten.then((fn) => fn()); unsubscribe.then((cleanup) => cleanup());
}; };
}, []); }, []);