From 0a9b6f13a7ab063548c9324be8aa1aab26950bd3 Mon Sep 17 00:00:00 2001 From: b1ek Date: Mon, 19 Feb 2024 02:56:07 +1000 Subject: [PATCH] (feat) cache the selected language as well --- static/script/editor.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/static/script/editor.js b/static/script/editor.js index a258823..78fe8ba 100644 --- a/static/script/editor.js +++ b/static/script/editor.js @@ -11,22 +11,24 @@ let proxy = URL.createObjectURL(new Blob([` require(["vs/editor/editor.main"], function () { - let init_lang = 'python'; /** @type {string[]} */ const supported_langs = get_langs(); - let cache_key = 'sandy-cached-code'; + const code_key = 'sandy-cached-code'; + const lang_key = 'sandy-cached-lang'; const storage = window.localStorage; + const init_lang = storage.getItem(lang_key) ?? 'python'; + let editor = monaco.editor.create(document.getElementById('container'), { - value: storage.getItem(cache_key) ?? '# put code here', + value: storage.getItem(code_key) ?? '# put code here', language: init_lang, theme: 'vs-dark' }); function upd_code() { window.code = editor.getValue(); - storage.setItem(cache_key, window.code); + storage.setItem(code_key, window.code); } function timer_code() { setTimeout(() => { @@ -52,7 +54,9 @@ require(["vs/editor/editor.main"], function () { document.getElementById('lang').onchange = (e) => { e = e.target; - monaco.editor.setModelLanguage(editor.getModel(), e.options[e.selectedIndex].id) + const lang = e.options[e.selectedIndex].id; + monaco.editor.setModelLanguage(editor.getModel(), lang); + storage.setItem(lang_key, lang); } document.getElementById('run').onclick = (e) => {