Skip to content

Build your personal knowledge base with Trilium Notes

fix(code): stop the Android keyboard suggesting words in code notes

Elian Doran7d74719ddab1
diff --git a/apps/client/src/widgets/type_widgets/code/Code.tsx b/apps/client/src/widgets/type_widgets/code/Code.tsx
index 9e00c37c47..c6fab5a670 100644
--- a/apps/client/src/widgets/type_widgets/code/Code.tsx
+++ b/apps/client/src/widgets/type_widgets/code/Code.tsx
@@ -30,6 +30,8 @@ export interface EditableCodeProps extends TypeWidgetProps {
placeholder?: string;
/** Optional external ref to the underlying CodeMirror `EditorView`. Populated once the editor has initialized. */
editorRef?: Ref<VanillaCodeMirror>;
+ /** Lets an on-screen keyboard offer word completions and autocorrection, for an editor holding prose rather than code. */
+ allowKeyboardSuggestions?: boolean;
}
export function ReadOnlyCode({ note, viewScope, ntxId, noteContext, editorRef }: TypeWidgetProps & { editorRef?: Ref<VanillaCodeMirror> }) {
diff --git a/apps/client/src/widgets/type_widgets/markdown/Markdown.tsx b/apps/client/src/widgets/type_widgets/markdown/Markdown.tsx
index b9774df816..0cc8c6f03c 100644
--- a/apps/client/src/widgets/type_widgets/markdown/Markdown.tsx
+++ b/apps/client/src/widgets/type_widgets/markdown/Markdown.tsx
@@ -120,6 +120,7 @@ export default function Markdown(props: TypeWidgetProps) {
onContentChanged={setContent}
previewContent={<MarkdownPreview ntxId={props.ntxId} />}
forceOrientation={isDesktop() ? "horizontal" : "vertical"}
+ allowKeyboardSuggestions
/>
</MarkdownContext.Provider>
);
diff --git a/packages/codemirror/src/index.spec.ts b/packages/codemirror/src/index.spec.ts
index cf924ab456..73590e061a 100644
--- a/packages/codemirror/src/index.spec.ts
+++ b/packages/codemirror/src/index.spec.ts
@@ -67,6 +67,17 @@ describe("CodeMirror", () => {
editor = build({ tabIndex: 7 });
expect(editor.dom.tabIndex).toBe(7);
});
+
+ it("keeps on-screen keyboard suggestions off unless the editor holds prose", () => {
+ // Android reads `autocomplete`, not the `spellcheck`/`autocorrect` pair CodeMirror sets
+ // on its own, so its absence is what lets Gboard complete and correct words in code.
+ editor = build();
+ expect(editor.contentDOM.getAttribute("autocomplete")).toBe("off");
+
+ editor.destroy();
+ editor = build({ allowKeyboardSuggestions: true });
+ expect(editor.contentDOM.hasAttribute("autocomplete")).toBe(false);
+ });
});
describe("text access", () => {
diff --git a/packages/codemirror/src/index.ts b/packages/codemirror/src/index.ts
index 067a8f0d62..c12a5929c5 100644
--- a/packages/codemirror/src/index.ts
+++ b/packages/codemirror/src/index.ts
@@ -42,6 +42,12 @@ const lintTooltipTheme = EditorView.baseTheme({
}
});
+// Chromium maps `autocomplete="off"` on the focused element to Android's
+// `TYPE_TEXT_FLAG_NO_SUGGESTIONS`, which is what stops Gboard from offering word completions and
+// rewriting what was typed. The `spellcheck`, `autocorrect` and `writingsuggestions` attributes
+// CodeMirror already sets reach no equivalent flag, so the on-screen keyboard ignores them.
+const noKeyboardSuggestions = EditorView.contentAttributes.of({ autocomplete: "off" });
+
type ContentChangedListener = () => void;
export interface EditorConfig {
@@ -57,6 +63,11 @@ export interface EditorConfig {
indentSize?: number;
/** If true, indent using a tab character instead of spaces. Defaults to false. */
useTabs?: boolean;
+ /**
+ * Lets an on-screen keyboard offer word completions and autocorrection. Defaults to false,
+ * which suits code; an editor holding prose, such as the Markdown note type, sets it to true.
+ */
+ allowKeyboardSuggestions?: boolean;
onContentChanged?: ContentChangedListener;
}
@@ -139,6 +150,10 @@ export default class CodeMirror extends EditorView {
];
}
+ if (!config.allowKeyboardSuggestions) {
+ extensions.push(noKeyboardSuggestions);
+ }
+
extensions.push(EditorView.updateListener.of((v) => this.#onDocumentUpdated(v)));
if (!config.readOnly) {