From a660482b24d671bdeda2eeffbebc808935dae737 Mon Sep 17 00:00:00 2001 From: Daniel Odrinski Date: Tue, 2 May 2023 21:16:16 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20[#676]=20Further=20optimis?= =?UTF-8?q?e=20appearance.js=20to=20reduce=20code=20duplication?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/js/appearance.js | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/assets/js/appearance.js b/assets/js/appearance.js index 21c62d10..1e487458 100644 --- a/assets/js/appearance.js +++ b/assets/js/appearance.js @@ -26,20 +26,19 @@ window.addEventListener("DOMContentLoaded", (event) => { const switcher = document.getElementById("appearance-switcher"); const switcherMobile = document.getElementById("appearance-switcher-mobile"); - var targetAppearance = document.documentElement.classList.contains("dark") ? "dark" : "light" - updateMeta() - this.updateLogo?.(targetAppearance) + updateMeta(); + this.updateLogo?.(getTargetAppearance()); if (switcher) { switcher.addEventListener("click", () => { document.documentElement.classList.toggle("dark"); - var targetAppearance = document.documentElement.classList.contains("dark") ? "dark" : "light" + var targetAppearance = getTargetAppearance(); localStorage.setItem( "appearance", targetAppearance ); - updateMeta() - this.updateLogo?.(targetAppearance) + updateMeta(); + this.updateLogo?.(targetAppearance); }); switcher.addEventListener("contextmenu", (event) => { event.preventDefault(); @@ -49,13 +48,13 @@ window.addEventListener("DOMContentLoaded", (event) => { if (switcherMobile) { switcherMobile.addEventListener("click", () => { document.documentElement.classList.toggle("dark"); - var targetAppearance = document.documentElement.classList.contains("dark") ? "dark" : "light" + var targetAppearance = getTargetAppearance(); localStorage.setItem( "appearance", targetAppearance ); - updateMeta() - this.updateLogo?.(targetAppearance) + updateMeta(); + this.updateLogo?.(targetAppearance); }); switcherMobile.addEventListener("contextmenu", (event) => { event.preventDefault(); @@ -88,3 +87,7 @@ var updateLogo = (targetAppearance) => { } {{ end }} {{- end }} + +var getTargetAppearance = () => { + return document.documentElement.classList.contains("dark") ? "dark" : "light" +}