From 28f3e454b6dd1286a999f7d4fc65757959b937a9 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Tue, 24 Sep 2024 17:34:46 +0200 Subject: [PATCH] Slightly different way of addressing #1085 saving files after local update with setTexrt --- plug-api/syscalls/editor.ts | 7 +++++-- web/client.ts | 4 ++-- web/syscalls/editor.ts | 6 ++++-- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/plug-api/syscalls/editor.ts b/plug-api/syscalls/editor.ts index 9cef3b9c..348db1c9 100644 --- a/plug-api/syscalls/editor.ts +++ b/plug-api/syscalls/editor.ts @@ -28,8 +28,11 @@ export function getText(): Promise { * This updates the editor text, but in a minimal-diff way: * it compares the current editor text with the new text, and only sends the changes to the editor, thereby preserving cursor location */ -export function setText(newText: string): Promise { - return syscall("editor.setText", newText); +export function setText( + newText: string, + isolateHistory = false, +): Promise { + return syscall("editor.setText", newText, isolateHistory); } /** diff --git a/web/client.ts b/web/client.ts index 3c83ec1a..0b697bb2 100644 --- a/web/client.ts +++ b/web/client.ts @@ -659,7 +659,7 @@ export class Client implements ConfigContainer { if (this.currentPage) { if ( !this.ui.viewState.unsavedChanges || - this.ui.viewState.uiOptions.forcedROMode + this.ui.viewState.uiOptions.forcedROMode || this.readOnlyMode ) { // No unsaved changes, or read-only mode, not gonna save return resolve(); @@ -1143,7 +1143,7 @@ export class Client implements ConfigContainer { this.space.watchPage(pageName); } else { // Just apply minimal patches so that the cursor is preserved - await editor.setText(doc.text); + await editor.setText(doc.text, true); } // Note: these events are dispatched asynchronously deliberately (not waiting for results) diff --git a/web/syscalls/editor.ts b/web/syscalls/editor.ts index 35b6a37c..26d8cedb 100644 --- a/web/syscalls/editor.ts +++ b/web/syscalls/editor.ts @@ -25,12 +25,14 @@ export function editorSyscalls(client: Client): SysCallMapping { "editor.getText": () => { return client.editorView.state.sliceDoc(); }, - "editor.setText": (_ctx, newText: string) => { + "editor.setText": (_ctx, newText: string, shouldIsolateHistory = false) => { const currentText = client.editorView.state.sliceDoc(); const allChanges = diffAndPrepareChanges(currentText, newText); client.editorView.dispatch({ changes: allChanges, - annotations: isolateHistory.of("full"), + annotations: shouldIsolateHistory + ? isolateHistory.of("full") + : undefined, }); }, "editor.getCursor": (): number => {