|
30 | 30 | const categoryLabels = data.categories || {}; |
31 | 31 | const repo = data.site?.repo || "https://github.com/TMHSDigital/Free-Game-Dev-Assets"; |
32 | 32 | const byId = Object.fromEntries(data.entries.map((e) => [e.id, e])); |
| 33 | + |
| 34 | + // Old permalinks (#entry-<id>) now have real pages. |
| 35 | + const legacy = location.hash.match(/^#entry-(.+)$/); |
| 36 | + if (legacy && byId[decodeURIComponent(legacy[1])]) { |
| 37 | + location.replace(`entry/${encodeURIComponent(decodeURIComponent(legacy[1]))}/`); |
| 38 | + return; |
| 39 | + } |
33 | 40 | const PERSPECTIVE_LABELS = { |
34 | 41 | top_down: "top-down", |
35 | 42 | isometric_3_4: "3/4 view", |
|
195 | 202 | <span class="entry-edge" aria-hidden="true"></span> |
196 | 203 | <div class="entry-body"> |
197 | 204 | <div class="entry-top"> |
198 | | - <h3><a class="entry-link" href="#entry-${escapeHtml(entry.id)}" data-id="${escapeHtml(entry.id)}">${escapeHtml(entry.name)}</a></h3> |
| 205 | + <h3><a class="entry-link" href="entry/${escapeHtml(entry.id)}/" data-id="${escapeHtml(entry.id)}">${escapeHtml(entry.name)}</a></h3> |
199 | 206 | <span class="entry-flags">${flags}</span> |
200 | 207 | </div> |
201 | 208 | <p>${escapeHtml(entry.summary || "")}</p> |
|
322 | 329 | grid.innerHTML = ""; |
323 | 330 | empty.hidden = false; |
324 | 331 | groupHeadings = []; |
325 | | - displayOrder = []; |
326 | 332 | return; |
327 | 333 | } |
328 | 334 | empty.hidden = true; |
329 | 335 |
|
330 | 336 | if (!shouldGroup()) { |
331 | 337 | grid.innerHTML = list.map(cardHtml).join(""); |
332 | 338 | groupHeadings = []; |
333 | | - displayOrder = list; |
334 | 339 | syncSpy(); |
335 | 340 | return; |
336 | 341 | } |
337 | 342 |
|
338 | 343 | const out = []; |
339 | | - displayOrder = []; |
340 | 344 | for (const cat of Object.keys(categoryLabels)) { |
341 | 345 | const group = list.filter((e) => e.category === cat); |
342 | 346 | if (!group.length) continue; |
343 | | - displayOrder.push(...group); |
344 | 347 | const label = categoryLabels[cat]?.label || cat; |
345 | 348 | out.push( |
346 | 349 | `<h3 class="group-heading" id="group-${escapeHtml(cat)}" data-cat="${escapeHtml(cat)}">` + |
|
419 | 422 | if (!entry || !byId[entry.id]) return ""; |
420 | 423 | return `<tr> |
421 | 424 | <td class="need">${escapeHtml(entry.need || "")}</td> |
422 | | - <td><a href="${escapeHtml(entry.url)}" rel="noopener noreferrer">${escapeHtml(entry.name)}</a></td> |
| 425 | + <td><a href="entry/${escapeHtml(entry.id)}/">${escapeHtml(entry.name)}</a></td> |
423 | 426 | <td class="license">${escapeHtml(entry.license)} · ${escapeHtml(commercialLabel(entry.commercial))}</td> |
424 | 427 | </tr>`; |
425 | 428 | }) |
|
435 | 438 | .join(""); |
436 | 439 | } |
437 | 440 |
|
438 | | - /* ------------------------------------------------------------- dialog */ |
| 441 | + /* ------------------------------------------------------ scroll memory */ |
439 | 442 |
|
440 | | - let lastTrigger = null; |
441 | | - let currentEntryId = null; |
442 | | - /** Entries in the order they are on screen, so prev/next matches the list. */ |
443 | | - let displayOrder = []; |
| 443 | + // Back from an entry page should land where you were, even when the |
| 444 | + // browser reloads this page instead of restoring it from its cache. |
| 445 | + const scrollKey = () => `fgda:scroll:${location.pathname}${location.search}`; |
444 | 446 |
|
445 | | - function stepEntry(delta) { |
446 | | - const i = displayOrder.findIndex((e) => e.id === currentEntryId); |
447 | | - if (i === -1) return; |
448 | | - const next = displayOrder[i + delta]; |
449 | | - if (next) openEntry(next.id, null, delta); |
450 | | - } |
451 | | - |
452 | | - /** `stepping` is the direction (1 or -1) when paging, otherwise false. */ |
453 | | - function openEntry(id, trigger, stepping = false) { |
454 | | - const entry = byId[id]; |
455 | | - if (!entry) return; |
456 | | - if (!stepping) lastTrigger = trigger || null; |
457 | | - currentEntryId = entry.id; |
458 | | - const pos = displayOrder.findIndex((e) => e.id === entry.id); |
459 | | - const prev = pos > 0 ? displayOrder[pos - 1] : null; |
460 | | - const next = pos !== -1 && pos < displayOrder.length - 1 ? displayOrder[pos + 1] : null; |
461 | | - // An entry opened from a permalink can be filtered out of the current |
462 | | - // list; there is no honest "next" for it, so the pager is omitted. |
463 | | - const pager = |
464 | | - pos === -1 |
465 | | - ? "" |
466 | | - : `<nav class="dialog-pager" aria-label="Browse entries"> |
467 | | - <button type="button" class="btn-ghost" data-step="-1"${prev ? ` title="${escapeHtml(prev.name)}"` : " disabled"}><span aria-hidden="true">←</span> Previous</button> |
468 | | - <span class="dialog-position">${pos + 1} of ${displayOrder.length}</span> |
469 | | - <button type="button" class="btn-ghost" data-step="1"${next ? ` title="${escapeHtml(next.name)}"` : " disabled"}>Next <span aria-hidden="true">→</span></button> |
470 | | - </nav>`; |
471 | | - const cat = categoryLabels[entry.category]?.label || entry.category; |
472 | | - const age = verifiedAge(entry.verified); |
473 | | - const attribution = entry.attribution_string |
474 | | - ? `<div class="attribution"> |
475 | | - <p class="attribution-label">Credit line</p> |
476 | | - <p class="attribution-string" id="attribution-string">${escapeHtml(entry.attribution_string)}</p> |
477 | | - <button type="button" class="btn-ghost" id="copy-attribution" data-copy="${escapeHtml(entry.attribution_string)}">Copy credit line</button> |
478 | | - <span class="copy-status" id="copy-status" role="status" aria-live="polite"></span> |
479 | | - </div>` |
480 | | - : entry.attribution_required === true |
481 | | - ? `<p class="attribution-note">Attribution is required and no canned credit line is recorded. Read the entry for what the source asks for.</p>` |
482 | | - : ""; |
483 | | - |
484 | | - $("#dialog-body").innerHTML = ` |
485 | | - <h2 id="dialog-title">${escapeHtml(entry.name)}</h2> |
486 | | - <p class="dialog-flags">${escapeHtml(entry.status)} · ${escapeHtml(commercialLabel(entry.commercial))} · ${escapeHtml(entry.license)}</p> |
487 | | - <p class="lead">${escapeHtml(entry.summary || "")}</p> |
488 | | - <dl class="dialog-meta"> |
489 | | - <div><dt>Category</dt><dd>${escapeHtml(cat)}</dd></div> |
490 | | - ${entry.publisher ? `<div><dt>Publisher</dt><dd>${escapeHtml(entry.publisher)}</dd></div>` : ""} |
491 | | - ${entry.license_spdx ? `<div><dt>SPDX</dt><dd>${escapeHtml(entry.license_spdx)}</dd></div>` : ""} |
492 | | - <div><dt>Attribution</dt><dd>${escapeHtml(String(entry.attribution_required))}</dd></div> |
493 | | - ${entry.camera_perspective ? `<div><dt>Perspective</dt><dd>${escapeHtml(PERSPECTIVE_LABELS[entry.camera_perspective] || entry.camera_perspective)}</dd></div>` : ""} |
494 | | - ${entry.grid_dimensions ? `<div><dt>Grid</dt><dd>${escapeHtml(entry.grid_dimensions)}</dd></div>` : ""} |
495 | | - <div><dt>Formats</dt><dd>${escapeHtml((entry.formats || []).join(", ") || "—")}</dd></div> |
496 | | - <div><dt>Tags</dt><dd>${escapeHtml((entry.tags || []).join(", ") || "—")}</dd></div> |
497 | | - <div><dt>Verified</dt><dd class="verified is-${age.bucket}">${escapeHtml(age.text)}</dd></div> |
498 | | - </dl> |
499 | | - ${attribution} |
500 | | - <div class="dialog-actions"> |
501 | | - <a class="btn" href="${escapeHtml(entry.url)}" rel="noopener noreferrer">Open source</a> |
502 | | - <a class="btn-ghost" href="${escapeHtml(`${repo}/blob/main/${entry.path}`)}" rel="noopener noreferrer">Entry and evidence</a> |
503 | | - </div> |
504 | | - ${pager}`; |
505 | | - |
506 | | - const copy = $("#copy-attribution"); |
507 | | - if (copy) { |
508 | | - copy.addEventListener("click", async () => { |
509 | | - const status = $("#copy-status"); |
510 | | - try { |
511 | | - await navigator.clipboard.writeText(copy.getAttribute("data-copy")); |
512 | | - status.textContent = "Copied"; |
513 | | - } catch { |
514 | | - // Clipboard access can be refused; select the text so it can be |
515 | | - // copied by hand rather than failing silently. |
516 | | - const range = document.createRange(); |
517 | | - range.selectNodeContents($("#attribution-string")); |
518 | | - const sel = getSelection(); |
519 | | - sel.removeAllRanges(); |
520 | | - sel.addRange(range); |
521 | | - status.textContent = "Select and copy"; |
522 | | - } |
523 | | - }); |
| 447 | + function rememberScroll() { |
| 448 | + try { |
| 449 | + sessionStorage.setItem(scrollKey(), String(Math.round(window.scrollY))); |
| 450 | + } catch { |
| 451 | + // Storage can be unavailable; scrolling back is a convenience. |
524 | 452 | } |
| 453 | + } |
525 | 454 |
|
526 | | - history.replaceState(null, "", `${location.pathname}${location.search}#entry-${entry.id}`); |
527 | | - const dialog = $("#entry-dialog"); |
528 | | - // showModal() throws on a dialog that is already open, which is exactly |
529 | | - // the state stepping leaves it in. |
530 | | - if (!dialog.open) dialog.showModal(); |
531 | | - if (stepping) { |
532 | | - // Keep focus on the pager so repeated Enter keeps paging; at the end of |
533 | | - // the list the same-direction button is disabled, so fall back to the |
534 | | - // other one rather than dropping focus to the document. |
535 | | - const same = $(`#dialog-body [data-step="${stepping}"]`); |
536 | | - const other = $(`#dialog-body [data-step="${-stepping}"]`); |
537 | | - const target = same && !same.disabled ? same : other; |
538 | | - if (target) target.focus(); |
| 455 | + function restoreScroll() { |
| 456 | + let y = null; |
| 457 | + try { |
| 458 | + y = sessionStorage.getItem(scrollKey()); |
| 459 | + sessionStorage.removeItem(scrollKey()); |
| 460 | + } catch { |
| 461 | + return; |
539 | 462 | } |
| 463 | + if (y !== null) requestAnimationFrame(() => window.scrollTo(0, Number(y))); |
540 | 464 | } |
541 | 465 |
|
542 | | - function bindDialog() { |
543 | | - const dialog = $("#entry-dialog"); |
544 | | - dialog.addEventListener("close", () => { |
545 | | - history.replaceState(null, "", `${location.pathname}${location.search}`); |
546 | | - // After paging, return to the entry you ended on, not the one you |
547 | | - // opened; otherwise closing throws you back up the list. |
548 | | - const endedOn = currentEntryId && document.querySelector(`#entry-${CSS.escape(currentEntryId)} .entry-link`); |
549 | | - const target = endedOn || (lastTrigger && document.contains(lastTrigger) ? lastTrigger : null); |
550 | | - if (target) { |
551 | | - target.closest(".entry-card")?.scrollIntoView({ block: "nearest" }); |
552 | | - target.focus(); |
553 | | - } |
554 | | - lastTrigger = null; |
555 | | - currentEntryId = null; |
556 | | - }); |
557 | | - dialog.addEventListener("click", (e) => { |
558 | | - // Clicking the backdrop closes, matching the Escape affordance. |
559 | | - if (e.target === dialog) { |
560 | | - dialog.close(); |
561 | | - return; |
562 | | - } |
563 | | - const step = e.target.closest("[data-step]"); |
564 | | - if (step && !step.disabled) stepEntry(Number(step.getAttribute("data-step"))); |
565 | | - }); |
566 | | - dialog.addEventListener("keydown", (e) => { |
567 | | - if (e.key === "ArrowRight") { |
568 | | - e.preventDefault(); |
569 | | - stepEntry(1); |
570 | | - } else if (e.key === "ArrowLeft") { |
571 | | - e.preventDefault(); |
572 | | - stepEntry(-1); |
573 | | - } |
574 | | - }); |
575 | | - } |
| 466 | + // Restored from the back/forward cache: the page kept its own position, |
| 467 | + // so drop the saved one before a later visit can reuse it. |
| 468 | + window.addEventListener("pageshow", (e) => { |
| 469 | + if (!e.persisted) return; |
| 470 | + try { |
| 471 | + sessionStorage.removeItem(scrollKey()); |
| 472 | + } catch { |
| 473 | + // Nothing to clear. |
| 474 | + } |
| 475 | + }); |
576 | 476 |
|
577 | 477 | /* ----------------------------------------------------------- controls */ |
578 | 478 |
|
|
614 | 514 | apply(); |
615 | 515 | }); |
616 | 516 |
|
| 517 | + // The whole card is the title link (a stretched ::after), so every click |
| 518 | + // that leaves for an entry page lands here. |
617 | 519 | $("#entry-grid").addEventListener("click", (e) => { |
618 | | - const link = e.target.closest(".entry-link"); |
619 | | - if (!link) return; |
620 | | - e.preventDefault(); |
621 | | - openEntry(link.getAttribute("data-id"), link); |
| 520 | + if (e.target.closest("a.entry-link")) rememberScroll(); |
622 | 521 | }); |
623 | 522 |
|
624 | 523 | $("#active-filters").addEventListener("click", (e) => { |
|
651 | 550 | window.addEventListener("resize", onScroll, { passive: true }); |
652 | 551 |
|
653 | 552 | // "/" focuses search, Escape leaves it. Suppressed whenever the user is |
654 | | - // already typing somewhere or the entry dialog has focus. |
| 553 | + // already typing somewhere. |
655 | 554 | document.addEventListener("keydown", (e) => { |
656 | 555 | const el = document.activeElement; |
657 | 556 | const typing = |
|
660 | 559 | el.tagName === "TEXTAREA" || |
661 | 560 | el.tagName === "SELECT" || |
662 | 561 | el.isContentEditable); |
663 | | - if (e.key === "/" && !typing && !$("#entry-dialog").open) { |
| 562 | + if (e.key === "/" && !typing) { |
664 | 563 | e.preventDefault(); |
665 | 564 | search.focus(); |
666 | 565 | search.select(); |
|
695 | 594 | renderStarters(); |
696 | 595 | renderGuides(); |
697 | 596 | bindControls(); |
698 | | - bindDialog(); |
699 | 597 | syncControls(); |
700 | 598 | apply(); |
| 599 | + restoreScroll(); |
701 | 600 |
|
702 | | - // A shared link may point at one entry. Open it once the grid exists. |
703 | | - const hash = location.hash.match(/^#entry-(.+)$/); |
704 | | - if (hash && byId[hash[1]]) { |
705 | | - const card = document.getElementById(`entry-${hash[1]}`); |
706 | | - card?.scrollIntoView({ block: "center" }); |
707 | | - openEntry(hash[1], card?.querySelector(".entry-link") || null); |
708 | | - } |
709 | 601 | })(); |
0 commit comments