From b4a78c26f35d0b69fe46e58c44f23b834ecd7849 Mon Sep 17 00:00:00 2001 From: nickofolas Date: Thu, 20 Jan 2022 10:35:43 -0600 Subject: [PATCH 1/5] 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 { > + )} From bc50f5180abacdea0951263364b5352e27ee600c Mon Sep 17 00:00:00 2001 From: nickofolas Date: Thu, 20 Jan 2022 11:05:12 -0600 Subject: [PATCH 2/5] Refactor --- src/ScriptEditor/ui/ScriptEditorRoot.tsx | 142 +++++++++++------------ 1 file changed, 69 insertions(+), 73 deletions(-) diff --git a/src/ScriptEditor/ui/ScriptEditorRoot.tsx b/src/ScriptEditor/ui/ScriptEditorRoot.tsx index 00e71e36a..249da8f36 100644 --- a/src/ScriptEditor/ui/ScriptEditorRoot.tsx +++ b/src/ScriptEditor/ui/ScriptEditorRoot.tsx @@ -34,6 +34,7 @@ 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"; @@ -42,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 @@ -700,7 +702,7 @@ export function Root(props: IProps): React.ReactElement { if (openScript.code !== serverScriptCode) { PromptEvent.emit({ - txt: "Do you want to overwrite " + openScript.fileName + " with the contents saved on the server?", + txt: "Do you want to overwrite " + openScript.fileName + " with the contents saved on the server? This cannot be undone.", resolve: (result: boolean) => { if (result) { // Save changes @@ -718,7 +720,6 @@ export function Root(props: IProps): React.ReactElement { updateRAM(openScript.code); editorRef.current.focus(); } - rerender(); } }, }); @@ -773,79 +774,74 @@ export function Root(props: IProps): React.ReactElement { overflowX: "scroll", }} > - {openScripts.map(({ fileName, hostname }, index) => ( - - {(provided) => ( -
- - - -
- )} -
- ))} + style={{ + ...(currentScript?.fileName === openScripts[index].fileName ? { + background: Settings.theme.button, + borderColor: Settings.theme.button, + color: Settings.theme.primary + } : { + background: Settings.theme.backgroundsecondary, + borderColor: Settings.theme.backgroundsecondary, + color: Settings.theme.secondary + }) + }} + > + {hostname}:~/{fileName} {dirty(index)} + + + + + + + )} + + ) + })} {provided.placeholder} )} From 99ef71de4a7551bed99cee45b1bf08561884494b Mon Sep 17 00:00:00 2001 From: nickofolas Date: Thu, 20 Jan 2022 17:14:38 -0600 Subject: [PATCH 3/5] Update prompt message content --- src/ScriptEditor/ui/ScriptEditorRoot.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ScriptEditor/ui/ScriptEditorRoot.tsx b/src/ScriptEditor/ui/ScriptEditorRoot.tsx index 249da8f36..03887abb5 100644 --- a/src/ScriptEditor/ui/ScriptEditorRoot.tsx +++ b/src/ScriptEditor/ui/ScriptEditorRoot.tsx @@ -702,7 +702,8 @@ export function Root(props: IProps): React.ReactElement { if (openScript.code !== serverScriptCode) { PromptEvent.emit({ - txt: "Do you want to overwrite " + openScript.fileName + " with the contents saved on the server? This cannot be undone.", + 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 From a5ad97f47d3a8af4f3c0132d8904a6ba01b700f2 Mon Sep 17 00:00:00 2001 From: nickofolas Date: Thu, 20 Jan 2022 17:28:19 -0600 Subject: [PATCH 4/5] Remove unnecessary editor code --- src/ScriptEditor/ui/ScriptEditorRoot.tsx | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/ScriptEditor/ui/ScriptEditorRoot.tsx b/src/ScriptEditor/ui/ScriptEditorRoot.tsx index 03887abb5..ab7d58baa 100644 --- a/src/ScriptEditor/ui/ScriptEditorRoot.tsx +++ b/src/ScriptEditor/ui/ScriptEditorRoot.tsx @@ -715,8 +715,6 @@ export function Root(props: IProps): React.ReactElement { } 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(); From 1a8a24587f3272a66173924d309893a5f1e0b1f8 Mon Sep 17 00:00:00 2001 From: nickofolas Date: Thu, 20 Jan 2022 17:45:54 -0600 Subject: [PATCH 5/5] Ensure that the proper editor is updated by syncing --- src/ScriptEditor/ui/ScriptEditorRoot.tsx | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/ScriptEditor/ui/ScriptEditorRoot.tsx b/src/ScriptEditor/ui/ScriptEditorRoot.tsx index ab7d58baa..a53823671 100644 --- a/src/ScriptEditor/ui/ScriptEditorRoot.tsx +++ b/src/ScriptEditor/ui/ScriptEditorRoot.tsx @@ -709,6 +709,9 @@ export function Root(props: IProps): React.ReactElement { // 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);