2021-10-11 22:59:37 +02:00
|
|
|
import React, { useState, useEffect, useRef, useMemo } from "react";
|
2021-08-20 07:21:37 +02:00
|
|
|
import Editor from "@monaco-editor/react";
|
2021-08-20 07:57:32 +02:00
|
|
|
import * as monaco from "monaco-editor";
|
2021-12-17 18:34:00 +01:00
|
|
|
|
2021-08-27 01:14:56 +02:00
|
|
|
type IStandaloneCodeEditor = monaco.editor.IStandaloneCodeEditor;
|
2021-09-25 05:36:28 +02:00
|
|
|
import { OptionsModal } from "./OptionsModal";
|
2021-08-20 08:14:27 +02:00
|
|
|
import { Options } from "./Options";
|
2021-08-20 07:21:37 +02:00
|
|
|
import { isValidFilePath } from "../../Terminal/DirectoryHelpers";
|
|
|
|
import { IPlayer } from "../../PersonObjects/IPlayer";
|
2021-09-17 08:58:02 +02:00
|
|
|
import { IRouter } from "../../ui/Router";
|
2021-09-25 20:42:57 +02:00
|
|
|
import { dialogBoxCreate } from "../../ui/React/DialogBox";
|
2021-09-24 22:34:21 +02:00
|
|
|
import { isScriptFilename } from "../../Script/isScriptFilename";
|
2021-08-20 07:21:37 +02:00
|
|
|
import { Script } from "../../Script/Script";
|
|
|
|
import { TextFile } from "../../TextFile";
|
2021-10-29 05:04:26 +02:00
|
|
|
import { calculateRamUsage, checkInfiniteLoop } from "../../Script/RamCalculations";
|
2021-08-20 07:21:37 +02:00
|
|
|
import { RamCalculationErrorCode } from "../../Script/RamCalculationErrorCodes";
|
|
|
|
import { numeralWrapper } from "../../ui/numeralFormat";
|
2021-10-08 06:21:30 +02:00
|
|
|
import { CursorPositions } from "../CursorPositions";
|
2021-10-28 05:19:19 +02:00
|
|
|
|
2021-08-21 06:17:26 +02:00
|
|
|
import { NetscriptFunctions } from "../../NetscriptFunctions";
|
|
|
|
import { WorkerScript } from "../../Netscript/WorkerScript";
|
|
|
|
import { Settings } from "../../Settings/Settings";
|
2021-09-09 05:47:34 +02:00
|
|
|
import { iTutorialNextStep, ITutorial, iTutorialSteps } from "../../InteractiveTutorial";
|
2021-10-11 22:59:37 +02:00
|
|
|
import { debounce } from "lodash";
|
2021-10-11 23:57:17 +02:00
|
|
|
import { saveObject } from "../../SaveObject";
|
2021-10-12 16:56:19 +02:00
|
|
|
import { loadThemes } from "./themes";
|
2021-12-14 02:08:58 +01:00
|
|
|
import { GetServer } from "../../Server/AllServers";
|
2021-08-21 06:17:26 +02:00
|
|
|
|
2021-09-25 05:36:28 +02:00
|
|
|
import Button from "@mui/material/Button";
|
|
|
|
import Typography from "@mui/material/Typography";
|
|
|
|
import Link from "@mui/material/Link";
|
|
|
|
import Box from "@mui/material/Box";
|
|
|
|
import TextField from "@mui/material/TextField";
|
|
|
|
import IconButton from "@mui/material/IconButton";
|
|
|
|
import SettingsIcon from "@mui/icons-material/Settings";
|
|
|
|
|
2021-10-30 18:34:14 +02:00
|
|
|
import libSource from "!!raw-loader!../NetscriptDefinitions.d.ts";
|
2021-10-28 05:19:19 +02:00
|
|
|
|
2021-10-10 04:59:06 +02:00
|
|
|
let symbolsLoaded = false;
|
2021-08-21 06:17:26 +02:00
|
|
|
let symbols: string[] = [];
|
2021-10-05 03:06:55 +02:00
|
|
|
export function SetupTextEditor(): void {
|
2021-09-05 01:09:30 +02:00
|
|
|
const ns = NetscriptFunctions({} as WorkerScript);
|
2021-08-21 06:17:26 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
function populate(ns: any): string[] {
|
|
|
|
let symbols: string[] = [];
|
|
|
|
const keys = Object.keys(ns);
|
|
|
|
for (const key of keys) {
|
|
|
|
if (typeof ns[key] === "object") {
|
|
|
|
symbols.push(key);
|
|
|
|
symbols = symbols.concat(populate(ns[key]));
|
|
|
|
}
|
|
|
|
if (typeof ns[key] === "function") {
|
|
|
|
symbols.push(key);
|
|
|
|
}
|
2021-08-21 06:17:26 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
return symbols;
|
|
|
|
}
|
|
|
|
symbols = populate(ns);
|
2021-09-05 02:14:33 +02:00
|
|
|
|
2021-10-27 21:16:16 +02:00
|
|
|
const exclude = ["heart", "break", "exploit", "bypass", "corporation", "alterReality"];
|
2021-10-16 00:25:22 +02:00
|
|
|
symbols = symbols.filter((symbol: string) => !exclude.includes(symbol)).sort();
|
2021-10-05 03:06:55 +02:00
|
|
|
}
|
2021-08-20 07:21:37 +02:00
|
|
|
|
|
|
|
interface IProps {
|
2021-09-05 01:09:30 +02:00
|
|
|
filename: string;
|
|
|
|
code: string;
|
2021-10-16 03:27:02 +02:00
|
|
|
hostname: string;
|
2021-09-05 01:09:30 +02:00
|
|
|
player: IPlayer;
|
2021-09-17 08:58:02 +02:00
|
|
|
router: IRouter;
|
2021-08-27 01:14:56 +02:00
|
|
|
}
|
2021-08-20 07:21:37 +02:00
|
|
|
|
2021-08-21 07:54:39 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2021-08-20 07:21:37 +02:00
|
|
|
// How to load function definition in monaco
|
|
|
|
// https://github.com/Microsoft/monaco-editor/issues/1415
|
|
|
|
// https://microsoft.github.io/monaco-editor/api/modules/monaco.languages.html
|
|
|
|
// https://www.npmjs.com/package/@monaco-editor/react#development-playground
|
2021-08-20 10:03:00 +02:00
|
|
|
// https://microsoft.github.io/monaco-editor/playground.html#extending-language-services-custom-languages
|
2021-08-20 19:57:32 +02:00
|
|
|
// https://github.com/threehams/typescript-error-guide/blob/master/stories/components/Editor.tsx#L11-L39
|
2021-10-16 00:27:02 +02:00
|
|
|
// https://blog.checklyhq.com/customizing-monaco/
|
2021-08-20 07:21:37 +02:00
|
|
|
|
2021-08-23 08:09:49 +02:00
|
|
|
// These variables are used to reload a script when it's clicked on. Because we
|
|
|
|
// won't have references to the old script.
|
|
|
|
let lastFilename = "";
|
|
|
|
let lastCode = "";
|
2021-10-16 03:27:02 +02:00
|
|
|
let hostname = "";
|
2021-08-23 08:09:49 +02:00
|
|
|
let lastPosition: monaco.Position | null = null;
|
|
|
|
|
2021-08-20 07:21:37 +02:00
|
|
|
export function Root(props: IProps): React.ReactElement {
|
2021-09-05 01:09:30 +02:00
|
|
|
const editorRef = useRef<IStandaloneCodeEditor | null>(null);
|
2021-12-17 18:34:00 +01:00
|
|
|
const vimStatusRef = useRef<HTMLElement>(null);
|
|
|
|
const [vimEditor, setVimEditor] = useState<any>(null);
|
|
|
|
const [editor, setEditor] = useState<IStandaloneCodeEditor|null>(null);
|
2021-09-09 05:47:34 +02:00
|
|
|
const [filename, setFilename] = useState(props.filename ? props.filename : lastFilename);
|
2021-09-22 07:10:29 +02:00
|
|
|
const [code, setCode] = useState<string>(props.filename ? props.code : lastCode);
|
2021-10-29 05:04:26 +02:00
|
|
|
const [decorations, setDecorations] = useState<string[]>([]);
|
2021-10-16 03:27:02 +02:00
|
|
|
hostname = props.filename ? props.hostname : hostname;
|
|
|
|
if (hostname === "") {
|
|
|
|
hostname = props.player.getCurrentServer().hostname;
|
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
const [ram, setRAM] = useState("RAM: ???");
|
2021-10-11 22:59:37 +02:00
|
|
|
const [updatingRam, setUpdatingRam] = useState(false);
|
2021-09-25 05:36:28 +02:00
|
|
|
const [optionsOpen, setOptionsOpen] = useState(false);
|
2021-09-05 01:09:30 +02:00
|
|
|
const [options, setOptions] = useState<Options>({
|
|
|
|
theme: Settings.MonacoTheme,
|
|
|
|
insertSpaces: Settings.MonacoInsertSpaces,
|
2021-10-05 03:06:55 +02:00
|
|
|
fontSize: Settings.MonacoFontSize,
|
2021-12-17 18:34:00 +01:00
|
|
|
vim: Settings.MonacoVim,
|
2021-09-05 01:09:30 +02:00
|
|
|
});
|
|
|
|
|
2021-10-11 22:59:37 +02:00
|
|
|
const debouncedSetRAM = useMemo(
|
|
|
|
() =>
|
|
|
|
debounce((s) => {
|
|
|
|
setRAM(s);
|
|
|
|
setUpdatingRam(false);
|
|
|
|
}, 300),
|
|
|
|
[],
|
|
|
|
);
|
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// store the last known state in case we need to restart without nano.
|
|
|
|
useEffect(() => {
|
2021-09-20 23:26:47 +02:00
|
|
|
if (props.filename === undefined) return;
|
2021-09-05 01:09:30 +02:00
|
|
|
lastFilename = props.filename;
|
|
|
|
lastCode = props.code;
|
|
|
|
lastPosition = null;
|
|
|
|
}, []);
|
2021-08-20 07:21:37 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
function save(): void {
|
|
|
|
if (editorRef.current !== null) {
|
|
|
|
const position = editorRef.current.getPosition();
|
|
|
|
if (position !== null) {
|
|
|
|
CursorPositions.saveCursor(filename, {
|
|
|
|
row: position.lineNumber,
|
|
|
|
column: position.column,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lastPosition = null;
|
2021-08-23 08:09:49 +02:00
|
|
|
|
2021-09-19 06:46:39 +02:00
|
|
|
// this is duplicate code with saving later.
|
2021-09-09 05:47:34 +02:00
|
|
|
if (ITutorial.isRunning && ITutorial.currStep === iTutorialSteps.TerminalTypeScript) {
|
2021-09-05 01:09:30 +02:00
|
|
|
//Make sure filename + code properly follow tutorial
|
|
|
|
if (filename !== "n00dles.script") {
|
2021-10-16 03:27:02 +02:00
|
|
|
dialogBoxCreate("Leave the script name as 'n00dles.script'!");
|
2021-09-05 01:09:30 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-09-09 05:47:34 +02:00
|
|
|
if (code.replace(/\s/g, "").indexOf("while(true){hack('n00dles');}") == -1) {
|
2021-09-05 01:09:30 +02:00
|
|
|
dialogBoxCreate("Please copy and paste the code from the tutorial!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Save the script
|
2021-12-14 02:08:58 +01:00
|
|
|
const server = GetServer(hostname);
|
2021-09-09 05:47:34 +02:00
|
|
|
if (server === null) throw new Error("Server should not be null but it is.");
|
2021-09-19 06:46:39 +02:00
|
|
|
let found = false;
|
2021-09-05 01:09:30 +02:00
|
|
|
for (let i = 0; i < server.scripts.length; i++) {
|
|
|
|
if (filename == server.scripts[i].filename) {
|
2021-10-16 03:27:02 +02:00
|
|
|
server.scripts[i].saveScript(filename, code, hostname, server.scripts);
|
2021-09-19 06:46:39 +02:00
|
|
|
found = true;
|
2021-08-20 07:57:32 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2021-08-20 07:21:37 +02:00
|
|
|
|
2021-09-19 06:46:39 +02:00
|
|
|
if (!found) {
|
|
|
|
const script = new Script();
|
2021-10-16 03:27:02 +02:00
|
|
|
script.saveScript(filename, code, hostname, server.scripts);
|
2021-09-19 06:46:39 +02:00
|
|
|
server.scripts.push(script);
|
|
|
|
}
|
|
|
|
|
|
|
|
iTutorialNextStep();
|
2021-08-20 07:21:37 +02:00
|
|
|
|
2021-09-19 06:46:39 +02:00
|
|
|
props.router.toTerminal();
|
|
|
|
return;
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2021-08-20 07:21:37 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
if (filename == "") {
|
|
|
|
dialogBoxCreate("You must specify a filename!");
|
|
|
|
return;
|
|
|
|
}
|
2021-08-20 07:21:37 +02:00
|
|
|
|
2021-09-24 00:47:43 +02:00
|
|
|
if (!isValidFilePath(filename)) {
|
2021-09-05 01:09:30 +02:00
|
|
|
dialogBoxCreate(
|
|
|
|
"Script filename can contain only alphanumerics, hyphens, and underscores, and must end with an extension.",
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2021-08-20 07:21:37 +02:00
|
|
|
|
2021-12-14 02:08:58 +01:00
|
|
|
const server = GetServer(hostname);
|
2021-09-09 05:47:34 +02:00
|
|
|
if (server === null) throw new Error("Server should not be null but it is.");
|
2021-09-24 00:47:43 +02:00
|
|
|
if (isScriptFilename(filename)) {
|
2021-09-05 01:09:30 +02:00
|
|
|
//If the current script already exists on the server, overwrite it
|
|
|
|
for (let i = 0; i < server.scripts.length; i++) {
|
|
|
|
if (filename == server.scripts[i].filename) {
|
2021-09-25 07:06:17 +02:00
|
|
|
server.scripts[i].saveScript(filename, code, props.player.currentServer, server.scripts);
|
2021-10-12 00:47:05 +02:00
|
|
|
if (Settings.SaveGameOnFileSave) saveObject.saveGame();
|
2021-09-17 08:58:02 +02:00
|
|
|
props.router.toTerminal();
|
2021-09-05 01:09:30 +02:00
|
|
|
return;
|
2021-08-20 07:21:37 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2021-08-20 07:21:37 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
//If the current script does NOT exist, create a new one
|
|
|
|
const script = new Script();
|
2021-09-25 07:06:17 +02:00
|
|
|
script.saveScript(filename, code, props.player.currentServer, server.scripts);
|
2021-09-05 01:09:30 +02:00
|
|
|
server.scripts.push(script);
|
|
|
|
} else if (filename.endsWith(".txt")) {
|
|
|
|
for (let i = 0; i < server.textFiles.length; ++i) {
|
|
|
|
if (server.textFiles[i].fn === filename) {
|
|
|
|
server.textFiles[i].write(code);
|
2021-10-12 00:47:05 +02:00
|
|
|
if (Settings.SaveGameOnFileSave) saveObject.saveGame();
|
2021-09-17 08:58:02 +02:00
|
|
|
props.router.toTerminal();
|
2021-09-05 01:09:30 +02:00
|
|
|
return;
|
2021-08-20 07:21:37 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
const textFile = new TextFile(filename, code);
|
|
|
|
server.textFiles.push(textFile);
|
|
|
|
} else {
|
2021-09-09 05:47:34 +02:00
|
|
|
dialogBoxCreate("Invalid filename. Must be either a script (.script, .js, or .ns) or " + " or text file (.txt)");
|
2021-09-05 01:09:30 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-10-11 23:57:17 +02:00
|
|
|
|
2021-10-12 00:47:05 +02:00
|
|
|
if (Settings.SaveGameOnFileSave) saveObject.saveGame();
|
2021-09-17 08:58:02 +02:00
|
|
|
props.router.toTerminal();
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2021-08-20 07:21:37 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
function beautify(): void {
|
|
|
|
if (editorRef.current === null) return;
|
2021-10-23 20:40:46 +02:00
|
|
|
editorRef.current.getAction("editor.action.formatDocument").run();
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2021-08-20 07:21:37 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
function onFilenameChange(event: React.ChangeEvent<HTMLInputElement>): void {
|
2021-10-08 06:21:30 +02:00
|
|
|
lastFilename = event.target.value;
|
2021-09-05 01:09:30 +02:00
|
|
|
setFilename(event.target.value);
|
|
|
|
}
|
2021-08-20 07:21:37 +02:00
|
|
|
|
2021-10-29 07:23:15 +02:00
|
|
|
function infLoop(newCode: string): void {
|
|
|
|
if (editorRef.current === null) return;
|
|
|
|
if (!filename.endsWith(".ns") && !filename.endsWith(".js")) return;
|
|
|
|
const awaitWarning = checkInfiniteLoop(newCode);
|
|
|
|
if (awaitWarning !== -1) {
|
|
|
|
const newDecorations = editorRef.current.deltaDecorations(decorations, [
|
|
|
|
{
|
|
|
|
range: {
|
|
|
|
startLineNumber: awaitWarning,
|
|
|
|
startColumn: 1,
|
|
|
|
endLineNumber: awaitWarning,
|
|
|
|
endColumn: 10,
|
|
|
|
},
|
|
|
|
options: {
|
|
|
|
isWholeLine: true,
|
|
|
|
glyphMarginClassName: "myGlyphMarginClass",
|
|
|
|
glyphMarginHoverMessage: {
|
|
|
|
value: "Possible infinite loop, await something.",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
setDecorations(newDecorations);
|
|
|
|
} else {
|
|
|
|
const newDecorations = editorRef.current.deltaDecorations(decorations, []);
|
|
|
|
setDecorations(newDecorations);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
function updateCode(newCode?: string): void {
|
|
|
|
if (newCode === undefined) return;
|
|
|
|
lastCode = newCode;
|
|
|
|
setCode(newCode);
|
2021-10-11 22:59:37 +02:00
|
|
|
updateRAM(newCode);
|
2021-11-02 21:10:01 +01:00
|
|
|
try {
|
|
|
|
if (editorRef.current !== null) {
|
|
|
|
lastPosition = editorRef.current.getPosition();
|
|
|
|
infLoop(newCode);
|
|
|
|
}
|
|
|
|
} catch (err) {}
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2021-08-20 07:21:37 +02:00
|
|
|
|
2021-10-11 22:59:37 +02:00
|
|
|
// calculate it once the first time the file is loaded.
|
|
|
|
useEffect(() => {
|
|
|
|
updateRAM(code);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
async function updateRAM(newCode: string): Promise<void> {
|
|
|
|
setUpdatingRam(true);
|
|
|
|
const codeCopy = newCode + "";
|
2021-09-09 05:47:34 +02:00
|
|
|
const ramUsage = await calculateRamUsage(codeCopy, props.player.getCurrentServer().scripts);
|
2021-09-05 01:09:30 +02:00
|
|
|
if (ramUsage > 0) {
|
2021-10-11 22:59:37 +02:00
|
|
|
debouncedSetRAM("RAM: " + numeralWrapper.formatRAM(ramUsage));
|
2021-09-05 01:09:30 +02:00
|
|
|
return;
|
2021-08-20 07:21:37 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
switch (ramUsage) {
|
|
|
|
case RamCalculationErrorCode.ImportError: {
|
2021-10-11 22:59:37 +02:00
|
|
|
debouncedSetRAM("RAM: Import Error");
|
2021-09-05 01:09:30 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case RamCalculationErrorCode.URLImportError: {
|
2021-10-11 22:59:37 +02:00
|
|
|
debouncedSetRAM("RAM: HTTP Import Error");
|
2021-09-05 01:09:30 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case RamCalculationErrorCode.SyntaxError:
|
|
|
|
default: {
|
2021-10-11 22:59:37 +02:00
|
|
|
debouncedSetRAM("RAM: Syntax Error");
|
2021-09-05 01:09:30 +02:00
|
|
|
break;
|
|
|
|
}
|
2021-08-20 07:21:37 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
return new Promise<void>(() => undefined);
|
|
|
|
}
|
2021-08-20 07:21:37 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
useEffect(() => {
|
2021-09-06 21:06:08 +02:00
|
|
|
function maybeSave(event: KeyboardEvent): void {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (Settings.DisableHotkeys) return;
|
|
|
|
//Ctrl + b
|
|
|
|
if (event.keyCode == 66 && (event.ctrlKey || event.metaKey)) {
|
|
|
|
event.preventDefault();
|
|
|
|
save();
|
|
|
|
}
|
2021-08-20 07:57:32 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
document.addEventListener("keydown", maybeSave);
|
|
|
|
return () => document.removeEventListener("keydown", maybeSave);
|
|
|
|
});
|
2021-08-20 07:57:32 +02:00
|
|
|
|
2021-12-17 18:34:00 +01:00
|
|
|
useEffect(() => {
|
|
|
|
// setup monaco-vim
|
|
|
|
if (options.vim && editor && !vimEditor) {
|
|
|
|
try {
|
|
|
|
// This library is not typed
|
|
|
|
// @ts-expect-error
|
|
|
|
window.require(["monaco-vim"], function (MonacoVim: any) {
|
|
|
|
setVimEditor(MonacoVim.initVimMode(editor, vimStatusRef.current));
|
|
|
|
MonacoVim.VimMode.Vim.defineEx('write', 'w', function() {
|
|
|
|
// your own implementation on what you want to do when :w is pressed
|
|
|
|
save();
|
|
|
|
});
|
|
|
|
MonacoVim.VimMode.Vim.defineEx('quit', 'q', function() {
|
|
|
|
save();
|
|
|
|
});
|
|
|
|
editor.focus();
|
|
|
|
});
|
|
|
|
} catch {}
|
|
|
|
} else if (!options.vim) {
|
|
|
|
// Whem vim mode is disabled
|
|
|
|
vimEditor?.dispose();
|
|
|
|
setVimEditor(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
vimEditor?.dispose();
|
|
|
|
}
|
|
|
|
}, [ options, editorRef, editor, vimEditor])
|
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
function onMount(editor: IStandaloneCodeEditor): void {
|
2021-12-17 18:34:00 +01:00
|
|
|
// Required when switching between site navigation (e.g. from Script Editor -> Terminal and back)
|
|
|
|
// the `useEffect()` for vim mode is called before editor is mounted.
|
|
|
|
setEditor(editor);
|
2021-09-05 01:09:30 +02:00
|
|
|
editorRef.current = editor;
|
|
|
|
if (editorRef.current === null) return;
|
|
|
|
const position = CursorPositions.getCursor(filename);
|
|
|
|
if (position.row !== -1)
|
|
|
|
editorRef.current.setPosition({
|
|
|
|
lineNumber: position.row,
|
|
|
|
column: position.column,
|
|
|
|
});
|
|
|
|
else if (lastPosition !== null)
|
|
|
|
editorRef.current.setPosition({
|
|
|
|
lineNumber: lastPosition.lineNumber,
|
|
|
|
column: lastPosition.column + 1,
|
|
|
|
});
|
2021-12-17 18:34:00 +01:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
editorRef.current.focus();
|
|
|
|
}
|
2021-08-20 07:57:32 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
function beforeMount(monaco: any): void {
|
2021-10-10 04:59:06 +02:00
|
|
|
if (symbolsLoaded) return;
|
|
|
|
symbolsLoaded = true;
|
2021-09-05 01:09:30 +02:00
|
|
|
monaco.languages.registerCompletionItemProvider("javascript", {
|
|
|
|
provideCompletionItems: () => {
|
|
|
|
const suggestions = [];
|
|
|
|
for (const symbol of symbols) {
|
|
|
|
suggestions.push({
|
|
|
|
label: symbol,
|
|
|
|
kind: monaco.languages.CompletionItemKind.Function,
|
|
|
|
insertText: symbol,
|
2021-09-09 05:47:34 +02:00
|
|
|
insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
|
2021-09-05 01:09:30 +02:00
|
|
|
});
|
2021-08-21 07:54:39 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
return { suggestions: suggestions };
|
|
|
|
},
|
|
|
|
});
|
2021-10-16 00:25:22 +02:00
|
|
|
(async function () {
|
2021-10-16 00:58:10 +02:00
|
|
|
// We have to improve the default js language otherwise theme sucks
|
2021-10-16 00:25:22 +02:00
|
|
|
const l = await monaco.languages
|
|
|
|
.getLanguages()
|
|
|
|
.find((l: any) => l.id === "javascript")
|
|
|
|
.loader();
|
|
|
|
l.language.tokenizer.root.unshift(["ns", { token: "ns" }]);
|
2021-11-06 22:46:55 +01:00
|
|
|
for (const symbol of symbols) l.language.tokenizer.root.unshift([symbol, { token: "netscriptfunction" }]);
|
2021-10-16 00:58:10 +02:00
|
|
|
const otherKeywords = ["let", "const", "var", "function"];
|
|
|
|
const otherKeyvars = ["true", "false", "null", "undefined"];
|
|
|
|
otherKeywords.forEach((k) => l.language.tokenizer.root.unshift([k, { token: "otherkeywords" }]));
|
|
|
|
otherKeyvars.forEach((k) => l.language.tokenizer.root.unshift([k, { token: "otherkeyvars" }]));
|
|
|
|
l.language.tokenizer.root.unshift(["this", { token: "this" }]);
|
2021-10-16 00:25:22 +02:00
|
|
|
})();
|
|
|
|
|
2021-10-28 05:19:19 +02:00
|
|
|
const source = (libSource + "").replace(/export /g, "");
|
|
|
|
monaco.languages.typescript.javascriptDefaults.addExtraLib(source, "netscript.d.ts");
|
|
|
|
monaco.languages.typescript.typescriptDefaults.addExtraLib(source, "netscript.d.ts");
|
2021-10-12 16:56:19 +02:00
|
|
|
loadThemes(monaco);
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2021-12-17 18:34:00 +01:00
|
|
|
|
|
|
|
// TODO: Make this responsive to window resizes
|
|
|
|
// Toolbars are roughly 108px + vim bar 34px
|
|
|
|
// Get percentage of space that toolbars represent and the rest should be the
|
|
|
|
// editor
|
|
|
|
const editorHeight = 100 - (((108 + (options.vim ? 34 : 0)) / window.innerHeight) * 100);
|
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return (
|
2021-09-17 08:04:44 +02:00
|
|
|
<>
|
2021-09-25 05:36:28 +02:00
|
|
|
<Box display="flex" flexDirection="row" alignItems="center">
|
2021-10-08 06:21:30 +02:00
|
|
|
<TextField
|
2021-10-16 03:28:17 +02:00
|
|
|
placeholder="filename"
|
2021-10-08 06:21:30 +02:00
|
|
|
type="text"
|
|
|
|
tabIndex={1}
|
|
|
|
value={filename}
|
|
|
|
onChange={onFilenameChange}
|
2021-10-16 03:27:02 +02:00
|
|
|
InputProps={{ startAdornment: <Typography>{hostname}:~/</Typography> }}
|
2021-10-08 06:21:30 +02:00
|
|
|
/>
|
2021-09-25 05:36:28 +02:00
|
|
|
<IconButton onClick={() => setOptionsOpen(true)}>
|
|
|
|
<>
|
|
|
|
<SettingsIcon />
|
|
|
|
options
|
|
|
|
</>
|
|
|
|
</IconButton>
|
|
|
|
</Box>
|
2021-09-05 01:09:30 +02:00
|
|
|
<Editor
|
|
|
|
beforeMount={beforeMount}
|
|
|
|
onMount={onMount}
|
2021-10-01 19:08:37 +02:00
|
|
|
loading={<Typography>Loading script editor!</Typography>}
|
2021-12-17 18:34:00 +01:00
|
|
|
height={`${editorHeight}%`}
|
2021-10-10 04:59:06 +02:00
|
|
|
defaultLanguage="javascript"
|
2021-09-05 01:09:30 +02:00
|
|
|
defaultValue={code}
|
|
|
|
onChange={updateCode}
|
|
|
|
theme={options.theme}
|
2021-10-29 05:04:26 +02:00
|
|
|
options={{ ...options, glyphMargin: true }}
|
2021-09-05 01:09:30 +02:00
|
|
|
/>
|
2021-12-17 18:34:00 +01:00
|
|
|
|
|
|
|
<Box ref={vimStatusRef} className="monaco-editor" display="flex" flexDirection="row" sx={{ p: 1 }} alignItems="center"></Box>
|
|
|
|
|
2021-09-25 05:36:28 +02:00
|
|
|
<Box display="flex" flexDirection="row" sx={{ m: 1 }} alignItems="center">
|
|
|
|
<Button onClick={beautify}>Beautify</Button>
|
2021-10-11 22:59:37 +02:00
|
|
|
<Typography color={updatingRam ? "secondary" : "primary"} sx={{ mx: 1 }}>
|
|
|
|
{ram}
|
|
|
|
</Typography>
|
2021-09-25 05:36:28 +02:00
|
|
|
<Button onClick={save}>Save & Close (Ctrl/Cmd + b)</Button>
|
2021-11-18 01:56:17 +01:00
|
|
|
<Typography sx={{ mx: 1 }}>
|
|
|
|
{" "}
|
|
|
|
Documentation:{" "}
|
|
|
|
<Link target="_blank" href="https://bitburner.readthedocs.io/en/latest/index.html">
|
|
|
|
Basic
|
|
|
|
</Link>{" "}
|
|
|
|
|
|
|
|
|
<Link target="_blank" href="https://github.com/danielyxie/bitburner/blob/dev/markdown/bitburner.ns.md">
|
|
|
|
Full
|
|
|
|
</Link>
|
|
|
|
</Typography>
|
2021-09-25 05:36:28 +02:00
|
|
|
</Box>
|
|
|
|
<OptionsModal
|
|
|
|
open={optionsOpen}
|
|
|
|
onClose={() => setOptionsOpen(false)}
|
|
|
|
options={{
|
|
|
|
theme: Settings.MonacoTheme,
|
|
|
|
insertSpaces: Settings.MonacoInsertSpaces,
|
2021-10-05 03:06:55 +02:00
|
|
|
fontSize: Settings.MonacoFontSize,
|
2021-12-17 18:34:00 +01:00
|
|
|
vim: Settings.MonacoVim,
|
2021-09-25 05:36:28 +02:00
|
|
|
}}
|
|
|
|
save={(options: Options) => {
|
|
|
|
setOptions(options);
|
|
|
|
Settings.MonacoTheme = options.theme;
|
|
|
|
Settings.MonacoInsertSpaces = options.insertSpaces;
|
2021-10-05 03:06:55 +02:00
|
|
|
Settings.MonacoFontSize = options.fontSize;
|
2021-12-17 18:34:00 +01:00
|
|
|
Settings.MonacoVim = options.vim;
|
2021-09-25 05:36:28 +02:00
|
|
|
}}
|
|
|
|
/>
|
2021-09-17 08:04:44 +02:00
|
|
|
</>
|
2021-09-05 01:09:30 +02:00
|
|
|
);
|
2021-08-23 12:21:05 +02:00
|
|
|
}
|