45 lines
2.1 KiB
JavaScript
45 lines
2.1 KiB
JavaScript
// storebound.dev: copy buttons and the AI tool tabs. No network, no storage.
|
|
(() => {
|
|
const copyText = async text => {
|
|
try { await navigator.clipboard.writeText(text); return true; } catch {}
|
|
const ta = document.createElement('textarea');
|
|
ta.value = text; ta.setAttribute('readonly', ''); ta.style.position = 'fixed'; ta.style.opacity = '0';
|
|
document.body.appendChild(ta); ta.select();
|
|
let ok = false; try { ok = document.execCommand('copy'); } catch {}
|
|
ta.remove(); return ok;
|
|
};
|
|
for (const btn of document.querySelectorAll('.copy')) {
|
|
btn.addEventListener('click', async () => {
|
|
const target = btn.dataset.copyTarget && document.getElementById(btn.dataset.copyTarget);
|
|
// Drop "# comment" tails so the copied commands run as is.
|
|
const text = btn.dataset.copy ?? (target ? target.innerText.split('\n').map(l => l.replace(/\s+#.*$/, '')).join('\n').trim() : '');
|
|
const ok = await copyText(text);
|
|
btn.textContent = ok ? 'Copied' : 'Press Ctrl+C';
|
|
btn.classList.toggle('done', ok);
|
|
setTimeout(() => { btn.textContent = 'Copy'; btn.classList.remove('done'); }, 1600);
|
|
});
|
|
}
|
|
|
|
for (const root of document.querySelectorAll('[data-tabs]')) {
|
|
const tabs = [...root.querySelectorAll('[role="tab"]')];
|
|
const select = (tab, focus) => {
|
|
for (const t of tabs) {
|
|
const on = t === tab;
|
|
t.setAttribute('aria-selected', String(on));
|
|
t.tabIndex = on ? 0 : -1;
|
|
document.getElementById(t.getAttribute('aria-controls')).hidden = !on;
|
|
}
|
|
if (focus) { tab.focus(); tab.scrollIntoView({ block: 'nearest', inline: 'nearest' }); }
|
|
};
|
|
tabs.forEach((t, i) => {
|
|
t.addEventListener('click', () => select(t, false));
|
|
t.addEventListener('keydown', e => {
|
|
const k = { ArrowRight: i + 1, ArrowLeft: i - 1, Home: 0, End: tabs.length - 1 }[e.key];
|
|
if (k === undefined) return;
|
|
e.preventDefault();
|
|
select(tabs[(k + tabs.length) % tabs.length], true);
|
|
});
|
|
});
|
|
select(tabs.find(t => t.getAttribute('aria-selected') === 'true') ?? tabs[0], false);
|
|
}
|
|
})();
|