-
Notifications
You must be signed in to change notification settings - Fork 0
/
themecatalog.go
105 lines (98 loc) · 3.28 KB
/
themecatalog.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Content managed by Project Forge, see [projectforge.md] for details.
package clib
import (
"fmt"
"strings"
"github.com/muesli/gamut"
"github.com/pkg/errors"
"github.com/samber/lo"
"github.com/valyala/fasthttp"
"admini.dev/admini/app"
"admini.dev/admini/app/controller"
"admini.dev/admini/app/controller/cutil"
"admini.dev/admini/app/lib/telemetry"
"admini.dev/admini/app/lib/theme"
"admini.dev/admini/app/util"
"admini.dev/admini/views"
"admini.dev/admini/views/vtheme"
)
func ThemeColor(rc *fasthttp.RequestCtx) {
controller.Act("theme.color", rc, func(as *app.State, ps *cutil.PageState) (string, error) {
col, err := cutil.RCRequiredString(rc, "color", false)
if err != nil {
return "", err
}
col = strings.ToLower(col)
if !strings.HasPrefix(col, "#") {
col = "#" + col
}
ps.Title = fmt.Sprintf("[%s] Theme", col)
x := theme.ColorTheme(col, gamut.Hex(col))
ps.Data = x
return controller.Render(rc, as, &vtheme.Edit{Theme: x, Icon: "app"}, ps, "Themes||/theme", col)
})
}
func ThemeColorEdit(rc *fasthttp.RequestCtx) {
controller.Act("theme.color.edit", rc, func(as *app.State, ps *cutil.PageState) (string, error) {
color := string(rc.URI().QueryArgs().Peek("color"))
if color == "" {
return "", errors.New("must provide color in query string")
}
if !strings.HasPrefix(color, "#") {
return "", errors.New("provided color must be a hex string")
}
t := theme.ColorTheme(strings.TrimPrefix(color, "#"), gamut.Hex(color))
ps.Data = t
ps.Title = "Edit theme [" + t.Key + "]"
page := &vtheme.Edit{Theme: t, Icon: "app", Exists: as.Themes.FileExists(t.Key)}
return controller.Render(rc, as, page, ps, "Themes||/theme", t.Key)
})
}
func ThemePalette(rc *fasthttp.RequestCtx) {
controller.Act("theme.palette", rc, func(as *app.State, ps *cutil.PageState) (string, error) {
pal, err := cutil.RCRequiredString(rc, "palette", false)
if err != nil {
return "", err
}
ps.Title = fmt.Sprintf("[%s] Themes", pal)
_, span, _ := telemetry.StartSpan(ps.Context, "theme:load", ps.Logger)
x, err := theme.PaletteThemes(pal)
span.Complete()
if err != nil {
return "", err
}
if string(rc.URI().QueryArgs().Peek("t")) == "go" {
ps.Data = strings.Join(lo.Map(x, func(t *theme.Theme, _ int) string {
return t.ToGo()
}), util.StringDefaultLinebreak)
return controller.Render(rc, as, &views.Debug{}, ps, "Themes")
}
return controller.Render(rc, as, &vtheme.Add{Palette: pal, Themes: x}, ps, "Themes")
})
}
func ThemePaletteEdit(rc *fasthttp.RequestCtx) {
controller.Act("theme.palette.edit", rc, func(as *app.State, ps *cutil.PageState) (string, error) {
palette, err := cutil.RCRequiredString(rc, "palette", false)
if err != nil {
return "", err
}
key, err := cutil.RCRequiredString(rc, "theme", false)
if err != nil {
return "", err
}
if key == theme.ThemeDefault.Key {
return controller.FlashAndRedir(false, "Unable to edit default theme", "/theme", rc, ps)
}
themes, err := theme.PaletteThemes(palette)
if err != nil {
return "", err
}
t := themes.Get(key)
if t == nil {
return "", errors.Errorf("invalid theme [%s] for palette [%s]", key, palette)
}
ps.Data = t
ps.Title = "Edit theme [" + t.Key + "]"
return controller.Render(rc, as, &vtheme.Edit{Theme: t, Icon: "app"}, ps, "Themes||/theme", t.Key)
})
}