// The slide deck: the 1920 x 1080 slides scaled to fit the window, navigation // (buttons with one-word hints, arrow keys, Home / End, a table of contents), // the current slide in the URL hash, KaTeX equations, solution and details // reveals, the Flatiron logo and the "Crafted by a human" link. // // sims: the interactive content on each slide, by slide id. Each has draw() // and pause(), and setLabels(labels) on slides that reveal details. // revealLabels: the labels to set on revealing details, by slide id. import { setDeckScale } from './canvas.js'; const SLIDE_W = 1920; const SLIDE_H = 1080; const MARGIN = 16; export function initDeck({ sims = {}, revealLabels = {} } = {}) { const pages = [...document.querySelectorAll('.page')]; const pageIds = pages.map((p) => p.id); const navPrev = document.getElementById('nav-prev'); const navNext = document.getElementById('nav-next'); const navPos = document.getElementById('nav-pos'); // Flatiron logo on every slide. pages.forEach((p) => { const img = document.createElement('img'); img.className = 'logo'; img.alt = 'Flatiron Institute'; img.src = 'assets/flatiron-logo.png'; p.appendChild(img); }); // Table of contents: the slide counter in the nav bar opens a list of slide // titles; T toggles it and Escape closes it. const toc = document.getElementById('toc'); const tocList = document.getElementById('toc-list'); pages.forEach((p) => { const li = document.createElement('li'); const b = document.createElement('button'); b.textContent = p.querySelector('h1, h2').textContent; b.addEventListener('click', () => { location.hash = p.id; setTocOpen(false); }); li.appendChild(b); tocList.appendChild(li); }); function setTocOpen(open) { toc.hidden = !open; navPos.setAttribute('aria-expanded', String(open)); if (open) tocList.querySelector('.current')?.scrollIntoView({ block: 'nearest' }); } navPos.addEventListener('click', (e) => { e.stopPropagation(); setTocOpen(toc.hidden); }); document.addEventListener('click', (e) => { if (!toc.hidden && !toc.contains(e.target)) setTocOpen(false); }); function show(id) { pages.forEach((p) => p.classList.toggle('active', p.id === id)); const i = pageIds.indexOf(id); const prev = pages[i - 1]; const next = pages[i + 1]; // label each button with a one-word hint of where it goes navPrev.textContent = prev ? `← ${prev.dataset.hint}` : '←'; navNext.textContent = next ? `${next.dataset.hint} →` : '→'; navPrev.disabled = !prev; navNext.disabled = !next; navPos.textContent = `${i + 1} / ${pages.length} ☰`; tocList.querySelectorAll('button').forEach((b, j) => b.classList.toggle('current', j === i)); for (const [name, sim] of Object.entries(sims)) { if (name === id) sim.draw(); else sim.pause(); } } // Scale the deck to the largest size that fits, centered, and redraw the // visible diagram at the new resolution. const stage = document.querySelector('main'); const deck = document.getElementById('deck'); function fitDeck() { stage.style.bottom = `${document.querySelector('.pager').offsetHeight}px`; const w = stage.clientWidth; const h = stage.clientHeight; const scale = Math.min((w - 2 * MARGIN) / SLIDE_W, (h - 2 * MARGIN) / SLIDE_H); setDeckScale(scale); deck.style.transform = `scale(${scale})`; deck.style.left = `${(w - SLIDE_W * scale) / 2}px`; deck.style.top = `${(h - SLIDE_H * scale) / 2}px`; const active = document.querySelector('.page.active'); if (active) sims[active.id]?.draw(); } window.addEventListener('resize', fitDeck); // Canvas text uses the bundled fonts, so redraw once they have loaded. document.fonts?.ready.then(fitDeck); document.querySelectorAll('[data-tex]').forEach((el) => { katex.render(el.dataset.tex, el, { displayMode: el.tagName === 'DIV' }); }); document.querySelectorAll('[data-solution]').forEach((toggle) => { toggle.addEventListener('click', () => { const solution = document.getElementById(toggle.dataset.solution); solution.hidden = !solution.hidden; toggle.textContent = solution.hidden ? 'Show solution' : 'Hide solution'; }); }); // Slides that start centered: revealing splits them into text and diagram // panels and labels the diagram. document.querySelectorAll('[data-reveal]').forEach((button) => { button.addEventListener('click', () => { const id = button.dataset.reveal; const section = document.getElementById(id); button.hidden = true; section.classList.add('split'); section.querySelector('.text').hidden = false; sims[id]?.setLabels?.(revealLabels[id]); }); }); // Replay of how recipe.md was written, from app/crafted-link.js (npm run crafted-link). const link = document.getElementById('crafted-link'); if (window.CRAFTED_LINK && link) { link.href = window.CRAFTED_LINK; link.hidden = false; } // The current slide lives in the URL hash, so reloading stays put. function showFromHash() { const id = location.hash.slice(1); show(document.getElementById(id)?.classList.contains('page') ? id : pageIds[0]); window.scrollTo(0, 0); } window.addEventListener('hashchange', showFromHash); fitDeck(); showFromHash(); document.querySelectorAll('[data-goto]').forEach((el) => { el.addEventListener('click', () => { location.hash = el.dataset.goto; }); }); function goToIndex(i) { location.hash = pageIds[Math.max(0, Math.min(pageIds.length - 1, i))]; } function currentIndex() { return pageIds.indexOf(document.querySelector('.page.active').id); } navPrev.addEventListener('click', () => goToIndex(currentIndex() - 1)); navNext.addEventListener('click', () => goToIndex(currentIndex() + 1)); document.addEventListener('keydown', (e) => { if (e.altKey || e.ctrlKey || e.metaKey) return; if (e.target.closest?.('input, select, textarea')) return; // arrow keys belong to the control if (e.key === 't' || e.key === 'T') { setTocOpen(toc.hidden); return; } if (e.key === 'Escape' && !toc.hidden) { setTocOpen(false); return; } const target = { ArrowLeft: currentIndex() - 1, ArrowRight: currentIndex() + 1, Home: 0, End: pageIds.length - 1, }[e.key]; if (target === undefined) return; e.preventDefault(); goToIndex(target); }); }