Fixes #1955
This commit is contained in:
@@ -1,9 +1,5 @@
|
|||||||
import customMarkdownStyle from "../style.ts";
|
import customMarkdownStyle from "../style.ts";
|
||||||
import {
|
import { history, insertNewlineAndIndent } from "@codemirror/commands";
|
||||||
history,
|
|
||||||
insertNewlineAndIndent,
|
|
||||||
isolateHistory,
|
|
||||||
} from "@codemirror/commands";
|
|
||||||
import {
|
import {
|
||||||
autocompletion,
|
autocompletion,
|
||||||
closeBrackets,
|
closeBrackets,
|
||||||
@@ -20,6 +16,7 @@ import {
|
|||||||
unfoldEffect,
|
unfoldEffect,
|
||||||
} from "@codemirror/language";
|
} from "@codemirror/language";
|
||||||
import {
|
import {
|
||||||
|
Annotation,
|
||||||
Compartment,
|
Compartment,
|
||||||
EditorState,
|
EditorState,
|
||||||
type Extension,
|
type Extension,
|
||||||
@@ -57,6 +54,11 @@ import { codeCopyPlugin } from "../codemirror/code_copy.ts";
|
|||||||
import { disableSpellcheck } from "../codemirror/spell_checking.ts";
|
import { disableSpellcheck } from "../codemirror/spell_checking.ts";
|
||||||
import type { ClickEvent } from "@silverbulletmd/silverbullet/type/client";
|
import type { ClickEvent } from "@silverbulletmd/silverbullet/type/client";
|
||||||
|
|
||||||
|
// Annotation marking a transaction whose changes came from outside the
|
||||||
|
// editor's edit stream (e.g. a page re-fetch from storage), so the
|
||||||
|
// save-on-change handler can skip it and avoid an immediate re-save loop.
|
||||||
|
export const externalUpdate = Annotation.define<boolean>();
|
||||||
|
|
||||||
export function createEditorState(
|
export function createEditorState(
|
||||||
client: Client,
|
client: Client,
|
||||||
pageName: string,
|
pageName: string,
|
||||||
@@ -292,9 +294,9 @@ export function createEditorState(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (update.docChanged) {
|
if (update.docChanged) {
|
||||||
// Find if there's a history isolate in the transaction, if so it came from a local reload and we don't do anything
|
// Skip saving if the change came from outside the editor (e.g. storage reload)
|
||||||
if (
|
if (
|
||||||
update.transactions.some((t) => t.annotation(isolateHistory))
|
update.transactions.some((t) => t.annotation(externalUpdate))
|
||||||
) {
|
) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,10 @@ import {
|
|||||||
notFoundError,
|
notFoundError,
|
||||||
offlineError,
|
offlineError,
|
||||||
} from "@silverbulletmd/silverbullet/constants";
|
} from "@silverbulletmd/silverbullet/constants";
|
||||||
import { createEditorState } from "./codemirror/editor_state.ts";
|
import {
|
||||||
|
createEditorState,
|
||||||
|
externalUpdate,
|
||||||
|
} from "./codemirror/editor_state.ts";
|
||||||
import { diffAndPrepareChanges } from "./codemirror/cm_util.ts";
|
import { diffAndPrepareChanges } from "./codemirror/cm_util.ts";
|
||||||
import { DocumentEditor } from "./document_editor.ts";
|
import { DocumentEditor } from "./document_editor.ts";
|
||||||
import { fsEndpoint } from "./spaces/constants.ts";
|
import { fsEndpoint } from "./spaces/constants.ts";
|
||||||
@@ -382,7 +385,7 @@ export class ContentManager {
|
|||||||
this.client.editorView.setState(editorState);
|
this.client.editorView.setState(editorState);
|
||||||
} else {
|
} else {
|
||||||
// Just apply minimal patches so that the cursor is preserved
|
// Just apply minimal patches so that the cursor is preserved
|
||||||
this.setEditorText(doc.text, true);
|
this.applyExternalPatches(doc.text);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.client.space.watchFile(path);
|
this.client.space.watchFile(path);
|
||||||
@@ -492,6 +495,18 @@ export class ContentManager {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Like setEditorText, but marks the transaction as an external update (e.g.
|
||||||
|
// a page re-fetch from storage) so the save-on-change handler skips it and
|
||||||
|
// we avoid an immediate re-save loop.
|
||||||
|
private applyExternalPatches(newText: string) {
|
||||||
|
const currentText = this.client.editorView.state.sliceDoc();
|
||||||
|
const allChanges = diffAndPrepareChanges(currentText, newText);
|
||||||
|
this.client.editorView.dispatch({
|
||||||
|
changes: allChanges,
|
||||||
|
annotations: [isolateHistory.of("full"), externalUpdate.of(true)],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private navigateWithinPage(pageState: LocationState) {
|
private navigateWithinPage(pageState: LocationState) {
|
||||||
if (!isMarkdownPath(pageState.path)) return;
|
if (!isMarkdownPath(pageState.path)) return;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user