mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2024-11-18 05:33:54 +01:00
BUGFIX: Save position of cursor when switching tabs and unmounting editor (#1297)
This commit is contained in:
parent
7f8757b536
commit
8703da4ab6
@ -1,29 +0,0 @@
|
||||
interface Position {
|
||||
row: number;
|
||||
column: number;
|
||||
}
|
||||
|
||||
class PositionTracker {
|
||||
positions: Map<string, Position>;
|
||||
|
||||
constructor() {
|
||||
this.positions = new Map<string, Position>();
|
||||
}
|
||||
|
||||
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();
|
@ -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<HTMLDivElement | null>(null);
|
||||
const editorRef = useRef<monaco.editor.IStandaloneCodeEditor | null>(null);
|
||||
const subscription = useRef<monaco.IDisposable | null>(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();
|
||||
|
@ -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}
|
||||
/>
|
||||
<div style={{ flex: "0 0 5px" }} />
|
||||
<Editor onMount={onMount} onChange={updateCode} />
|
||||
<Editor onMount={onMount} onChange={updateCode} onUnmount={onUnmountEditor} />
|
||||
|
||||
{VimStatus}
|
||||
|
||||
|
@ -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();
|
||||
|
Loading…
Reference in New Issue
Block a user