From a625bc9b0bf10bc96bbdfcf9643d3d08f0fc5a56 Mon Sep 17 00:00:00 2001 From: Erzbir Date: Thu, 9 Jul 2026 17:38:01 +0800 Subject: [PATCH] refactor: standardize storage keys --- src/module/header.js | 7 ++++--- templates/modules/layout.html | 22 +++++++++++++++++++++- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/module/header.js b/src/module/header.js index 76fc455..87f5433 100644 --- a/src/module/header.js +++ b/src/module/header.js @@ -2,7 +2,8 @@ import {terminal} from "../terminal.js"; const THEME_MODE_STORAGE_KEY = "theme-mode"; const COLOR_SCHEME_STORAGE_KEY = "theme-color-scheme"; -const PIXEL_STYLE_STORAGE_KEY = "pixel_style"; +const PIXEL_STYLE_STORAGE_KEY = "pixel-style"; +const PIXEL_STYLE_ATTRIBUTE = "pixel_style"; const COLOR_SCHEME_SELECTOR_PATTERN = /html\[theme-color-scheme\s*=\s*(['"]?)([^'"\]\s]+)\1\]/g; function getStoredPreference(key) { @@ -222,9 +223,9 @@ function registerPixelToggle() { pixelToggle.addEventListener("click", function () { const storedStatus = getStoredPreference(PIXEL_STYLE_STORAGE_KEY); const currentStatus = storedStatus - || document.documentElement.getAttribute(PIXEL_STYLE_STORAGE_KEY); + || document.documentElement.getAttribute(PIXEL_STYLE_ATTRIBUTE); const newStatus = currentStatus === "true" ? "false" : "true"; - document.documentElement.setAttribute(PIXEL_STYLE_STORAGE_KEY, newStatus); + document.documentElement.setAttribute(PIXEL_STYLE_ATTRIBUTE, newStatus); setStoredPreference(PIXEL_STYLE_STORAGE_KEY, newStatus); window.TerminalTheme?.syncPixelFont?.(); }); diff --git a/templates/modules/layout.html b/templates/modules/layout.html index 247f75d..5931691 100644 --- a/templates/modules/layout.html +++ b/templates/modules/layout.html @@ -28,7 +28,7 @@ const root = document.documentElement; const storageKeys = { themeMode: "theme-mode", - pixelStyle: "pixel_style", + pixelStyle: "pixel-style", colorScheme: "theme-color-scheme" }; const expirationSuffix = "-expires-at"; @@ -42,6 +42,25 @@ localStorage.removeItem(key + expirationSuffix); } + function migrateStoredPreference(oldKey, newKey) { + const oldValue = localStorage.getItem(oldKey); + if (oldValue === null) { + localStorage.removeItem(oldKey + expirationSuffix); + return; + } + + if (localStorage.getItem(newKey) === null) { + localStorage.setItem(newKey, oldValue); + + const oldExpiration = localStorage.getItem(oldKey + expirationSuffix); + if (oldExpiration !== null) { + localStorage.setItem(newKey + expirationSuffix, oldExpiration); + } + } + + removeStoredPreference(oldKey); + } + function getStoredPreference(key) { const value = localStorage.getItem(key); if (value === null) return null; @@ -173,6 +192,7 @@ syncPixelStyle }; + migrateStoredPreference("pixel_style", storageKeys.pixelStyle); syncSystemFont(); syncThemeMode(); syncPixelStyle();