From 946137054dfd23f2ae7ddf77a1cbd4a4b9486054 Mon Sep 17 00:00:00 2001 From: Adam Hovorka Date: Mon, 27 Apr 2020 00:24:34 -0600 Subject: Add stats / word count box --- main.js | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'main.js') diff --git a/main.js b/main.js index 9e27824..83d1a6a 100644 --- a/main.js +++ b/main.js @@ -16,6 +16,11 @@ let state = { path: "" }; +const wordCount = () => + document.querySelector("#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 db = new Dexie("vanguard-editor"); db.version(1).stores({files:"name"}); db.on("populate", () => db.files.add({name: "main", data})); @@ -23,6 +28,7 @@ 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(); setTimeout(() => document.documentElement.scrollTop = document.getElementById("main").lastChild.offsetTop); @@ -54,6 +60,7 @@ 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(); setTimeout(() => document.documentElement.scrollTop = document.getElementById("main").lastChild.offsetTop); @@ -67,6 +74,7 @@ function loadFile(path) { db.files.put({name:"main",data:(data=d)}); //location.hash = "#/"+data.current; location.hash = "#/"; + wordCount(); state.path = renderPath(); //setTimeout(() => document.documentElement.scrollTop = // document.getElementById("main").lastChild.offsetTop); @@ -264,6 +272,8 @@ window.addEventListener("hashchange", () => { data.paths = newpaths; + wordCount(); + db.files.put({name:"main",data}); } -- cgit v1.2.3-70-g09d2