From fc875923d8cba63a9240f05244db5444fa4c3b55 Mon Sep 17 00:00:00 2001 From: Adam Hovorka Date: Sun, 26 Apr 2020 16:29:23 -0600 Subject: Add the ability to load stories via URL --- .gitignore | 1 + main.js | 34 +++++++++++++++++++++++++++------- 2 files changed, 28 insertions(+), 7 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..47fd629 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +stories/ diff --git a/main.js b/main.js index 7b8f761..cd64ecb 100644 --- a/main.js +++ b/main.js @@ -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); -- cgit v1.2.3-54-g00ecf