优化 components 添加顺序
This commit is contained in:
parent
26950a37bb
commit
9b46a86d5d
13
cache/map.go
vendored
13
cache/map.go
vendored
|
@ -44,21 +44,20 @@ func (m *MapCache[K, V]) setCacheFn(fn func(...any) (map[K]V, error)) {
|
||||||
m.cacheFunc = func(a ...any) (V, error) {
|
m.cacheFunc = func(a ...any) (V, error) {
|
||||||
var err error
|
var err error
|
||||||
var r map[K]V
|
var r map[K]V
|
||||||
var id K
|
var k K
|
||||||
ctx, ok := a[0].(context.Context)
|
ctx, ok := a[0].(context.Context)
|
||||||
if ok {
|
if ok {
|
||||||
id = a[1].(K)
|
k, ok = a[1].(K)
|
||||||
r, err = fn(ctx, []K{id})
|
if ok {
|
||||||
} else {
|
r, err = fn(ctx, []K{k})
|
||||||
id = a[0].(K)
|
}
|
||||||
r, err = fn([]K{id})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
var rr V
|
var rr V
|
||||||
return rr, err
|
return rr, err
|
||||||
}
|
}
|
||||||
return r[id], err
|
return r[k], err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,4 +17,5 @@ const (
|
||||||
Defaults = "default"
|
Defaults = "default"
|
||||||
|
|
||||||
HeadScript = "headScript"
|
HeadScript = "headScript"
|
||||||
|
FooterScript = "footerScript"
|
||||||
)
|
)
|
||||||
|
|
|
@ -26,7 +26,7 @@ type Handle struct {
|
||||||
Stats int
|
Stats int
|
||||||
Templ string
|
Templ string
|
||||||
Class []string
|
Class []string
|
||||||
Scripts map[string][]func(*Handle) string
|
Components map[string][]Components
|
||||||
ThemeMods wpconfig.ThemeMods
|
ThemeMods wpconfig.ThemeMods
|
||||||
HandleFns []HandleFn[*Handle]
|
HandleFns []HandleFn[*Handle]
|
||||||
}
|
}
|
||||||
|
@ -40,16 +40,27 @@ func NewHandle(c *gin.Context, scene int, theme string) *Handle {
|
||||||
Session: sessions.Default(c),
|
Session: sessions.Default(c),
|
||||||
GinH: gin.H{},
|
GinH: gin.H{},
|
||||||
Scene: scene,
|
Scene: scene,
|
||||||
|
Stats: constraints.Ok,
|
||||||
ThemeMods: mods,
|
ThemeMods: mods,
|
||||||
Scripts: make(map[string][]func(*Handle) string),
|
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]) {
|
func (h *Handle) PushHandleFn(fns ...HandleFn[*Handle]) {
|
||||||
h.HandleFns = append(h.HandleFns, fns...)
|
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)
|
v, ok := reload.GetStr(name)
|
||||||
if !ok {
|
if !ok {
|
||||||
v = fn(h)
|
v = fn(h)
|
||||||
|
@ -58,12 +69,11 @@ func (h *Handle) PlushComponent(name string, fn func(*Handle) string) {
|
||||||
h.GinH[name] = v
|
h.GinH[name] = v
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handle) PushHeadScript(name string, fn ...func(*Handle) string) {
|
func (h *Handle) PushHeadScript(fn ...Components) {
|
||||||
h.Scripts[name] = append(h.Scripts[name], fn...)
|
h.Components[constraints.HeadScript] = append(h.Components[constraints.HeadScript], fn...)
|
||||||
}
|
}
|
||||||
|
func (h *Handle) PushFooterScript(fn ...Components) {
|
||||||
func Default[T any](t T) T {
|
h.Components[constraints.FooterScript] = append(h.Components[constraints.FooterScript], fn...)
|
||||||
return t
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handle) GetPassword() {
|
func (h *Handle) GetPassword() {
|
||||||
|
@ -105,19 +115,26 @@ func (h *Handle) Render() {
|
||||||
h.PreCodeAndStats()
|
h.PreCodeAndStats()
|
||||||
h.PreTemplate()
|
h.PreTemplate()
|
||||||
h.ExecHandleFns()
|
h.ExecHandleFns()
|
||||||
h.PushHeadScript(constraints.HeadScript, CalSiteIcon, CalCustomCss)
|
h.PushHeadScript(Components{CalSiteIcon, 10}, Components{CalCustomCss, -1})
|
||||||
h.PlushComponent("customLogo", CalCustomLogo)
|
h.AddComponent("customLogo", CalCustomLogo)
|
||||||
h.CalMultipleScript()
|
h.CalMultipleComponents()
|
||||||
h.CalBodyClass()
|
h.CalBodyClass()
|
||||||
h.C.HTML(h.Code, h.Templ, h.GinH)
|
h.C.HTML(h.Code, h.Templ, h.GinH)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handle) CalMultipleScript() {
|
func (h *Handle) PushComponents(name string, components ...Components) {
|
||||||
for k, ss := range h.Scripts {
|
h.Components[name] = append(h.Components[name], components...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handle) CalMultipleComponents() {
|
||||||
|
for k, ss := range h.Components {
|
||||||
v, ok := reload.GetStr(k)
|
v, ok := reload.GetStr(k)
|
||||||
if !ok {
|
if !ok {
|
||||||
v = strings.Join(slice.FilterAndMap(ss, func(t func(*Handle) string) (string, bool) {
|
slice.SortSelf(ss, func(i, j Components) bool {
|
||||||
s := t(h)
|
return i.Order > j.Order
|
||||||
|
})
|
||||||
|
v = strings.Join(slice.FilterAndMap(ss, func(t Components) (string, bool) {
|
||||||
|
s := t.Fn(h)
|
||||||
return s, s != ""
|
return s, s != ""
|
||||||
}), "\n")
|
}), "\n")
|
||||||
reload.SetStr(k, v)
|
reload.SetStr(k, v)
|
||||||
|
|
|
@ -37,7 +37,10 @@ func Hook(h *common.Handle) {
|
||||||
func dispatch(next common.HandleFn[*common.Handle], h *common.Handle) {
|
func dispatch(next common.HandleFn[*common.Handle], h *common.Handle) {
|
||||||
h.WidgetAreaData()
|
h.WidgetAreaData()
|
||||||
h.GetPassword()
|
h.GetPassword()
|
||||||
h.PushHeadScript(constraints.HeadScript, CalCustomBackGround, colorSchemeCss)
|
h.PushHeadScript(
|
||||||
|
common.NewComponents(CalCustomBackGround, 10),
|
||||||
|
common.NewComponents(colorSchemeCss, 10),
|
||||||
|
)
|
||||||
h.PushHandleFn(customHeader)
|
h.PushHandleFn(customHeader)
|
||||||
switch h.Scene {
|
switch h.Scene {
|
||||||
case constraints.Detail:
|
case constraints.Detail:
|
||||||
|
|
|
@ -37,7 +37,9 @@ func dispatch(next common.HandleFn[*common.Handle], h *common.Handle) {
|
||||||
h.WidgetAreaData()
|
h.WidgetAreaData()
|
||||||
h.GetPassword()
|
h.GetPassword()
|
||||||
h.PushHandleFn(calClass)
|
h.PushHandleFn(calClass)
|
||||||
h.PushHeadScript(constraints.HeadScript, colorScheme)
|
h.PushHeadScript(
|
||||||
|
common.NewComponents(colorScheme, 10),
|
||||||
|
)
|
||||||
h.GinH["HeaderImage"] = getHeaderImage(h)
|
h.GinH["HeaderImage"] = getHeaderImage(h)
|
||||||
switch h.Scene {
|
switch h.Scene {
|
||||||
case constraints.Detail:
|
case constraints.Detail:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user