修复脚本管理增加文件夹

This commit is contained in:
whyour 2025-04-19 16:34:28 +08:00
parent 03a23f6f31
commit 8009634b44
3 changed files with 23 additions and 34 deletions

View File

@ -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);

View File

@ -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 = () => {
}}
/>
) : (
<UnsupportedFilePreview
filename={currentNode?.title || ''}
onForceOpen={handleForceOpen}
/>
<UnsupportedFilePreview onForceOpen={handleForceOpen} />
)}
</SplitPane>
)}

View File

@ -295,7 +295,7 @@ export function findNode<T extends Record<string, any> & { children?: T[] }>(
find(c);
return item;
return item as T | undefined;
}
export function logEnded(log: string): boolean {