refactor: extract theme preferences module
This commit is contained in:
@@ -8,206 +8,7 @@
|
|||||||
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
|
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
||||||
<title th:text="${title}"></title>
|
<title th:text="${title}"></title>
|
||||||
<script no-pjax th:inline="javascript">
|
<th:block th:replace="~{modules/theme-preferences :: theme-preferences}"/>
|
||||||
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}]]*/ ""
|
|
||||||
};
|
|
||||||
</script>
|
|
||||||
<script 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.setAttribute("data-color-scheme", actualMode);
|
|
||||||
root.setAttribute("theme-color-scheme", getStoredColorScheme() || resolveTheme(actualMode));
|
|
||||||
}
|
|
||||||
|
|
||||||
function applyColorScheme(scheme) {
|
|
||||||
if (!scheme || scheme === "default") {
|
|
||||||
removeStoredPreference(storageKeys.colorScheme);
|
|
||||||
syncThemeMode();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
setStoredPreference(storageKeys.colorScheme, scheme);
|
|
||||||
root.setAttribute("theme-color-scheme", scheme);
|
|
||||||
}
|
|
||||||
|
|
||||||
function syncSystemFont() {
|
|
||||||
root.setAttribute("system_font_enable", String(config.systemFontEnabled));
|
|
||||||
}
|
|
||||||
|
|
||||||
function syncPixelStyle() {
|
|
||||||
const storedStatus = getStoredPreference(storageKeys.pixelStyle)
|
|
||||||
|| String(config.pixelStyle);
|
|
||||||
|
|
||||||
root.setAttribute("pixel_style", storedStatus);
|
|
||||||
}
|
|
||||||
|
|
||||||
function syncPixelFont() {
|
|
||||||
const existingLink = document.getElementById("terminal-pixel-font");
|
|
||||||
const pixelEnabled = root.getAttribute("pixel_style") === "true";
|
|
||||||
|
|
||||||
if (!pixelEnabled && existingLink) {
|
|
||||||
existingLink.remove();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!pixelEnabled || existingLink || !config.pixelCssHref) return;
|
|
||||||
|
|
||||||
const link = document.createElement("link");
|
|
||||||
link.id = "terminal-pixel-font";
|
|
||||||
link.setAttribute("no-pjax", "");
|
|
||||||
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>
|
|
||||||
<link no-pjax rel="stylesheet" th:href="@{/assets/dist/main.css?v={version}(version=${theme.spec.version})}"/>
|
<link no-pjax rel="stylesheet" th:href="@{/assets/dist/main.css?v={version}(version=${theme.spec.version})}"/>
|
||||||
<script no-pjax type="module" th:src="@{/assets/dist/main.js?v={version}(version=${theme.spec.version})}"></script>
|
<script no-pjax type="module" th:src="@{/assets/dist/main.js?v={version}(version=${theme.spec.version})}"></script>
|
||||||
<script no-pjax type="module" th:if="${theme.config.basic.pjax_enable}"
|
<script no-pjax type="module" th:if="${theme.config.basic.pjax_enable}"
|
||||||
|
|||||||
@@ -0,0 +1,202 @@
|
|||||||
|
<th:block th:fragment="theme-preferences">
|
||||||
|
<script 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}]]*/ ""
|
||||||
|
};
|
||||||
|
</script>
|
||||||
|
<script 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.setAttribute("data-color-scheme", actualMode);
|
||||||
|
root.setAttribute("theme-color-scheme", getStoredColorScheme() || resolveTheme(actualMode));
|
||||||
|
}
|
||||||
|
|
||||||
|
function applyColorScheme(scheme) {
|
||||||
|
if (!scheme || scheme === "default") {
|
||||||
|
removeStoredPreference(storageKeys.colorScheme);
|
||||||
|
syncThemeMode();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setStoredPreference(storageKeys.colorScheme, scheme);
|
||||||
|
root.setAttribute("theme-color-scheme", scheme);
|
||||||
|
}
|
||||||
|
|
||||||
|
function syncSystemFont() {
|
||||||
|
root.setAttribute("system_font_enable", String(config.systemFontEnabled));
|
||||||
|
}
|
||||||
|
|
||||||
|
function syncPixelStyle() {
|
||||||
|
const storedStatus = getStoredPreference(storageKeys.pixelStyle)
|
||||||
|
|| String(config.pixelStyle);
|
||||||
|
|
||||||
|
root.setAttribute("pixel_style", storedStatus);
|
||||||
|
}
|
||||||
|
|
||||||
|
function syncPixelFont() {
|
||||||
|
const existingLink = document.getElementById("terminal-pixel-font");
|
||||||
|
const pixelEnabled = root.getAttribute("pixel_style") === "true";
|
||||||
|
|
||||||
|
if (!pixelEnabled && existingLink) {
|
||||||
|
existingLink.remove();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!pixelEnabled || existingLink || !config.pixelCssHref) return;
|
||||||
|
|
||||||
|
const link = document.createElement("link");
|
||||||
|
link.id = "terminal-pixel-font";
|
||||||
|
link.setAttribute("no-pjax", "");
|
||||||
|
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>
|
||||||
Reference in New Issue
Block a user