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}