Added autosave and name saving

pull/6251/head
Tobeqz 5 years ago
parent 3ce518d1d0
commit 5b4e2dd101

@ -1,5 +1,5 @@
<script> <script>
import { createEventDispatcher, getContext } from 'svelte'; import { createEventDispatcher, getContext, onDestroy, onMount } from 'svelte';
import { stores } from '@sapper/app'; import { stores } from '@sapper/app';
import UserMenu from './UserMenu.svelte'; import UserMenu from './UserMenu.svelte';
import { Icon } from '@sveltejs/site-kit'; import { Icon } from '@sveltejs/site-kit';
@ -82,12 +82,18 @@
saving = false; saving = false;
} }
function saveLocal() {
localStorage.setItem("repl_content", JSON.stringify(repl.toJSON()));
localStorage.setItem("repl_name", name);
}
async function save() { async function save() {
if (saving) return; if (saving) return;
if (!canSave) { if (!canSave) {
// Save to local storage // Save to local storage
localStorage.setItem("repl_content", JSON.stringify(repl.toJSON())) // TODO: Add some feedback to let the user know they saved
saveLocal();
fork(true); fork(true);
return; return;
} }
@ -165,6 +171,20 @@ export default app;` });
downloading = false; downloading = false;
} }
const autosave_interval = setInterval(() => {
// Only run when user isn't logged in
// We could also run save() every 5 seconds
// That would make it autosave for logged in users
// as well. But that's not what this PR is about.
if (!canSave) {
saveLocal()
}
}, 2000);
onDestroy(() => {
clearInterval(autosave_interval);
});
</script> </script>
<svelte:window on:keydown={handleKeydown} /> <svelte:window on:keydown={handleKeydown} />

@ -53,6 +53,7 @@
if (id == 'hello-world' && local_repl_content && !$session.user) { if (id == 'hello-world' && local_repl_content && !$session.user) {
setTimeout(() => { setTimeout(() => {
repl.set(JSON.parse(local_repl_content)) repl.set(JSON.parse(local_repl_content))
name = localStorage.getItem("repl_name")
}, 300) // Wait for repl var (it's undefined when this function gets called) }, 300) // Wait for repl var (it's undefined when this function gets called)
// TODO: Fix this possible race condition // TODO: Fix this possible race condition
return return

Loading…
Cancel
Save