refactor: improve code maintainability

This commit is contained in:
Erzbir
2026-06-15 22:06:13 +08:00
parent 72346d78d2
commit fdec0aafa1
12 changed files with 421 additions and 291 deletions
+32 -12
View File
@@ -1,29 +1,49 @@
function register_themeToggle() {
import {terminal} from "../terminal.js";
const THEME_MODE_STORAGE_KEY = "theme-mode";
const PIXEL_STYLE_STORAGE_KEY = "pixel_style";
function registerSearchButton() {
const searchButton = document.querySelector('[data-header-action="open-search"]');
if (!searchButton) return;
searchButton.addEventListener("click", function () {
if (typeof window.SearchWidget?.open === "function") {
window.SearchWidget.open();
}
});
}
function registerThemeToggle() {
const toggle = document.getElementById("theme-toggle");
if (!toggle || typeof window.applyTheme !== "function") return;
toggle.addEventListener("click", function () {
const currentScheme = document.documentElement.getAttribute("data-color-scheme");
const nextMode = currentScheme === "dark" ? "light" : "dark";
applyTheme(nextMode);
window.applyTheme(nextMode);
const isSystemMode = currentScheme === "system";
if (!isSystemMode) {
localStorage.setItem("theme-mode", nextMode);
localStorage.setItem(THEME_MODE_STORAGE_KEY, nextMode);
}
});
}
function register_pixelToggle() {
const pixel_toggle = document.getElementById('pixel-toggle');
function registerPixelToggle() {
const pixelToggle = document.getElementById("pixel-toggle");
if (!pixelToggle) return;
pixel_toggle.addEventListener('click', function () {
let storedStatus = localStorage.getItem("pixel_style");
let newStatus = storedStatus === 'true' ? 'false' : 'true';
document.documentElement.setAttribute("pixel_style", newStatus);
localStorage.setItem("pixel_style", newStatus);
pixelToggle.addEventListener("click", function () {
const storedStatus = localStorage.getItem(PIXEL_STYLE_STORAGE_KEY);
const newStatus = storedStatus === "true" ? "false" : "true";
document.documentElement.setAttribute(PIXEL_STYLE_STORAGE_KEY, newStatus);
localStorage.setItem(PIXEL_STYLE_STORAGE_KEY, newStatus);
window.TerminalTheme?.syncPixelFont?.();
});
}
terminal.registerInitFunc(register_pixelToggle);
terminal.registerInitFunc(register_themeToggle);
terminal.registerInitFunc(registerSearchButton);
terminal.registerInitFunc(registerPixelToggle);
terminal.registerInitFunc(registerThemeToggle);