From e08cc78e72dfb0bc72db0a3aa362b86fa06d4851 Mon Sep 17 00:00:00 2001 From: Adam Hovorka Date: Tue, 28 Apr 2020 10:17:15 -0600 Subject: Add path word count alongside total word count --- index.html | 3 ++- main.js | 20 +++++++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/index.html b/index.html index 11566ac..8bf6371 100644 --- a/index.html +++ b/index.html @@ -22,7 +22,8 @@
-
Word Count:
+
Total Word Count:
+
Path Word Count:
diff --git a/main.js b/main.js index 83d1a6a..9285947 100644 --- a/main.js +++ b/main.js @@ -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; -- cgit v1.2.3-54-g00ecf