import { PageScope } from './page-scope'; import { mountEffects } from './effects'; import { mountNavigation } from './navigation'; import { mountPixelGrid } from './pixel-grid'; import { syncMotion } from './motion'; const enhancements = [ { selector: '[data-palette-tool]', name: 'palette tool', load: () => import('./palette-tool').then(({ mountPaletteTool }) => mountPaletteTool), failed: () => { const status = document.querySelector('[data-palette-tool] [data-status]'); if (status) { status.textContent = 'The palette tool could not load. Reload this page in a current browser.'; status.dataset.error = 'true'; } }, }, { selector: '.link-graph', name: 'link graph', load: () => import('./graph').then(({ mountGraphs }) => mountGraphs), failed: () => document.querySelectorAll('.graph-fallback').forEach((list) => { list.hidden = false; }), }, ]; let page: PageScope | undefined; document.addEventListener('astro:before-swap', () => { page?.dispose(); page = undefined; }); function mountPage() { if (page) return; page = new PageScope(); const scope = page; syncMotion(); mountPixelGrid(page); for (const { selector, name, load, failed } of enhancements) if (document.querySelector(selector)) void load() .then((mount) => { if (!scope.signal.aborted) mount(scope); }) .catch((error) => { if (scope.signal.aborted) return; console.error(`Could not load the ${name}.`, error); failed(); }); mountNavigation(page); mountEffects(page); } document.addEventListener('astro:after-swap', mountPage); document.addEventListener('astro:page-load', mountPage);