Encapsulate note store more

This commit is contained in:
Joscha 2025-02-08 18:16:35 +01:00
parent c2b4592bc9
commit 3628fa260b
3 changed files with 22 additions and 27 deletions

View file

@ -10,25 +10,14 @@ export type Note = {
export const useNotesStore = defineStore("notes", () => {
const notes = ref<Map<string, Note>>(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,
};
});