From 28263de53d297105cb52c2c0ffcd700060531fd7 Mon Sep 17 00:00:00 2001 From: MF Date: Thu, 18 Jun 2026 13:40:41 +0200 Subject: [PATCH] Improve notebook path creation (#135) Backspace on an empty sub-notebook field returns to top-level creation, and top-level names like Parent/Child now create nested notebooks in one action. Validated with pnpm build. Reviewed-on: https://codeberg.org/ArkHost/HelixNotes/pulls/135 --- src/lib/components/Sidebar.svelte | 36 ++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/src/lib/components/Sidebar.svelte b/src/lib/components/Sidebar.svelte index 529a20e..089d0d2 100644 --- a/src/lib/components/Sidebar.svelte +++ b/src/lib/components/Sidebar.svelte @@ -213,11 +213,23 @@ await focusNewNotebookInput(); } + function getNotebookNameSegments(name: string): string[] | null { + const segments = name.split('/').map((segment) => segment.trim()); + if (segments.some((segment) => !segment || segment === '.' || segment === '..' || segment.includes('\\'))) { + return null; + } + return segments; + } + async function handleCreateNotebook() { if (!newNotebookName.trim()) return; try { + const trimmedName = newNotebookName.trim(); const parentRel = newNotebookParent?.relative_path ?? null; - await createNotebook(parentRel, newNotebookName.trim()); + const name = parentRel ? trimmedName : getNotebookNameSegments(trimmedName)?.join('/'); + if (!name) return; + + await createNotebook(parentRel, name); newNotebookName = ''; showNewNotebook = false; newNotebookParent = null; @@ -227,6 +239,23 @@ } } + function handleNewNotebookKeydown(e: KeyboardEvent) { + if (e.key === 'Enter') { + handleCreateNotebook(); + return; + } + if (e.key === 'Escape') { + showNewNotebook = false; + newNotebookName = ''; + newNotebookParent = null; + return; + } + if (e.key === 'Backspace' && newNotebookParent && newNotebookName.length === 0) { + e.preventDefault(); + newNotebookParent = null; + } + } + function startNewSubNotebook(nb: NotebookEntry) { contextMenu = null; newNotebookParent = nb; @@ -657,10 +686,7 @@ type="text" bind:value={newNotebookName} placeholder={newNotebookParent ? 'Sub-notebook name...' : 'Notebook name...'} - onkeydown={(e) => { - if (e.key === 'Enter') handleCreateNotebook(); - if (e.key === 'Escape') { showNewNotebook = false; newNotebookName = ''; newNotebookParent = null; } - }} + onkeydown={handleNewNotebookKeydown} /> {/if}