UI: Add "Run" action to run current script in editor (#1987)

This commit is contained in:
catloversg
2025-03-04 17:43:31 +07:00
committed by GitHub
parent 8cdafdc7b9
commit 23ad55554e
7 changed files with 316 additions and 74 deletions
+33 -2
View File
@@ -47,6 +47,7 @@ import {
import { SpecialServers } from "../../Server/data/SpecialServers";
import { SnackbarEvents } from "../../ui/React/Snackbar";
import { ToastVariant } from "@enums";
import { createRunningScriptInstance, startWorkerScript } from "../../NetscriptWorker";
// Extend acorn-walk to support TypeScript nodes.
extendAcornWalkForTypeScriptNodes(walk.base);
@@ -219,6 +220,32 @@ function Root(props: IProps): React.ReactElement {
rerender();
}, [rerender]);
const run = useCallback(() => {
if (currentScript === null) {
return;
}
// Check if "currentScript" is a script. It may be a text file.
if (!hasScriptExtension(currentScript.path)) {
dialogBoxCreate(`Cannot run ${currentScript.path}. It is not a script.`);
return;
}
// Check if the current script's server is valid.
const server = GetServer(currentScript.hostname);
if (server === null) {
return;
}
// Always save before doing anything else.
save();
const result = createRunningScriptInstance(server, currentScript.path, null, 1, []);
if (!result.success) {
dialogBoxCreate(result.message);
return;
}
startWorkerScript(result.runningScript, server);
}, [save]);
useEffect(() => {
function keydown(event: KeyboardEvent): void {
if (Settings.DisableHotkeys) {
@@ -234,10 +261,14 @@ function Root(props: IProps): React.ReactElement {
event.preventDefault();
Router.toPage(Page.Terminal);
}
if (keyBindingTypes.has(ScriptEditorAction.Run)) {
event.preventDefault();
run();
}
}
document.addEventListener("keydown", keydown);
return () => document.removeEventListener("keydown", keydown);
}, [save]);
}, [save, run]);
function infLoop(ast: AST, code: string): void {
if (editorRef.current === null || currentScript === null || isLegacyScript(currentScript.path)) {
@@ -568,7 +599,7 @@ function Root(props: IProps): React.ReactElement {
{statusBarRef.current}
<Toolbar onSave={save} editor={editorRef.current} />
<Toolbar onSave={save} onRun={run} editor={editorRef.current} />
</div>
{!currentScript && <NoOpenScripts />}
</>
+7 -1
View File
@@ -29,9 +29,10 @@ type IStandaloneCodeEditor = monaco.editor.IStandaloneCodeEditor;
interface IProps {
editor: IStandaloneCodeEditor | null;
onSave: () => void;
onRun: () => void;
}
export function Toolbar({ editor, onSave }: IProps) {
export function Toolbar({ editor, onSave, onRun }: IProps) {
const [ramInfoOpen, { on: openRAMInfo, off: closeRAMInfo }] = useBoolean(false);
const [optionsOpen, { on: openOptions, off: closeOptions }] = useBoolean(false);
@@ -76,6 +77,11 @@ export function Toolbar({ editor, onSave }: IProps) {
Terminal
</Button>
</Tooltip>
<Tooltip title={parseKeyCombinationsToString(CurrentKeyBindings[ScriptEditorAction.Run])}>
<Button sx={{ mr: 1 }} onClick={onRun}>
Run
</Button>
</Tooltip>
<Typography>
<NsApiDocumentationLink />
</Typography>