Skip to content

gitflarePublicPartly agent-ready50

cf-durable-whiteboard

Realtime collaborative whiteboard on Cloudflare Durable Objects: presence cursors, WebSocket hibernation, SQLite storage, undo, PNG export.

Keep board zoom and pan stable across reloads6eb4fbd
JavaScript45 lines2.2 KB
export const COLOURS = ["#e8edf7", "#ff6b6b", "#ffd166", "#06d6a0", "#3df0ff", "#b794f6", "#ff9f1c", "#f15bb5"];export const SIZES = [2, 5, 10];export const ID_ALPHABET = "abcdefghjkmnpqrstuvwxyz23456789";export function newBoardId(rand = Math.random) {  let s = ""; for (let i = 0; i < 8; i++) s += ID_ALPHABET[Math.floor(rand() * ID_ALPHABET.length)];  return s;}export const isBoardId = (s) => typeof s === "string" && /^[a-z0-9]{4,32}$/.test(s);export const shapeId = (rand = Math.random) => "s" + Math.floor(rand() * 2 ** 40).toString(36) + Date.now().toString(36);export const msg = {  hello: (name) => ({ type: "hello", name }),  add: (shape) => ({ type: "op", op: { type: "add", shape } }),  update: (shape) => ({ type: "op", op: { type: "update", shape } }),  del: (id) => ({ type: "op", op: { type: "delete", id } }),  clear: () => ({ type: "op", op: { type: "clear" } }),  undo: () => ({ type: "undo" }),  redo: () => ({ type: "redo" }),  cursor: (x, y) => ({ type: "cursor", x: Math.round(x), y: Math.round(y) }),};export function applyOp(shapes, op) {  switch (op && op.type) {    case "add": case "update": shapes.set(op.shape.id, op.shape); break;    case "delete": shapes.delete(op.id); break;    case "clear": shapes.clear(); break;    case "batch": for (const o of op.ops) applyOp(shapes, o); break;  }  return shapes;}export function backoff(attempt, rand = Math.random) {  const base = Math.min(15000, 500 * 2 ** attempt);  return Math.round(base / 2 + rand() * base / 2);}export const peopleLabel = (n) => `${n} ${n === 1 ? "person" : "people"} here`;export function parseServer(data) {  try { const m = JSON.parse(data); return m && typeof m.type === "string" ? m : null; } catch { return null; }}export function loadRecent(store) {  try { const r = JSON.parse(store.getItem("recent") || "[]"); return Array.isArray(r) ? r.filter(isBoardId).slice(0, 10) : []; } catch { return []; }}export function pushRecent(store, id) {  const r = [id, ...loadRecent(store).filter((x) => x !== id)].slice(0, 10);  store.setItem("recent", JSON.stringify(r)); return r;}export const initials = (n) => (String(n || "?").trim().split(/\s+/).map((w) => w[0]).join("").slice(0, 2) || "?").toUpperCase();