From 8009634b44965994bda58ab835138faa3e94192a Mon Sep 17 00:00:00 2001 From: whyour Date: Sat, 19 Apr 2025 16:34:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=84=9A=E6=9C=AC=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E5=A2=9E=E5=8A=A0=E6=96=87=E4=BB=B6=E5=A4=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/script/editNameModal.tsx | 4 ++- src/pages/script/index.tsx | 51 +++++++++++------------------- src/utils/index.ts | 2 +- 3 files changed, 23 insertions(+), 34 deletions(-) diff --git a/src/pages/script/editNameModal.tsx b/src/pages/script/editNameModal.tsx index 66d0de32..702a0e3e 100644 --- a/src/pages/script/editNameModal.tsx +++ b/src/pages/script/editNameModal.tsx @@ -27,6 +27,7 @@ const EditScriptNameModal = ({ filename: string; path: string; key: string; + type: string; }) => void; }) => { const [form] = Form.useForm(); @@ -52,11 +53,12 @@ const EditScriptNameModal = ({ directory ? intl.get('创建文件夹成功') : intl.get('创建文件成功'), ); const key = path ? `${path}/` : ''; - const filename = file ? file.name : inputFilename; + const filename = file ? file.name : (directory || inputFilename); handleCancel({ filename, path, key: `${key}${filename}`, + type: directory ? 'directory' : 'file', }); } setLoading(false); diff --git a/src/pages/script/index.tsx b/src/pages/script/index.tsx index c5ebf7cc..b50c9985 100644 --- a/src/pages/script/index.tsx +++ b/src/pages/script/index.tsx @@ -347,18 +347,29 @@ const Script = () => { setIsAddFileModalVisible(true); }; - const addFileModalClose = ( - { filename, path, key }: { filename: string; path: string; key: string } = { + const addFileModalClose = async ( + { + filename, + path, + key, + type, + }: { filename: string; path: string; key: string; type?: string } = { filename: '', path: '', key: '', }, ) => { if (filename) { - let newData = [...data]; - const _file = { title: filename, key, parent: path }; + const res = await request.get(`${config.apiPrefix}scripts`); + let newData = res.data; + if (type === 'directory' && filename.includes('/')) { + const parts = filename.split('/'); + parts.pop(); + const parentPath = parts.join('/'); + path = path ? `${path}/${parentPath}` : parentPath; + } + const item = findNode(newData, (c) => c.key === key); if (path) { - newData = depthFirstSearch(newData, (c) => c.key === path, _file); const keys = path.split('/'); const sKeys: string[] = []; keys.reduce((p, c) => { @@ -366,35 +377,14 @@ const Script = () => { return `${p}/${c}`; }); setExpandedKeys([...expandedKeys, ...sKeys, path]); - } else { - newData.unshift(_file); } setData(newData); - onSelect(_file.title, _file); - handleIsEditing(_file.title, true); + onSelect(item.title, item); + handleIsEditing(item.title, true); } setIsAddFileModalVisible(false); }; - const downloadFile = () => { - request - .post(`${config.apiPrefix}scripts/download`, { - filename: currentNode.title, - }) - .then(({ code, data }) => { - if (code === 200) { - const blob = new Blob([data], { type: 'application/json' }); - const url = URL.createObjectURL(blob); - const a = document.createElement('a'); - a.href = url; - a.download = currentNode.title; - document.documentElement.appendChild(a); - a.click(); - document.documentElement.removeChild(a); - } - }); - }; - const initState = () => { setSelect(intl.get('请选择脚本文件')); setCurrentNode(null); @@ -703,10 +693,7 @@ const Script = () => { }} /> ) : ( - + )} )} diff --git a/src/utils/index.ts b/src/utils/index.ts index 9d6a35ca..381a7337 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -295,7 +295,7 @@ export function findNode & { children?: T[] }>( find(c); - return item; + return item as T | undefined; } export function logEnded(log: string): boolean {