aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Hovorka <[email protected]>2020-04-28 10:17:15 -0600
committerAdam Hovorka <[email protected]>2020-04-28 10:17:15 -0600
commite08cc78e72dfb0bc72db0a3aa362b86fa06d4851 (patch)
tree6884f51a9895fa296828563284f215f4e4a37a86
parent946137054dfd23f2ae7ddf77a1cbd4a4b9486054 (diff)
Add path word count alongside total word count
-rw-r--r--index.html3
-rw-r--r--main.js20
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 @@
</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>
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;