优化 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) { 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
} }
} }

View File

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

View File

@ -14,42 +14,53 @@ import (
) )
type Handle struct { type Handle struct {
Index *IndexHandle Index *IndexHandle
Detail *DetailHandle Detail *DetailHandle
C *gin.Context C *gin.Context
Theme string Theme string
Session sessions.Session Session sessions.Session
GinH gin.H GinH gin.H
Password string Password string
Scene int Scene int
Code int Code int
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]
} }
func NewHandle(c *gin.Context, scene int, theme string) *Handle { func NewHandle(c *gin.Context, scene int, theme string) *Handle {
mods, err := wpconfig.GetThemeMods(theme) mods, err := wpconfig.GetThemeMods(theme)
logs.ErrPrintln(err, "获取mods失败") logs.ErrPrintln(err, "获取mods失败")
return &Handle{ return &Handle{
C: c, C: c,
Theme: theme, Theme: theme,
Session: sessions.Default(c), Session: sessions.Default(c),
GinH: gin.H{}, GinH: gin.H{},
Scene: scene, Scene: scene,
ThemeMods: mods, Stats: constraints.Ok,
Scripts: make(map[string][]func(*Handle) string), 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]) { 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)

View File

@ -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:

View File

@ -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: