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
Insert a paragraph at the top of the body when pressing Enter in the title
Elian Doraneab25379fd40
diff --git a/apps/client/src/components/app_context.ts b/apps/client/src/components/app_context.tsindex 7f48496951..c8a6861c71 100644--- a/apps/client/src/components/app_context.ts+++ b/apps/client/src/components/app_context.ts@@ -93,7 +93,15 @@ export type CommandMappings = {"api-log-messages": CommandData;focusTree: CommandData;focusOnTitle: CommandData;- focusOnDetail: CommandData;+ focusOnDetail: CommandData & {+ /**+ * When set, instead of merely focusing the editor, an empty paragraph is inserted at the very+ * top of the document and the cursor is placed there (Notion-like behavior when pressing Enter+ * in the title). Only honored by text notes. If the first block is already an empty paragraph,+ * the cursor is placed in it rather than stacking another empty paragraph.+ */+ insertNewlineAtTop?: boolean;+ };searchNotes: CommandData & {searchString?: string;ancestorNoteId?: string | null;diff --git a/apps/client/src/widgets/note_title.tsx b/apps/client/src/widgets/note_title.tsxindex 34b27c9298..ae4b5ac357 100644--- a/apps/client/src/widgets/note_title.tsx+++ b/apps/client/src/widgets/note_title.tsx@@ -110,10 +110,11 @@ export default function NoteTitleWidget(props: {className?: string}) {return;}- // Focus on the note content when pressing enter.+ // Pressing Enter moves to the note content. For text notes this inserts a new empty+ // paragraph at the top of the document (Notion-like) rather than just focusing.if (e.key === "Enter") {e.preventDefault();- parentComponent.triggerCommand("focusOnDetail", { ntxId: noteContext?.ntxId });+ parentComponent.triggerCommand("focusOnDetail", { ntxId: noteContext?.ntxId, insertNewlineAtTop: true });return;}diff --git a/apps/client/src/widgets/type_widgets/text/EditableText.tsx b/apps/client/src/widgets/type_widgets/text/EditableText.tsxindex 02fae231d7..f43e6f0228 100644--- a/apps/client/src/widgets/type_widgets/text/EditableText.tsx+++ b/apps/client/src/widgets/type_widgets/text/EditableText.tsx@@ -91,10 +91,14 @@ export default function EditableText({ note, parentComponent, ntxId, noteContexteditor.editing.view.focus();});- useTriliumEvent("focusOnDetail", async ({ ntxId: eventNtxId }) => {+ useTriliumEvent("focusOnDetail", async ({ ntxId: eventNtxId, insertNewlineAtTop }) => {if (eventNtxId !== ntxId) return;- const editor = await waitForEditor();- editor?.editing.view.focus();+ const editor = await waitForEditor() as CKTextEditor | undefined;+ if (!editor) return;+ if (insertNewlineAtTop) {+ placeCursorInNewTopParagraph(editor);+ }+ editor.editing.view.focus();});useLegacyImperativeHandlers({@@ -291,6 +295,28 @@ export default function EditableText({ note, parentComponent, ntxId, noteContext);}+/**+ * Inserts an empty paragraph at the very top of the document and places the cursor in it, giving the+ * Notion-like behavior when pressing Enter in the note title. If the first block is already an empty+ * paragraph, the cursor is placed in it rather than stacking another empty paragraph.+ */+function placeCursorInNewTopParagraph(editor: CKTextEditor) {+ editor.model.change((writer) => {+ const root = editor.model.document.getRoot();+ if (!root) return;++ const firstChild = root.getChild(0);+ if (firstChild?.is("element", "paragraph") && firstChild.isEmpty) {+ writer.setSelection(firstChild, "in");+ return;+ }++ const paragraph = writer.createElement("paragraph");+ writer.insert(paragraph, root, 0);+ writer.setSelection(paragraph, "in");+ });+}+function useTemplates() {const [ templates, setTemplates ] = useState<TemplateDefinition[]>();