From 2f5e9469794e3f7ed5877e55e0438b008afa5c45 Mon Sep 17 00:00:00 2001 From: whyour Date: Tue, 23 Nov 2021 23:36:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=B0=83=E8=AF=95=E8=BF=90?= =?UTF-8?q?=E8=A1=8C=E6=97=B6=E6=96=87=E4=BB=B6=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/api/script.ts | 22 +++++++++------- src/layouts/index.tsx | 18 +++++++------ src/pages/script/editModal.tsx | 48 +++++++++++++++++++++------------- src/pages/script/index.tsx | 6 ++--- 4 files changed, 55 insertions(+), 39 deletions(-) diff --git a/back/api/script.ts b/back/api/script.ts index 75f85f8a..0fdc5654 100644 --- a/back/api/script.ts +++ b/back/api/script.ts @@ -9,7 +9,7 @@ import { Logger } from 'winston'; import config from '../config'; import * as fs from 'fs'; import { celebrate, Joi } from 'celebrate'; -import path from 'path'; +import path, { join } from 'path'; import ScriptService from '../services/script'; const route = Router(); @@ -44,7 +44,7 @@ export default (app: Router) => { children.push({ title: childFile, value: childFile, - key: `${fileOrDir}-${childFile}`, + key: `${fileOrDir}/${childFile}`, mtime: statObj.mtimeMs, parent: fileOrDir, }); @@ -84,10 +84,12 @@ export default (app: Router) => { async (req: Request, res: Response, next: NextFunction) => { const logger: Logger = Container.get('logger'); try { - const path = req.query.path ? `${req.query.path}/` : ''; - const content = getFileContentByName( - `${config.scriptPath}${path}${req.params.file}`, + const filePath = join( + config.scriptPath, + req.query.path as string, + req.params.file, ); + const content = getFileContentByName(filePath); res.send({ code: 200, data: content }); } catch (e) { logger.error('🔥 error: %o', e); @@ -101,7 +103,7 @@ export default (app: Router) => { celebrate({ body: Joi.object({ filename: Joi.string().required(), - path: Joi.string().allow(''), + path: Joi.string().optional().allow(''), content: Joi.string().allow(''), originFilename: Joi.string().allow(''), }), @@ -161,7 +163,7 @@ export default (app: Router) => { celebrate({ body: Joi.object({ filename: Joi.string().required(), - path: Joi.string().allow(''), + path: Joi.string().optional().allow(''), content: Joi.string().required(), }), }), @@ -173,7 +175,7 @@ export default (app: Router) => { content: string; path: string; }; - const filePath = `${config.scriptPath}${path}/${filename}`; + const filePath = join(config.scriptPath, path, filename); fs.writeFileSync(filePath, content); return res.send({ code: 200 }); } catch (e) { @@ -198,7 +200,7 @@ export default (app: Router) => { filename: string; path: string; }; - const filePath = `${config.scriptPath}${path}/${filename}`; + const filePath = join(config.scriptPath, path, filename); fs.unlinkSync(filePath); res.send({ code: 200 }); } catch (e) { @@ -254,7 +256,7 @@ export default (app: Router) => { filename: string; path: string; }; - const filePath = `${path}/${filename}`; + const filePath = join(path, filename); const scriptService = Container.get(ScriptService); const result = await scriptService.runScript(filePath); res.send(result); diff --git a/src/layouts/index.tsx b/src/layouts/index.tsx index 8baa5123..e69dbaf2 100644 --- a/src/layouts/index.tsx +++ b/src/layouts/index.tsx @@ -275,14 +275,16 @@ export default function (props: any) { }} onCollapse={setCollapsed} collapsed={collapsed} - rightContentRender={() => ( - - - } /> - admin - - - )} + rightContentRender={() => + ctx.isPhone && ( + + + } /> + admin + + + ) + } collapsedButtonRender={(collapsed) => ( void; }) => { const [value, setValue] = useState(''); const [language, setLanguage] = useState('javascript'); - const [fileName, setFileName] = useState(''); + const [cNode, setCNode] = useState(); const [selectedKey, setSelectedKey] = useState(''); const [saveModalVisible, setSaveModalVisible] = useState(false); const [settingModalVisible, setSettingModalVisible] = @@ -53,28 +53,31 @@ const EditModal = ({ }; const onSelect = (value: any, node: any) => { - if (node.value === fileName || !value) { + if (node.key === selectedKey || !value) { return; } const newMode = LangMap[value.slice(-3)] || ''; - setFileName(value); + setCNode(node); setLanguage(newMode); getDetail(node); setSelectedKey(node.key); }; const getDetail = (node: any) => { - request.get(`${config.apiPrefix}scripts/${node.value}`).then((data) => { - setValue(data.data); - }); + request + .get(`${config.apiPrefix}scripts/${node.value}?path=${node.parent || ''}`) + .then((data) => { + setValue(data.data); + }); }; const run = () => { + setLog(''); request .put(`${config.apiPrefix}scripts/run`, { data: { - filename: fileName, - path: '', + filename: cNode.value, + path: cNode.parent || '', }, }) .then((data) => {}); @@ -98,21 +101,21 @@ const EditModal = ({ }, [socketMessage]); useEffect(() => { - if (currentFile) { - setFileName(currentFile); + if (currentNode) { + setCNode(currentNode); setValue(content as string); - setSelectedKey(currentFile); + setSelectedKey(currentNode.key); } - }, [currentFile, content]); + }, [content, currentNode]); return ( - {fileName}