Skip to content

Build your personal knowledge base with Trilium Notes

fix(codemirror): don't render the whole document into a hidden editor

Claudedfd5cc035309
diff --git a/packages/codemirror/src/index.spec.ts b/packages/codemirror/src/index.spec.ts
index b07a2a57d5..cf924ab456 100644
--- a/packages/codemirror/src/index.spec.ts
+++ b/packages/codemirror/src/index.spec.ts
@@ -98,6 +98,47 @@ describe("CodeMirror", () => {
});
});
+ describe("setText while hidden", () => {
+ const bigDoc = Array.from({ length: 10_000 }, (_, i) => `line ${i}`).join("\n");
+
+ /** happy-dom has no layout, so offsetParent cannot tell hidden from visible on its own. */
+ function pinOffsetParent(el: HTMLElement, value: Element | null) {
+ Object.defineProperty(el, "offsetParent", { value, configurable: true });
+ }
+
+ it("keeps the viewport small instead of rendering every line of the new document", () => {
+ editor = build();
+ pinOffsetParent(editor.dom, null);
+ editor.setText(bigDoc);
+
+ expect(editor.getText()).toBe(bigDoc);
+ // The regression rendered all 10,000 lines into the hidden DOM.
+ expect(editor.dom.querySelectorAll(".cm-line").length).toBeLessThan(1_000);
+
+ // The rebuilt view keeps accepting transactions once visible again.
+ pinOffsetParent(editor.dom, document.body);
+ editor.setText("visible again");
+ expect(editor.getText()).toBe("visible again");
+ editor.dispatch({ changes: { from: 0, insert: "still " } });
+ expect(editor.getText()).toBe("still visible again");
+ });
+
+ it("skips the change notification for hidden loads", () => {
+ const onContentChanged = vi.fn();
+ editor = build({ onContentChanged });
+
+ pinOffsetParent(editor.dom, null);
+ editor.setText(bigDoc);
+ // The hidden path swaps the state in without an update, so no change notification
+ // fires — hidden loads are programmatic, and the caller resets history right after.
+ expect(onContentChanged).not.toHaveBeenCalled();
+
+ pinOffsetParent(editor.dom, document.body);
+ editor.setText("typed");
+ expect(onContentChanged).toHaveBeenCalledTimes(1);
+ });
+ });
+
describe("update notifications", () => {
it("calls onContentChanged only for document changes", () => {
const onContentChanged = vi.fn();
diff --git a/packages/codemirror/src/index.ts b/packages/codemirror/src/index.ts
index bcfd0fe401..067a8f0d62 100644
--- a/packages/codemirror/src/index.ts
+++ b/packages/codemirror/src/index.ts
@@ -210,13 +210,25 @@ export default class CodeMirror extends EditorView {
}
setText(content: string) {
- this.dispatch({
+ const transaction = this.state.update({
changes: {
from: 0,
to: this.state.doc.length,
insert: content || "",
}
- })
+ });
+ // Dispatching a full-document replacement maps the viewport onto the whole new document.
+ // A visible editor's next measure shrinks it back, but an editor inside a display:none
+ // subtree cannot measure (`measure()` bails out off-screen), so every line of the new
+ // document stays rendered in the DOM for as long as the editor stays hidden — e.g. a
+ // background tab that received its content after being covered. Rebuilding the view from
+ // the updated state resets the viewport to the initial estimate instead; the first
+ // measure after the editor becomes visible then sizes it to the real geometry.
+ if (this.dom.offsetParent === null) {
+ this.setState(transaction.state);
+ } else {
+ this.dispatch(transaction);
+ }
}
async setTheme(theme: ThemeDefinition) {