feat: add color scheme switcher
This commit is contained in:
@@ -210,6 +210,11 @@ spec:
|
||||
value: "light"
|
||||
- label: "跟随系统"
|
||||
value: "system"
|
||||
- $formkit: checkbox
|
||||
name: color_scheme_switcher_enable
|
||||
label: 启用访客配色切换
|
||||
value: false
|
||||
help: 开启后在页头显示配色选择下拉框
|
||||
- $formkit: select
|
||||
name: dark_color_scheme
|
||||
id: dark_color_scheme
|
||||
|
||||
+175
-4
@@ -1,7 +1,9 @@
|
||||
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 COLOR_SCHEME_SELECTOR_PATTERN = /html\[theme-color-scheme\s*=\s*(['"]?)([^'"\]\s]+)\1\]/g;
|
||||
|
||||
function registerSearchButton() {
|
||||
const searchButton = document.querySelector('[data-header-action="open-search"]');
|
||||
@@ -22,11 +24,179 @@ function registerThemeToggle() {
|
||||
const currentScheme = document.documentElement.getAttribute("data-color-scheme");
|
||||
const nextMode = currentScheme === "dark" ? "light" : "dark";
|
||||
|
||||
window.applyTheme(nextMode);
|
||||
|
||||
const isSystemMode = currentScheme === "system";
|
||||
if (!isSystemMode) {
|
||||
window.applyTheme(nextMode, {clearColorScheme: true});
|
||||
localStorage.setItem(THEME_MODE_STORAGE_KEY, nextMode);
|
||||
syncColorSchemePicker();
|
||||
});
|
||||
}
|
||||
|
||||
function collectColorSchemes(cssRules, schemes) {
|
||||
for (const rule of cssRules) {
|
||||
if (rule.selectorText) {
|
||||
for (const match of rule.selectorText.matchAll(COLOR_SCHEME_SELECTOR_PATTERN)) {
|
||||
schemes.add(match[2]);
|
||||
}
|
||||
}
|
||||
|
||||
if (rule.cssRules) {
|
||||
collectColorSchemes(rule.cssRules, schemes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getDefinedColorSchemes() {
|
||||
const schemes = new Set();
|
||||
|
||||
for (const styleSheet of document.styleSheets) {
|
||||
try {
|
||||
collectColorSchemes(styleSheet.cssRules, schemes);
|
||||
} catch {
|
||||
// Cross-origin stylesheets do not expose their CSS rules.
|
||||
}
|
||||
}
|
||||
|
||||
return schemes;
|
||||
}
|
||||
|
||||
function createColorSchemeOption(scheme) {
|
||||
const option = document.createElement("button");
|
||||
option.className = "color-scheme-option";
|
||||
option.type = "button";
|
||||
option.role = "option";
|
||||
option.tabIndex = -1;
|
||||
option.dataset.colorScheme = scheme;
|
||||
option.setAttribute("aria-selected", "false");
|
||||
option.textContent = scheme;
|
||||
return option;
|
||||
}
|
||||
|
||||
function populateColorSchemeMenu(menu) {
|
||||
menu.replaceChildren(createColorSchemeOption("default"));
|
||||
|
||||
for (const scheme of getDefinedColorSchemes()) {
|
||||
menu.appendChild(createColorSchemeOption(scheme));
|
||||
}
|
||||
}
|
||||
|
||||
function syncColorSchemePicker() {
|
||||
const picker = document.getElementById("color-scheme-picker");
|
||||
if (!picker) return;
|
||||
|
||||
const storedScheme = localStorage.getItem(COLOR_SCHEME_STORAGE_KEY);
|
||||
const options = Array.from(picker.querySelectorAll("[data-color-scheme]"));
|
||||
const hasStoredOption = storedScheme && options
|
||||
.some(option => option.dataset.colorScheme === storedScheme);
|
||||
|
||||
if (storedScheme && !hasStoredOption) {
|
||||
const storedMode = localStorage.getItem(THEME_MODE_STORAGE_KEY)
|
||||
|| window.TerminalThemeConfig?.defaultMode
|
||||
|| "system";
|
||||
|
||||
window.applyTheme?.(storedMode, {clearColorScheme: true});
|
||||
}
|
||||
|
||||
const selectedScheme = hasStoredOption ? storedScheme : "default";
|
||||
const trigger = picker.querySelector("#color-scheme-trigger");
|
||||
if (trigger) {
|
||||
const description = `选择配色,当前:${selectedScheme}`;
|
||||
trigger.setAttribute("aria-label", description);
|
||||
trigger.title = description;
|
||||
}
|
||||
|
||||
for (const option of options) {
|
||||
option.setAttribute(
|
||||
"aria-selected",
|
||||
String(option.dataset.colorScheme === selectedScheme)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function setColorSchemeMenuOpen(picker, open) {
|
||||
const trigger = picker.querySelector("#color-scheme-trigger");
|
||||
const menu = picker.querySelector("#color-scheme-menu");
|
||||
if (!trigger || !menu) return;
|
||||
|
||||
trigger.setAttribute("aria-expanded", String(open));
|
||||
menu.hidden = !open;
|
||||
}
|
||||
|
||||
function focusColorSchemeOption(picker, direction) {
|
||||
const options = Array.from(picker.querySelectorAll("[data-color-scheme]"));
|
||||
if (!options.length) return;
|
||||
|
||||
const selectedIndex = options.findIndex(
|
||||
option => option.getAttribute("aria-selected") === "true"
|
||||
);
|
||||
const fallbackIndex = direction === "last" ? options.length - 1 : 0;
|
||||
options[selectedIndex >= 0 ? selectedIndex : fallbackIndex].focus();
|
||||
}
|
||||
|
||||
function registerColorSchemePicker() {
|
||||
const picker = document.getElementById("color-scheme-picker");
|
||||
const trigger = picker?.querySelector("#color-scheme-trigger");
|
||||
const menu = picker?.querySelector("#color-scheme-menu");
|
||||
if (!picker || !trigger || !menu
|
||||
|| typeof window.TerminalTheme?.applyColorScheme !== "function") return;
|
||||
|
||||
populateColorSchemeMenu(menu);
|
||||
syncColorSchemePicker();
|
||||
|
||||
trigger.addEventListener("click", function () {
|
||||
const open = trigger.getAttribute("aria-expanded") !== "true";
|
||||
setColorSchemeMenuOpen(picker, open);
|
||||
if (open) focusColorSchemeOption(picker, "first");
|
||||
});
|
||||
|
||||
trigger.addEventListener("keydown", function (event) {
|
||||
if (event.key !== "ArrowDown" && event.key !== "ArrowUp") return;
|
||||
event.preventDefault();
|
||||
setColorSchemeMenuOpen(picker, true);
|
||||
focusColorSchemeOption(picker, event.key === "ArrowUp" ? "last" : "first");
|
||||
});
|
||||
|
||||
menu.addEventListener("click", function (event) {
|
||||
const option = event.target.closest("[data-color-scheme]");
|
||||
if (!option || !menu.contains(option)) return;
|
||||
|
||||
window.TerminalTheme.applyColorScheme(option.dataset.colorScheme);
|
||||
syncColorSchemePicker();
|
||||
setColorSchemeMenuOpen(picker, false);
|
||||
trigger.focus();
|
||||
});
|
||||
|
||||
menu.addEventListener("keydown", function (event) {
|
||||
const options = Array.from(menu.querySelectorAll("[data-color-scheme]"));
|
||||
const currentIndex = options.indexOf(document.activeElement);
|
||||
|
||||
if (event.key === "Escape") {
|
||||
event.preventDefault();
|
||||
setColorSchemeMenuOpen(picker, false);
|
||||
trigger.focus();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!["ArrowDown", "ArrowUp", "Home", "End"].includes(event.key)) return;
|
||||
event.preventDefault();
|
||||
|
||||
let nextIndex = currentIndex;
|
||||
if (event.key === "Home") nextIndex = 0;
|
||||
if (event.key === "End") nextIndex = options.length - 1;
|
||||
if (event.key === "ArrowDown") nextIndex = (currentIndex + 1) % options.length;
|
||||
if (event.key === "ArrowUp") {
|
||||
nextIndex = (currentIndex - 1 + options.length) % options.length;
|
||||
}
|
||||
options[nextIndex].focus();
|
||||
});
|
||||
|
||||
picker.addEventListener("focusout", function (event) {
|
||||
if (!picker.contains(event.relatedTarget)) {
|
||||
setColorSchemeMenuOpen(picker, false);
|
||||
}
|
||||
});
|
||||
|
||||
document.addEventListener("click", function (event) {
|
||||
if (!picker.contains(event.target)) {
|
||||
setColorSchemeMenuOpen(picker, false);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -45,5 +215,6 @@ function registerPixelToggle() {
|
||||
}
|
||||
|
||||
terminal.registerInitFunc(registerSearchButton);
|
||||
terminal.registerInitFunc(registerColorSchemePicker);
|
||||
terminal.registerInitFunc(registerPixelToggle);
|
||||
terminal.registerInitFunc(registerThemeToggle);
|
||||
|
||||
+72
-1
@@ -8,20 +8,91 @@
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
|
||||
.color-scheme-picker {
|
||||
position: relative;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
flex: 0 0 auto;
|
||||
width: max-content;
|
||||
margin-left: calc(var(--size-theme-font) * 0.5);
|
||||
}
|
||||
|
||||
.color-scheme-trigger {
|
||||
transition: none;
|
||||
animation: none;
|
||||
}
|
||||
|
||||
.color-scheme-menu {
|
||||
position: absolute;
|
||||
z-index: 20;
|
||||
top: calc(100% + var(--width-theme-border));
|
||||
right: 0;
|
||||
display: block;
|
||||
width: max-content;
|
||||
min-width: 100%;
|
||||
max-height: calc(var(--size-theme-font) * 12);
|
||||
overflow-y: auto;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
background: var(--color-theme-background);
|
||||
border: var(--width-theme-border) var(--style-theme-border) var(--color-theme-header);
|
||||
|
||||
&[hidden] {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
|
||||
.color-scheme-option {
|
||||
display: block;
|
||||
width: 100%;
|
||||
margin: 0;
|
||||
padding: calc(var(--size-theme-font) * 0.35) calc(var(--size-theme-font) * 0.4);
|
||||
color: var(--color-theme-header);
|
||||
background: var(--color-theme-background);
|
||||
border: 0;
|
||||
border-bottom: calc(var(--width-theme-border) / 2) var(--style-theme-border) var(--color-theme-header);
|
||||
border-radius: 0;
|
||||
font-size: calc(var(--size-theme-font) * 0.8);
|
||||
font-weight: 500;
|
||||
text-align: left;
|
||||
white-space: nowrap;
|
||||
transition: none;
|
||||
animation: none;
|
||||
|
||||
&:last-child {
|
||||
border-bottom: 0;
|
||||
}
|
||||
|
||||
&:focus-visible,
|
||||
&[aria-selected="true"] {
|
||||
color: var(--color-theme-background);
|
||||
background: var(--color-theme-header);
|
||||
outline: none;
|
||||
}
|
||||
}
|
||||
|
||||
#color-scheme-trigger,
|
||||
#theme-toggle,
|
||||
#search-button,
|
||||
#pixel-toggle {
|
||||
background-color: transparent;
|
||||
color: var(--color-theme-header);
|
||||
border: var(--width-theme-border) var(--style-theme-border) var(--color-theme-header);
|
||||
margin: 0 0 0 calc(var(--size-theme-font) * 0.5);
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
#theme-toggle,
|
||||
#search-button,
|
||||
#pixel-toggle {
|
||||
margin-left: calc(var(--size-theme-font) * 0.5);
|
||||
}
|
||||
}
|
||||
|
||||
&-logo {
|
||||
display: flex;
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
|
||||
&::after {
|
||||
content: "";
|
||||
|
||||
@@ -25,6 +25,42 @@
|
||||
d="M6 2h8v2H6zM4 6V4h2v2zm0 8H2V6h2zm2 2H4v-2h2zm8 0v2H6v-2zm2-2h-2v2h2v2h2v2h2v2h2v-2h-2v-2h-2v-2h-2zm0-8h2v8h-2zm0 0V4h-2v2z"/>
|
||||
</svg>
|
||||
</button>
|
||||
<div
|
||||
id="color-scheme-picker"
|
||||
class="color-scheme-picker"
|
||||
th:if="${theme.config.style.color_scheme_switcher_enable != false}">
|
||||
<button
|
||||
id="color-scheme-trigger"
|
||||
class="color-scheme-trigger"
|
||||
type="button"
|
||||
aria-label="Selector Scheme"
|
||||
aria-haspopup="listbox"
|
||||
aria-expanded="false"
|
||||
aria-controls="color-scheme-menu">
|
||||
<svg class="icon-regular button-icon" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
|
||||
<path fill="currentColor" d="M12.6 3.6c-5.96 0-10.8 4.3-10.8 9.6c0 3.97 3.23 7.2 7.2 7.2h1.98c.98 0 1.62-1 1.25-1.91c-.5-1.22.4-2.59 1.73-2.59h2.24c3.31 0 6-2.69 6-6c0-3.48-4.3-6.3-9.6-6.3ZM6 14.4a1.8 1.8 0 1 1 0-3.6a1.8 1.8 0 0 1 0 3.6Zm3.6-4.8a1.8 1.8 0 1 1 0-3.6a1.8 1.8 0 0 1 0 3.6Zm6 0a1.8 1.8 0 1 1 0-3.6a1.8 1.8 0 0 1 0 3.6Zm3.6 4.8a1.8 1.8 0 1 1 0-3.6a1.8 1.8 0 0 1 0 3.6Z"></path>
|
||||
</svg>
|
||||
<svg class="icon-pixel button-icon" aria-hidden="true" xmlns="http://www.w3.org/2000/svg"
|
||||
viewBox="0 0 24 24">
|
||||
<g fill="currentColor"><path d="M22.29 6.285h1.14V12h-1.14ZM21.143 12h1.147v2.287h-1.147ZM21.143 3.998h1.147v2.287h-1.147ZM20.003 14.288h1.14v1.14h-1.14ZM20.003 2.858h1.14v1.14h-1.14ZM18.855 15.428h1.148v1.147h-1.148ZM18.855 8.573h1.148v4.567h-1.148ZM17.715 16.575h1.14v1.14h-1.14ZM17.715 13.14h1.14v1.148h-1.14ZM17.715 1.718h2.287v1.14h-2.287ZM16.575 17.715h1.14v1.14h-1.14ZM16.575 14.288h1.14v1.14h-1.14ZM15.428 7.433h3.427v1.14h-3.427ZM15.428 18.855h1.147v1.148h-1.147ZM14.288 20.003h1.14v1.14h-1.14ZM12 15.428h4.575v1.147H12ZM14.288 8.573h1.14v1.14h-1.14ZM14.288 6.285h2.287v-1.14h1.14V3.998h-1.14v-1.14h-2.287v1.14h-1.14v1.147h1.14zM13.148 21.143h1.14v1.14h-1.14ZM13.148 9.712h1.14v1.148h-1.14ZM12 10.86h1.148V12H12ZM6.285 22.283h6.863v1.147H6.285ZM10.86 12H12v3.428h-1.14ZM9.712.57h8.003v1.148H9.712ZM10.86 16.575H8.573v1.14h-1.14v2.288h1.14v1.14h2.287v-1.14H12v-2.288h-1.14zM7.433 1.718h2.28v1.14h-2.28ZM7.433 9.712h2.28v-1.14h1.147V6.285H9.713v-1.14h-2.28v1.14H6.284v2.287h1.147zM4.005 15.428h2.28v-1.14h1.148V12H6.285v-1.14h-2.28V12H2.858v2.287h1.147zM5.145 2.858h2.288v1.14H5.145ZM5.145 21.143h1.14v1.14h-1.14ZM2.858 20.003h2.287v1.14H2.858ZM4.005 3.998h1.14v1.147h-1.14ZM2.858 5.145h1.147v2.288H2.858ZM1.718 17.715h1.14v2.287h-1.14ZM1.718 7.433h1.14v2.28h-1.14Z"/><path d="M.57 9.712h1.148v8.003H.57Z"/></g>
|
||||
</svg>
|
||||
</button>
|
||||
<div
|
||||
id="color-scheme-menu"
|
||||
class="color-scheme-menu"
|
||||
role="listbox"
|
||||
aria-label="Color Scheme List"
|
||||
hidden>
|
||||
<button
|
||||
class="color-scheme-option"
|
||||
type="button"
|
||||
role="option"
|
||||
aria-selected="true"
|
||||
data-color-scheme="default">
|
||||
default
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<button id="theme-toggle" aria-label="Toggle Theme" type="button">
|
||||
<svg class="icon-regular button-icon" aria-hidden="true" xmlns="http://www.w3.org/2000/svg"
|
||||
preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24">
|
||||
|
||||
@@ -27,24 +27,45 @@
|
||||
const root = document.documentElement;
|
||||
const storageKeys = {
|
||||
themeMode: "theme-mode",
|
||||
pixelStyle: "pixel_style"
|
||||
pixelStyle: "pixel_style",
|
||||
colorScheme: "theme-color-scheme"
|
||||
};
|
||||
|
||||
function resolveTheme(scheme) {
|
||||
if (scheme === "light") {
|
||||
function resolveTheme(mode) {
|
||||
if (mode === "light") {
|
||||
return config.lightTheme === "custom" ? config.customLightTheme : config.lightTheme;
|
||||
}
|
||||
return config.darkTheme === "custom" ? config.customDarkTheme : config.darkTheme;
|
||||
}
|
||||
|
||||
function getStoredColorScheme() {
|
||||
const scheme = localStorage.getItem(storageKeys.colorScheme);
|
||||
return scheme && scheme !== "default" ? scheme : "";
|
||||
}
|
||||
|
||||
function getSystemScheme() {
|
||||
return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
|
||||
}
|
||||
|
||||
function applyTheme(mode) {
|
||||
function applyTheme(mode, options = {}) {
|
||||
if (options.clearColorScheme) {
|
||||
localStorage.removeItem(storageKeys.colorScheme);
|
||||
}
|
||||
|
||||
const actualMode = mode === "system" ? getSystemScheme() : mode;
|
||||
root.setAttribute("data-color-scheme", actualMode);
|
||||
root.setAttribute("theme-color-scheme", resolveTheme(actualMode));
|
||||
root.setAttribute("theme-color-scheme", getStoredColorScheme() || resolveTheme(actualMode));
|
||||
}
|
||||
|
||||
function applyColorScheme(scheme) {
|
||||
if (!scheme || scheme === "default") {
|
||||
localStorage.removeItem(storageKeys.colorScheme);
|
||||
syncThemeMode();
|
||||
return;
|
||||
}
|
||||
|
||||
localStorage.setItem(storageKeys.colorScheme, scheme);
|
||||
root.setAttribute("theme-color-scheme", scheme);
|
||||
}
|
||||
|
||||
function syncSystemFont() {
|
||||
@@ -104,6 +125,7 @@
|
||||
window.applyTheme = applyTheme;
|
||||
window.TerminalTheme = {
|
||||
applyTheme,
|
||||
applyColorScheme,
|
||||
applyCustomCss,
|
||||
syncPixelFont,
|
||||
syncPixelStyle
|
||||
|
||||
Reference in New Issue
Block a user