import { ref } from "vue"; import { useNotesStore, type Note } from "../stores/notes"; import { captureMorphOrigin } from "./useEditorMorph"; // Shared controller for hosting the NoteEditor modal. Every view that opens the // editor used to re-declare the same editing/open/close/navigate glue; this is the // single implementation. // // - `onClose` lets a host clean up its own state (e.g. a board's compose flag). // - `list` is the host's local note array, tried first when resolving a navigated // [[wiki-link]] target before falling back to the store, then a fetch — so views // that keep their own list (reminders, timeline, search, graph) still resolve // locally without duplicating the lookup. export function useNoteEditor(options: { onClose?: () => void; list?: () => Note[] } = {}) { const notes = useNotesStore(); const editing = ref(null); function open(note: Note): void { // Every view opens the editor through here, so this is the one place that knows // which card the user actually clicked — and therefore the only place the // grow-from-the-card animation can be told where to start (M7). captureMorphOrigin(note.id); editing.value = note; } function close(): void { editing.value = null; options.onClose?.(); } async function navigate(id: string): Promise { const local = options.list?.() ?? notes.items; const found = local.find((n) => n.id === id) ?? notes.items.find((n) => n.id === id) ?? (await notes.fetchOne(id)); if (found) editing.value = found; } return { editing, open, close, navigate }; }