Skip to content

Commit 142ff0e

Browse files
feat(ux): on note creation interraction , set focus to newly created note
first, set focus to file name with all text selected then, on enter focus the text editor
1 parent 25d3485 commit 142ff0e

4 files changed

Lines changed: 33 additions & 5 deletions

File tree

apps/app/src/components/sidebar/file_manager/toolbar.svelte

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,21 @@
4040
)
4141
return;
4242
is_processing = true;
43-
await add_new_note(focused_subtree, focused_directory, root_path);
43+
opened_filenode = await add_new_note(
44+
focused_subtree,
45+
focused_directory,
46+
root_path
47+
);
4448
is_processing = false;
49+
50+
const input: HTMLInputElement | null = document.getElementById(
51+
'note_file_name_input'
52+
) as HTMLInputElement | null;
53+
if (!input) return;
54+
setTimeout(() => {
55+
input.focus();
56+
input.select();
57+
}, 50);
4558
}}
4659
><div class="i-tabler:edit size-5"></div>
4760
</button>

apps/app/src/components/text_editor/index.svelte

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import Prosemark from './prosemark.svelte';
66
import { toast } from 'svelte-sonner';
77
import { current_platform_type } from '@/lib/file_tree';
8+
import { type EditorView } from '@codemirror/view';
9+
810
let {
911
filenode = $bindable(),
1012
root_path,
@@ -15,6 +17,7 @@
1517
let text_content: string | undefined = $state();
1618
let current_file_name: string | undefined = $state();
1719
let is_file_named_changed: boolean = $state(false);
20+
let editor_view: EditorView | undefined = $state();
1821
1922
$effect(() => {
2023
if (!filenode) return;
@@ -39,6 +42,7 @@
3942
<div class="max-w-170 w-full font-sans">
4043
<input
4144
type="text"
45+
id="note_file_name_input"
4246
oninput={(e) => {
4347
current_file_name = current_file_name?.replace(
4448
/[^A-Za-z0-9 _.\-()]/g,
@@ -60,12 +64,19 @@
6064
if (e instanceof Error) toast.error(e.message);
6165
}
6266
}}
67+
onkeydown={async (e) => {
68+
if (e.key === 'Enter') {
69+
e.preventDefault();
70+
if (editor_view) editor_view.focus();
71+
}
72+
}}
6373
bind:value={current_file_name}
6474
class="w-full outline-none b-0 focus:ring-0 mb-16 mt-10 font-semibold text-5xl"
6575
/>
6676

6777
<Prosemark
6878
{text_content}
79+
bind:editor_view
6980
write_to_file={(content) => {
7081
if (!filenode) return;
7182
writeTextFile(filenode?.path, content);

apps/app/src/components/text_editor/prosemark.svelte

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@
1717
let {
1818
text_content,
1919
write_to_file,
20+
editor_view = $bindable(),
2021
}: {
2122
text_content: string | undefined;
23+
editor_view: EditorView | undefined;
2224
write_to_file: (markdown_content_state: string) => void;
2325
} = $props();
2426
let element: HTMLDivElement | undefined = $state();
25-
let editor: EditorView | undefined = $state();
2627
let is_contents_changed = $state(false);
2728
$effect(() => {
2829
let newEditor: EditorView | undefined;
@@ -51,7 +52,7 @@
5152
],
5253
});
5354
}
54-
editor = newEditor;
55+
editor_view = newEditor;
5556
5657
return () => {
5758
newEditor?.destroy();
@@ -62,8 +63,8 @@
6263
<div
6364
bind:this={element}
6465
onfocusout={() => {
65-
if (!editor || !is_contents_changed) return;
66-
write_to_file(editor.state.doc.toString());
66+
if (!editor_view || !is_contents_changed) return;
67+
write_to_file(editor_view.state.doc.toString());
6768
is_contents_changed = false;
6869
}}
6970
class="pb-50vh"

apps/app/src/lib/file_tree/operations.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ export async function add_new_note(
6464
is_directory: false,
6565
children: [],
6666
});
67+
const node = subtree.find((n) => n.path === new_file_path);
68+
if (!node) throw new Error('Failed to find the newly created note node.');
69+
return node;
6770
}
6871
export async function add_new_folder(
6972
subtree: FileNode[],

0 commit comments

Comments
 (0)