Browse Source

editor: autosave

mj-deploy
Vašek Šraier 4 years ago
parent
commit
911f1030cc
  1. 62
      frontend/src/Editor.svelte

62
frontend/src/Editor.svelte

@ -8,8 +8,11 @@
import TaskDisplay from "./TaskDisplay.svelte"; import TaskDisplay from "./TaskDisplay.svelte";
import TaskDetailEditor from "./TaskDetailEditor.svelte"; import TaskDetailEditor from "./TaskDetailEditor.svelte";
import { forceSimulation } from "./force-simulation"; import { forceSimulation } from "./force-simulation";
import { refresh as refreshTaskStatuses, taskStatuses } from './task-status-cache' import {
import { isLoggedIn } from "./ksp-task-grabber"; refresh as refreshTaskStatuses,
taskStatuses,
} from "./task-status-cache";
import { isLoggedIn } from "./ksp-task-grabber";
export let tasks: TasksFile; export let tasks: TasksFile;
@ -64,6 +67,18 @@ import { isLoggedIn } from "./ksp-task-grabber";
await saveTasks(tasks); await saveTasks(tasks);
} }
// autosave ;)
let saveTimeoutHandle: number | null = null;
function autosave() {
if (saveTimeoutHandle != null) clearTimeout(saveTimeoutHandle);
saveTimeoutHandle = setTimeout(async () => {
saveTimeoutHandle = null;
await saveTasks(tasks);
}, 5000);
}
$: { tasks; autosave(); };
function saveLocally() { function saveLocally() {
saveToLocalDisk("tasks.json", tasksToString(tasks)); saveToLocalDisk("tasks.json", tasksToString(tasks));
} }
@ -76,7 +91,7 @@ import { isLoggedIn } from "./ksp-task-grabber";
open( open(
TaskDetailEditor, TaskDetailEditor,
{ task: t, tasks: tasks }, { task: t, tasks: tasks },
{ closeButton: false, styleWindow: { width: "50vw"} }, { closeButton: false, styleWindow: { width: "50vw" } },
{ {
onClose: () => { onClose: () => {
tasks = tasks; tasks = tasks;
@ -160,22 +175,27 @@ import { isLoggedIn } from "./ksp-task-grabber";
async function loadYear() { async function loadYear() {
if (!isLoggedIn()) { if (!isLoggedIn()) {
alert("Musíš se přihlásit") alert("Musíš se přihlásit");
return return;
} }
const y = prompt("Který ročník (číslo 26...X)") const y = prompt("Který ročník (číslo 26...X)");
await refreshTaskStatuses([`${y}-Z1-1`]) await refreshTaskStatuses([`${y}-Z1-1`]);
const newTasks = Array.from($taskStatuses.values()).filter(t => t.id.startsWith(y + "-") && !tasks.tasks.find(tt => tt.type == "open-data" && tt.taskReference == t.id)) const newTasks = Array.from($taskStatuses.values()).filter(
const newDescriptors: TaskDescriptor[] = (t) =>
newTasks.map(t => ({ t.id.startsWith(y + "-") &&
type: "open-data", !tasks.tasks.find(
id: t.id, (tt) => tt.type == "open-data" && tt.taskReference == t.id
taskReference: t.id, )
requires: [], );
position: [0, 0], const newDescriptors: TaskDescriptor[] = newTasks.map((t) => ({
title: t.name type: "open-data",
})) id: t.id,
tasks.tasks = [...tasks.tasks, ...newDescriptors] taskReference: t.id,
requires: [],
position: [0, 0],
title: t.name,
}));
tasks.tasks = [...tasks.tasks, ...newDescriptors];
} }
</script> </script>
@ -332,7 +352,11 @@ import { isLoggedIn } from "./ksp-task-grabber";
</div> </div>
{/if} {/if}
<div> <div>
<button on:click={loadYear} disabled={!isLoggedIn()} title={isLoggedIn() ? "Nahraje všechny úlohy z jednoho ročníku, které tu ještě nejsou" : "Je nutné být přihlášený a na stránce v KSP template."}>Nahrát celý ročník</button> <button
on:click={loadYear}
disabled={!isLoggedIn()}
title={isLoggedIn() ? 'Nahraje všechny úlohy z jednoho ročníku, které tu ještě nejsou' : 'Je nutné být přihlášený a na stránce v KSP template.'}>Nahrát
celý ročník</button>
</div> </div>
</div> </div>