Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(code): keep keyboard suggestions in a plain-text code note
Elian Doranc42f329282e0
diff --git a/apps/client/src/widgets/type_widgets/code/Code.tsx b/apps/client/src/widgets/type_widgets/code/Code.tsxindex c6fab5a670..96c3bfdd09 100644--- a/apps/client/src/widgets/type_widgets/code/Code.tsx+++ b/apps/client/src/widgets/type_widgets/code/Code.tsx@@ -179,7 +179,7 @@ export function EditableCode({ note, ntxId, noteContext, debounceUpdate, parentC);}-export function CodeEditor({ ntxId, containerRef: externalContainerRef, editorRef: externalEditorRef, mime, onInitialized, lineWrapping, ...editorProps }: CodeMirrorProps & Pick<TypeWidgetProps, "ntxId">) {+export function CodeEditor({ ntxId, containerRef: externalContainerRef, editorRef: externalEditorRef, mime, onInitialized, lineWrapping, allowKeyboardSuggestions, ...editorProps }: CodeMirrorProps & Pick<TypeWidgetProps, "ntxId">) {const codeEditorRef = useRef<VanillaCodeMirror>(null);const containerRef = useSyncedRef(externalContainerRef);const initialized = useRef($.Deferred());@@ -246,6 +246,7 @@ export function CodeEditor({ ntxId, containerRef: externalContainerRef, editorReeditorRef={codeEditorRef}containerRef={containerRef}lineWrapping={lineWrapping ?? codeLineWrapEnabled}+ allowKeyboardSuggestions={allowKeyboardSuggestions ?? mime === "text/plain"}indentSize={editorProps.indentSize ?? (parseInt(codeNoteTabWidth) || 4)}useTabs={editorProps.useTabs ?? codeNoteIndentWithTabs}onInitialized={() => {diff --git a/apps/client/src/widgets/type_widgets/code/CodeMirror.tsx b/apps/client/src/widgets/type_widgets/code/CodeMirror.tsxindex 26aaf47830..1e17fc953c 100644--- a/apps/client/src/widgets/type_widgets/code/CodeMirror.tsx+++ b/apps/client/src/widgets/type_widgets/code/CodeMirror.tsx@@ -17,7 +17,7 @@ export interface CodeMirrorProps extends Omit<EditorConfig, "parent"> {customRequestHandler?: boolean;}-export default function CodeMirror({ className, content, mime, editorRef: externalEditorRef, containerRef: externalContainerRef, onInitialized, lineWrapping, customRequestHandler, ...extraOpts }: CodeMirrorProps) {+export default function CodeMirror({ className, content, mime, editorRef: externalEditorRef, containerRef: externalContainerRef, onInitialized, lineWrapping, customRequestHandler, allowKeyboardSuggestions, ...extraOpts }: CodeMirrorProps) {const parentRef = useSyncedRef(externalContainerRef);const codeEditorRef = useRef<VanillaCodeMirror>();@@ -27,6 +27,7 @@ export default function CodeMirror({ className, content, mime, editorRef: externconst codeEditor = new VanillaCodeMirror({parent: parentRef.current,+ allowKeyboardSuggestions,...extraOpts});codeEditorRef.current = codeEditor;@@ -55,6 +56,10 @@ export default function CodeMirror({ className, content, mime, editorRef: externcodeEditorRef.current?.setScriptApiContext({ customRequestHandler: !!customRequestHandler });}, [ customRequestHandler ]);+ // React to the language switching between prose and code, which decides whether the+ // on-screen keyboard is allowed to suggest words.+ useEffect(() => codeEditorRef.current?.setAllowKeyboardSuggestions(!!allowKeyboardSuggestions), [ allowKeyboardSuggestions ]);+// React to line wrapping.useEffect(() => codeEditorRef.current?.setLineWrapping(!!lineWrapping), [ lineWrapping ]);diff --git a/packages/codemirror/src/index.spec.ts b/packages/codemirror/src/index.spec.tsindex 73590e061a..9ad45f60ed 100644--- a/packages/codemirror/src/index.spec.ts+++ b/packages/codemirror/src/index.spec.ts@@ -78,6 +78,17 @@ describe("CodeMirror", () => {editor = build({ allowKeyboardSuggestions: true });expect(editor.contentDOM.hasAttribute("autocomplete")).toBe(false);});++ it("follows a switch between prose and code after construction", () => {+ // The language dropdown can move a note between `text/plain` and a real language, so+ // the attribute has to track it rather than being fixed when the editor is built.+ editor = build();+ editor.setAllowKeyboardSuggestions(true);+ expect(editor.contentDOM.hasAttribute("autocomplete")).toBe(false);++ editor.setAllowKeyboardSuggestions(false);+ expect(editor.contentDOM.getAttribute("autocomplete")).toBe("off");+ });});describe("text access", () => {diff --git a/packages/codemirror/src/index.ts b/packages/codemirror/src/index.tsindex c12a5929c5..1fbae138dd 100644--- a/packages/codemirror/src/index.ts+++ b/packages/codemirror/src/index.ts@@ -82,6 +82,7 @@ export default class CodeMirror extends EditorView {private historyCompartment: Compartment;private themeCompartment: Compartment;private lineWrappingCompartment: Compartment;+ private keyboardSuggestionsCompartment: Compartment;private indentUnitCompartment: Compartment;private searchHighlightCompartment: Compartment;private typeCompletionCompartment: Compartment;@@ -102,6 +103,7 @@ export default class CodeMirror extends EditorView {const historyCompartment = new Compartment();const themeCompartment = new Compartment();const lineWrappingCompartment = new Compartment();+ const keyboardSuggestionsCompartment = new Compartment();const indentUnitCompartment = new Compartment();const searchHighlightCompartment = new Compartment();const typeCompletionCompartment = new Compartment();@@ -117,6 +119,7 @@ export default class CodeMirror extends EditorView {...extensions,languageCompartment.of([]),lineWrappingCompartment.of(config.lineWrapping ? EditorView.lineWrapping : []),+ keyboardSuggestionsCompartment.of(config.allowKeyboardSuggestions ? [] : noKeyboardSuggestions),searchMatchHighlightTheme,lintTooltipTheme,searchHighlightCompartment.of([]),@@ -150,10 +153,6 @@ export default class CodeMirror extends EditorView {];}- if (!config.allowKeyboardSuggestions) {- extensions.push(noKeyboardSuggestions);- }-extensions.push(EditorView.updateListener.of((v) => this.#onDocumentUpdated(v)));if (!config.readOnly) {@@ -182,6 +181,7 @@ export default class CodeMirror extends EditorView {this.historyCompartment = historyCompartment;this.themeCompartment = themeCompartment;this.lineWrappingCompartment = lineWrappingCompartment;+ this.keyboardSuggestionsCompartment = keyboardSuggestionsCompartment;this.indentUnitCompartment = indentUnitCompartment;this.searchHighlightCompartment = searchHighlightCompartment;this.typeCompletionCompartment = typeCompletionCompartment;@@ -259,6 +259,12 @@ export default class CodeMirror extends EditorView {});}+ setAllowKeyboardSuggestions(allow: boolean) {+ this.dispatch({+ effects: [ this.keyboardSuggestionsCompartment.reconfigure(allow ? [] : noKeyboardSuggestions) ]+ });+ }+setIndent(size: number, useTabs: boolean) {if (!Number.isFinite(size) || size < 1) size = 4;if (size > 16) size = 16;