From 006ce356220c1a089fc468bbfdc0dea819cd9288 Mon Sep 17 00:00:00 2001
From: Ilion Beyst <ilion.beyst@gmail.com>
Date: Tue, 22 Feb 2022 18:48:59 +0100
Subject: persist bot code in localstorage

---
 web/pw-server/src/routes/index.svelte | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

(limited to 'web/pw-server/src/routes/index.svelte')

diff --git a/web/pw-server/src/routes/index.svelte b/web/pw-server/src/routes/index.svelte
index 56cb6f4..59552bc 100644
--- a/web/pw-server/src/routes/index.svelte
+++ b/web/pw-server/src/routes/index.svelte
@@ -9,8 +9,8 @@
   import type { Ace } from "ace-builds";
   import ace from "ace-builds/src-noconflict/ace?client";
   import * as AcePythonMode from "ace-builds/src-noconflict/mode-python?client";
-
-  import defaultBotCode from "../assets/bot_template.txt?raw";
+  import { getBotCode, saveBotCode } from "$lib/bot_code";
+  import { debounce } from "$lib/utils";
 
   let matches = [];
 
@@ -24,8 +24,16 @@
   });
 
   function init_editor() {
-    editSession = new ace.EditSession(defaultBotCode);
+    editSession = new ace.EditSession(getBotCode());
     editSession.setMode(new AcePythonMode.Mode());
+
+    const saveCode = () => {
+      const code = editSession.getDocument().getValue();
+      saveBotCode(code);
+    };
+
+    // cast to any because the type annotations are wrong here
+    (editSession as any).on("change", debounce(saveCode, 2000));
   }
 
   async function submitCode() {
-- 
cgit v1.2.3

