From 89c0396778e635cb81f8e94a128326e8392a4cfa Mon Sep 17 00:00:00 2001 From: Joscha Date: Wed, 5 Feb 2025 15:46:50 +0100 Subject: [PATCH] Add proper keys for notes --- gdn-app/src/components/CNavbar.vue | 39 ++++++++++++++++++------------ gdn-app/src/components/CNote.vue | 31 ++++++++++++++++-------- gdn-app/src/stores/notes.ts | 27 +++++++++++++++------ 3 files changed, 64 insertions(+), 33 deletions(-) diff --git a/gdn-app/src/components/CNavbar.vue b/gdn-app/src/components/CNavbar.vue index 29334e7..69ad8a5 100644 --- a/gdn-app/src/components/CNavbar.vue +++ b/gdn-app/src/components/CNavbar.vue @@ -4,29 +4,38 @@ import { RiDeleteBinFill, RiNodeTree, RiSettings3Fill } from "@remixicon/vue"; import CNavbarButton from "./CNavbarButton.vue"; import CNavbarDropdown from "./CNavbarDropdown.vue"; import { useUiStore } from "@/stores/ui"; -import { useNotesStore } from "@/stores/notes"; +import { Note, useNotesStore } from "@/stores/notes"; const repos = useReposStore(); const notes = useNotesStore(); const ui = useUiStore(); -function createSomeNodes() { +function mkNote(id: string, ...children: string[]): Note { + return notes.addNote({ id, text: id, children }); +} + +function createSomeNotes() { notes.clearNotes(); - const root = notes.addNote("root"); + const n2n1 = mkNote("n2n1"); + const n2n2 = mkNote("n2n2"); + const n2n3 = mkNote("n2n3"); + + const n1 = mkNote("n1"); + const n2 = mkNote("n2", n2n1.id, n2n2.id, n2n3.id); + const n3 = mkNote("n3"); + const n4 = mkNote("n4"); + const n5 = mkNote("n5", "NaN (not a note)"); + + const root = mkNote("root", n1.id, n2.id, n3.id, n4.id, n5.id, n2.id); + ui.anchor = root.id; - notes.appendChildNote(root.id, "n1")!; - const n2 = notes.appendChildNote(root.id, "n2")!; - notes.appendChildNote(root.id, "n3")!; - notes.appendChildNote(root.id, "n4")!; - const n5 = notes.appendChildNote(root.id, "n5")!; - - notes.appendChildNote(n2.id, "n2n1")!; - notes.appendChildNote(n2.id, "n2n2")!; - notes.appendChildNote(n2.id, "n2n3")!; - - n5.children.push("NaN (Not a Note)"); + // Shuffle children of root + root.children = root.children + .map((it) => ({ it, rand: Math.random() })) + .sort((a, b) => a.rand - b.rand) + .map(({ it }) => it); } @@ -39,7 +48,7 @@ function createSomeNodes() { - + diff --git a/gdn-app/src/components/CNote.vue b/gdn-app/src/components/CNote.vue index 25df2b5..b353a73 100644 --- a/gdn-app/src/components/CNote.vue +++ b/gdn-app/src/components/CNote.vue @@ -8,36 +8,47 @@ const { notes } = useNotesStore(); const props = defineProps<{ id: string | undefined }>(); const note = computed(() => (props.id ? notes.get(props.id) : undefined)); +const children = computed(() => { + if (note.value === undefined) return []; + const seen = new Map(); + const children: [string, string][] = []; + for (const id of note.value.children) { + const n = seen.get(id) || 0; + seen.set(id, n + 1); + const key = `${id}-${n}`; + children.push([id, key]); + } + return children; +}); + const open = ref(false); diff --git a/gdn-app/src/stores/notes.ts b/gdn-app/src/stores/notes.ts index d2c7dbf..1d459aa 100644 --- a/gdn-app/src/stores/notes.ts +++ b/gdn-app/src/stores/notes.ts @@ -1,7 +1,7 @@ import { defineStore } from "pinia"; import { ref } from "vue"; -type Note = { +export type Note = { id: string; text: string; children: string[]; @@ -10,16 +10,26 @@ type Note = { export const useNotesStore = defineStore("notes", () => { const notes = ref>(new Map()); - function addNote(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 addNote(note: Note): Note { + notes.value.set(note.id, note); + return notes.value.get(note.id)!; // Re-getting so returned Note is reactive } - function appendChildNote(parentId: string, text: string): Note | undefined { + 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 = addNote(text); + const note = addNewNote(text); parent.children.push(note.id); return note; } @@ -31,7 +41,8 @@ export const useNotesStore = defineStore("notes", () => { return { notes, addNote, - appendChildNote, + addNewNote, + appendNewChildNote, clearNotes, }; });