diff options
author | Adam Hovorka <[email protected]> | 2020-04-26 16:29:23 -0600 |
---|---|---|
committer | Adam Hovorka <[email protected]> | 2020-04-26 16:29:23 -0600 |
commit | fc875923d8cba63a9240f05244db5444fa4c3b55 (patch) | |
tree | 3a54b6cc1bac222f6634cf388a77878e57fd7d43 | |
parent | b2310b38a14ef15e790dc3d25659981a2e08b205 (diff) |
Add the ability to load stories via URL
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | main.js | 34 |
2 files changed, 28 insertions, 7 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..47fd629 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +stories/ @@ -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); |