|
6 | 6 | { id: "welcome", shortTitle: "Welcome" }, |
7 | 7 | ...content.lessons.map((lesson) => ({ id: lesson.id, shortTitle: lesson.shortTitle })), |
8 | 8 | ]; |
| 9 | + const appScriptUrl = new URL(document.currentScript.src); |
| 10 | + const basePath = appScriptUrl.pathname.slice(0, appScriptUrl.pathname.lastIndexOf("/") + 1); |
9 | 11 |
|
10 | 12 | const blankState = () => ({ |
11 | 13 | currentStep: 0, |
|
20 | 22 | const screen = document.querySelector("#screen"); |
21 | 23 | const navigation = document.querySelector("#step-navigation"); |
22 | 24 | const resetButton = document.querySelector("#reset-button"); |
| 25 | + const brandLink = document.querySelector(".brand"); |
| 26 | + |
| 27 | + function stepIndexFromPath() { |
| 28 | + const relativePath = window.location.pathname.startsWith(basePath) |
| 29 | + ? window.location.pathname.slice(basePath.length) |
| 30 | + : ""; |
| 31 | + const route = decodeURIComponent(relativePath).replace(/^\/+|\/+$/g, ""); |
| 32 | + |
| 33 | + if (!route || route === "index.html" || route === "404.html") return 0; |
| 34 | + return steps.findIndex((step) => step.id === route); |
| 35 | + } |
| 36 | + |
| 37 | + function pathForStep(index) { |
| 38 | + const step = steps[index]; |
| 39 | + return step.id === "welcome" ? basePath : `${basePath}${encodeURIComponent(step.id)}`; |
| 40 | + } |
| 41 | + |
| 42 | + function updatePath(index, mode = "push") { |
| 43 | + const path = pathForStep(index); |
| 44 | + if (window.location.pathname === path) return; |
| 45 | + window.history[`${mode}State`]({ step: steps[index].id }, "", path); |
| 46 | + } |
23 | 47 |
|
24 | 48 | function escapeHtml(value) { |
25 | 49 | return String(value) |
|
33 | 57 | function render() { |
34 | 58 | renderNavigation(); |
35 | 59 | const step = steps[state.currentStep]; |
| 60 | + document.title = step.id === "welcome" ? content.meta.title : `${step.shortTitle} · ${content.meta.title}`; |
36 | 61 | if (step.id === "welcome") renderWelcome(); |
37 | 62 | else renderLesson(content.lessons.find((lesson) => lesson.id === step.id)); |
38 | 63 | updateProgress(); |
|
350 | 375 | if (!state.completed.includes(stepId)) state.completed.push(stepId); |
351 | 376 | } |
352 | 377 |
|
353 | | - function goToStep(index) { |
| 378 | + function goToStep(index, historyMode = "push") { |
354 | 379 | state.currentStep = Math.max(0, Math.min(index, steps.length - 1)); |
| 380 | + updatePath(state.currentStep, historyMode); |
355 | 381 | render(); |
356 | 382 | document.querySelector("#tutorial-main").focus({ preventScroll: true }); |
357 | 383 | } |
358 | 384 |
|
359 | 385 | resetButton.addEventListener("click", () => { |
360 | 386 | if (!window.confirm("Reset this tutorial session, including edited code and answers?")) return; |
361 | 387 | state = blankState(); |
| 388 | + goToStep(0, "replace"); |
| 389 | + }); |
| 390 | + |
| 391 | + brandLink.addEventListener("click", (event) => { |
| 392 | + event.preventDefault(); |
| 393 | + goToStep(0); |
| 394 | + }); |
| 395 | + |
| 396 | + window.addEventListener("popstate", () => { |
| 397 | + const routeIndex = stepIndexFromPath(); |
| 398 | + state.currentStep = routeIndex >= 0 ? routeIndex : 0; |
362 | 399 | render(); |
| 400 | + document.querySelector("#tutorial-main").focus({ preventScroll: true }); |
363 | 401 | }); |
364 | 402 |
|
| 403 | + const initialRouteIndex = stepIndexFromPath(); |
| 404 | + state.currentStep = initialRouteIndex >= 0 ? initialRouteIndex : 0; |
| 405 | + if (initialRouteIndex < 0) updatePath(0, "replace"); |
365 | 406 | render(); |
366 | 407 | })(); |
0 commit comments