Files
pages/internal/server/theme.go
T
Anya Tokugawa c1175d389f Implement Knowledge City platform, organizer and DAV integration
Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming.

Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
2026-09-27 14:44:43 +09:00

115 lines
3.0 KiB
Go

package server
import (
"encoding/json"
"os"
"path/filepath"
"strings"
)
type ThemeOption struct {
ID string `json:"id"`
Label string `json:"label"`
CSS string `json:"css"`
}
func builtinThemeOptions() []ThemeOption {
return []ThemeOption{
{ID: "dark", Label: "Dark", CSS: "/static/css/theme/dark.css"},
{ID: "light", Label: "Light", CSS: "/static/css/theme/light.css"},
{ID: "monokai", Label: "Monokai", CSS: "/static/css/theme/monokai.css"},
{ID: "solarized-dark", Label: "Solarized Dark", CSS: "/static/css/theme/solarized-dark.css"},
{ID: "solarized-light", Label: "Solarized Light", CSS: "/static/css/theme/solarized-light.css"},
{ID: "terminal-green", Label: "Terminal Green", CSS: "/static/css/theme/terminal-green.css"},
{ID: "amber-crt", Label: "Amber CRT", CSS: "/static/css/theme/amber-crt.css"},
{ID: "lain-dark", Label: "Lain Dark", CSS: "/static/css/theme/lain-dark.css"},
{ID: "nanoha-dark", Label: "Nanoha Dark", CSS: "/static/css/theme/nanoha-dark.css"},
{ID: "fate-t-dark", Label: "Fate.T. (Fate Testarossa) Dark", CSS: "/static/css/theme/fate-t-dark.css"},
{ID: "index-white", Label: "Index White", CSS: "/static/css/theme/index-white.css"},
{ID: "index-dark", Label: "Index Dark", CSS: "/static/css/theme/index-dark.css"},
{ID: "railgun-white", Label: "Railgun White", CSS: "/static/css/theme/railgun-white.css"},
{ID: "railgun-dark", Label: "Railgun Dark", CSS: "/static/css/theme/railgun-dark.css"},
{ID: "accelerator-dark", Label: "Accelerator Dark", CSS: "/static/css/theme/accelerator-dark.css"},
}
}
func externalThemeOptions() []ThemeOption {
path := "/data/themes/themes.json"
b, err := os.ReadFile(path)
if err != nil {
return nil
}
var items []ThemeOption
if err := json.Unmarshal(b, &items); err != nil {
return nil
}
var out []ThemeOption
for _, item := range items {
item.ID = strings.TrimSpace(item.ID)
item.Label = strings.TrimSpace(item.Label)
item.CSS = strings.TrimSpace(item.CSS)
if item.ID == "" || item.Label == "" || item.CSS == "" {
continue
}
if strings.Contains(item.ID, "/") || strings.Contains(item.ID, "..") {
continue
}
if !strings.HasPrefix(item.CSS, "/") {
item.CSS = "/-/theme/" + filepath.Base(item.CSS)
}
out = append(out, item)
}
return out
}
func themeOptions() []ThemeOption {
items := builtinThemeOptions()
items = append(items, externalThemeOptions()...)
return items
}
func findTheme(id string) (ThemeOption, bool) {
for _, opt := range themeOptions() {
if opt.ID == id {
return opt, true
}
}
return ThemeOption{}, false
}
func isAllowedTheme(theme string) bool {
_, ok := findTheme(theme)
return ok
}
func (s *Server) getTheme(domainUUID ...string) string {
theme := s.getDomainConfig("theme", domainUUID...)
if theme == "" {
return "dark"
}
if !isAllowedTheme(theme) {
return "dark"
}
return theme
}
func (s *Server) getThemeCSS(domainUUID ...string) string {
opt, ok := findTheme(s.getTheme(domainUUID...))
if !ok {
return "/static/css/theme/dark.css"
}
return opt.CSS
}