Skip to content

Commit

Permalink
save current story in browser local storage every 30s and reload on o…
Browse files Browse the repository at this point in the history
…pen to prevent data loss
  • Loading branch information
Echsecutor committed Dec 16, 2024
1 parent 0a1fc31 commit 74c5913
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions editor/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
const data_url_regexp = /^data:image\/([a-z]*);base64,(.*)$/;

var story = {};
const current_editor_story_key = "current_editor_story";

const text_area = document.getElementById("text");
const text_label = document.getElementById("text_label");
Expand Down Expand Up @@ -1021,7 +1022,17 @@ document

document.addEventListener("keydown", handle_global_key_down);

async function load_example() {
async function load_last_story_or_example() {
try {
const storyJson = localStorage.getItem(current_editor_story_key);
if (storyJson) {
story = JSON.parse(storyJson);
return;
}
} catch (err) {
console.error("Error loading story from local storage", err);
}

const url = "../stories/example_story.json";
try {
const response = await fetch(url);
Expand Down Expand Up @@ -1155,9 +1166,26 @@ async function depth_first_search(linearized_history, end_at, passing_through) {
return null;
}

function local_save() {
console.debug("local save");
localStorage.setItem(
current_editor_story_key,
JSON.stringify(story, null, 2)
);
}

function set_save_interval() {
window.setInterval(local_save, 30000);
}

function on_load() {
redraw_adventure_graph();
load_variables_menu();
}

load_example().then(on_load);
async function init() {
load_last_story_or_example().then(on_load);
set_save_interval();
}

init();

0 comments on commit 74c5913

Please sign in to comment.