From b4a78c26f35d0b69fe46e58c44f23b834ecd7849 Mon Sep 17 00:00:00 2001 From: nickofolas Date: Thu, 20 Jan 2022 10:35:43 -0600 Subject: [PATCH] Implement editor tab actions --- src/ScriptEditor/ui/ScriptEditorRoot.tsx | 71 ++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 5 deletions(-) diff --git a/src/ScriptEditor/ui/ScriptEditorRoot.tsx b/src/ScriptEditor/ui/ScriptEditorRoot.tsx index c21681b4c..00e71e36a 100644 --- a/src/ScriptEditor/ui/ScriptEditorRoot.tsx +++ b/src/ScriptEditor/ui/ScriptEditorRoot.tsx @@ -33,6 +33,7 @@ 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 Table from "@mui/material/Table"; import TableCell from "@mui/material/TableCell"; import TableRow from "@mui/material/TableRow"; @@ -692,17 +693,55 @@ 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 " + openScript.fileName + " with the contents saved on the server?", + resolve: (result: boolean) => { + if (result) { + // Save changes + openScript.code = serverScriptCode; + + if (editorRef.current !== null && openScript !== null) { + if (openScript.model === undefined || openScript.model.isDisposed()) { + regenerateModel(openScript); + } + editorRef.current.setModel(openScript.model); + + editorRef.current.setPosition(openScript.lastPosition); + editorRef.current.revealLineInCenter(openScript.lastPosition.lineNumber); + editorRef.current.setValue(openScript.code); + updateRAM(openScript.code); + editorRef.current.focus(); + } + rerender(); + } + }, + }); + } + } + 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: @@ -754,6 +793,10 @@ export function Root(props: IProps): React.ReactElement { > + )}