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 {