7212d95Hardening and softening tutorialJeremy Magland 1// The slide deck: the 1920 x 1080 slides scaled to fit the window, navigation
2// (buttons with one-word hints, arrow keys, Home / End, a table of contents),
3// the current slide in the URL hash, KaTeX equations, solution and details
4// reveals, the Flatiron logo and the "Crafted by a human" link.
5//
6// sims: the interactive content on each slide, by slide id. Each has draw()
7// and pause(), and setLabels(labels) on slides that reveal details.
8// revealLabels: the labels to set on revealing details, by slide id.
10import { setDeckScale } from './canvas.js';
12const SLIDE_W = 1920;
13const SLIDE_H = 1080;
14const MARGIN = 16;
16export function initDeck({ sims = {}, revealLabels = {} } = {}) {
17 const pages = [...document.querySelectorAll('.page')];
18 const pageIds = pages.map((p) => p.id);
19 const navPrev = document.getElementById('nav-prev');
20 const navNext = document.getElementById('nav-next');
21 const navPos = document.getElementById('nav-pos');
23 // Flatiron logo on every slide.
24 pages.forEach((p) => {
25 const img = document.createElement('img');
26 img.className = 'logo';
27 img.alt = 'Flatiron Institute';
28 img.src = 'assets/flatiron-logo.png';
29 p.appendChild(img);
30 });
32 // Table of contents: the slide counter in the nav bar opens a list of slide
33 // titles; T toggles it and Escape closes it.
34 const toc = document.getElementById('toc');
35 const tocList = document.getElementById('toc-list');
37 pages.forEach((p) => {
38 const li = document.createElement('li');
39 const b = document.createElement('button');
40 b.textContent = p.querySelector('h1, h2').textContent;
41 b.addEventListener('click', () => {
42 location.hash = p.id;
43 setTocOpen(false);
44 });
45 li.appendChild(b);
46 tocList.appendChild(li);
47 });
49 function setTocOpen(open) {
50 toc.hidden = !open;
51 navPos.setAttribute('aria-expanded', String(open));
52 if (open) tocList.querySelector('.current')?.scrollIntoView({ block: 'nearest' });
53 }
55 navPos.addEventListener('click', (e) => {
56 e.stopPropagation();
57 setTocOpen(toc.hidden);
58 });
59 document.addEventListener('click', (e) => {
60 if (!toc.hidden && !toc.contains(e.target)) setTocOpen(false);
61 });
63 function show(id) {
64 pages.forEach((p) => p.classList.toggle('active', p.id === id));
65 const i = pageIds.indexOf(id);
66 const prev = pages[i - 1];
67 const next = pages[i + 1];
68 // label each button with a one-word hint of where it goes
69 navPrev.textContent = prev ? `← ${prev.dataset.hint}` : '←';
70 navNext.textContent = next ? `${next.dataset.hint} →` : '→';
71 navPrev.disabled = !prev;
72 navNext.disabled = !next;
73 navPos.textContent = `${i + 1} / ${pages.length} ☰`;
74 tocList.querySelectorAll('button').forEach((b, j) => b.classList.toggle('current', j === i));
75 for (const [name, sim] of Object.entries(sims)) {
76 if (name === id) sim.draw();
77 else sim.pause();
78 }
79 }
81 // Scale the deck to the largest size that fits, centered, and redraw the
82 // visible diagram at the new resolution.
83 const stage = document.querySelector('main');
84 const deck = document.getElementById('deck');
86 function fitDeck() {
87 stage.style.bottom = `${document.querySelector('.pager').offsetHeight}px`;
88 const w = stage.clientWidth;
89 const h = stage.clientHeight;
90 const scale = Math.min((w - 2 * MARGIN) / SLIDE_W, (h - 2 * MARGIN) / SLIDE_H);
91 setDeckScale(scale);
92 deck.style.transform = `scale(${scale})`;
93 deck.style.left = `${(w - SLIDE_W * scale) / 2}px`;
94 deck.style.top = `${(h - SLIDE_H * scale) / 2}px`;
95 const active = document.querySelector('.page.active');
96 if (active) sims[active.id]?.draw();
97 }
99 window.addEventListener('resize', fitDeck);
100 // Canvas text uses the bundled fonts, so redraw once they have loaded.
101 document.fonts?.ready.then(fitDeck);
103 document.querySelectorAll('[data-tex]').forEach((el) => {
104 katex.render(el.dataset.tex, el, { displayMode: el.tagName === 'DIV' });
105 });
107 document.querySelectorAll('[data-solution]').forEach((toggle) => {
108 toggle.addEventListener('click', () => {
109 const solution = document.getElementById(toggle.dataset.solution);
110 solution.hidden = !solution.hidden;
111 toggle.textContent = solution.hidden ? 'Show solution' : 'Hide solution';
112 });
113 });
115 // Slides that start centered: revealing splits them into text and diagram
116 // panels and labels the diagram.
117 document.querySelectorAll('[data-reveal]').forEach((button) => {
118 button.addEventListener('click', () => {
119 const id = button.dataset.reveal;
120 const section = document.getElementById(id);
121 button.hidden = true;
122 section.classList.add('split');
123 section.querySelector('.text').hidden = false;
124 sims[id]?.setLabels?.(revealLabels[id]);
125 });
126 });
128 // Replay of how recipe.md was written, from app/crafted-link.js (npm run crafted-link).
129 const link = document.getElementById('crafted-link');
130 if (window.CRAFTED_LINK && link) {
131 link.href = window.CRAFTED_LINK;
132 link.hidden = false;
133 }
135 // The current slide lives in the URL hash, so reloading stays put.
136 function showFromHash() {
137 const id = location.hash.slice(1);
138 show(document.getElementById(id)?.classList.contains('page') ? id : pageIds[0]);
139 window.scrollTo(0, 0);
140 }
142 window.addEventListener('hashchange', showFromHash);
143 fitDeck();
144 showFromHash();
146 document.querySelectorAll('[data-goto]').forEach((el) => {
147 el.addEventListener('click', () => {
148 location.hash = el.dataset.goto;
149 });
150 });
152 function goToIndex(i) {
153 location.hash = pageIds[Math.max(0, Math.min(pageIds.length - 1, i))];
154 }
156 function currentIndex() {
157 return pageIds.indexOf(document.querySelector('.page.active').id);
158 }
160 navPrev.addEventListener('click', () => goToIndex(currentIndex() - 1));
161 navNext.addEventListener('click', () => goToIndex(currentIndex() + 1));
163 document.addEventListener('keydown', (e) => {
164 if (e.altKey || e.ctrlKey || e.metaKey) return;
165 if (e.target.closest?.('input, select, textarea')) return; // arrow keys belong to the control
166 if (e.key === 't' || e.key === 'T') {
167 setTocOpen(toc.hidden);
168 return;
169 }
170 if (e.key === 'Escape' && !toc.hidden) {
171 setTocOpen(false);
172 return;
173 }
174 const target = {
175 ArrowLeft: currentIndex() - 1,
176 ArrowRight: currentIndex() + 1,
177 Home: 0,
178 End: pageIds.length - 1,
179 }[e.key];
180 if (target === undefined) return;
181 e.preventDefault();
182 goToIndex(target);
183 });
184}