From 2704548eb8a88db1d3db25778b52d3a27b8e280c Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Sun, 11 May 2025 18:51:44 +0300 Subject: [PATCH] chore(code): stop loading unnecessary extensions for read-only mode --- packages/codemirror/src/index.ts | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/packages/codemirror/src/index.ts b/packages/codemirror/src/index.ts index 160140e23..7307e9f88 100644 --- a/packages/codemirror/src/index.ts +++ b/packages/codemirror/src/index.ts @@ -37,7 +37,6 @@ export default class CodeMirror extends EditorView { extensions = [ ...extensions, languageCompartment.of([]), - historyCompartment.of(history()), syntaxHighlighting(defaultHighlightStyle, { fallback: true }), highlightActiveLine(), highlightSelectionMatches(), @@ -52,22 +51,26 @@ export default class CodeMirror extends EditorView { ]) ] - if (config.readOnly) { - extensions.push(EditorState.readOnly.of(true)); - } + if (!config.readOnly) { + // Logic specific to editable notes + if (config.placeholder) { + extensions.push(placeholder(config.placeholder)); + } - if (config.placeholder) { - extensions.push(placeholder(config.placeholder)); + if (config.onContentChanged) { + extensions.push(EditorView.updateListener.of((v) => this.#onDocumentUpdated(v))); + } + + extensions.push(historyCompartment.of(history())); + } else { + // Logic specific to read-only notes + extensions.push(EditorState.readOnly.of(true)); } if (config.lineWrapping) { extensions.push(EditorView.lineWrapping); } - if (config.onContentChanged) { - extensions.push(EditorView.updateListener.of((v) => this.#onDocumentUpdated(v))); - } - super({ parent: config.parent, extensions @@ -101,6 +104,10 @@ export default class CodeMirror extends EditorView { * Clears the history of undo/redo. Generally useful when changing to a new document. */ clearHistory() { + if (this.config.readOnly) { + return; + } + this.dispatch({ effects: [ this.historyCompartment.reconfigure([]) ] });