Skip to content

Build your personal knowledge base with Trilium Notes

client/code editor: hide the caret on read-only notes/embeds

Adorian Doranc3fc3564d2f3
diff --git a/packages/codemirror/src/index.spec.ts b/packages/codemirror/src/index.spec.ts
index be5ac46fe5..d26c664262 100644
--- a/packages/codemirror/src/index.spec.ts
+++ b/packages/codemirror/src/index.spec.ts
@@ -285,9 +285,11 @@ describe("CodeMirror", () => {
editor = build({ readOnly: true, placeholder: "Type here" });
const hasPlaceholder = () => !!editor?.dom.querySelector(".cm-placeholder");
expect(hasPlaceholder()).toBe(false);
+ expect(getHiddenCaretRules(editor)).toHaveLength(2);
editor.setReadOnly(false);
expect([ editor.state.readOnly, hasPlaceholder() ]).toEqual([ false, true ]);
+ expect(getHiddenCaretRules(editor)).toEqual([]);
editor.dispatch({ changes: { from: 0, insert: "a" } });
expect(undo(editor)).toBe(true);
expect(editor.getText()).toBe("");
@@ -296,6 +298,7 @@ describe("CodeMirror", () => {
editor.setReadOnly(true);
editor.setReadOnly(true);
expect(editor.state.readOnly).toBe(true);
+ expect(getHiddenCaretRules(editor)).toHaveLength(2);
editor.insertBlankLineAtTop();
expect(editor.getText()).toBe("b");
@@ -304,6 +307,20 @@ describe("CodeMirror", () => {
expect(editor.getText()).toBe("b");
});
+ it("hides the caret of a read-only editor over that of the color theme", async () => {
+ const theme = getThemeById("github-dark");
+ expect(theme).not.toBeNull();
+ if (!theme) return;
+ editor = build({ readOnly: true });
+ await editor.setTheme(theme);
+
+ const caretRules = editor.state.facet(EditorView.styleModule)
+ .flatMap((module) => module.getRules().split("\n"))
+ .filter((rule) => / \.cm-content \{[^}]*caret-color/.test(rule));
+ expect(caretRules.length).toBeGreaterThan(1);
+ expect(caretRules[0]).toContain("caret-color: transparent;");
+ });
+
it("toggles line wrapping both ways", () => {
editor = build();
editor.setLineWrapping(true);
@@ -814,6 +831,14 @@ function getGutterRules(view: CodeMirror) {
.filter((rule) => / \.cm-gutters \{/.test(rule));
}
+/** The rules of `view` that hide the caret and the block cursor of Vim. */
+function getHiddenCaretRules(view: CodeMirror) {
+ return view.state.facet(EditorView.styleModule)
+ .flatMap((module) => module.getRules().split("\n"))
+ .filter((rule) => / \.cm-content \{caret-color: transparent;\}/.test(rule)
+ || / \.cm-vimCursorLayer \{display: none;\}/.test(rule));
+}
+
/** A keydown of `key` from `target`, which the editor does not handle, as it is not dispatched. */
function createKeydown(key: string, target: EventTarget, init: KeyboardEventInit = {}) {
const event = new KeyboardEvent("keydown", { key, ...init });
diff --git a/packages/codemirror/src/index.ts b/packages/codemirror/src/index.ts
index 7781dcbc47..924dea7f24 100644
--- a/packages/codemirror/src/index.ts
+++ b/packages/codemirror/src/index.ts
@@ -652,12 +652,25 @@ function getTooltipHost() {
return host;
}
-/** The read-only facet, or the placeholder of an editable editor. */
+/** The read-only facet with a hidden caret, or the placeholder of an editable editor. */
function buildEditability(isReadOnly: boolean, placeholderText: string | undefined): Extension {
- if (isReadOnly) return EditorState.readOnly.of(true);
+ if (isReadOnly) return [ EditorState.readOnly.of(true), hiddenCaretTheme ];
return placeholderText ? placeholder(placeholderText) : [];
}
+/**
+ * Hides the caret, and the block cursor that Vim draws in its place. `Prec.highest` outranks the
+ * `.cm-content` rule of the color theme.
+ */
+const hiddenCaretTheme = Prec.highest(EditorView.theme({
+ ".cm-content": {
+ caretColor: "transparent"
+ },
+ ".cm-vimCursorLayer": {
+ display: "none"
+ }
+}));
+
/** The extension of each theme that an editor has loaded. */
const loadedThemes = new WeakMap<ThemeDefinition, Extension>();