Skip to content

Build your personal knowledge base with Trilium Notes

feat(code): adjust word wrapping automatically in preview

Elian Dorand79977b9a41b
diff --git a/apps/client/src/widgets/type_widgets/options/code_notes/code_theme.ts b/apps/client/src/widgets/type_widgets/options/code_notes/code_theme.ts
index e9cf624261..a47d9144cf 100644
--- a/apps/client/src/widgets/type_widgets/options/code_notes/code_theme.ts
+++ b/apps/client/src/widgets/type_widgets/options/code_notes/code_theme.ts
@@ -141,6 +141,7 @@ export default class CodeTheme extends OptionsWidget {
}
this.editor.setText(SAMPLE_CODE);
this.editor.setMimeType(SAMPLE_MIME);
+ this.editor.setLineWrapping(options.codeLineWrapEnabled === "true");
// Load the theme.
const themeId = options.codeNoteTheme;
diff --git a/packages/codemirror/src/index.ts b/packages/codemirror/src/index.ts
index 7272d151e1..24a093caec 100644
--- a/packages/codemirror/src/index.ts
+++ b/packages/codemirror/src/index.ts
@@ -27,11 +27,13 @@ export default class CodeMirror extends EditorView {
private languageCompartment: Compartment;
private historyCompartment: Compartment;
private themeCompartment: Compartment;
+ private lineWrappingCompartment: Compartment;
constructor(config: EditorConfig) {
const languageCompartment = new Compartment();
const historyCompartment = new Compartment();
const themeCompartment = new Compartment();
+ const lineWrappingCompartment = new Compartment();
let extensions: Extension[] = [];
@@ -42,6 +44,7 @@ export default class CodeMirror extends EditorView {
extensions = [
...extensions,
languageCompartment.of([]),
+ lineWrappingCompartment.of(config.lineWrapping ? EditorView.lineWrapping : []),
themeCompartment.of([
syntaxHighlighting(defaultHighlightStyle, { fallback: true })
]),
@@ -74,10 +77,6 @@ export default class CodeMirror extends EditorView {
extensions.push(EditorState.readOnly.of(true));
}
- if (config.lineWrapping) {
- extensions.push(EditorView.lineWrapping);
- }
-
super({
parent: config.parent,
extensions
@@ -86,6 +85,7 @@ export default class CodeMirror extends EditorView {
this.languageCompartment = languageCompartment;
this.historyCompartment = historyCompartment;
this.themeCompartment = themeCompartment;
+ this.lineWrappingCompartment = lineWrappingCompartment;
}
#onDocumentUpdated(v: ViewUpdate) {
@@ -115,6 +115,12 @@ export default class CodeMirror extends EditorView {
});
}
+ setLineWrapping(wrapping: boolean) {
+ this.dispatch({
+ effects: [ this.lineWrappingCompartment.reconfigure(wrapping ? EditorView.lineWrapping : []) ]
+ });
+ }
+
/**
* Clears the history of undo/redo. Generally useful when changing to a new document.
*/