refactor: standardize storage keys

This commit is contained in:
Erzbir
2026-07-09 17:38:01 +08:00
parent a3010ae155
commit a625bc9b0b
2 changed files with 25 additions and 4 deletions
+4 -3
View File
@@ -2,7 +2,8 @@ import {terminal} from "../terminal.js";
const THEME_MODE_STORAGE_KEY = "theme-mode"; const THEME_MODE_STORAGE_KEY = "theme-mode";
const COLOR_SCHEME_STORAGE_KEY = "theme-color-scheme"; 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; const COLOR_SCHEME_SELECTOR_PATTERN = /html\[theme-color-scheme\s*=\s*(['"]?)([^'"\]\s]+)\1\]/g;
function getStoredPreference(key) { function getStoredPreference(key) {
@@ -222,9 +223,9 @@ function registerPixelToggle() {
pixelToggle.addEventListener("click", function () { pixelToggle.addEventListener("click", function () {
const storedStatus = getStoredPreference(PIXEL_STYLE_STORAGE_KEY); const storedStatus = getStoredPreference(PIXEL_STYLE_STORAGE_KEY);
const currentStatus = storedStatus const currentStatus = storedStatus
|| document.documentElement.getAttribute(PIXEL_STYLE_STORAGE_KEY); || document.documentElement.getAttribute(PIXEL_STYLE_ATTRIBUTE);
const newStatus = currentStatus === "true" ? "false" : "true"; 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); setStoredPreference(PIXEL_STYLE_STORAGE_KEY, newStatus);
window.TerminalTheme?.syncPixelFont?.(); window.TerminalTheme?.syncPixelFont?.();
}); });
+21 -1
View File
@@ -28,7 +28,7 @@
const root = document.documentElement; const root = document.documentElement;
const storageKeys = { const storageKeys = {
themeMode: "theme-mode", themeMode: "theme-mode",
pixelStyle: "pixel_style", pixelStyle: "pixel-style",
colorScheme: "theme-color-scheme" colorScheme: "theme-color-scheme"
}; };
const expirationSuffix = "-expires-at"; const expirationSuffix = "-expires-at";
@@ -42,6 +42,25 @@
localStorage.removeItem(key + expirationSuffix); 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) { function getStoredPreference(key) {
const value = localStorage.getItem(key); const value = localStorage.getItem(key);
if (value === null) return null; if (value === null) return null;
@@ -173,6 +192,7 @@
syncPixelStyle syncPixelStyle
}; };
migrateStoredPreference("pixel_style", storageKeys.pixelStyle);
syncSystemFont(); syncSystemFont();
syncThemeMode(); syncThemeMode();
syncPixelStyle(); syncPixelStyle();