From 6114114f755707ba757b914c53887752fe587ec8 Mon Sep 17 00:00:00 2001 From: Bao Zhiyuan Date: Thu, 26 Dec 2024 10:03:51 +0800 Subject: [PATCH 1/2] fix: preserve theme --- moonbit-tour/src/main.ts | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/moonbit-tour/src/main.ts b/moonbit-tour/src/main.ts index 0a76d67a..ab0f8efb 100644 --- a/moonbit-tour/src/main.ts +++ b/moonbit-tour/src/main.ts @@ -29,23 +29,32 @@ const moonSvg = `("#theme")!; +setTheme(theme); -function toggleTheme() { +function setTheme(theme: Theme) { if (theme === "light") { - theme = "dark"; - document.querySelector("html")?.classList.add("dark"); - monaco.editor.setTheme("dark-plus"); - themeButton.innerHTML = moonSvg; - } else { - theme = "light"; document.querySelector("html")?.classList.remove("dark"); monaco.editor.setTheme("light-plus"); themeButton.innerHTML = sunSvg; + } else { + document.querySelector("html")?.classList.add("dark"); + monaco.editor.setTheme("dark-plus"); + themeButton.innerHTML = moonSvg; } + localStorage.setItem("theme", theme); +} + +function toggleTheme() { + theme = theme === "light" ? "dark" : "light"; + setTheme(theme); } themeButton.addEventListener("click", toggleTheme); @@ -116,7 +125,6 @@ monaco.editor.create(editor, { alwaysConsumeMouseWheel: false, }, fontFamily: "monospace", - theme: "light-plus", }); run(); From d4dfe80b7e7cabde8630f4d20dccffa991e1ced6 Mon Sep 17 00:00:00 2001 From: Bao Zhiyuan Date: Thu, 26 Dec 2024 10:08:48 +0800 Subject: [PATCH 2/2] fix: tweak theme switch icon --- moonbit-tour/index.html | 10 +++++----- moonbit-tour/src/main.ts | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/moonbit-tour/index.html b/moonbit-tour/index.html index af05d211..7cb26365 100644 --- a/moonbit-tour/index.html +++ b/moonbit-tour/index.html @@ -21,13 +21,13 @@
%CODE%

         
diff --git a/moonbit-tour/src/main.ts b/moonbit-tour/src/main.ts index ab0f8efb..8da12805 100644 --- a/moonbit-tour/src/main.ts +++ b/moonbit-tour/src/main.ts @@ -43,11 +43,11 @@ function setTheme(theme: Theme) { if (theme === "light") { document.querySelector("html")?.classList.remove("dark"); monaco.editor.setTheme("light-plus"); - themeButton.innerHTML = sunSvg; + themeButton.innerHTML = moonSvg; } else { document.querySelector("html")?.classList.add("dark"); monaco.editor.setTheme("dark-plus"); - themeButton.innerHTML = moonSvg; + themeButton.innerHTML = sunSvg; } localStorage.setItem("theme", theme); }