Skip to content

Commit

Permalink
Slightly different way of addressing #1085
Browse files Browse the repository at this point in the history
saving files after local update with setTexrt
  • Loading branch information
zefhemel committed Sep 24, 2024
1 parent 362b590 commit 28f3e45
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
7 changes: 5 additions & 2 deletions plug-api/syscalls/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ export function getText(): Promise<string> {
* 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<void> {
return syscall("editor.setText", newText);
export function setText(
newText: string,
isolateHistory = false,
): Promise<void> {
return syscall("editor.setText", newText, isolateHistory);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions web/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions web/syscalls/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down

0 comments on commit 28f3e45

Please sign in to comment.