feat: support custom css

This commit is contained in:
Erzbir
2025-08-05 16:21:00 +08:00
parent 6171834015
commit 41d2408359
2 changed files with 50 additions and 6 deletions
+28 -4
View File
@@ -46,9 +46,12 @@ spec:
value: "system" value: "system"
- $formkit: select - $formkit: select
name: dark_color_schema name: dark_color_schema
id: dark_color_schema
label: 深色模式配色方案 label: 深色模式配色方案
value: "night" value: "night"
options: options:
- label: "custom"
value: "custom"
- label: "day" - label: "day"
value: "day" value: "day"
- label: "night" - label: "night"
@@ -63,19 +66,27 @@ spec:
value: "hopbush" value: "hopbush"
- label: "pistachio" - label: "pistachio"
value: "pistachio" value: "pistachio"
- label: "hopbush"
value: "hopbush"
- label: "blue" - label: "blue"
value: "blue" value: "blue"
- label: "studio" - label: "studio"
value: "studio" value: "studio"
- label: "vanilla" - label: "vanilla"
value: "vanilla" value: "vanilla"
- $formkit: text
name: custom_dark
if: "$get(dark_color_schema).value === 'custom'"
value: ""
help: "在此填写自定义配色的名称"
validation: "required"
- $formkit: select - $formkit: select
name: light_color_schema name: light_color_schema
id: light_color_schema
label: 浅色模式配色方案 label: 浅色模式配色方案
value: "day" value: "day"
options: options:
- label: "custom"
value: "custom"
- label: "day" - label: "day"
value: "day" value: "day"
- label: "night" - label: "night"
@@ -90,14 +101,27 @@ spec:
value: "hopbush" value: "hopbush"
- label: "pistachio" - label: "pistachio"
value: "pistachio" value: "pistachio"
- label: "hopbush"
value: "hopbush"
- label: "blue" - label: "blue"
value: "blue" value: "blue"
- label: "studio" - label: "studio"
value: "studio" value: "studio"
- label: "vanilla" - label: "vanilla"
value: "vanilla" value: "vanilla"
- $formkit: text
name: custom_light
if: "$get(light_color_schema).value === 'custom'"
value: ""
help: "在此填写自定义配色的名称"
validation: "required"
- $formkit: code
name: custom
label: 自定义样式
language: css
height: 150px
value: ""
help: 可填写自定义的 CSS, 会覆盖主题的样式
- group: icp - group: icp
label: 备案设置 label: 备案设置
formSchema: formSchema:
+22 -2
View File
@@ -10,13 +10,23 @@
<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"> <script no-pjax th:inline="javascript">
const dark_theme = /*[[${theme.config.style.dark_color_schema}]]*/; let dark_theme = /*[[${theme.config.style.dark_color_schema}]]*/;
const light_theme = /*[[${theme.config.style.light_color_schema}]]*/; let light_theme = /*[[${theme.config.style.light_color_schema}]]*/;
const default_mode = /*[[${theme.config.style.default_schema_mode}]]*/; const default_mode = /*[[${theme.config.style.default_schema_mode}]]*/;
if (dark_theme === 'custom') {
dark_theme = /*[[${theme.config.style.custom_dark}]]*/
}
if (light_theme === 'custom') {
light_theme = /*[[${theme.config.style.custom_light}]]*/
}
let storedMode = localStorage.getItem("theme-mode"); let storedMode = localStorage.getItem("theme-mode");
function getSystemScheme() { function getSystemScheme() {
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'; return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
} }
function applyTheme(mode) { function applyTheme(mode) {
const actualMode = mode === 'system' ? getSystemScheme() : mode; const actualMode = mode === 'system' ? getSystemScheme() : mode;
const targetTheme = actualMode === "light" ? light_theme : dark_theme; const targetTheme = actualMode === "light" ? light_theme : dark_theme;
@@ -50,6 +60,16 @@
<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}" <script no-pjax type="module" th:if="${theme.config.basic.pjax}"
th:src="@{/assets/dist/libs/pjax.js?v={version}(version=${theme.spec.version})}"></script> th:src="@{/assets/dist/libs/pjax.js?v={version}(version=${theme.spec.version})}"></script>
<script no-pjax th:inline="javascript">
function applyRawCss(text) {
const style = document.createElement('style');
style.textContent = text;
document.head.appendChild(style);
}
const custom = /*[[${theme.config.style.custom}]]*/;
applyRawCss(custom);
</script>
</head> </head>
<body> <body>