let pressed = null;
function chipOf(target) {
return target instanceof Element ? target.closest('a[data-zeyn-chip]') : null;
}
document.addEventListener('keydown', (e) => {
const chip = e.key === ' ' && !e.repeat ? chipOf(e.target) : null;
if (!chip) return;
e.preventDefault();
chip.click();
});
document.addEventListener(
'click',
(e) => {
const chip = chipOf(e.target);
if (!chip || e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) return;
pressed = { node: chip.closest('[data-zeyn-node]')?.dataset.zeynNode ?? '', value: chip.dataset.value ?? '', y: scrollY };
},
true,
);
document.addEventListener('zeyn:content', () => {
if (!pressed) return;
const { node, value, y } = pressed;
pressed = null;
const all = [...document.querySelectorAll('#z-main a[data-zeyn-chip]')].filter((c) => c.dataset.value === value);
const same = all.find((c) => c.closest('[data-zeyn-node]')?.dataset.zeynNode === node);
const chip = same ?? all[0];
if (!chip) return;
if (same) window.scrollTo(0, y);
chip.focus({ preventScroll: Boolean(same) });
});