diff --git a/src/ScriptEditor/ui/ScriptEditorRoot.tsx b/src/ScriptEditor/ui/ScriptEditorRoot.tsx index c21681b4c..a53823671 100644 --- a/src/ScriptEditor/ui/ScriptEditorRoot.tsx +++ b/src/ScriptEditor/ui/ScriptEditorRoot.tsx @@ -33,6 +33,8 @@ import Typography from "@mui/material/Typography"; import Link from "@mui/material/Link"; import Box from "@mui/material/Box"; import SettingsIcon from "@mui/icons-material/Settings"; +import SyncIcon from '@mui/icons-material/Sync'; +import CloseIcon from '@mui/icons-material/Close'; import Table from "@mui/material/Table"; import TableCell from "@mui/material/TableCell"; import TableRow from "@mui/material/TableRow"; @@ -41,6 +43,7 @@ import { PromptEvent } from "../../ui/React/PromptManager"; import { Modal } from "../../ui/React/Modal"; import libSource from "!!raw-loader!../NetscriptDefinitions.d.ts"; +import { Tooltip } from "@mui/material"; interface IProps { // Map of filename -> code @@ -692,17 +695,56 @@ export function Root(props: IProps): React.ReactElement { } } + function onTabUpdate(index: number): void { + const openScript = openScripts[index]; + const serverScriptCode = getServerCode(index); + if (serverScriptCode === null) return; + + if (openScript.code !== serverScriptCode) { + PromptEvent.emit({ + txt: "Do you want to overwrite the current editor content with the contents of " + + openScript.fileName + " on the server? This cannot be undone.", + resolve: (result: boolean) => { + if (result) { + // Save changes + openScript.code = serverScriptCode; + + // Switch to target tab + onTabClick(index) + + if (editorRef.current !== null && openScript !== null) { + if (openScript.model === undefined || openScript.model.isDisposed()) { + regenerateModel(openScript); + } + editorRef.current.setModel(openScript.model); + + editorRef.current.setValue(openScript.code); + updateRAM(openScript.code); + editorRef.current.focus(); + } + } + }, + }); + } + } + function dirty(index: number): string { + const openScript = openScripts[index]; + const serverScriptCode = getServerCode(index); + if (serverScriptCode === null) return " *"; + + // The server code is stored with its starting & trailing whitespace removed + const openScriptFormatted = Script.formatCode(openScript.code); + return serverScriptCode !== openScriptFormatted ? " *" : ""; + } + + function getServerCode(index: number): string | null { const openScript = openScripts[index]; const server = GetServer(openScript.hostname); if (server === null) throw new Error(`Server '${openScript.hostname}' should not be null, but it is.`); const serverScript = server.scripts.find((s) => s.filename === openScript.fileName); - if (serverScript === undefined) return " *"; - - // The server code is stored with its starting & trailing whitespace removed - const openScriptFormatted = Script.formatCode(openScript.code); - return serverScript.code !== openScriptFormatted ? " *" : ""; + return serverScript?.code ?? null; } // Toolbars are roughly 112px: @@ -734,57 +776,74 @@ export function Root(props: IProps): React.ReactElement { overflowX: "scroll", }} > - {openScripts.map(({ fileName, hostname }, index) => ( - - {(provided) => ( -
- - -
- )} -
- ))} + + + + + + + )} + + ) + })} {provided.placeholder} )}