Skip to content

Build your personal knowledge base with Trilium Notes

Insert a blank line at the top of code notes when pressing Enter in the title

Elian Doran428e72308118
diff --git a/apps/client/src/widgets/type_widgets/code/Code.tsx b/apps/client/src/widgets/type_widgets/code/Code.tsx
index 9559ac3403..69eaa01a97 100644
--- a/apps/client/src/widgets/type_widgets/code/Code.tsx
+++ b/apps/client/src/widgets/type_widgets/code/Code.tsx
@@ -228,9 +228,14 @@ export function CodeEditor({ parentComponent, ntxId, containerRef: externalConta
editor.focus();
});
- useTriliumEvent("focusOnDetail", ({ ntxId: eventNtxId }) => {
+ useTriliumEvent("focusOnDetail", ({ ntxId: eventNtxId, insertNewlineAtTop }) => {
if (eventNtxId !== ntxId) return;
- codeEditorRef.current?.focus();
+ const editor = codeEditorRef.current;
+ if (!editor) return;
+ if (insertNewlineAtTop) {
+ editor.insertBlankLineAtTop();
+ }
+ editor.focus();
});
return <CodeMirror
diff --git a/packages/codemirror/src/index.ts b/packages/codemirror/src/index.ts
index df362b7758..1956704c0c 100644
--- a/packages/codemirror/src/index.ts
+++ b/packages/codemirror/src/index.ts
@@ -275,6 +275,24 @@ export default class CodeMirror extends EditorView {
});
}
+ /**
+ * Inserts a blank line at the very top of the document and places the cursor on it — the code-note
+ * analog of the Notion-like behavior when pressing Enter in the note title. If the first line is
+ * already empty, the cursor is simply moved there instead of inserting another blank line, so a
+ * brand-new (empty) note does not end up with two blank lines. No-op for read-only editors, since
+ * CodeMirror's `readOnly` facet does not block programmatic transactions.
+ */
+ insertBlankLineAtTop() {
+ if (this.config.readOnly) {
+ return;
+ }
+ const firstLineEmpty = this.state.doc.line(1).length === 0;
+ this.dispatch({
+ ...(firstLineEmpty ? {} : { changes: { from: 0, insert: "\n" } }),
+ selection: EditorSelection.cursor(0)
+ });
+ }
+
async performFind(searchTerm: string, matchCase: boolean, wholeWord: boolean) {
const plugin = createSearchHighlighter();
this.dispatch({