Skip to content

Build your personal knowledge base with Trilium Notes

fix(code): give a prose editor back the keyboard's correction and capitalization

Elian Doranbcf24e8b54ff
diff --git a/packages/codemirror/src/index.spec.ts b/packages/codemirror/src/index.spec.ts
index 9ad45f60ed..60c4a321ea 100644
--- a/packages/codemirror/src/index.spec.ts
+++ b/packages/codemirror/src/index.spec.ts
@@ -81,7 +81,7 @@ describe("CodeMirror", () => {
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.
+ // the attributes have to track it rather than being fixed when the editor is built.
editor = build();
editor.setAllowKeyboardSuggestions(true);
expect(editor.contentDOM.hasAttribute("autocomplete")).toBe(false);
@@ -89,6 +89,23 @@ describe("CodeMirror", () => {
editor.setAllowKeyboardSuggestions(false);
expect(editor.contentDOM.getAttribute("autocomplete")).toBe("off");
});
+
+ it("gives a prose editor back the keyboard help CodeMirror turns off for code", () => {
+ // CodeMirror hard-codes both to off for every editor, so leaving its defaults alone
+ // would cost a Markdown note its sentence capitalization and its typo correction.
+ editor = build();
+ expect(editor.contentDOM.getAttribute("autocorrect")).toBe("off");
+ expect(editor.contentDOM.getAttribute("autocapitalize")).toBe("off");
+
+ editor.setAllowKeyboardSuggestions(true);
+ expect(editor.contentDOM.getAttribute("autocorrect")).toBe("on");
+ expect(editor.contentDOM.getAttribute("autocapitalize")).toBe("sentences");
+
+ editor.destroy();
+ editor = build({ allowKeyboardSuggestions: true });
+ expect(editor.contentDOM.getAttribute("autocorrect")).toBe("on");
+ expect(editor.contentDOM.getAttribute("autocapitalize")).toBe("sentences");
+ });
});
describe("text access", () => {
diff --git a/packages/codemirror/src/index.ts b/packages/codemirror/src/index.ts
index 1fbae138dd..0c4e000879 100644
--- a/packages/codemirror/src/index.ts
+++ b/packages/codemirror/src/index.ts
@@ -48,6 +48,14 @@ const lintTooltipTheme = EditorView.baseTheme({
// CodeMirror already sets reach no equivalent flag, so the on-screen keyboard ignores them.
const noKeyboardSuggestions = EditorView.contentAttributes.of({ autocomplete: "off" });
+// CodeMirror sets `autocorrect="off"` and `autocapitalize="off"` on every editor it builds, which
+// suits code. An editor holding prose overrides both, since `contentAttributes` is merged in after
+// those defaults, so the on-screen keyboard capitalizes sentences and fixes typos again.
+const proseKeyboardAttributes = EditorView.contentAttributes.of({
+ autocorrect: "on",
+ autocapitalize: "sentences"
+});
+
type ContentChangedListener = () => void;
export interface EditorConfig {
@@ -64,8 +72,9 @@ export interface EditorConfig {
/** 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.
+ * Lets an on-screen keyboard offer word completions, correct typos and capitalize sentences.
+ * Defaults to false, which suits code; an editor holding prose, such as the Markdown note
+ * type, sets it to true.
*/
allowKeyboardSuggestions?: boolean;
onContentChanged?: ContentChangedListener;
@@ -119,7 +128,7 @@ export default class CodeMirror extends EditorView {
...extensions,
languageCompartment.of([]),
lineWrappingCompartment.of(config.lineWrapping ? EditorView.lineWrapping : []),
- keyboardSuggestionsCompartment.of(config.allowKeyboardSuggestions ? [] : noKeyboardSuggestions),
+ keyboardSuggestionsCompartment.of(config.allowKeyboardSuggestions ? proseKeyboardAttributes : noKeyboardSuggestions),
searchMatchHighlightTheme,
lintTooltipTheme,
searchHighlightCompartment.of([]),
@@ -261,7 +270,7 @@ export default class CodeMirror extends EditorView {
setAllowKeyboardSuggestions(allow: boolean) {
this.dispatch({
- effects: [ this.keyboardSuggestionsCompartment.reconfigure(allow ? [] : noKeyboardSuggestions) ]
+ effects: [ this.keyboardSuggestionsCompartment.reconfigure(allow ? proseKeyboardAttributes : noKeyboardSuggestions) ]
});
}