-
Notifications
You must be signed in to change notification settings - Fork 4
/
themepreview.go
105 lines (96 loc) · 2.98 KB
/
themepreview.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
package clib
import (
"fmt"
"strings"
"github.com/muesli/gamut"
"github.com/valyala/fasthttp"
"projectforge.dev/projectforge/app"
"projectforge.dev/projectforge/app/controller"
"projectforge.dev/projectforge/app/controller/cutil"
"projectforge.dev/projectforge/app/lib/telemetry"
"projectforge.dev/projectforge/app/lib/theme"
"projectforge.dev/projectforge/app/util"
"projectforge.dev/projectforge/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}, ps, "admin", "Themes", col)
})
}
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
}
prj := string(rc.URI().QueryArgs().Peek("project"))
prjTitle := util.AppName
if prj != "" {
curr, _ := as.Services.Projects.Get(prj)
if curr != nil {
prjTitle = curr.Title()
}
}
ps.Title = fmt.Sprintf("[%s] Themes", pal)
_, span, _ := telemetry.StartSpan(ps.Context, "theme:load", ps.Logger)
x, err := theme.PaletteThemes(pal)
if err != nil {
return "", err
}
span.Complete()
ps.Data = x
return controller.Render(rc, as, &vtheme.Add{Project: prj, Palette: pal, Themes: x, Title: prjTitle}, ps, "admin", "Themes")
})
}
func ThemePreview(rc *fasthttp.RequestCtx) {
controller.Act("theme.preview", rc, func(as *app.State, ps *cutil.PageState) (string, error) {
pallette, thm, err := themeFromRC(rc)
if err != nil {
return "", err
}
if prj := string(rc.URI().QueryArgs().Peek("project")); prj != "" {
p, err := as.Services.Projects.Get(prj)
if err != nil {
return "", err
}
p.Theme = thm
err = as.Services.Projects.Save(p, ps.Logger)
if err != nil {
return "", err
}
msg := fmt.Sprintf("set theme to [%s:%s]", pallette, thm.Key)
return controller.FlashAndRedir(true, msg, "/p/"+p.Key, rc, ps)
}
ps.Title = fmt.Sprintf("Preview [%s]", thm.Key)
ps.Data = thm
page := &vtheme.Edit{Theme: thm}
return controller.Render(rc, as, page, ps, "admin", "Themes||/admin/theme", thm.Key)
})
}
func themeFromRC(rc *fasthttp.RequestCtx) (string, *theme.Theme, error) {
color, err := cutil.RCRequiredString(rc, "color", false)
if err != nil {
pal, err := cutil.RCRequiredString(rc, "palette", false)
if err != nil {
return "", nil, err
}
key, err := cutil.RCRequiredString(rc, "key", false)
if err != nil {
return "", nil, err
}
ret, err := theme.PaletteTheme(pal, key)
return pal, ret, err
}
return color, theme.ColorTheme(color, gamut.Hex(color)), nil
}