aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Hovorka <[email protected]>2020-04-26 16:29:23 -0600
committerAdam Hovorka <[email protected]>2020-04-26 16:29:23 -0600
commitfc875923d8cba63a9240f05244db5444fa4c3b55 (patch)
tree3a54b6cc1bac222f6634cf388a77878e57fd7d43
parentb2310b38a14ef15e790dc3d25659981a2e08b205 (diff)
Add the ability to load stories via URL
-rw-r--r--.gitignore1
-rw-r--r--main.js34
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/
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);