Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167// Small DOM + data helpers shared across the UI.
export const SVG_NS = 'http://www.w3.org/2000/svg';export const XLINK_NS = 'http://www.w3.org/1999/xlink';export const XML_NS = 'http://www.w3.org/XML/1998/namespace';
// h('div.cls#id', {attrs}, ...children) — tiny hyperscript.export function h (tag, props, ...children) { if (props != null && (typeof props !== 'object' || props instanceof Node || Array.isArray(props))) { children.unshift(props); props = null; } const m = tag.match(/^([a-z0-9-]+)?((?:[.#][\w-]+)*)$/i); const el = document.createElement(m?.[1] || 'div'); if (m?.[2]) { for (const part of m[2].match(/[.#][\w-]+/g)) { if (part[0] === '.') el.classList.add(part.slice(1)); else el.id = part.slice(1); } } if (props) { for (const [k, v] of Object.entries(props)) { if (v == null || v === false) continue; if (k === 'class' || k === 'className') el.className += (el.className ? ' ' : '') + v; else if (k === 'style' && typeof v === 'object') { for (const [sk, sv] of Object.entries(v)) { if (sk.startsWith('--')) el.style.setProperty(sk, sv); else el.style[sk] = sv; } } else if (k === 'dataset') Object.assign(el.dataset, v); else if (k.startsWith('on') && typeof v === 'function') el.addEventListener(k.slice(2).toLowerCase(), v); else if (k === 'html') el.innerHTML = v; else if (k in el && typeof v !== 'string' && k !== 'value') { try { el[k] = v; } catch { el.setAttribute(k, v); } } else if (k === 'value') el.value = v; else el.setAttribute(k, v === true ? '' : v); } } append(el, children); return el;}
export function append (el, children) { for (const c of children.flat(Infinity)) { if (c == null || c === false) continue; el.append(c instanceof Node ? c : document.createTextNode(String(c))); } return el;}
export function svgEl (tag, attrs = {}, ...children) { const el = document.createElementNS(SVG_NS, tag); for (const [k, v] of Object.entries(attrs)) if (v != null) el.setAttribute(k, v); for (const c of children.flat()) if (c) el.append(c); return el;}
export function clear (el) { while (el.firstChild) el.removeChild(el.firstChild); return el; }
export const clamp = (v, a, b) => Math.min(b, Math.max(a, v));export const round = (v, p = 3) => { const f = 10 ** p; return Math.round(v * f) / f; };export const fmt = (v, p = 3) => String(round(v, p));
export function debounce (fn, ms = 150) { let t; return (...args) => { clearTimeout(t); t = setTimeout(() => fn(...args), ms); };}
export function throttle (fn, ms = 50) { let last = 0, pending = null; return (...args) => { const now = performance.now(); if (now - last >= ms) { last = now; fn(...args); } else { pending = args; setTimeout(() => { if (pending) { last = performance.now(); fn(...pending); pending = null; } }, ms - (now - last)); } };}
export function bytes (n) { if (n < 1024) return `${n} B`; if (n < 1024 * 1024) return `${(n / 1024).toFixed(1)} kB`; return `${(n / 1024 / 1024).toFixed(2)} MB`;}
export function uid (prefix = 'k') { return `${prefix}${Math.random().toString(36).slice(2, 8)}`; }
export function esc (s) { return String(s).replace(/[&<>"']/g, c => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c]));}
export function isMac () { return navigator.platform.includes('Mac'); }export const MOD = isMac() ? '⌘' : 'Ctrl+';export function modKey (e) { return isMac() ? e.metaKey : e.ctrlKey; }
// Parse a CSS-ish length: "12", "12px", "1.5em", "50%".export function parseLength (s) { if (s == null) return null; const m = String(s).trim().match(/^([+-]?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?)\s*([a-z%]*)$/i); if (!m) return null; return { value: parseFloat(m[1]), unit: m[2] || '' };}
export function isNumeric (s) { return /^[+-]?(\d+\.?\d*|\.\d+)(e[+-]?\d+)?$/i.test(String(s).trim()); }
// Normalise any CSS color to #rrggbb / rgba() using the canvas parser.let colorCtx = null;export function parseColor (s) { if (!s || typeof s !== 'string') return null; const v = s.trim(); if (/^(none|transparent|currentcolor|inherit|url\(|context-)/i.test(v)) return v.toLowerCase() === 'transparent' ? { r: 0, g: 0, b: 0, a: 0, hex: '#000000', css: 'transparent' } : null; if (!colorCtx) colorCtx = document.createElement('canvas').getContext('2d', { willReadFrequently: true }); colorCtx.fillStyle = '#010203'; colorCtx.fillStyle = v; const out = colorCtx.fillStyle; if (out === '#010203' && !/^#010203$|^rgb\(1,\s*2,\s*3\)$/i.test(v.replace(/\s/g, ''))) { // Could be genuinely #010203; recheck with a different sentinel. colorCtx.fillStyle = '#040506'; colorCtx.fillStyle = v; if (colorCtx.fillStyle === '#040506') return null; } let r, g, b, a = 1; if (out.startsWith('#')) { r = parseInt(out.slice(1, 3), 16); g = parseInt(out.slice(3, 5), 16); b = parseInt(out.slice(5, 7), 16); } else { const m = out.match(/rgba?\(([^)]+)\)/); const p = m[1].split(',').map(x => parseFloat(x)); [r, g, b] = p; a = p.length > 3 ? p[3] : 1; } const hex = '#' + [r, g, b].map(x => x.toString(16).padStart(2, '0')).join(''); return { r, g, b, a, hex, css: a < 1 ? `rgba(${r}, ${g}, ${b}, ${round(a, 3)})` : hex };}
export function hexWithAlpha (hex, a) { if (a >= 1) return hex; const c = parseColor(hex); return `rgba(${c.r}, ${c.g}, ${c.b}, ${round(a, 3)})`;}
export function textWidthClass (s, max = 40) { return s.length > max ? s.slice(0, max - 1) + '…' : s; }
export function download (blob, name) { const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = name; document.body.append(a); a.click(); a.remove(); setTimeout(() => URL.revokeObjectURL(url), 5000);}
export async function api (path, { method = 'GET', body, signal } = {}) { const res = await fetch(path, { method, headers: body ? { 'Content-Type': 'application/json' } : undefined, body: body ? JSON.stringify(body) : undefined, signal, }); const ct = res.headers.get('content-type') || ''; if (!res.ok) { let msg = res.statusText; try { msg = (await res.json()).error || msg; } catch { /* ignore */ } throw new Error(msg); } if (ct.includes('application/json')) return res.json(); return res.blob();}
export function basename (p) { return String(p || '').split('/').pop(); }export function dirname (p) { const i = String(p || '').lastIndexOf('/'); return i > 0 ? p.slice(0, i) : '/'; }
export function pluralize (n, s, p = s + 's') { return `${n} ${n === 1 ? s : p}`; }
// Emitter mixinexport class Emitter extends EventTarget { on (type, fn) { const h = e => fn(e.detail, e); this.addEventListener(type, h); return () => this.removeEventListener(type, h); } emit (type, detail) { this.dispatchEvent(new CustomEvent(type, { detail })); }}