From 8703da4ab6f979a9940f8ccbc6040e6623e8be21 Mon Sep 17 00:00:00 2001 From: catloversg <152669316+catloversg@users.noreply.github.com> Date: Thu, 23 May 2024 15:51:33 +0700 Subject: [PATCH] BUGFIX: Save position of cursor when switching tabs and unmounting editor (#1297) --- src/ScriptEditor/CursorPositions.ts | 29 ------------------------ src/ScriptEditor/ui/Editor.tsx | 5 +++- src/ScriptEditor/ui/ScriptEditorRoot.tsx | 18 ++++++++++++++- src/Terminal/commands/common/editor.ts | 2 -- 4 files changed, 21 insertions(+), 33 deletions(-) delete mode 100644 src/ScriptEditor/CursorPositions.ts diff --git a/src/ScriptEditor/CursorPositions.ts b/src/ScriptEditor/CursorPositions.ts deleted file mode 100644 index 9181bd487..000000000 --- a/src/ScriptEditor/CursorPositions.ts +++ /dev/null @@ -1,29 +0,0 @@ -interface Position { - row: number; - column: number; -} - -class PositionTracker { - positions: Map; - - constructor() { - this.positions = new Map(); - } - - saveCursor(filename: string, pos: Position): void { - this.positions.set(filename, pos); - } - - getCursor(filename: string): Position { - const position = this.positions.get(filename); - if (!position) { - return { - row: -1, - column: -1, - }; - } - return position; - } -} - -export const CursorPositions: PositionTracker = new PositionTracker(); diff --git a/src/ScriptEditor/ui/Editor.tsx b/src/ScriptEditor/ui/Editor.tsx index 220aa5fed..7c69c2a89 100644 --- a/src/ScriptEditor/ui/Editor.tsx +++ b/src/ScriptEditor/ui/Editor.tsx @@ -10,9 +10,11 @@ interface EditorProps { onMount: (editor: monaco.editor.IStandaloneCodeEditor) => void; /** Function to be ran every time the code is updated */ onChange: (newCode?: string) => void; + /** This function is called before unmounting the editor */ + onUnmount: () => void; } -export function Editor({ onMount, onChange }: EditorProps) { +export function Editor({ onMount, onChange, onUnmount }: EditorProps) { const containerDiv = useRef(null); const editorRef = useRef(null); const subscription = useRef(null); @@ -41,6 +43,7 @@ export function Editor({ onMount, onChange }: EditorProps) { // Unmounting return () => { + onUnmount(); subscription.current?.dispose(); monaco.editor.getModels().forEach((model) => model.dispose()); editorRef.current?.dispose(); diff --git a/src/ScriptEditor/ui/ScriptEditorRoot.tsx b/src/ScriptEditor/ui/ScriptEditorRoot.tsx index 6562dfe78..5e0cc21ad 100644 --- a/src/ScriptEditor/ui/ScriptEditorRoot.tsx +++ b/src/ScriptEditor/ui/ScriptEditorRoot.tsx @@ -233,6 +233,11 @@ function Root(props: IProps): React.ReactElement { function onTabClick(index: number): void { if (currentScript !== null) { + // Save the current position of the cursor. + const currentPosition = editorRef.current?.getPosition(); + if (currentPosition) { + currentScript.lastPosition = currentPosition; + } // Save currentScript to openScripts const curIndex = currentTabIndex(); if (curIndex !== undefined) { @@ -356,6 +361,17 @@ function Root(props: IProps): React.ReactElement { } } + function onUnmountEditor() { + if (!currentScript) { + return; + } + // Save the current position of the cursor. + const currentPosition = editorRef.current?.getPosition(); + if (currentPosition) { + currentScript.lastPosition = currentPosition; + } + } + const { VimStatus } = useVimEditor({ editor: editorRef.current, vim: options.vim, @@ -392,7 +408,7 @@ function Root(props: IProps): React.ReactElement { onTabUpdate={onTabUpdate} />
- + {VimStatus} diff --git a/src/Terminal/commands/common/editor.ts b/src/Terminal/commands/common/editor.ts index f7d7531e4..5fc9124a9 100644 --- a/src/Terminal/commands/common/editor.ts +++ b/src/Terminal/commands/common/editor.ts @@ -2,7 +2,6 @@ import { Terminal } from "../../../Terminal"; import { ScriptEditorRouteOptions, Page } from "../../../ui/Router"; import { Router } from "../../../ui/GameRoot"; import { BaseServer } from "../../../Server/BaseServer"; -import { CursorPositions } from "../../../ScriptEditor/CursorPositions"; import { ScriptFilePath, hasScriptExtension } from "../../../Paths/ScriptFilePath"; import { TextFilePath, hasTextExtension } from "../../../Paths/TextFilePath"; import { getGlobbedFileMap } from "../../../Paths/GlobbedFiles"; @@ -54,7 +53,6 @@ export function commonEditor( const file = server.getContentFile(path); const content = file ? file.content : isNs2(path) ? newNs2Template : ""; files.set(path, content); - if (content === newNs2Template) CursorPositions.saveCursor(path, { row: 3, column: 5 }); } if (hasNs1) { sendDeprecationNotice();