gitflarePublicPartly agent-ready50
cf-durable-whiteboard
Realtime collaborative whiteboard on Cloudflare Durable Objects: presence cursors, WebSocket hibernation, SQLite storage, undo, PNG export.
- LicenseApache-2.0
- LanguageTypeScript
- Last landed
6eb4fbdOct 20, 2026
Keep board zoom and pan stable across reloads
6eb4fbdimport { COLOURS, SIZES, msg } from "./protocol.js";import { makeView, zoomAt, panBy } from "./viewport.js";import { exportPng } from "./render.js";import { ICONS, TOOLS } from "./icons.js";const $ = (i) => document.getElementById(i);const pressed = (g, b) => [...g.children].forEach((x) => x.setAttribute("aria-pressed", String(x === b)));export function setupToolbar(api, cv, id) { const { st, render, toast } = api; const setTool = (t) => { st.tool = t; st.sel = null; pressed($("tools"), $("tools").querySelector(`[data-tool="${t}"]`)); cv.style.cursor = t === "pan" ? "grab" : t === "select" ? "default" : "crosshair"; render(); }; for (const [t, label] of TOOLS) { const b = document.createElement("button"); b.className = "icon"; b.type = "button"; b.dataset.tool = t; b.title = label; b.setAttribute("aria-label", label); b.innerHTML = ICONS[t]; b.onclick = () => setTool(t); $("tools").append(b); } SIZES.forEach((n, i) => { const b = document.createElement("button"); b.className = "icon"; b.type = "button"; const lab = ["Thin", "Medium", "Thick"][i] + " pen"; b.setAttribute("aria-label", lab); b.title = lab; b.innerHTML = `<span class="dot" style="width:${n + 4}px;height:${n + 4}px"></span>`; b.onclick = () => { st.size = n; pressed($("sizes"), b); }; $("sizes").append(b); }); COLOURS.forEach((col) => { const b = document.createElement("button"); b.className = "sw"; b.type = "button"; b.style.background = col; b.setAttribute("aria-label", "Colour " + col); b.title = col; b.onclick = () => { st.colour = col; pressed($("colours"), b); }; $("colours").append(b); }); pressed($("sizes"), $("sizes").children[1]); pressed($("colours"), $("colours").children[0]); setTool("pen"); for (const k of ["undo", "redo", "clear", "export"]) $(k).innerHTML = ICONS[k]; $("undo").onclick = () => api.send(msg.undo()); $("redo").onclick = () => api.send(msg.redo()); $("clear").onclick = () => $("dlg-clear").showModal(); $("dlg-clear").onclose = () => { if ($("dlg-clear").returnValue === "ok") { st.shapes.clear(); api.send(msg.clear()); render(); } }; $("export").onclick = () => $("dlg-export").showModal(); $("dlg-export").onclose = async () => { const mode = $("dlg-export").returnValue; if (mode !== "view" && mode !== "all") return; const blob = await exportPng(st.shapes.values(), mode, st.view, cv.clientWidth, cv.clientHeight, !matchMedia("(prefers-color-scheme: light)").matches); const a = document.createElement("a"); a.href = URL.createObjectURL(blob); a.download = `board-${id}.png`; a.click(); setTimeout(() => URL.revokeObjectURL(a.href), 1000); toast("PNG exported"); }; $("copy").onclick = async () => { try { await navigator.clipboard.writeText(location.href); toast("Link copied"); } catch { toast("Copy failed. Copy the address bar instead."); } }; const zoomBy = (f) => { st.view = zoomAt(st.view, cv.clientWidth / 2, cv.clientHeight / 2, f); render(); }; $("zin").onclick = () => zoomBy(1.25); $("zout").onclick = () => zoomBy(0.8); $("zlabel").onclick = () => { st.view = makeView(); render(); }; cv.addEventListener("wheel", (e) => { e.preventDefault(); const r = cv.getBoundingClientRect(); if (e.ctrlKey || e.metaKey) st.view = zoomAt(st.view, e.clientX - r.left, e.clientY - r.top, Math.exp(-e.deltaY * 0.01)); else st.view = panBy(st.view, -e.deltaX, -e.deltaY); render(); }, { passive: false }); const keys = { v: "select", p: "pen", r: "rect", o: "ellipse", a: "arrow", n: "note", e: "eraser", h: "pan" }; addEventListener("keydown", (e) => { if (/^(TEXTAREA|INPUT)$/.test(e.target.tagName) || document.querySelector("dialog[open]")) return; const mod = e.ctrlKey || e.metaKey, k = e.key.toLowerCase(); if (mod && k === "z") { e.preventDefault(); api.send(e.shiftKey ? msg.redo() : msg.undo()); } else if (mod && k === "y") { e.preventDefault(); api.send(msg.redo()); } else if (!mod && e.key === " ") { st.space = true; e.preventDefault(); } else if (!mod && keys[k]) setTool(keys[k]); else if ((e.key === "Delete" || e.key === "Backspace") && st.sel) { api.del(st.sel.id); st.sel = null; } else if (e.key === "+" || e.key === "=") zoomBy(1.25); else if (e.key === "-") zoomBy(0.8); }); addEventListener("keyup", (e) => { if (e.key === " ") st.space = false; });}