aboutsummaryrefslogtreecommitdiff
path: root/main.js
diff options
context:
space:
mode:
Diffstat (limited to 'main.js')
-rw-r--r--main.js20
1 files changed, 13 insertions, 7 deletions
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;