优化代码
This commit is contained in:
parent
57a21010a8
commit
322d2ebe0a
|
@ -21,6 +21,15 @@ func GetAnyValBy[T any](k string, fn func() T) T {
|
||||||
anyMap.Store(k, vv)
|
anyMap.Store(k, vv)
|
||||||
return vv
|
return vv
|
||||||
}
|
}
|
||||||
|
func GetAnyValBys[T, A any](k string, a A, fn func(A) T) T {
|
||||||
|
v, ok := anyMap.Load(k)
|
||||||
|
if ok {
|
||||||
|
return v.(T)
|
||||||
|
}
|
||||||
|
vv := fn(a)
|
||||||
|
anyMap.Store(k, vv)
|
||||||
|
return vv
|
||||||
|
}
|
||||||
|
|
||||||
func GetStrBy[T any](key, delimiter string, t T, fn ...func(T) string) string {
|
func GetStrBy[T any](key, delimiter string, t T, fn ...func(T) string) string {
|
||||||
v, ok := str.Load(key)
|
v, ok := str.Load(key)
|
||||||
|
|
|
@ -12,13 +12,18 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var widgetFn = map[string]func(*wp.Handle) string{
|
var widgetFn = map[string]wp.Components{
|
||||||
"search": widget.Search,
|
"search": {Fn: widget.Search, CacheKey: "widgetSearch"},
|
||||||
"recent-posts": widget.RecentPosts,
|
"recent-posts": {Fn: widget.RecentPosts},
|
||||||
"recent-comments": widget.RecentComments,
|
"recent-comments": {Fn: widget.RecentComments},
|
||||||
"archives": widget.Archive,
|
"archives": {Fn: widget.Archive},
|
||||||
"categories": widget.Category,
|
"categories": {Fn: widget.Category},
|
||||||
"meta": widget.Meta,
|
"meta": {Fn: widget.Meta, CacheKey: "widgetMeta"},
|
||||||
|
}
|
||||||
|
|
||||||
|
type Widget struct {
|
||||||
|
Fn func(*wp.Handle) string
|
||||||
|
CacheKey string
|
||||||
}
|
}
|
||||||
|
|
||||||
func WidgetArea(h *wp.Handle) {
|
func WidgetArea(h *wp.Handle) {
|
||||||
|
@ -30,12 +35,12 @@ func WidgetArea(h *wp.Handle) {
|
||||||
delete(args, "{$before_widget}")
|
delete(args, "{$before_widget}")
|
||||||
}
|
}
|
||||||
v := wpconfig.GetPHPArrayVal("sidebars_widgets", []any{}, "sidebar-1")
|
v := wpconfig.GetPHPArrayVal("sidebars_widgets", []any{}, "sidebar-1")
|
||||||
sidebar := slice.FilterAndMap(v, func(t any) (func(*wp.Handle) string, bool) {
|
sidebar := slice.FilterAndMap(v, func(t any) (wp.Components, bool) {
|
||||||
vv := t.(string)
|
vv := t.(string)
|
||||||
ss := strings.Split(vv, "-")
|
ss := strings.Split(vv, "-")
|
||||||
id := ss[len(ss)-1]
|
id := ss[len(ss)-1]
|
||||||
name := strings.Join(ss[0:len(ss)-1], "-")
|
name := strings.Join(ss[0:len(ss)-1], "-")
|
||||||
fn, ok := widgetFn[name]
|
components, ok := widgetFn[name]
|
||||||
if ok {
|
if ok {
|
||||||
if id != "2" {
|
if id != "2" {
|
||||||
wp.SetComponentsArgsForMap(h, name, "{$id}", id)
|
wp.SetComponentsArgsForMap(h, name, "{$id}", id)
|
||||||
|
@ -47,15 +52,14 @@ func WidgetArea(h *wp.Handle) {
|
||||||
}
|
}
|
||||||
wp.SetComponentsArgsForMap(h, name, "{$before_widget}", fmt.Sprintf(beforeWidget, vv, n))
|
wp.SetComponentsArgsForMap(h, name, "{$before_widget}", fmt.Sprintf(beforeWidget, vv, n))
|
||||||
}
|
}
|
||||||
if len(args) > 0 {
|
for k, val := range args {
|
||||||
for k, val := range args {
|
wp.SetComponentsArgsForMap(h, name, k, val)
|
||||||
wp.SetComponentsArgsForMap(h, name, k, val)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return fn, true
|
components.Order = 10
|
||||||
|
return components, true
|
||||||
}
|
}
|
||||||
return nil, false
|
return components, false
|
||||||
})
|
})
|
||||||
h.PushGroupComponentFns(constraints.SidebarsWidgets, 10, sidebar...)
|
h.PushComponents(constraints.SidebarsWidgets, sidebar...)
|
||||||
h.SetData("categories", cache.CategoriesTags(h.C, constraints.Category))
|
h.SetData("categories", cache.CategoriesTags(h.C, constraints.Category))
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,9 +40,10 @@ type HandlePlugins map[string]HandleFn[*Handle]
|
||||||
|
|
||||||
// Components Order 为执行顺序,降序执行
|
// Components Order 为执行顺序,降序执行
|
||||||
type Components struct {
|
type Components struct {
|
||||||
Str string
|
Str string
|
||||||
Fn func(*Handle) string
|
Fn func(*Handle) string
|
||||||
Order int
|
Order int
|
||||||
|
CacheKey string
|
||||||
}
|
}
|
||||||
|
|
||||||
type HandleFn[T any] func(T)
|
type HandleFn[T any] func(T)
|
||||||
|
@ -185,7 +186,7 @@ func NewHandle(c *gin.Context, scene int, theme string) *Handle {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handle) NewCacheComponent(name string, order int, fn func(handle *Handle) string) Components {
|
func (h *Handle) NewCacheComponent(name string, order int, fn func(handle *Handle) string) Components {
|
||||||
return Components{Str: h.CacheStr(name, fn), Order: order}
|
return Components{Fn: fn, CacheKey: name, Order: order}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handle) PushHandleFn(statsOrScene int, fns ...HandleCall) {
|
func (h *Handle) PushHandleFn(statsOrScene int, fns ...HandleCall) {
|
||||||
|
@ -201,13 +202,7 @@ func (h *Handle) PushGroupHandleFn(statsOrScene, order int, fns ...HandleFn[*Han
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handle) AddCacheComponent(name string, fn func(*Handle) string) {
|
func (h *Handle) AddCacheComponent(name string, fn func(*Handle) string) {
|
||||||
h.ginH[name] = h.CacheStr(name, fn)
|
h.ginH[name] = reload.GetAnyValBys(name, h, fn)
|
||||||
}
|
|
||||||
|
|
||||||
func (h *Handle) CacheStr(name string, fn func(*Handle) string) string {
|
|
||||||
return reload.GetAnyValBy(name, func() string {
|
|
||||||
return fn(h)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handle) PushHeadScript(fn ...Components) {
|
func (h *Handle) PushHeadScript(fn ...Components) {
|
||||||
|
@ -298,7 +293,7 @@ func (h *Handle) Render() {
|
||||||
func (h *Handle) CommonComponents() {
|
func (h *Handle) CommonComponents() {
|
||||||
h.AddCacheComponent("customLogo", CalCustomLogo)
|
h.AddCacheComponent("customLogo", CalCustomLogo)
|
||||||
h.PushCacheGroupHeadScript("siteIconAndCustomCss", 0, CalSiteIcon, CalCustomCss)
|
h.PushCacheGroupHeadScript("siteIconAndCustomCss", 0, CalSiteIcon, CalCustomCss)
|
||||||
h.PushGroupHandleFn(constraints.AllStats, 10, CalMultipleComponents, CalBodyClass)
|
h.PushGroupHandleFn(constraints.AllStats, 10, CalComponents, CalBodyClass)
|
||||||
h.PushHandleFn(constraints.AllStats, NewHandleFn(func(h *Handle) {
|
h.PushHandleFn(constraints.AllStats, NewHandleFn(func(h *Handle) {
|
||||||
h.C.HTML(h.Code, h.templ, h.ginH)
|
h.C.HTML(h.Code, h.templ, h.ginH)
|
||||||
}, 0))
|
}, 0))
|
||||||
|
@ -329,21 +324,30 @@ func (h *Handle) PushGroupComponentFns(name string, order int, fns ...func(*Hand
|
||||||
h.components[name] = append(h.components[name], calls...)
|
h.components[name] = append(h.components[name], calls...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func CalMultipleComponents(h *Handle) {
|
func CalComponents(h *Handle) {
|
||||||
for k, ss := range h.components {
|
for k, ss := range h.components {
|
||||||
slice.Sort(ss, func(i, j Components) bool {
|
slice.Sort(ss, func(i, j Components) bool {
|
||||||
return i.Order > j.Order
|
return i.Order > j.Order
|
||||||
})
|
})
|
||||||
v := strings.Join(slice.FilterAndMap(ss, func(t Components) (string, bool) {
|
var s []string
|
||||||
s := t.Str
|
for _, component := range ss {
|
||||||
if s == "" && t.Fn != nil {
|
if component.Str != "" {
|
||||||
s = t.Fn(h)
|
s = append(s, component.Str)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
return s, s != ""
|
if component.Fn != nil {
|
||||||
}), "\n")
|
v := ""
|
||||||
kk := strings.Split(k, "_")
|
if component.CacheKey != "" {
|
||||||
key := kk[len(kk)-1]
|
v = reload.GetAnyValBys(component.CacheKey, h, component.Fn)
|
||||||
h.ginH[key] = v
|
} else {
|
||||||
|
v = component.Fn(h)
|
||||||
|
}
|
||||||
|
if v != "" {
|
||||||
|
s = append(s, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
h.ginH[k] = strings.Join(s, "\n")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user