diff options
Diffstat (limited to 'main.js')
-rw-r--r-- | main.js | 34 |
1 files changed, 27 insertions, 7 deletions
@@ -19,12 +19,15 @@ let state = { const db = new Dexie("vanguard-editor"); db.version(1).stores({files:"name"}); db.on("populate", () => db.files.add({name: "main", data})); -db.files.get("main").then(d => { data = d.data; - location.hash = "#/"+data.current; - state.path = renderPath(); - setTimeout(() => document.documentElement.scrollTop = - document.getElementById("main").lastChild.offsetTop); -}).catch(e => console.error); +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; + state.path = renderPath(); + setTimeout(() => document.documentElement.scrollTop = + document.getElementById("main").lastChild.offsetTop); + }).catch(e => console.error); +} else loadFile(location.hash.slice(1)); function download(a, text, name, type) { const file = new Blob([text], {type: type}); @@ -58,6 +61,21 @@ document.getElementById("open-file").addEventListener("change", e => { }}; r.readAsText(e.target.files[0]); }); +function loadFile(path) { + fetch("./stories/"+path).then(r => r.json()).then(d => { + db.files.put({name:"main",data:(data=d)}); + //location.hash = "#/"+data.current; + location.hash = "#/"; + state.path = renderPath(); + //setTimeout(() => document.documentElement.scrollTop = + // document.getElementById("main").lastChild.offsetTop); + }).catch(e => { + console.error(e); + alert("Error: Couldn't load file"); + location.hash = "#/"; + }); +} + const viewtpl = document.getElementById("view-entry").content; const edittpl = document.getElementById("edit-entry").content; const choicetpl = document.getElementById("edit-choice").content; @@ -144,7 +162,9 @@ function renderPath() { if (!path.startsWith("/")) { if (/^[0-9]+$/.test(path) && data.pages[+path] && data.pages[+path].paths[0]) path = data.pages[+path].paths[0]; - else path = ""; + else if (/^[0-9a-z.-]+\.json$/i.test(path)) { + loadFile(path); return; + } else path = ""; } else path = path.slice(1); state.editing = path.endsWith("edit"); if (state.editing) path = path.slice(0,-5); |