From baa691fa03dcfe78772e2b80ef512ddb62ae23f8 Mon Sep 17 00:00:00 2001 From: remi Date: Sat, 11 Jun 2022 00:47:43 +0200 Subject: [PATCH] history popstate stuff. lil messy implementation, but idfk how to clean up,,,, --- main.js | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/main.js b/main.js index 5bba123..abb5e99 100644 --- a/main.js +++ b/main.js @@ -59,7 +59,7 @@ document.addEventListener("keydown", (e) => { //if search box not focused, and if we're not on the home page, go back to home page if (!header.querySelector("input").matches(":focus") && !header.classList.contains("fullscreen")) { e.preventDefault(); - animateHeader(true); + goHome(); } else if (header.querySelector("input").matches(":focus") && ime != null) { toggleIME(); } else if (header.querySelector("input").matches(":focus")) { @@ -234,7 +234,7 @@ function search(word) { return result; } -function doSearch() { +function doSearch(state = true) { let val = header.querySelector("input").value; let results = search(val); if (results.length == 0) { @@ -323,6 +323,16 @@ function doSearch() { } main.prepend(header); } + const url = new URL(window.location); + url.searchParams.set('s', val); + if (state == true) history.pushState(val, "", url); +} + +function goHome(state = true) { + animateHeader(true); + const url = new URL(window.location); + url.searchParams.delete('s'); + if (state == true) history.pushState(null, "", url); } window.onload = () => { @@ -331,10 +341,24 @@ window.onload = () => { header.querySelector("#search button").onclick = () => {toggleIME();}; header.querySelector("span.heonian").onclick = () => { if (!header.classList.contains("fullscreen")) { - animateHeader(true); + goHome(); } }; loadDictionary(); + window.addEventListener('popstate', (e) => { + if (e.state == null) { + if (!header.classList.contains("fullscreen")) { + goHome(false); + } + } else { + if (header.classList.contains("fullscreen")) { + animateHeader(false); + } + header.querySelector("input").value = e.state; + doSearch(false); + } + console.log("location: " + document.location + ", state: " + JSON.stringify(e.state)); + }); } const sortObject = obj => Object.keys(obj).sort().reduce((res, key) => (res[key] = obj[key], res), {}) \ No newline at end of file