mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-26 14:26:00 +00:00
shelltab uses require editor
This commit is contained in:
@@ -97,14 +97,15 @@ function GenerateSctriptButton({ modalState }) {
|
|||||||
|
|
||||||
const handleGenerateScript = async () => {
|
const handleGenerateScript = async () => {
|
||||||
const code = await createImpExpScript(extensions, values);
|
const code = await createImpExpScript(extensions, values);
|
||||||
openNewTab(setOpenedTabs, {
|
openNewTab(
|
||||||
title: 'Shell',
|
setOpenedTabs,
|
||||||
icon: 'img shell',
|
{
|
||||||
tabComponent: 'ShellTab',
|
title: 'Shell',
|
||||||
props: {
|
icon: 'img shell',
|
||||||
initialScript: code,
|
tabComponent: 'ShellTab',
|
||||||
},
|
},
|
||||||
});
|
code
|
||||||
|
);
|
||||||
modalState.close();
|
modalState.close();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import ReactDOM from 'react-dom';
|
import ReactDOM from 'react-dom';
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import localforage from 'localforage';
|
|
||||||
import axios from '../utility/axios';
|
import axios from '../utility/axios';
|
||||||
import { useSetOpenedTabs } from '../utility/globalState';
|
import { useSetOpenedTabs } from '../utility/globalState';
|
||||||
import { VerticalSplitter } from '../widgets/Splitter';
|
import { VerticalSplitter } from '../widgets/Splitter';
|
||||||
@@ -13,27 +12,16 @@ import ShellToolbar from '../query/ShellToolbar';
|
|||||||
import RunnerOutputPane from '../query/RunnerOutputPane';
|
import RunnerOutputPane from '../query/RunnerOutputPane';
|
||||||
import useShowModal from '../modals/showModal';
|
import useShowModal from '../modals/showModal';
|
||||||
import ImportExportModal from '../modals/ImportExportModal';
|
import ImportExportModal from '../modals/ImportExportModal';
|
||||||
|
import useEditorData from '../utility/useEditorData';
|
||||||
|
|
||||||
export default function ShellTab({
|
const configRegex = /\s*\/\/\s*@ImportExportConfigurator\s*\n\s*\/\/\s*(\{[^\n]+\})\n/;
|
||||||
tabid,
|
const requireRegex = /\s*(\/\/\s*@require\s+[^\n]+)\n/g;
|
||||||
initialArgs,
|
|
||||||
tabVisible,
|
export default function ShellTab({ tabid, tabVisible, toolbarPortalRef, ...other }) {
|
||||||
toolbarPortalRef,
|
|
||||||
initialScript,
|
|
||||||
storageKey,
|
|
||||||
...other
|
|
||||||
}) {
|
|
||||||
const localStorageKey = `tabdata_shell_${tabid}`;
|
|
||||||
const [shellText, setShellText] = React.useState(initialScript || '');
|
|
||||||
const shellTextRef = React.useRef(shellText);
|
|
||||||
const [busy, setBusy] = React.useState(false);
|
const [busy, setBusy] = React.useState(false);
|
||||||
const showModal = useShowModal();
|
const showModal = useShowModal();
|
||||||
|
const { editorData, setEditorData } = useEditorData({ tabid });
|
||||||
|
|
||||||
const saveToStorage = React.useCallback(() => localforage.setItem(localStorageKey, shellTextRef.current), [
|
|
||||||
localStorageKey,
|
|
||||||
shellTextRef,
|
|
||||||
]);
|
|
||||||
const saveToStorageDebounced = React.useMemo(() => _.debounce(saveToStorage, 5000), [saveToStorage]);
|
|
||||||
const setOpenedTabs = useSetOpenedTabs();
|
const setOpenedTabs = useSetOpenedTabs();
|
||||||
|
|
||||||
const [executeNumber, setExecuteNumber] = React.useState(0);
|
const [executeNumber, setExecuteNumber] = React.useState(0);
|
||||||
@@ -41,25 +29,6 @@ export default function ShellTab({
|
|||||||
|
|
||||||
const socket = useSocket();
|
const socket = useSocket();
|
||||||
|
|
||||||
React.useEffect(() => {
|
|
||||||
window.addEventListener('beforeunload', saveToStorage);
|
|
||||||
return () => {
|
|
||||||
saveToStorage();
|
|
||||||
window.removeEventListener('beforeunload', saveToStorage);
|
|
||||||
};
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
React.useEffect(() => {
|
|
||||||
if (!storageKey)
|
|
||||||
changeTab(tabid, setOpenedTabs, (tab) => ({
|
|
||||||
...tab,
|
|
||||||
props: {
|
|
||||||
...tab.props,
|
|
||||||
storageKey: localStorageKey,
|
|
||||||
},
|
|
||||||
}));
|
|
||||||
}, [storageKey]);
|
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
changeTab(tabid, setOpenedTabs, (tab) => ({ ...tab, busy }));
|
changeTab(tabid, setOpenedTabs, (tab) => ({ ...tab, busy }));
|
||||||
}, [busy]);
|
}, [busy]);
|
||||||
@@ -79,12 +48,6 @@ export default function ShellTab({
|
|||||||
}
|
}
|
||||||
}, [runnerId, socket]);
|
}, [runnerId, socket]);
|
||||||
|
|
||||||
const handleChange = (text) => {
|
|
||||||
if (text != null) shellTextRef.current = text;
|
|
||||||
setShellText(text);
|
|
||||||
saveToStorageDebounced();
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleExecute = async () => {
|
const handleExecute = async () => {
|
||||||
if (busy) return;
|
if (busy) return;
|
||||||
setExecuteNumber((num) => num + 1);
|
setExecuteNumber((num) => num + 1);
|
||||||
@@ -92,7 +55,9 @@ export default function ShellTab({
|
|||||||
|
|
||||||
let runid = runnerId;
|
let runid = runnerId;
|
||||||
const resp = await axios.post('runners/start', {
|
const resp = await axios.post('runners/start', {
|
||||||
script: selectedText || shellText,
|
script: selectedText
|
||||||
|
? [...(editorData || '').matchAll(requireRegex)].map((x) => `${x[1]}\n`).join('') + selectedText
|
||||||
|
: editorData,
|
||||||
});
|
});
|
||||||
runid = resp.data.runid;
|
runid = resp.data.runid;
|
||||||
setRunnerId(runid);
|
setRunnerId(runid);
|
||||||
@@ -112,10 +77,8 @@ export default function ShellTab({
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const configRegex = /\s*\/\/\s*@ImportExportConfigurator\s*\n\s*\/\/\s*(\{[^\n]+\})\n/;
|
|
||||||
|
|
||||||
const handleEdit = () => {
|
const handleEdit = () => {
|
||||||
const jsonTextMatch = shellText.match(configRegex);
|
const jsonTextMatch = (editorData || '').match(configRegex);
|
||||||
if (jsonTextMatch) {
|
if (jsonTextMatch) {
|
||||||
showModal((modalState) => (
|
showModal((modalState) => (
|
||||||
<ImportExportModal modalState={modalState} initialValues={JSON.parse(jsonTextMatch[1])} />
|
<ImportExportModal modalState={modalState} initialValues={JSON.parse(jsonTextMatch[1])} />
|
||||||
@@ -127,8 +90,8 @@ export default function ShellTab({
|
|||||||
<>
|
<>
|
||||||
<VerticalSplitter>
|
<VerticalSplitter>
|
||||||
<JavaScriptEditor
|
<JavaScriptEditor
|
||||||
value={shellText}
|
value={editorData || ''}
|
||||||
onChange={handleChange}
|
onChange={setEditorData}
|
||||||
tabVisible={tabVisible}
|
tabVisible={tabVisible}
|
||||||
onKeyDown={handleKeyDown}
|
onKeyDown={handleKeyDown}
|
||||||
editorRef={editorRef}
|
editorRef={editorRef}
|
||||||
@@ -144,7 +107,7 @@ export default function ShellTab({
|
|||||||
busy={busy}
|
busy={busy}
|
||||||
cancel={handleCancel}
|
cancel={handleCancel}
|
||||||
edit={handleEdit}
|
edit={handleEdit}
|
||||||
editAvailable={configRegex.test(shellText || '')}
|
editAvailable={configRegex.test(editorData || '')}
|
||||||
/>,
|
/>,
|
||||||
toolbarPortalRef.current
|
toolbarPortalRef.current
|
||||||
)}
|
)}
|
||||||
|
|||||||
Reference in New Issue
Block a user