207 lines
8.1 KiB
HTML
207 lines
8.1 KiB
HTML
<th:block th:fragment="theme-preferences">
|
|
<script data-no-pjax th:inline="javascript">
|
|
window.TerminalThemeConfig = {
|
|
systemFontEnabled: /*[[${theme.config.style.system_font_enable}]]*/ false,
|
|
darkTheme: /*[[${theme.config.style.dark_color_scheme}]]*/ "dark",
|
|
lightTheme: /*[[${theme.config.style.light_color_scheme}]]*/ "light",
|
|
customDarkTheme: /*[[${theme.config.style.custom_dark}]]*/ "",
|
|
customLightTheme: /*[[${theme.config.style.custom_light}]]*/ "",
|
|
defaultMode: /*[[${theme.config.style.default_scheme_mode}]]*/ "system",
|
|
preferTTL: /*[[${theme.config.style.prefer_ttl ?: 24}]]*/ 24,
|
|
pixelStyle: /*[[${theme.config.style.pixel_style}]]*/ false,
|
|
pixelCssHref: /*[[@{/assets/dist/pixel.css?v={version}(version=${theme.spec.version})}]]*/ "",
|
|
customCss: /*[[${theme.config.style.custom}]]*/ "",
|
|
messages: {
|
|
currentColorScheme: /*[[#{header.color_scheme.current}]]*/ "Current color scheme",
|
|
showToc: /*[[#{toc.show}]]*/ "Show table of contents",
|
|
hideToc: /*[[#{toc.hide}]]*/ "Hide table of contents",
|
|
networkError: /*[[#{moment.network_error}]]*/ "Network error. Please try again."
|
|
}
|
|
};
|
|
</script>
|
|
<script data-no-pjax>
|
|
(function () {
|
|
const config = window.TerminalThemeConfig || {};
|
|
const root = document.documentElement;
|
|
const storageKeys = {
|
|
themeMode: "theme-mode",
|
|
pixelStyle: "pixel-style",
|
|
colorScheme: "theme-color-scheme"
|
|
};
|
|
const expirationSuffix = "-expires-at";
|
|
const expirationHours = Number(config.preferTTL);
|
|
const storageTtl = Number.isFinite(expirationHours) && expirationHours > 0
|
|
? expirationHours * 60 * 60 * 1000
|
|
: 0;
|
|
|
|
function removeStoredPreference(key) {
|
|
localStorage.removeItem(key);
|
|
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;
|
|
|
|
const expirationKey = key + expirationSuffix;
|
|
if (!storageTtl) {
|
|
localStorage.removeItem(expirationKey);
|
|
return value;
|
|
}
|
|
|
|
const expiresAt = Number(localStorage.getItem(expirationKey));
|
|
if (!Number.isFinite(expiresAt) || expiresAt <= 0) {
|
|
localStorage.setItem(expirationKey, String(Date.now() + storageTtl));
|
|
return value;
|
|
}
|
|
|
|
if (expiresAt <= Date.now()) {
|
|
removeStoredPreference(key);
|
|
return null;
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
function setStoredPreference(key, value) {
|
|
localStorage.setItem(key, value);
|
|
if (storageTtl) {
|
|
localStorage.setItem(key + expirationSuffix, String(Date.now() + storageTtl));
|
|
} else {
|
|
localStorage.removeItem(key + expirationSuffix);
|
|
}
|
|
}
|
|
|
|
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 = getStoredPreference(storageKeys.colorScheme);
|
|
return scheme && scheme !== "default" ? scheme : "";
|
|
}
|
|
|
|
function getSystemScheme() {
|
|
return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
|
|
}
|
|
|
|
function applyTheme(mode, options = {}) {
|
|
if (options.clearColorScheme) {
|
|
removeStoredPreference(storageKeys.colorScheme);
|
|
}
|
|
|
|
const actualMode = mode === "system" ? getSystemScheme() : mode;
|
|
root.dataset.colorScheme = actualMode;
|
|
root.dataset.themeColorScheme = getStoredColorScheme() || resolveTheme(actualMode);
|
|
}
|
|
|
|
function applyColorScheme(scheme) {
|
|
if (!scheme || scheme === "default") {
|
|
removeStoredPreference(storageKeys.colorScheme);
|
|
syncThemeMode();
|
|
return;
|
|
}
|
|
|
|
setStoredPreference(storageKeys.colorScheme, scheme);
|
|
root.dataset.themeColorScheme = scheme;
|
|
}
|
|
|
|
function syncSystemFont() {
|
|
root.dataset.systemFontEnabled = String(config.systemFontEnabled);
|
|
}
|
|
|
|
function syncPixelStyle() {
|
|
root.dataset.pixelStyle = getStoredPreference(storageKeys.pixelStyle)
|
|
|| String(config.pixelStyle);
|
|
}
|
|
|
|
function syncPixelFont() {
|
|
const existingLink = document.getElementById("terminal-pixel-font");
|
|
const pixelEnabled = root.dataset.pixelStyle === "true";
|
|
|
|
if (!pixelEnabled && existingLink) {
|
|
existingLink.remove();
|
|
return;
|
|
}
|
|
|
|
if (!pixelEnabled || existingLink || !config.pixelCssHref) return;
|
|
|
|
const link = document.createElement("link");
|
|
link.id = "terminal-pixel-font";
|
|
link.dataset.noPjax = "";
|
|
link.rel = "stylesheet";
|
|
link.href = config.pixelCssHref;
|
|
document.head.appendChild(link);
|
|
}
|
|
|
|
function applyCustomCss(text = config.customCss) {
|
|
if (!text || document.getElementById("terminal-custom-css")) return;
|
|
|
|
const style = document.createElement("style");
|
|
style.id = "terminal-custom-css";
|
|
style.textContent = text;
|
|
document.head.appendChild(style);
|
|
}
|
|
|
|
function syncThemeMode() {
|
|
const storedMode = getStoredPreference(storageKeys.themeMode)
|
|
|| config.defaultMode
|
|
|| "system";
|
|
|
|
applyTheme(storedMode);
|
|
}
|
|
|
|
window.applyTheme = applyTheme;
|
|
window.TerminalTheme = {
|
|
applyTheme,
|
|
applyColorScheme,
|
|
applyCustomCss,
|
|
storage: {
|
|
get: getStoredPreference,
|
|
set: setStoredPreference,
|
|
remove: removeStoredPreference
|
|
},
|
|
syncPixelFont,
|
|
syncPixelStyle
|
|
};
|
|
|
|
migrateStoredPreference("pixel_style", storageKeys.pixelStyle);
|
|
syncSystemFont();
|
|
syncThemeMode();
|
|
syncPixelStyle();
|
|
syncPixelFont();
|
|
|
|
window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", () => {
|
|
const mode = getStoredPreference(storageKeys.themeMode)
|
|
|| config.defaultMode
|
|
|| "system";
|
|
if (mode === "system") {
|
|
applyTheme("system");
|
|
}
|
|
});
|
|
})();
|
|
</script>
|
|
</th:block>
|