Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237import { uuidv7 } from './api';import { boardOf, everyBoard, tileElement, type Board, type Pane, type Window,} from './board.svelte';import { boards } from './boards.svelte';import { layout } from './layout.svelte';import { readPalette, prefs } from './prefs.svelte';import { leaf } from './tiles/tree';import { readText } from './type';
// A pane popped out of a board (a window, a section, a group, a canvas) is// shown by a page of its own, `/?pop=<board>&window=<the window>` or// `&pane=<its id>&tile=<its tile and windows>` (Popout.svelte), with its own// realm and socket, so it keeps going while the board's tab is hidden. The// board takes the pane off (Board.popOut) and remembers where it sat; when the// page closes the board puts it back (Board.bringBack) in the shape the page// left it in. The two talk over a BroadcastChannel, so the board learns again// which panes are out after a reload of its own: it asks, and every// popped-out page answers.
type Message = | { kind: 'who' } | { kind: 'out'; board: string; id: string } | { kind: 'closed'; board: string; id: string; pane: Pane } | { kind: 'open'; board: string; window: Window };
/** How long the board waits for a page to answer, or to come back after it went (a reload), before it takes a window back. */const GRACE = 1500;
type Page = NonNullable<ReturnType<typeof open>>;
const params = new URLSearchParams(location.search);
/** A pane's record longer than this goes to the page in sessionStorage, which a popup opened by this page starts with a copy of, and its address carries a key. */const LONG = 4000;
/** On a popped-out pane's own page: the board it came from, the pane's id (a window's or a tile's), and the pane as it left. */export const popup = ((): { board: string; id: string; pane: Pane } | null => { const board = params.get('pop'); try { const window = params.get('window') && JSON.parse(params.get('window')!); if (window) return board && window.id && window.type ? { board, id: window.id, pane: { tile: leaf(window.id), windows: { [window.id]: window } }, } : null; const id = params.get('pane'); const tile = params.get('tile') ?? ''; const pane = JSON.parse( tile.startsWith('@') ? (sessionStorage.getItem(`albedo.pop:${tile.slice(1)}`) ?? '') : tile, ); return board && id && pane?.tile?.kind && pane.windows ? { board, id, pane } : null; } catch { return null; }})();
const channel = new BroadcastChannel('albedo.popouts');
function post(message: Message) { channel.postMessage(message);}
function key(board: string, id: string) { return `${board}\n${id}`;}
/** Pops pane `id` of the active board (a window, or a section, group or canvas by its tile's id) out into a page of its own. Run it from a click or a key: the browser opens popups only for one. */export function popOut(id: string) { const board = boards.board; const window = board?.windows[id]; const pane = board?.pane(id); if ( !board || !pane || Object.values(pane.windows).some((window) => window.type === 'settings') || window?.type === 'note' || layout.phone || (!window && pane.tile === board.root) ) return; const rect = tileElement(id)?.getBoundingClientRect(); const size = `width=${Math.round(Math.max(rect?.width ?? 0, 480))},height=${Math.round(Math.max(rect?.height ?? 0, 480))}`; let query: Record<string, string> = { pop: board.id, window: JSON.stringify(window), }; if (!window) { const record = JSON.stringify(pane); const stored = record.length > LONG ? uuidv7() : null; if (stored) sessionStorage.setItem(`albedo.pop:${stored}`, record); query = { pop: board.id, pane: id, tile: stored ? `@${stored}` : record }; } const page = open( `${location.pathname}?${new URLSearchParams(query)}`, `albedo:${board.id}:${id}`, `popup,${size}`, ); if (!page) return; board.popOut(id); watch(board.id, id, page);}
// The board's side.
/** How often the board looks whether a page it opened has closed. */const CHECK = 50;
/** Panes whose page went away, waiting out GRACE in case it comes back; and the handles of pages this one opened. */const leaving = new Map< string, { timer: ReturnType<typeof setTimeout>; check?: ReturnType<typeof setInterval>; pane?: Pane; }>();const opened = new Map<string, Page>();let polling: ReturnType<typeof setInterval> | undefined;
function watch(board: string, id: string, page: Page) { opened.set(key(board, id), page); polling ??= setInterval(() => { for (const [at, page] of opened) if (page.closed) gone(at); if (!opened.size) (clearInterval(polling), (polling = undefined)); }, 1000);}
/** * A page went: its pane comes back as soon as the page this board opened is * closed, or, without its handle (opened before a reload), once GRACE has * passed with no word from it, since a page reloading says it went too. */function gone(at: string, pane?: Pane) { const page = opened.get(at); opened.delete(at); const last = pane ?? leaving.get(at)?.pane; stay(at); const [board, id] = at.split('\n'); const back = () => { stay(at); boardOf(board)?.bringBack(id, last); }; if (page?.closed) return back(); const check = page ? setInterval(() => page.closed && back(), CHECK) : undefined; leaving.set(at, { timer: setTimeout(back, GRACE), check, pane: last });}
/** Stops waiting for pane `at` to come back: its page answered, or it is back. */function stay(at: string) { const waiting = leaving.get(at); clearTimeout(waiting?.timer); clearInterval(waiting?.check); leaving.delete(at);}
/** Starts the board's side: panes away that no page answers for within GRACE come back. */export function start() { const heard = new Set<string>(); channel.onmessage = ({ data }: MessageEvent<Message>) => { if (data.kind === 'out') { const at = key(data.board, data.id); heard.add(at); stay(at); } else if (data.kind === 'closed') gone(key(data.board, data.id), data.pane); else if (data.kind === 'open' && data.window.type !== 'note') { const board = boardOf(data.board); if (!board) return; boards.show(board.id); board.adopt(data.window); } }; post({ kind: 'who' }); setTimeout(() => { for (const board of everyBoard()) for (const id of Object.keys(board.away)) if (!heard.has(key(board.id, id)) && !leaving.has(key(board.id, id))) board.bringBack(id); }, GRACE);}
// The popped-out page's side.
/** Starts a popped-out page: it answers for its pane, says when it goes (with the pane as it is then), and follows the board's prefs. */export function startPopup( board: Board, { board: from, id, pane }: NonNullable<typeof popup>,) { board.showPane(id, pane); board.openElsewhere = (opened) => post({ kind: 'open', board: from, window: opened }); const out = () => post({ kind: 'out', board: from, id }); channel.onmessage = ({ data }: MessageEvent<Message>) => data.kind === 'who' && out(); out(); addEventListener('pagehide', () => post({ kind: 'closed', board: from, id, pane: board.pane(board.root.id) ?? pane, }), ); addEventListener('storage', (event) => { if (event.key !== 'albedo.prefs.v4' || !event.newValue) return; try { const saved = JSON.parse(event.newValue); if (['system', 'light', 'dark'].includes(saved.theme)) prefs.theme = saved.theme; prefs.compactBars = saved.compactBars === true; prefs.closeNotes = saved.closeNotes === true; prefs.dots = saved.dots !== false; prefs.palettes = { light: readPalette(saved.palettes?.light, 'light'), dark: readPalette(saved.palettes?.dark, 'dark'), }; if (saved.fonts) prefs.fonts = saved.fonts; prefs.text = readText(saved.text); } catch {} });}