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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151import type { Attachment } from 'svelte/attachments';
/** * Keeps a popover inside the box it must not leave: moved back across, in CSS * pixels (the canvas and a window's zoom scale it), and capped to the vertical * room inside its clipping container (`.window > .body`, `dialog`, its window, * or the viewport) on the side it opens to (`up` or down; a tooltip leaves `up` * out and is only moved). */export function fit(pop: HTMLElement, up?: boolean) { const style = pop.style; style.transform = style.maxWidth = ''; const clip = pop.closest('.window > .body, dialog'); const rect = pop.getBoundingClientRect(); const win = pop.closest('.window:not(.auto-height)'); const wide = !win || rect.width > win.getBoundingClientRect().width - 16; const box = ( clip ?? (wide ? pop.closest('main') : win) ?? document.documentElement ).getBoundingClientRect(); const scale = rect.width / pop.offsetWidth || 1; const maxBoxW = Math.min(box.width, document.documentElement.clientWidth) - 16; const width = clip ? Math.min(rect.width, maxBoxW) : rect.width; const right = rect.left + width; const rightEdge = Math.min(box.right, document.documentElement.clientWidth) - 8; const leftEdge = Math.max(box.left, 0) + 8; const past = right > rightEdge ? rightEdge - right : rect.left < leftEdge ? leftEdge - rect.left : 0; if (past) style.transform = `translateX(${Math.round(past / scale)}px)`; if (up === undefined) return; const topEdge = Math.max(box.top, 0) + 8; const bottomEdge = Math.min(box.bottom, document.documentElement.clientHeight) - 8; const room = Math.max( 72, (up ? rect.bottom - topEdge : bottomEdge - rect.top) / scale, ); style.maxWidth = `${Math.floor(width / scale)}px`; style.maxHeight = style.overflowY = ''; style.removeProperty('--selector-height'); const computed = getComputedStyle(pop); const cssMax = parseFloat(computed.maxHeight) || Infinity; const maxH = Math.floor(Math.min(room, cssMax)); style.maxHeight = `${maxH}px`; if (maxH < 320) style.setProperty('--selector-height', `${maxH}px`); if (computed.display !== 'flex') style.overflowY = 'auto';}
/** `fit` once the popover is on the page and whenever its layout box resizes. */export const fitted = (up: boolean): Attachment<HTMLElement> => (pop) => { const update = () => pop.isConnected && fit(pop, up); queueMicrotask(update); window.addEventListener('resize', update); return () => window.removeEventListener('resize', update); };
/** Closes a popover on a press outside it (`keep` says what counts as inside) or on Escape, taken so the chat under it keeps its own state. Returns the cleanup, for an $effect. */export function dismissal( keep: (target: Element) => boolean, close: () => void,): () => void { const away = (event: PointerEvent) => event.target instanceof Element && !keep(event.target) && close(); const escape = (event: KeyboardEvent) => event.key === 'Escape' && (event.preventDefault(), close()); document.addEventListener('pointerdown', away, true); document.addEventListener('keydown', escape); return () => { document.removeEventListener('pointerdown', away, true); document.removeEventListener('keydown', escape); };}
/** * Remembers what has the focus now and returns a function that gives it back, * once whatever took it is gone and left it on the page: a popover closed by * Esc, a pick or its own button leaves the keyboard where it was. */export function keepFocus(): () => void { const before = document.activeElement; return () => queueMicrotask(() => { const now = document.activeElement; if ( before instanceof HTMLElement && before.isConnected && (!now || now === document.body) ) before.focus({ preventScroll: true }); });}
/** A word's menu escapes clipping windows but stays attached to that word. * On a phone its CSS supplies the bottom-sheet position instead. */export const floating = (anchor: HTMLElement | undefined): Attachment<HTMLElement> => (pop) => { pop.showPopover(); const update = () => { if (!anchor || !pop.isConnected) return; if (matchMedia('(max-width: 760px)').matches) { pop.style.left = pop.style.top = ''; return; } const button = anchor.getBoundingClientRect(); const menu = pop.getBoundingClientRect(); const scale = menu.width / pop.offsetWidth || 1; const left = Math.max( 8, Math.min(button.right - menu.width, innerWidth - menu.width - 8), ); const above = button.top - menu.height - 4; pop.style.left = `${left / scale}px`; pop.style.top = `${Math.max(8, above >= 8 ? above : button.bottom + 4) / scale}px`; }; const observer = new ResizeObserver(update); observer.observe(pop); // Canvas pan changes transforms without firing native scroll events. // Track the anchor only while this menu is mounted in the top layer. let frame: number; let previousBounds = ''; const followAnchor = () => { const bounds = anchor?.getBoundingClientRect(); const key = bounds ? `${bounds.x},${bounds.y},${bounds.width},${bounds.height}` : ''; if (key !== previousBounds) { previousBounds = key; update(); } frame = requestAnimationFrame(followAnchor); }; frame = requestAnimationFrame(followAnchor); queueMicrotask(update); window.addEventListener('resize', update); return () => { cancelAnimationFrame(frame); observer.disconnect(); window.removeEventListener('resize', update); }; };