任务详情脚本增加编辑功能

This commit is contained in:
whyour 2022-03-08 00:11:37 +08:00
parent 38df66a5b1
commit 0f0c79f52f

View File

@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react'; import React, { useEffect, useRef, useState } from 'react';
import { import {
Modal, Modal,
message, message,
@ -9,6 +9,7 @@ import {
Tag, Tag,
List, List,
Divider, Divider,
Typography,
} from 'antd'; } from 'antd';
import { import {
ClockCircleOutlined, ClockCircleOutlined,
@ -24,6 +25,8 @@ import config from '@/utils/config';
import CronLogModal from './logModal'; import CronLogModal from './logModal';
import Editor from '@monaco-editor/react'; import Editor from '@monaco-editor/react';
const { Text } = Typography;
const tabList = [ const tabList = [
{ {
key: 'log', key: 'log',
@ -59,6 +62,8 @@ const CronDetailModal = ({
const [log, setLog] = useState(''); const [log, setLog] = useState('');
const [value, setValue] = useState(''); const [value, setValue] = useState('');
const [isLogModalVisible, setIsLogModalVisible] = useState(false); const [isLogModalVisible, setIsLogModalVisible] = useState(false);
const editorRef = useRef<any>(null);
const [scriptInfo, setScriptInfo] = useState<any>({});
const contentList: any = { const contentList: any = {
log: ( log: (
@ -79,7 +84,6 @@ const CronDetailModal = ({
theme={theme} theme={theme}
value={value} value={value}
options={{ options={{
readOnly: true,
fontSize: 12, fontSize: 12,
lineNumbersMinChars: 3, lineNumbersMinChars: 3,
fontFamily: 'Source Code Pro', fontFamily: 'Source Code Pro',
@ -87,6 +91,9 @@ const CronDetailModal = ({
glyphMargin: false, glyphMargin: false,
wordWrap: 'on', wordWrap: 'on',
}} }}
onMount={(editor) => {
editorRef.current = editor;
}}
/> />
), ),
}; };
@ -128,6 +135,7 @@ const CronDetailModal = ({
s = p; s = p;
p = ''; p = '';
} }
setScriptInfo({ parent: p, filename: s });
request request
.get(`${config.apiPrefix}scripts/${s}?path=${p || ''}`) .get(`${config.apiPrefix}scripts/${s}?path=${p || ''}`)
.then((data) => { .then((data) => {
@ -136,6 +144,48 @@ const CronDetailModal = ({
} }
}; };
const saveFile = () => {
Modal.confirm({
title: `确认保存`,
content: (
<>
<Text style={{ wordBreak: 'break-all' }} type="warning">
{scriptInfo.filename}
</Text>{' '}
</>
),
onOk() {
const content = editorRef.current
? editorRef.current.getValue().replace(/\r\n/g, '\n')
: value;
return new Promise((resolve, reject) => {
request
.put(`${config.apiPrefix}scripts`, {
data: {
filename: scriptInfo.filename,
path: scriptInfo.parent || '',
content,
},
})
.then((_data: any) => {
if (_data.code === 200) {
message.success(`保存成功`);
} else {
message.error(_data);
}
resolve(null);
})
.catch((e) => reject(e));
});
},
onCancel() {
console.log('Cancel');
},
});
};
useEffect(() => { useEffect(() => {
if (cron && cron.id) { if (cron && cron.id) {
getLogs(); getLogs();
@ -245,6 +295,17 @@ const CronDetailModal = ({
onTabChange={(key) => { onTabChange={(key) => {
onTabChange(key); onTabChange(key);
}} }}
tabBarExtraContent={
activeTabKey === 'script' && (
<Button
type="primary"
style={{ marginRight: 8 }}
onClick={saveFile}
>
</Button>
)
}
bodyStyle={{ height: 'calc(80vh - 188px)', overflowY: 'auto' }} bodyStyle={{ height: 'calc(80vh - 188px)', overflowY: 'auto' }}
> >
{contentList[activeTabKey]} {contentList[activeTabKey]}