diff options
author | Adam Hovorka <[email protected]> | 2020-04-28 10:17:15 -0600 |
---|---|---|
committer | Adam Hovorka <[email protected]> | 2020-04-28 10:17:15 -0600 |
commit | e08cc78e72dfb0bc72db0a3aa362b86fa06d4851 (patch) | |
tree | 6884f51a9895fa296828563284f215f4e4a37a86 | |
parent | 946137054dfd23f2ae7ddf77a1cbd4a4b9486054 (diff) |
Add path word count alongside total word count
-rw-r--r-- | index.html | 3 | ||||
-rw-r--r-- | main.js | 20 |
2 files changed, 15 insertions, 8 deletions
@@ -22,7 +22,8 @@ </div> <div id="main"></div> <div id="stats"> - <div id="word-count">Word Count: <span></span></div> + <div id="total-word-count">Total Word Count: <span></span></div> + <div id="path-word-count">Path Word Count: <span></span></div> <div id="unreachable"></div> </div> @@ -16,10 +16,17 @@ let state = { path: "" }; -const wordCount = () => - document.querySelector("#word-count span").innerText = +const wordCount = () => { + document.querySelector("#total-word-count span").innerText = data.pages.reduce((a,p) => a + p.body.trim().split(/\s+/).length + p.choices.reduce((a,c) => a + c[1].trim().split(/\s+/).length, 0), 0); + const nodes = state.path.split("-").map((e,i,a) => a.slice(0,i).join("-")); + if (state.path) nodes.push(state.path); + document.querySelector("#path-word-count span").innerText = + nodes.reduce((a,p) => { const n = data.pages[data.paths[p]]; + return a + n.body.trim().split(/\s+/).length + + n.choices.reduce((a,c) => a + c[1].trim().split(/\s+/).length, 0)}, 0); +}; const db = new Dexie("vanguard-editor"); db.version(1).stores({files:"name"}); @@ -28,8 +35,8 @@ if (!/^#[0-9a-z.-]+\.json$/i.test(location.hash)) { db.files.get("main").then(d => { data = d.data; if (!location.hash || /^#\/?$/.test(location.hash)) location.hash = "#/"+data.current; - wordCount(); state.path = renderPath(); + wordCount(); setTimeout(() => document.documentElement.scrollTop = document.getElementById("main").lastChild.offsetTop); }).catch(e => console.error); @@ -60,8 +67,8 @@ document.getElementById("open-file").addEventListener("change", e => { data = JSON.parse(r.result); db.files.put({name:"main",data}); location.hash = "#/"+data.current; - wordCount(); state.path = renderPath(); + wordCount(); setTimeout(() => document.documentElement.scrollTop = document.getElementById("main").lastChild.offsetTop); } catch(e) { alert(e); @@ -74,8 +81,8 @@ function loadFile(path) { db.files.put({name:"main",data:(data=d)}); //location.hash = "#/"+data.current; location.hash = "#/"; - wordCount(); state.path = renderPath(); + wordCount(); //setTimeout(() => document.documentElement.scrollTop = // document.getElementById("main").lastChild.offsetTop); }).catch(e => { @@ -272,8 +279,6 @@ window.addEventListener("hashchange", () => { data.paths = newpaths; - wordCount(); - db.files.put({name:"main",data}); } @@ -289,6 +294,7 @@ window.addEventListener("hashchange", () => { } else document.documentElement.scrollTop = oldscroll; state.path = newpath; + wordCount(); } catch(e) { state.error = 1; |