diff --git a/gdn-app/src/components/CNavbar.vue b/gdn-app/src/components/CNavbar.vue index 9d0b0f8..ca2a528 100644 --- a/gdn-app/src/components/CNavbar.vue +++ b/gdn-app/src/components/CNavbar.vue @@ -11,8 +11,10 @@ const repos = useReposStore(); const notes = useNotesStore(); const ui = useUiStore(); -function mkNote(id: string, ...children: string[]): Note { - return notes.addNote({ id, text: id, children }); +function mkNote(text: string, ...children: string[]): Note { + const note = notes.createNote(text); + children.forEach((it) => note.children.push(it)); + return note; } function createSomeNotes() { diff --git a/gdn-app/src/components/CNote.vue b/gdn-app/src/components/CNote.vue index 6a12f96..0e0b582 100644 --- a/gdn-app/src/components/CNote.vue +++ b/gdn-app/src/components/CNote.vue @@ -22,11 +22,11 @@ const props = defineProps<{ forceOpen?: boolean; }>(); -const note = computed(() => notes.notes.get(props.noteId)); +const note = computed(() => notes.getNote(props.noteId)); // Our children and their locally unique keys. const children = computed(() => { - if (note.value === undefined) return []; + if (!note.value) return []; const seen = new Map(); const children: { id: string; key: string }[] = []; for (const id of note.value.children) { @@ -79,6 +79,7 @@ function onClick() { } function onEditButtonClick() { + if (!note.value) return; mode.value = "editing"; } @@ -87,11 +88,13 @@ function onEditEditorClose() { } function onEditEditorFinish(text: string) { - if (note.value) note.value.text = text; + if (!note.value) return; + note.value.text = text; onEditEditorClose(); } function onCreateButtonClick() { + if (!note.value) return; mode.value = "creating"; } @@ -100,7 +103,10 @@ function onCreateEditorClose() { } function onCreateEditorFinish(text: string) { - notes.appendNewChildNote(props.noteId, text); + if (!note.value) return; + + const newNote = notes.createNote(text); + note.value.children.push(newNote.id); const lastChild = children.value.at(-1); if (lastChild) ui.focusPath = pathAppend(props.path, lastChild.key); diff --git a/gdn-app/src/stores/notes.ts b/gdn-app/src/stores/notes.ts index 4226d11..65fcc74 100644 --- a/gdn-app/src/stores/notes.ts +++ b/gdn-app/src/stores/notes.ts @@ -10,25 +10,14 @@ export type Note = { export const useNotesStore = defineStore("notes", () => { const notes = ref>(new Map()); - function addNote(note: Note): Note { - notes.value.set(note.id, note); - return notes.value.get(note.id)!; // Re-getting so returned Note is reactive + function getNote(id: string): Note | undefined { + return notes.value.get(id); } - function addNewNote(text: string): Note { - return addNote({ - id: crypto.randomUUID(), - text, - children: [], - }); - } - - function appendNewChildNote(parentId: string, text: string): Note | undefined { - const parent = notes.value.get(parentId); - if (parent === undefined) return undefined; - const note = addNewNote(text); - parent.children.push(note.id); - return note; + function createNote(text: string): Note { + const id = crypto.randomUUID(); + notes.value.set(id, { id, text, children: [] }); + return notes.value.get(id)!; // Re-getting so returned Note is reactive } function clearNotes() { @@ -36,10 +25,8 @@ export const useNotesStore = defineStore("notes", () => { } return { - notes, - addNote, - addNewNote, - appendNewChildNote, + getNote, + createNote, clearNotes, }; });