diff --git a/src/gui/quicklinks.js b/src/gui/quicklinks.js index 6a3eacbc41091c8573c6e626d83b42ee63a88036..25d5589b0c11ade88829fc498109f522ea85349b 100644 --- a/src/gui/quicklinks.js +++ b/src/gui/quicklinks.js @@ -1,9 +1,14 @@ +/** + * Generate a list of links the player can safely jump to. + * + * @returns {DocumentFragment} + */ App.UI.quickMenu = (function() { // setup safe passages const jumpFrom = Story.lookup("tags", "jump-from-safe").map(passage => passage.title); const jumpTo = Story.lookup("tags", "jump-to-safe").map(passage => passage.title); - // setup hotkeys list + // setup hotkeys list, upper/lower case is important! const hotkeys = cleanHotkeys({ Main: "m", "Manage Personal Affairs": "x" @@ -24,14 +29,13 @@ App.UI.quickMenu = (function() { let history = []; let currentPassage; let historyNavigation = false; - $(document).on(':passageend', function() { - if (currentPassage === State.passage) { - // only reloaded page - return; - } - + $(document).on(':passageinit', event => { // if navigated here normally, add passage to history, otherwise remove last entry from history if (!historyNavigation) { + if (currentPassage === event.passage.title) { + // only reloaded passage + return; + } // if last passage can be jumped to add passage to history, otherwise clear history if (jumpTo.includes(currentPassage)) { history.push(currentPassage); @@ -42,15 +46,19 @@ App.UI.quickMenu = (function() { historyNavigation = false; history.shift(); } - currentPassage = State.passage; + currentPassage = event.passage.title; }); Mousetrap.bind("backspace", () => { // jump back in history + goBack(); + }); + + function goBack() { if (history.length > 0 && jumpFrom.includes(State.passage)) { historyNavigation = true; Engine.play(history[0]); } - }); + } function generateMenu() { if (!jumpFrom.includes(State.passage)) { @@ -60,6 +68,15 @@ App.UI.quickMenu = (function() { const fragment = document.createDocumentFragment(); + if (history.length > 0) { + const div = document.createElement("div"); + const a = document.createElement("a"); + a.append("Back"); + a.onclick = goBack; + div.append(a, " ", App.UI.DOM.makeElement("span", "[delete]", "hotkey")); + fragment.append(div); + } + for (let i = 0; i < jumpTo.length; i++) { if (jumpTo[i] !== State.passage) { fragment.append(generatePassageLink(jumpTo[i]));