main
Caleb
0.4.0: wiki page runs the live calculator (README.html loads the current source from the page repo), UI static mode, sh…
fc3ee8f
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
<!doctype html>
<html lang="en"><head>
<meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
<title>Hardware Calculator</title>
<style>
@font-face { font-family: 'Satoshi'; src: url('https://wiki.adom.inc/static/fonts/satoshi-400.woff2') format('woff2'); font-weight: 400; }
@font-face { font-family: 'Satoshi'; src: url('https://wiki.adom.inc/static/fonts/satoshi-500.woff2') format('woff2'); font-weight: 500; }
@font-face { font-family: 'Familjen Grotesk'; src: url('https://wiki.adom.inc/static/fonts/familjen-grotesk-700.woff2') format('woff2'); font-weight: 700; }
:root { --bg:#0d1117; --panel:#161b22; --panel2:#1c2330; --line:#262d38; --ink:#e6edf3; --dim:#8b949e; --teal:#00b8b0; --blue:#2f81f7; --warn:#f5a524; --err:#f85149; }
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; background: var(--bg); color: var(--ink); font: 14px/1.5 'Satoshi', -apple-system, 'Segoe UI', sans-serif; }
header { height: 44px; display: flex; align-items: center; gap: 12px; padding: 0 14px; border-bottom: 1px solid var(--line); background: var(--panel); }
header .logo { width: 22px; height: 22px; border-radius: 6px; background: linear-gradient(135deg, var(--teal), var(--blue)); display: grid; place-items: center; font: 700 12px 'Familjen Grotesk'; color: #04121a; }
header h1 { font: 700 15px 'Familjen Grotesk', sans-serif; letter-spacing: .01em; }
header .ver { color: var(--dim); font-size: 12px; }
header .sp { flex: 1; }
.icon-btn { width: 30px; height: 30px; border-radius: 8px; border: 1px solid transparent; background: none; color: #fff; display: grid; place-items: center; cursor: pointer; position: relative; }
.icon-btn:hover { background: var(--panel2); border-color: var(--line); }
[data-tip]:hover::after { content: attr(data-tip); position: absolute; top: 36px; right: 0; white-space: nowrap; background: #0b0f14; border: 1px solid var(--line); border-radius: 6px; padding: 4px 8px; font-size: 12px; color: var(--ink); z-index: 50; transition-delay: .4s; }
.app { display: grid; grid-template-columns: 250px 1fr; height: calc(100% - 44px); }
nav { border-right: 1px solid var(--line); overflow: auto; padding: 10px 8px 20px; }
nav input { width: 100%; background: var(--bg); border: 1px solid var(--line); border-radius: 8px; color: var(--ink); padding: 7px 10px; font: inherit; margin-bottom: 8px; }
nav input:focus { outline: none; border-color: var(--teal); }
nav .grp { font-size: 11px; letter-spacing: .06em; color: var(--dim); padding: 10px 8px 4px; }
nav .item { display: block; width: 100%; text-align: left; background: none; border: 0; color: #c9d1d9; padding: 6px 10px; border-radius: 7px; cursor: pointer; font: inherit; }
nav .item:hover { background: var(--panel2); }
nav .item.on { background: rgba(0,184,176,.14); color: var(--ink); box-shadow: inset 2px 0 0 var(--teal); }
main { overflow: auto; padding: 22px 28px 60px; }
.title h2 { font: 700 21px 'Familjen Grotesk', sans-serif; }
.title p { color: var(--dim); margin-top: 3px; }
.cols { display: grid; grid-template-columns: minmax(280px, 380px) 1fr; gap: 22px; margin-top: 18px; align-items: start; }
#pick { display: none; width: 100%; background: var(--bg); border: 1px solid var(--line); border-radius: 8px; color: var(--ink); padding: 8px 10px; font: 500 14px 'Satoshi', sans-serif; margin-bottom: 14px; }
@media (max-width: 900px) { .app { grid-template-columns: 1fr; } nav { display: none; } .cols { grid-template-columns: 1fr; } #pick { display: block; } }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 16px; }
.stack { display: grid; gap: 22px; }
#schem svg { width: 100%; height: auto; display: block; }
.card h3 { font-size: 12px; letter-spacing: .06em; color: var(--dim); font-weight: 500; margin-bottom: 10px; }
.field { margin-bottom: 11px; }
.field label { display: block; font-size: 12.5px; color: #aeb8c4; margin-bottom: 4px; }
.field .row { display: flex; align-items: center; background: var(--bg); border: 1px solid var(--line); border-radius: 8px; }
.field .row:focus-within { border-color: var(--teal); }
.field input, .field select { flex: 1; min-width: 0; background: none; border: 0; color: var(--ink); padding: 8px 10px; font: 500 14px 'Satoshi', monospace; }
.field select { cursor: pointer; }
.field select option { background: var(--panel); }
.field input:focus, .field select:focus { outline: none; }
.field .unit { color: var(--dim); padding-right: 10px; font-size: 12.5px; white-space: nowrap; }
.field.bad .row { border-color: var(--err); }
.hint { font-size: 12px; color: var(--dim); margin-top: 6px; }
.res { display: grid; grid-template-columns: 1fr auto; gap: 0; }
.res > div { padding: 8px 2px; border-bottom: 1px solid var(--line); }
.res .l { color: #aeb8c4; }
.res .v { text-align: right; font: 500 14.5px 'Satoshi', monospace; font-variant-numeric: tabular-nums; cursor: copy; }
.res .v:hover { color: var(--teal); }
.res .hl { color: var(--teal); font-size: 17px; font-weight: 700; }
.msg { border-radius: 8px; padding: 9px 12px; margin-top: 12px; font-size: 13px; }
.msg.err { background: rgba(248,81,73,.1); border: 1px solid rgba(248,81,73,.35); color: #ffb4ae; }
.msg.warn { background: rgba(245,165,36,.08); border: 1px solid rgba(245,165,36,.3); color: #f3d9a7; }
.msg.note { background: rgba(47,129,247,.07); border: 1px solid rgba(47,129,247,.25); color: #c4d8f6; }
.formula { margin-top: 14px; padding-top: 12px; border-top: 1px dashed var(--line); font-size: 12.5px; color: var(--dim); }
.formula code { color: #d2dae4; font: 12.5px ui-monospace, Menlo, monospace; display: block; margin: 3px 0 5px; white-space: pre-wrap; }
table { width: 100%; border-collapse: collapse; margin-top: 14px; font-size: 13px; }
th { text-align: left; color: var(--dim); font-weight: 500; font-size: 11.5px; letter-spacing: .04em; border-bottom: 1px solid var(--line); padding: 6px; }
td { padding: 6px; border-bottom: 1px solid var(--line); font-variant-numeric: tabular-nums; }
tbody tr:first-child td { color: var(--teal); }
.btn { background: var(--panel2); border: 1px solid var(--line); color: var(--ink); border-radius: 8px; padding: 6px 11px; font: inherit; font-size: 12.5px; cursor: pointer; }
.btn:hover { border-color: var(--teal); }
.actions { display: flex; gap: 8px; margin-top: 12px; }
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.55); display: none; align-items: flex-start; justify-content: center; padding-top: 70px; z-index: 40; }
.modal.on { display: flex; }
.modal .box { width: 460px; max-width: 94vw; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 18px; }
.modal h3 { font: 700 16px 'Familjen Grotesk', sans-serif; margin-bottom: 12px; display: flex; justify-content: space-between; }
.set { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 9px 0; border-bottom: 1px solid var(--line); }
.set span { color: #c9d1d9; font-size: 13px; }
.set select, .set input { background: var(--bg); border: 1px solid var(--line); color: var(--ink); border-radius: 7px; padding: 5px 8px; font: inherit; width: 110px; }
.toast { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); background: #0b0f14; border: 1px solid var(--line); border-radius: 9px; padding: 8px 14px; font-size: 13px; opacity: 0; transition: opacity .2s; pointer-events: none; z-index: 60; }
.toast.on { opacity: 1; }
</style></head>
<body>
<header>
<div class="logo">∑</div><h1>Hardware Calculator</h1><span class="ver" id="ver"></span><span class="sp"></span>
<button class="icon-btn" id="gear" data-tip="Settings" aria-label="Settings">
<svg width="17" height="17" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8"><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.7 1.7 0 0 0 .3 1.8l.1.1a2 2 0 1 1-2.8 2.8l-.1-.1a1.7 1.7 0 0 0-1.8-.3 1.7 1.7 0 0 0-1 1.5V21a2 2 0 1 1-4 0v-.1a1.7 1.7 0 0 0-1.1-1.5 1.7 1.7 0 0 0-1.8.3l-.1.1a2 2 0 1 1-2.8-2.8l.1-.1a1.7 1.7 0 0 0 .3-1.8 1.7 1.7 0 0 0-1.5-1H3a2 2 0 1 1 0-4h.1a1.7 1.7 0 0 0 1.5-1.1 1.7 1.7 0 0 0-.3-1.8l-.1-.1a2 2 0 1 1 2.8-2.8l.1.1a1.7 1.7 0 0 0 1.8.3H9a1.7 1.7 0 0 0 1-1.5V3a2 2 0 1 1 4 0v.1a1.7 1.7 0 0 0 1 1.5 1.7 1.7 0 0 0 1.8-.3l.1-.1a2 2 0 1 1 2.8 2.8l-.1.1a1.7 1.7 0 0 0-.3 1.8V9a1.7 1.7 0 0 0 1.5 1H21a2 2 0 1 1 0 4h-.1a1.7 1.7 0 0 0-1.5 1z"/></svg>
</button>
</header>
<div class="app">
<nav><input id="q" placeholder="Search calculators…" autocomplete="off"><div id="list"></div></nav>
<main>
<select id="pick" aria-label="Calculator"></select><div class="title"><h2 id="t"></h2><p id="blurb"></p></div>
<div class="cols">
<div class="card"><h3>Inputs</h3><div id="form"></div>
<div class="hint">Prefixes work: 4k7, 10u, 2.2M, 100n. Results update as you type.</div>
<div class="actions"><button class="btn" id="reset">Reset to defaults</button></div></div>
<div class="stack"><div class="card"><h3>Results</h3><div id="out"></div>
<div class="actions"><button class="btn" id="copy">Copy results</button><button class="btn" id="link">Copy link</button></div></div><div class="card" id="schemCard" hidden><h3 id="schemH">Schematic</h3><div id="schem"></div></div></div>
</div>
</main>
</div>
<div class="modal" id="modal"><div class="box"><h3>Settings <button class="icon-btn" id="close" aria-label="Close">✕</button></h3><div id="sets"></div>
<div class="hint" id="sver"></div></div></div>
<div class="toast" id="toast"></div>
<script src="calcs.js"></script>
<script src="schematics.js"></script>
<script>
const H = window.HWCALC, $ = s => document.querySelector(s);
const CAT = H.catalog();
let settings = {}, spec = {}, cur = 'divider', vals = {}, lastRes = null, usedHash = false, seenRev = 0;
// STATIC: the wiki page build (no server). The host sets window.HWCALC_STATIC = { version, commit, pageUrl, hash }.
const STATIC = window.HWCALC_STATIC || null;
function copyText(text, msg) {
const done = () => toast(msg), fallback = () => {
const t = document.createElement('textarea'); t.value = text; t.style.position = 'fixed'; t.style.opacity = '0'; document.body.appendChild(t); t.select();
let ok = false; try { ok = document.execCommand('copy'); } catch {} t.remove(); ok ? done() : toast('Copy blocked here. Select and copy: ' + text); };
if (navigator.clipboard && navigator.clipboard.writeText) navigator.clipboard.writeText(text).then(done, fallback); else fallback();
}
const openHash = hs => { const [hc, hq] = String(hs || '').replace(/^#/, '').split('?'); if (hc && CAT.find(c => c.id === hc)) { select(hc, Object.fromEntries(new URLSearchParams(hq || ''))); return true; } return false; };
const toast = m => { const t = $('#toast'); t.textContent = m; t.classList.add('on'); clearTimeout(t._h); t._h = setTimeout(() => t.classList.remove('on'), 1600); };
const esc = s => String(s).replace(/[&<>"]/g, c => ({ '&': '&', '<': '<', '>': '>', '"': '"' }[c]));
function renderPick() {
const groups = [...new Set(CAT.map(c => c.group))];
$('#pick').innerHTML = groups.map(g => `<optgroup label="${esc(g)}">` + CAT.filter(c => c.group === g).map(c => `<option value="${c.id}"${c.id === cur ? ' selected' : ''}>${esc(c.title)}</option>`).join('') + '</optgroup>').join('');
}
$('#pick').onchange = e => select(e.target.value);
function renderList() {
const q = $('#q').value.trim().toLowerCase(); let html = '', grp = '';
for (const c of CAT) {
if (q && !(c.title + ' ' + c.group + ' ' + c.blurb + ' ' + c.formula).toLowerCase().includes(q)) continue;
if (c.group !== grp) { grp = c.group; html += `<div class="grp">${esc(grp.toUpperCase())}</div>`; }
html += `<button class="item${c.id === cur ? ' on' : ''}" data-id="${c.id}">${esc(c.title)}</button>`;
}
$('#list').innerHTML = html || '<div class="hint" style="padding:8px">No match.</div>';
}
$('#list').onclick = e => { const b = e.target.closest('.item'); if (b) select(b.dataset.id); };
$('#q').oninput = renderList;
function select(id, inputs) {
cur = id; const c = CAT.find(x => x.id === id);
vals = {}; for (const f of c.inputs) vals[f.key] = (inputs && inputs[f.key] !== undefined) ? String(inputs[f.key]) : defaultFor(f);
$('#t').textContent = c.title; $('#blurb').textContent = c.blurb || '';
renderList(); renderPick(); renderForm(); compute(); pushState();
}
function defaultFor(f) {
if (f.setting && settings[f.setting] !== undefined) {
if (f.type !== 'select' || f.options.includes(settings[f.setting])) return String(settings[f.setting]);
}
return f.def ?? '';
}
function visible(f) { if (!f.show) return true; const s = Array.isArray(f.show) ? f.show : [f.show]; return Object.values(vals).some(v => s.includes(v)); }
function renderForm() {
const c = CAT.find(x => x.id === cur);
$('#form').innerHTML = c.inputs.filter(visible).map(f => `<div class="field" data-k="${f.key}"><label>${esc(f.label)}</label><div class="row">` +
(f.type === 'select' ? `<select data-k="${f.key}">${f.options.map(o => `<option${o === vals[f.key] ? ' selected' : ''}>${esc(o)}</option>`).join('')}</select>`
: `<input data-k="${f.key}" value="${esc(vals[f.key] ?? '')}" ${f.type === 'text' ? '' : 'inputmode="decimal"'} spellcheck="false">`) +
(f.unit ? `<span class="unit">${esc(f.unit)}</span>` : '') + `</div></div>`).join('');
}
$('#form').addEventListener('input', e => { const k = e.target.dataset.k; if (!k) return; vals[k] = e.target.value; if (e.target.tagName === 'SELECT') renderForm(); compute(); pushSoon(); });
$('#form').addEventListener('change', e => { if (e.target.tagName === 'SELECT') { vals[e.target.dataset.k] = e.target.value; renderForm(); compute(); pushSoon(); } });
function compute() {
const c = CAT.find(x => x.id === cur);
for (const f of c.inputs) { const el = document.querySelector(`.field[data-k="${f.key}"]`); if (!el || f.type === 'select' || f.type === 'text') continue;
const s = vals[f.key]; el.classList.toggle('bad', s !== '' && s !== undefined && !Number.isFinite(H.parseNum(s, f.si))); }
const r = H.run(cur, vals, settings); lastRes = r;
try { history.replaceState(null, '', '#' + cur + '?' + new URLSearchParams(vals).toString()); } catch {}
let h = '';
if (r.error) h += `<div class="msg err">${esc(r.error)}</div>`;
else h += `<div class="res">` + r.outputs.map(o => `<div class="l">${esc(o.label)}</div><div class="v${o.highlight ? ' hl' : ''}" title="Click to copy">${esc(o.text)}</div>`).join('') + `</div>`;
if (r.table) h += `<table><thead><tr>${r.table.head.map(x => `<th>${esc(x)}</th>`).join('')}</tr></thead><tbody>${r.table.rows.map(row => `<tr>${row.map(x => `<td>${esc(x)}</td>`).join('')}</tr>`).join('')}</tbody></table>`;
for (const w of r.warnings) h += `<div class="msg warn">${esc(w)}</div>`;
for (const n of r.notes) h += `<div class="msg note">${esc(n)}</div>`;
if (settings.show_formulas !== false) h += `<div class="formula">Formula<code>${esc(r.formula)}</code>Reference: ${esc(r.ref)}</div>`;
$('#out').innerHTML = h;
const svg = settings.show_schematics !== false && window.HWSCHEM ? HWSCHEM.draw(cur, vals, settings) : null;
$('#schemCard').hidden = !svg; if (svg) { $('#schem').innerHTML = svg; $('#schemH').textContent = cur === 'spiral' ? 'Layout' : /impedance|diffpair|trace|via|spacing/.test(cur) ? 'Cross-section' : 'Schematic'; }
}
$('#out').addEventListener('click', e => { if (e.target.classList.contains('v')) copyText(e.target.textContent, 'Copied ' + e.target.textContent); });
$('#copy').onclick = () => {
const r = lastRes; if (!r) return;
const lines = [r.title, ...Object.entries(vals).map(([k, v]) => ` ${k} = ${v}`), ...(r.outputs || []).map(o => `${o.label}: ${o.text}`), `Formula: ${r.formula}`, `Ref: ${r.ref}`];
copyText(lines.join('\n'), 'Results copied');
};
$('#link').onclick = () => { const h = '#' + cur + '?' + new URLSearchParams(vals).toString(); copyText(STATIC ? STATIC.pageUrl + h : location.href, 'Link copied (opens this calculation with these inputs)'); };
$('#reset').onclick = () => { select(cur); toast('Defaults restored'); };
let pushT = null; const pushSoon = () => { clearTimeout(pushT); pushT = setTimeout(pushState, 500); };
function pushState() { if (STATIC) return; fetch('ui/state', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ calc: cur, inputs: vals }) }).catch(() => {}); }
// settings
function renderSettings() {
$('#sets').innerHTML = Object.entries(spec).map(([k, s]) => `<div class="set" title="${esc(s.label)}"><span>${esc(s.label)}</span>` +
(s.type === 'enum' ? `<select data-s="${k}">${s.values.map(v => `<option${v === settings[k] ? ' selected' : ''}>${v}</option>`).join('')}</select>`
: s.type === 'bool' ? `<select data-s="${k}"><option${settings[k] ? ' selected' : ''}>on</option><option${!settings[k] ? ' selected' : ''}>off</option></select>`
: `<input data-s="${k}" value="${settings[k]}">`) + `</div>`).join('');
}
$('#sets').addEventListener('change', async e => {
const k = e.target.dataset.s; if (!k) return; let v = e.target.value;
if (spec[k].type === 'bool') v = v === 'on'; if (spec[k].type === 'number') v = Number(v);
if (STATIC) { settings[k] = v; toast('Applied (this page only)'); compute(); return; }
const r = await (await fetch('settings', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ [k]: v, aiThread: 'ui (human)' }) })).json();
if (r.ok) { settings = r.settings; toast('Saved'); compute(); } else { toast(r.error); renderSettings(); }
});
$('#gear').onclick = () => { renderSettings(); $('#modal').classList.add('on'); };
$('#close').onclick = () => $('#modal').classList.remove('on');
$('#modal').onclick = e => { if (e.target.id === 'modal') $('#modal').classList.remove('on'); };
(async () => {
if (STATIC) {
spec = Object.fromEntries(Object.entries(H.SETTINGS_SPEC).filter(([, s]) => s.ui));
settings = Object.fromEntries(Object.entries(spec).map(([k, s]) => [k, s.def]));
$('#ver').textContent = 'v' + STATIC.version; $('#sver').textContent = `Hardware Calculator ${STATIC.version} · live from the page repo${STATIC.commit ? ' @ ' + STATIC.commit.slice(0, 7) : ''} · settings last for this visit`;
if (!openHash(STATIC.hash)) select('crystal');
addEventListener('message', e => { const m = e.data || {}; if (m.type === 'hwcalc-open' && m.hash) openHash(m.hash); });
return;
}
try {
const s = await (await fetch('settings')).json(); settings = s.settings; spec = s.spec;
const st = await (await fetch('state')).json(); seenRev = st.openRev || 0; $('#ver').textContent = 'v' + st.version; $('#sver').textContent = `hwcalc ${st.version} · port ${st.port}`;
const hs = location.hash.slice(1); const [hc, hq] = hs.split('?');
if (hc && CAT.find(c => c.id === hc)) { usedHash = true; select(hc, Object.fromEntries(new URLSearchParams(hq || ''))); }
else select(st.ui.calc || 'divider', st.ui.inputs);
} catch { select('divider'); }
// backstop: an AI 'open' must land even if the SSE stream through the proxy drops or buffers
setInterval(async () => { try { const st = await (await fetch('state', { cache: 'no-store' })).json();
if ((st.openRev || 0) > seenRev) { seenRev = st.openRev; select(st.ui.calc, st.ui.inputs); toast(`Opened by ${st.openBy}`); } } catch {} }, 2500);
const es = new EventSource('events');
es.onmessage = e => { const m = JSON.parse(e.data);
if (m.type === 'hello' && !usedHash && m.ui && m.ui.calc && (m.ui.calc !== cur || JSON.stringify(m.ui.inputs || {}) !== JSON.stringify(vals))) select(m.ui.calc, m.ui.inputs);
if (m.type === 'open') { seenRev = Math.max(seenRev, m.rev || 0); select(m.ui.calc, m.ui.inputs); toast(`Opened by ${m.by}`); }
if (m.type === 'settings') { settings = m.settings; compute(); } };
})();
</script>
</body></html>