Something went wrong. Try again.
doll.systems community website
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111import { clamp } from '../lib/math';import type { PageScope } from './page-scope';import { navigate } from 'astro:transitions/client';import type { PageLink } from '../lib/layouts';import { updatePreview } from './page-preview';import { snapPixel } from './pixel-grid';
const previewLinks = '[data-preview], .reading-body a, .reading-entry h2 a';const previewPlacement = { inset: 10, edge: 16, x: 22, y: -30, hideDelay: 65 };export function mountNavigation(scope: PageScope) { const links: PageLink[] = JSON.parse( document.querySelector('#preview-data')?.textContent || '[]', ); const byId = new Map(links.map((link) => [link.id, link])); const byPath = new Map(links.map((link) => [link.href, link])); const preview = document.querySelector<HTMLElement>('.hover-preview')!; let active: Element | null = null; let hideTimer: ReturnType<typeof setTimeout>; scope.defer(() => clearTimeout(hideTimer)); const keyboardPreview = () => active === document.activeElement && active?.matches(':focus-visible');
function hidePreview() { clearTimeout(hideTimer); if (preview.classList.contains('visible')) { preview.classList.remove('visible'); document.dispatchEvent(new CustomEvent('preview:change', { detail: false })); } active = null; } function previewLink(target: Element): PageLink | undefined { const id = target.getAttribute('data-preview'); if (id !== null) return byId.get(id); if (target instanceof HTMLAnchorElement) { if (target.origin === location.origin) return byPath.get(target.pathname); } } function showPreview(target: Element, x: number, y: number) { const link = previewLink(target); if (!link) return; clearTimeout(hideTimer); updatePreview(preview, link); preview.style.left = `${snapPixel(clamp(x + previewPlacement.x, previewPlacement.inset, innerWidth - preview.offsetWidth - previewPlacement.edge))}px`; preview.style.top = `${snapPixel(clamp(y + previewPlacement.y, previewPlacement.inset, innerHeight - preview.offsetHeight - previewPlacement.edge))}px`; const wasVisible = preview.classList.contains('visible'); preview.classList.add('visible'); active = target; if (!wasVisible) document.dispatchEvent(new CustomEvent('preview:change', { detail: true })); } scope.on(document, 'pointerover', (event) => { if (event.pointerType === 'touch') return; const target = (event.target as Element).closest(previewLinks); if (target && target !== active) showPreview(target, event.clientX, event.clientY); }); scope.on(document, 'pointerout', (event) => { if ( !keyboardPreview() && active?.contains(event.target as Node) && !active.contains(event.relatedTarget as Node) ) hideTimer = setTimeout(hidePreview, previewPlacement.hideDelay); }); scope.on(document, 'focusin', (event) => { const target = (event.target as Element).closest(previewLinks); // Focus can scroll the link into view; position the preview after that scroll. if (target) requestAnimationFrame(() => { if (scope.signal.aborted || document.activeElement !== target) return; const rect = target.getBoundingClientRect(); showPreview(target, rect.right, rect.top); }); }); scope.on(document, 'focusout', hidePreview); scope.on(document, 'preview:hide', hidePreview); scope.on( window, 'scroll', () => { if (active && keyboardPreview()) { const rect = active.getBoundingClientRect(); if (rect.bottom > 0 && rect.top < innerHeight) { showPreview(active, rect.right, rect.top); return; } } hidePreview(); }, { passive: true, capture: true }, );
scope.on(document, 'keydown', (event) => { if (event.key === 'Escape') { hidePreview(); return; } const node = (event.target as Element).closest('.graph-node'); // SVG anchors do not activate consistently on Enter across browser engines. if ( node && (event.key === ' ' || event.key === 'Enter') && !event.metaKey && !event.ctrlKey && !event.altKey ) { event.preventDefault(); void navigate(node.getAttribute('href')!); return; } });}