// The open canvas: reactive page tree + pages, with undo/redo. import { layout, type Rect } from '$lib/model/layout'; import { insert, remove, canInsert, flatten, withTrunk, type Dir, type Tree } from '$lib/model/tree'; import { A4, newId, type CanvasData, type Origin, type PageData, type Stroke } from '$lib/model/types'; export class PageModel { readonly id: string; width = $state(A4.width); height = $state(A4.height); markdown = $state(''); strokes = $state.raw([]); readonly origin?: Origin; constructor(d: PageData) { this.id = d.id; this.width = d.width; this.height = d.height; this.markdown = d.markdown; this.strokes = d.strokes; this.origin = d.origin; } toData(): PageData { return { id: this.id, width: this.width, height: this.height, markdown: this.markdown, strokes: this.strokes, ...(this.origin ? { origin: this.origin } : {}) }; } } interface Command { undo(): void; redo(): void; } const MAX_UNDO = 200; export class CanvasDoc { path = $state(''); tree = $state.raw(withTrunk([])); pages = $state.raw>({}); activeId = $state(null); /** Bumped on every change; the workspace autosaves on it. */ version = $state(0); updatedAt: number; canUndo = $state(false); canRedo = $state(false); private undoStack: Command[] = []; private redoStack: Command[] = []; rects = $derived(layout(this.tree, (id) => this.pages[id] ?? A4)); constructor(path: string, data: CanvasData) { this.path = path; this.updatedAt = data.updatedAt; this.tree = data.tree; this.pages = Object.fromEntries(Object.values(data.pages).map((p) => [p.id, new PageModel(p)])); this.activeId = data.tree.trunk[0] ?? null; } get order() { return flatten(this.tree); } rectOf(id: string): Rect | undefined { return this.rects.get(id); } touch() { this.updatedAt = Date.now(); this.version++; } toData(): CanvasData { const pages: Record = {}; for (const id of flatten(this.tree)) if (this.pages[id]) pages[id] = this.pages[id].toData(); return { version: 1, updatedAt: this.updatedAt, tree: this.tree, pages }; } // ---- undo/redo ------------------------------------------------------- private exec(cmd: Command) { cmd.redo(); this.undoStack.push(cmd); if (this.undoStack.length > MAX_UNDO) this.undoStack.shift(); this.redoStack = []; this.syncFlags(); this.touch(); } undo() { const cmd = this.undoStack.pop(); if (!cmd) return; cmd.undo(); this.redoStack.push(cmd); this.syncFlags(); this.touch(); } redo() { const cmd = this.redoStack.pop(); if (!cmd) return; cmd.redo(); this.undoStack.push(cmd); this.syncFlags(); this.touch(); } private syncFlags() { this.canUndo = this.undoStack.length > 0; this.canRedo = this.redoStack.length > 0; } // ---- ink ------------------------------------------------------------- addStroke(pageId: string, stroke: Stroke) { this.setStrokes(pageId, (s) => [...s, stroke]); } /** Replace a page's strokes as one undoable step. */ setStrokes(pageId: string, fn: (s: Stroke[]) => Stroke[]) { const page = this.pages[pageId]; if (!page) return; const before = page.strokes; const after = fn(before); if (after === before) return; this.exec({ redo: () => (this.pages[pageId].strokes = after), undo: () => (this.pages[pageId].strokes = before) }); } // ---- tree ------------------------------------------------------------ private treeCommand(nextTree: Tree, nextPages: Record, nextActive: string | null) { const before = { tree: this.tree, pages: this.pages, active: this.activeId }; const after = { tree: nextTree, pages: nextPages, active: nextActive }; const apply = (s: typeof before) => { this.tree = s.tree; this.pages = s.pages; this.activeId = s.active && s.pages[s.active] ? s.active : (s.tree.trunk[0] ?? null); }; this.exec({ redo: () => apply(after), undo: () => apply(before) }); } /** Insert pages next to `anchor`; returns false if not allowed there. */ insertPages(anchor: string, dir: Dir, pages: PageData[]): boolean { if (!canInsert(this.tree, anchor, dir) || pages.length === 0) return false; const next = insert(this.tree, anchor, dir, pages.map((p) => p.id)); const models = { ...this.pages }; for (const p of pages) models[p.id] = new PageModel(p); this.treeCommand(next, models, pages[0].id); return true; } addBlankPage(anchor: string, dir: Dir): string | null { const a = this.pages[anchor]; // New pages inherit the size of the page they grow from (A4 if it's missing). const size = a ? { width: a.width, height: a.height } : A4; const id = newId(); return this.insertPages(anchor, dir, [{ id, ...size, markdown: '', strokes: [] }]) ? id : null; } /** A fresh canvas: one plain page with nothing on it. */ get isBlank() { const ids = this.order; const p = ids.length === 1 ? this.pages[ids[0]] : null; return !!p && !p.origin && !p.markdown.trim() && p.strokes.length === 0; } /** Replace every page with a new trunk (importing into a blank canvas). */ replaceWith(pages: PageData[]) { if (pages.length === 0) return; const models = Object.fromEntries(pages.map((p) => [p.id, new PageModel(p)])); this.treeCommand(withTrunk(pages.map((p) => p.id)), models, pages[0].id); } deletePage(id: string) { const { tree, removed } = remove(this.tree, id); if (removed.length === 0) return; const models = { ...this.pages }; for (const r of removed) delete models[r]; let nextTree = tree; let active = this.activeId && models[this.activeId] ? this.activeId : (tree.trunk[0] ?? null); // A canvas always keeps at least one page. if (tree.trunk.length === 0) { const fresh = newId(); models[fresh] = new PageModel({ id: fresh, ...A4, markdown: '', strokes: [] }); nextTree = withTrunk([fresh]); active = fresh; } this.treeCommand(nextTree, models, active); } resizePage(id: string, width: number, height: number) { const page = this.pages[id]; if (!page || page.origin) return; const before = { width: page.width, height: page.height }; const after = { width, height }; const set = (s: typeof before) => { const p = this.pages[id]; p.width = s.width; p.height = s.height; }; this.exec({ redo: () => set(after), undo: () => set(before) }); } }