优化 components 添加顺序

This commit is contained in:
xing 2023-02-28 15:17:16 +08:00
parent 26950a37bb
commit 9b46a86d5d
5 changed files with 67 additions and 45 deletions

13
cache/map.go vendored
View File

@ -44,21 +44,20 @@ func (m *MapCache[K, V]) setCacheFn(fn func(...any) (map[K]V, error)) {
m.cacheFunc = func(a ...any) (V, error) {
var err error
var r map[K]V
var id K
var k K
ctx, ok := a[0].(context.Context)
if ok {
id = a[1].(K)
r, err = fn(ctx, []K{id})
} else {
id = a[0].(K)
r, err = fn([]K{id})
k, ok = a[1].(K)
if ok {
r, err = fn(ctx, []K{k})
}
}
if err != nil {
var rr V
return rr, err
}
return r[id], err
return r[k], err
}
}

View File

@ -16,5 +16,6 @@ const (
Defaults = "default"
HeadScript = "headScript"
HeadScript = "headScript"
FooterScript = "footerScript"
)

View File

@ -14,42 +14,53 @@ import (
)
type Handle struct {
Index *IndexHandle
Detail *DetailHandle
C *gin.Context
Theme string
Session sessions.Session
GinH gin.H
Password string
Scene int
Code int
Stats int
Templ string
Class []string
Scripts map[string][]func(*Handle) string
ThemeMods wpconfig.ThemeMods
HandleFns []HandleFn[*Handle]
Index *IndexHandle
Detail *DetailHandle
C *gin.Context
Theme string
Session sessions.Session
GinH gin.H
Password string
Scene int
Code int
Stats int
Templ string
Class []string
Components map[string][]Components
ThemeMods wpconfig.ThemeMods
HandleFns []HandleFn[*Handle]
}
func NewHandle(c *gin.Context, scene int, theme string) *Handle {
mods, err := wpconfig.GetThemeMods(theme)
logs.ErrPrintln(err, "获取mods失败")
return &Handle{
C: c,
Theme: theme,
Session: sessions.Default(c),
GinH: gin.H{},
Scene: scene,
ThemeMods: mods,
Scripts: make(map[string][]func(*Handle) string),
C: c,
Theme: theme,
Session: sessions.Default(c),
GinH: gin.H{},
Scene: scene,
Stats: constraints.Ok,
ThemeMods: mods,
Components: make(map[string][]Components),
}
}
// Components Order 为执行顺序,降序执行
type Components struct {
Fn func(*Handle) string
Order int
}
func NewComponents(fn func(*Handle) string, order int) Components {
return Components{Fn: fn, Order: order}
}
func (h *Handle) PushHandleFn(fns ...HandleFn[*Handle]) {
h.HandleFns = append(h.HandleFns, fns...)
}
func (h *Handle) PlushComponent(name string, fn func(*Handle) string) {
func (h *Handle) AddComponent(name string, fn func(*Handle) string) {
v, ok := reload.GetStr(name)
if !ok {
v = fn(h)
@ -58,12 +69,11 @@ func (h *Handle) PlushComponent(name string, fn func(*Handle) string) {
h.GinH[name] = v
}
func (h *Handle) PushHeadScript(name string, fn ...func(*Handle) string) {
h.Scripts[name] = append(h.Scripts[name], fn...)
func (h *Handle) PushHeadScript(fn ...Components) {
h.Components[constraints.HeadScript] = append(h.Components[constraints.HeadScript], fn...)
}
func Default[T any](t T) T {
return t
func (h *Handle) PushFooterScript(fn ...Components) {
h.Components[constraints.FooterScript] = append(h.Components[constraints.FooterScript], fn...)
}
func (h *Handle) GetPassword() {
@ -105,19 +115,26 @@ func (h *Handle) Render() {
h.PreCodeAndStats()
h.PreTemplate()
h.ExecHandleFns()
h.PushHeadScript(constraints.HeadScript, CalSiteIcon, CalCustomCss)
h.PlushComponent("customLogo", CalCustomLogo)
h.CalMultipleScript()
h.PushHeadScript(Components{CalSiteIcon, 10}, Components{CalCustomCss, -1})
h.AddComponent("customLogo", CalCustomLogo)
h.CalMultipleComponents()
h.CalBodyClass()
h.C.HTML(h.Code, h.Templ, h.GinH)
}
func (h *Handle) CalMultipleScript() {
for k, ss := range h.Scripts {
func (h *Handle) PushComponents(name string, components ...Components) {
h.Components[name] = append(h.Components[name], components...)
}
func (h *Handle) CalMultipleComponents() {
for k, ss := range h.Components {
v, ok := reload.GetStr(k)
if !ok {
v = strings.Join(slice.FilterAndMap(ss, func(t func(*Handle) string) (string, bool) {
s := t(h)
slice.SortSelf(ss, func(i, j Components) bool {
return i.Order > j.Order
})
v = strings.Join(slice.FilterAndMap(ss, func(t Components) (string, bool) {
s := t.Fn(h)
return s, s != ""
}), "\n")
reload.SetStr(k, v)

View File

@ -37,7 +37,10 @@ func Hook(h *common.Handle) {
func dispatch(next common.HandleFn[*common.Handle], h *common.Handle) {
h.WidgetAreaData()
h.GetPassword()
h.PushHeadScript(constraints.HeadScript, CalCustomBackGround, colorSchemeCss)
h.PushHeadScript(
common.NewComponents(CalCustomBackGround, 10),
common.NewComponents(colorSchemeCss, 10),
)
h.PushHandleFn(customHeader)
switch h.Scene {
case constraints.Detail:

View File

@ -37,7 +37,9 @@ func dispatch(next common.HandleFn[*common.Handle], h *common.Handle) {
h.WidgetAreaData()
h.GetPassword()
h.PushHandleFn(calClass)
h.PushHeadScript(constraints.HeadScript, colorScheme)
h.PushHeadScript(
common.NewComponents(colorScheme, 10),
)
h.GinH["HeaderImage"] = getHeaderImage(h)
switch h.Scene {
case constraints.Detail: