persist bot code in localstorage
This commit is contained in:
parent
060b585da1
commit
006ce35622
3 changed files with 35 additions and 3 deletions
15
web/pw-server/src/lib/bot_code.ts
Normal file
15
web/pw-server/src/lib/bot_code.ts
Normal file
|
@ -0,0 +1,15 @@
|
|||
import defaultBotCode from "../assets/bot_template.txt?raw";
|
||||
|
||||
const BOT_CODE_KEY = "bot_code";
|
||||
|
||||
export function getBotCode() {
|
||||
let botCode = localStorage.getItem(BOT_CODE_KEY);
|
||||
if (!botCode) {
|
||||
botCode = defaultBotCode;
|
||||
}
|
||||
return botCode;
|
||||
}
|
||||
|
||||
export function saveBotCode(botCode: string) {
|
||||
localStorage.setItem(BOT_CODE_KEY, botCode);
|
||||
}
|
9
web/pw-server/src/lib/utils.ts
Normal file
9
web/pw-server/src/lib/utils.ts
Normal file
|
@ -0,0 +1,9 @@
|
|||
export function debounce(func: Function, timeout: number = 300) {
|
||||
let timer: ReturnType<typeof setTimeout>;
|
||||
return (...args: any[]) => {
|
||||
clearTimeout(timer);
|
||||
timer = setTimeout(() => {
|
||||
func.apply(this, args);
|
||||
}, timeout);
|
||||
};
|
||||
}
|
|
@ -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() {
|
||||
|
|
Loading…
Reference in a new issue