修复创建脚本选择目录

This commit is contained in:
whyour 2022-06-04 01:26:01 +08:00
parent 106b34e33a
commit 05d47be1c8
6 changed files with 52 additions and 36 deletions

View File

@ -9,10 +9,6 @@ export default defineConfig({
type: 'none', type: 'none',
}, },
fastRefresh: {}, fastRefresh: {},
// antd: {
// dark: true,
// },
mfsu: {},
esbuild: {}, esbuild: {},
webpack5: {}, webpack5: {},
dynamicImport: { dynamicImport: {

View File

@ -9,12 +9,12 @@ import Logger from './loaders/logger';
async function startServer() { async function startServer() {
const app = express(); const app = express();
await require('./loaders/db').default();
await require('./loaders/initFile').default(); await require('./loaders/initFile').default();
await require('./loaders/sentry').default({ expressApp: app }); await require('./loaders/sentry').default({ expressApp: app });
await require('./loaders/db').default();
await require('./loaders/app').default({ expressApp: app }); await require('./loaders/app').default({ expressApp: app });
const server = app const server = app

View File

@ -293,7 +293,6 @@ export function readDirs(
if (stats.isDirectory()) { if (stats.isDirectory()) {
return { return {
title: file, title: file,
value: file,
key, key,
type: 'directory', type: 'directory',
disabled: true, disabled: true,
@ -303,7 +302,6 @@ export function readDirs(
} }
return { return {
title: file, title: file,
value: file,
type: 'file', type: 'file',
key, key,
parent: relativePath, parent: relativePath,
@ -327,7 +325,6 @@ export function readDir(
const key = path.join(relativePath, file); const key = path.join(relativePath, file);
return { return {
title: file, title: file,
value: file,
type: stats.isDirectory() ? 'directory' : 'file', type: stats.isDirectory() ? 'directory' : 'file',
key, key,
parent: relativePath, parent: relativePath,

View File

@ -18,9 +18,7 @@ export default async () => {
await AppModel.sync(); await AppModel.sync();
await AuthModel.sync(); await AuthModel.sync();
await EnvModel.sync(); await EnvModel.sync();
await SubscriptionModel.sync({ alter: true }); await SubscriptionModel.sync();
await sequelize.sync();
// try { // try {
// const queryInterface = sequelize.getQueryInterface(); // const queryInterface = sequelize.getQueryInterface();

View File

@ -1,5 +1,14 @@
import React, { useEffect, useState } from 'react'; import React, { useEffect, useState } from 'react';
import { Modal, message, Input, Form, Select, Upload, Radio } from 'antd'; import {
Modal,
message,
Input,
Form,
Select,
Upload,
Radio,
TreeSelect,
} from 'antd';
import { request } from '@/utils/http'; import { request } from '@/utils/http';
import config from '@/utils/config'; import config from '@/utils/config';
import { UploadOutlined } from '@ant-design/icons'; import { UploadOutlined } from '@ant-design/icons';
@ -40,7 +49,7 @@ const EditScriptNameModal = ({
.then(({ code, data }) => { .then(({ code, data }) => {
if (code === 200) { if (code === 200) {
message.success('保存文件成功'); message.success('保存文件成功');
const key = values.path ? `${values.path}-` : ''; const key = values.path ? `${values.path}/` : '';
const filename = file ? file.name : values.filename; const filename = file ? file.name : values.filename;
handleCancel({ handleCancel({
filename, filename,
@ -64,15 +73,33 @@ const EditScriptNameModal = ({
setType(e.target.value); setType(e.target.value);
}; };
const getDirs = (data) => {
for (const item of data) {
if (item.children && item.children.length > 0) {
item.children = item.children
.filter((x) => x.type === 'directory')
.map((x) => ({ ...x, disabled: false }));
getDirs(item.children);
}
}
return data;
};
useEffect(() => {
const originDirs = treeData
.filter((x) => x.type === 'directory')
.map((x) => ({ ...x, disabled: false }));
const dirs = getDirs(originDirs);
setDirs(dirs);
}, [treeData]);
useEffect(() => { useEffect(() => {
form.resetFields(); form.resetFields();
const originDirs = treeData.filter((x) => x.disabled);
setDirs([{ key: '' }, ...originDirs]);
}, [visible]); }, [visible]);
return ( return (
<Modal <Modal
title="新建文件" title="新建脚本"
visible={visible} visible={visible}
forceRender forceRender
centered centered
@ -111,16 +138,14 @@ const EditScriptNameModal = ({
<Input placeholder="请输入文件名" /> <Input placeholder="请输入文件名" />
</Form.Item> </Form.Item>
)} )}
<Form.Item <Form.Item label="父目录" name="path">
label="父目录" <TreeSelect
name="path" allowClear
initialValue={dirs && dirs.length > 0 ? dirs[0].key : ''} treeData={dirs}
> fieldNames={{ value: 'key', label: 'title' }}
<Select placeholder="请选择父目录"> placeholder="请选择父目录"
{dirs.map((x) => ( treeDefaultExpandAll
<Option value={x.key}>{x.key || '根'}</Option> />
))}
</Select>
</Form.Item> </Form.Item>
{type === 'upload' && ( {type === 'upload' && (
<Form.Item label="文件" name="file"> <Form.Item label="文件" name="file">

View File

@ -104,7 +104,7 @@ const Script = ({ headerStyle, isPhone, theme, socketMessage }: any) => {
const getDetail = (node: any) => { const getDetail = (node: any) => {
request request
.get(`${config.apiPrefix}scripts/${node.value}?path=${node.parent || ''}`) .get(`${config.apiPrefix}scripts/${node.title}?path=${node.parent || ''}`)
.then((data) => { .then((data) => {
setValue(data.data); setValue(data.data);
}); });
@ -117,7 +117,6 @@ const Script = ({ headerStyle, isPhone, theme, socketMessage }: any) => {
const obj = { const obj = {
node: { node: {
title: s, title: s,
value: s,
key: p ? vkey : s, key: p ? vkey : s,
parent: p, parent: p,
}, },
@ -209,7 +208,7 @@ const Script = ({ headerStyle, isPhone, theme, socketMessage }: any) => {
<> <>
<Text style={{ wordBreak: 'break-all' }} type="warning"> <Text style={{ wordBreak: 'break-all' }} type="warning">
{currentNode.value} {currentNode.title}
</Text>{' '} </Text>{' '}
</> </>
@ -222,7 +221,7 @@ const Script = ({ headerStyle, isPhone, theme, socketMessage }: any) => {
request request
.put(`${config.apiPrefix}scripts`, { .put(`${config.apiPrefix}scripts`, {
data: { data: {
filename: currentNode.value, filename: currentNode.title,
path: currentNode.parent || '', path: currentNode.parent || '',
content, content,
}, },
@ -262,7 +261,7 @@ const Script = ({ headerStyle, isPhone, theme, socketMessage }: any) => {
request request
.delete(`${config.apiPrefix}scripts`, { .delete(`${config.apiPrefix}scripts`, {
data: { data: {
filename: currentNode.value, filename: currentNode.title,
path: currentNode.parent || '', path: currentNode.parent || '',
}, },
}) })
@ -315,8 +314,9 @@ const Script = ({ headerStyle, isPhone, theme, socketMessage }: any) => {
) => { ) => {
if (filename) { if (filename) {
const newData = [...data]; const newData = [...data];
const _file = { title: filename, key, value: filename, parent: path }; const _file = { title: filename, key, parent: path };
if (path) { if (path) {
// TODO: 更新左侧树数据
const parentNodeIndex = newData.findIndex((x) => x.key === path); const parentNodeIndex = newData.findIndex((x) => x.key === path);
if (parentNodeIndex !== -1) { if (parentNodeIndex !== -1) {
const parentNode = newData[parentNodeIndex]; const parentNode = newData[parentNodeIndex];
@ -331,7 +331,7 @@ const Script = ({ headerStyle, isPhone, theme, socketMessage }: any) => {
newData.unshift(_file); newData.unshift(_file);
} }
setData(newData); setData(newData);
onSelect(_file.value, _file); onSelect(_file.title, _file);
setIsEditing(true); setIsEditing(true);
} }
setIsAddFileModalVisible(false); setIsAddFileModalVisible(false);
@ -341,7 +341,7 @@ const Script = ({ headerStyle, isPhone, theme, socketMessage }: any) => {
request request
.post(`${config.apiPrefix}scripts/download`, { .post(`${config.apiPrefix}scripts/download`, {
data: { data: {
filename: currentNode.value, filename: currentNode.title,
}, },
}) })
.then((_data: any) => { .then((_data: any) => {
@ -349,7 +349,7 @@ const Script = ({ headerStyle, isPhone, theme, socketMessage }: any) => {
const url = URL.createObjectURL(blob); const url = URL.createObjectURL(blob);
const a = document.createElement('a'); const a = document.createElement('a');
a.href = url; a.href = url;
a.download = currentNode.value; a.download = currentNode.title;
document.documentElement.appendChild(a); document.documentElement.appendChild(a);
a.click(); a.click();
document.documentElement.removeChild(a); document.documentElement.removeChild(a);