From 5b678799540ce003f53bdfa167b7d3cf6aaaa41a Mon Sep 17 00:00:00 2001 From: Nicolas Mattia Date: Fri, 31 Oct 2025 17:41:34 +0100 Subject: [PATCH] Add story restart --- src/bin/cyoa/main.js | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/bin/cyoa/main.js b/src/bin/cyoa/main.js index f872436..f8c7bf6 100644 --- a/src/bin/cyoa/main.js +++ b/src/bin/cyoa/main.js @@ -1,3 +1,16 @@ +// Restart the story by clearing session storage and reloading page +function restartStory() { + + try { + const key = "Saved Session"; // found in browser storage + sessionStorage.removeItem(key); + } catch (err) { + console.warn("Could not clear Harlowe session storage:", err); + } + location.reload(); + +} +// // allow iterating through passage links with space and selecting with enter document.body.onkeydown = function(e) { if (e.key == " " || @@ -7,6 +20,13 @@ document.body.onkeydown = function(e) { e.preventDefault(); const links = Array.from(document.querySelectorAll("tw-link")); + + // Reached the end, so restart + if(links.length === 0) { + restartStory(); + return; + } + const selected = document.querySelector(":focus") || links[0]; const ix = links.indexOf(selected); @@ -17,13 +37,6 @@ document.body.onkeydown = function(e) { // handle "r" for restart if (e.key === "r" || e.key === "R") { - e.preventDefault(); - try { - const key = "Saved Session"; // found in browser storage - sessionStorage.removeItem(key); - } catch (err) { - console.warn("Could not clear Harlowe session storage:", err); - } - location.reload(); + restartStory(); } }