Add built-in white and black themes

This commit is contained in:
eezd committed 2026-10-20 10:26:49 +08:00
1 parent 7f65162115
commit 9fd7e2a948
6 files changed
+103 -5

No files matched your search

+33
View File
@@ -1,6 +1,7 @@
package api
import (
_ "embed"
"encoding/json"
"errors"
"fmt"
@@ -19,6 +20,29 @@ type themeManifest struct {
Version string `json:"version"`
}
//go:embed themes/white.css
var whiteThemeCSS string
//go:embed themes/black.css
var blackThemeCSS string
var builtinThemes = []struct {
themeManifest
css string
}{
{themeManifest{"white", "白色", "1"}, whiteThemeCSS},
{themeManifest{"black", "黑色", "1"}, blackThemeCSS},
}
func builtinTheme(id string) (themeManifest, string, bool) {
for _, theme := range builtinThemes {
if theme.ID == id {
return theme.themeManifest, theme.css, true
}
}
return themeManifest{}, "", false
}
func readThemeFile(root *os.Root, name string, limit int64) ([]byte, error) {
f, err := root.Open(name)
if err != nil {
@@ -43,6 +67,9 @@ func readThemeFile(root *os.Root, name string, limit int64) ([]byte, error) {
}
func (s *Server) readTheme(id string) (themeManifest, string, error) {
if manifest, css, ok := builtinTheme(id); ok {
return manifest, css, nil
}
var manifest themeManifest
if !themeIDPattern.MatchString(id) || id == "default" || s.ThemesDir == "" {
return manifest, "", fs.ErrNotExist
@@ -73,6 +100,9 @@ func (s *Server) readTheme(id string) (themeManifest, string, error) {
func (s *Server) listThemes(w http.ResponseWriter, r *http.Request) {
themes := []themeManifest{{ID: "default", Name: "默认", Version: "1"}}
for _, theme := range builtinThemes {
themes = append(themes, theme.themeManifest)
}
if s.ThemesDir != "" {
entries, err := os.ReadDir(s.ThemesDir)
if err != nil && !errors.Is(err, fs.ErrNotExist) {
@@ -80,6 +110,9 @@ func (s *Server) listThemes(w http.ResponseWriter, r *http.Request) {
return
}
for _, entry := range entries {
if _, _, ok := builtinTheme(entry.Name()); ok {
continue
}
if !entry.IsDir() || !themeIDPattern.MatchString(entry.Name()) || entry.Name() == "default" {
continue
}