From ec4dfad86a5f094cc175a3e86b35a5ddcae03306 Mon Sep 17 00:00:00 2001 From: xing Date: Sat, 18 Mar 2023 21:02:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20fix=20bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../theme/twentyseventeen/twentyseventeen.go | 2 +- .../theme/wp/components/widgetareadata.go | 13 ++++-- internal/theme/wp/wp.go | 40 +++++++++---------- plugin/digest/digest.go | 6 +-- 4 files changed, 33 insertions(+), 28 deletions(-) diff --git a/internal/theme/twentyseventeen/twentyseventeen.go b/internal/theme/twentyseventeen/twentyseventeen.go index ce2cb68..2a054c9 100644 --- a/internal/theme/twentyseventeen/twentyseventeen.go +++ b/internal/theme/twentyseventeen/twentyseventeen.go @@ -55,7 +55,7 @@ func ready(next wp.HandleFn[*wp.Handle], h *wp.Handle) { h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(calClass, 20)) h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(index, 100)) h.PushHandleFn(constraints.Detail, wp.NewHandleFn(detail, 100)) - h.PushGroupHandleFn(constraints.AllStats, 99, wp.PreCodeAndStats, wp.PreTemplate, errorsHandle) + h.PushGroupHandleFn(constraints.AllStats, 90, wp.PreCodeAndStats, wp.PreTemplate, errorsHandle) h.PushCacheGroupHeadScript("colorScheme-customHeader", 10, colorScheme, customHeader) h.PushHandleFn(constraints.Ok, wp.NewHandleFn(components.WidgetArea, 20)) pushScripts(h) diff --git a/internal/theme/wp/components/widgetareadata.go b/internal/theme/wp/components/widgetareadata.go index a9cc551..ef42ad8 100644 --- a/internal/theme/wp/components/widgetareadata.go +++ b/internal/theme/wp/components/widgetareadata.go @@ -3,6 +3,7 @@ package components import ( "fmt" "github.com/fthvgb1/wp-go/helper/slice" + "github.com/fthvgb1/wp-go/internal/cmd/reload" "github.com/fthvgb1/wp-go/internal/pkg/cache" "github.com/fthvgb1/wp-go/internal/pkg/constraints" "github.com/fthvgb1/wp-go/internal/pkg/constraints/widgets" @@ -12,7 +13,7 @@ import ( "strings" ) -var widgetFn = map[string]wp.Components{ +var widgetFn = map[string]wp.Components[string]{ "search": {Fn: widget.Search, CacheKey: "widgetSearch"}, "recent-posts": {Fn: widget.RecentPosts}, "recent-comments": {Fn: widget.RecentComments}, @@ -27,6 +28,12 @@ type Widget struct { } func WidgetArea(h *wp.Handle) { + sidebar := reload.GetAnyValBys("sidebarWidgets", h, sidebars) + h.PushComponents(constraints.SidebarsWidgets, sidebar...) + h.SetData("categories", cache.CategoriesTags(h.C, constraints.Category)) +} + +func sidebars(h *wp.Handle) []wp.Components[string] { args := wp.GetComponentsArgs(h, widgets.Widget, map[string]string{}) beforeWidget, ok := args["{$before_widget}"] if !ok { @@ -35,7 +42,7 @@ func WidgetArea(h *wp.Handle) { delete(args, "{$before_widget}") } v := wpconfig.GetPHPArrayVal("sidebars_widgets", []any{}, "sidebar-1") - sidebar := slice.FilterAndMap(v, func(t any) (wp.Components, bool) { + return slice.FilterAndMap(v, func(t any) (wp.Components[string], bool) { vv := t.(string) ss := strings.Split(vv, "-") id := ss[len(ss)-1] @@ -60,6 +67,4 @@ func WidgetArea(h *wp.Handle) { } return components, false }) - h.PushComponents(constraints.SidebarsWidgets, sidebar...) - h.SetData("categories", cache.CategoriesTags(h.C, constraints.Category)) } diff --git a/internal/theme/wp/wp.go b/internal/theme/wp/wp.go index c14a59a..77eacaa 100644 --- a/internal/theme/wp/wp.go +++ b/internal/theme/wp/wp.go @@ -27,7 +27,7 @@ type Handle struct { Stats int templ string class []string - components map[string][]Components + components map[string][]Components[string] themeMods wpconfig.ThemeMods handleFns map[int][]HandleCall err error @@ -39,9 +39,9 @@ type Handle struct { type HandlePlugins map[string]HandleFn[*Handle] // Components Order 为执行顺序,降序执行 -type Components struct { - Str string - Fn func(*Handle) string +type Components[T any] struct { + Val T + Fn func(*Handle) T Order int CacheKey string } @@ -178,15 +178,15 @@ func NewHandle(c *gin.Context, scene int, theme string) *Handle { scene: scene, Stats: constraints.Ok, themeMods: mods, - components: make(map[string][]Components), + components: make(map[string][]Components[string]), handleFns: make(map[int][]HandleCall), componentsArgs: make(map[string]any), componentFilterFn: make(map[string][]func(*Handle, string) string), } } -func (h *Handle) NewCacheComponent(name string, order int, fn func(handle *Handle) string) Components { - return Components{Fn: fn, CacheKey: name, Order: order} +func (h *Handle) NewCacheComponent(name string, order int, fn func(handle *Handle) string) Components[string] { + return Components[string]{Fn: fn, CacheKey: name, Order: order} } func (h *Handle) PushHandleFn(statsOrScene int, fns ...HandleCall) { @@ -205,7 +205,7 @@ func (h *Handle) AddCacheComponent(name string, fn func(*Handle) string) { h.ginH[name] = reload.GetAnyValBys(name, h, fn) } -func (h *Handle) PushHeadScript(fn ...Components) { +func (h *Handle) PushHeadScript(fn ...Components[string]) { h.PushComponents(constraints.HeadScript, fn...) } func (h *Handle) PushGroupHeadScript(order int, str ...string) { @@ -215,7 +215,7 @@ func (h *Handle) PushCacheGroupHeadScript(key string, order int, fns ...func(*Ha h.PushGroupCacheComponentFn(constraints.HeadScript, key, order, fns...) } -func (h *Handle) PushFooterScript(fn ...Components) { +func (h *Handle) PushFooterScript(fn ...Components[string]) { h.PushComponents(constraints.FooterScript, fn...) } @@ -299,24 +299,24 @@ func (h *Handle) CommonComponents() { }, 0)) } -func (h *Handle) PushComponents(name string, components ...Components) { +func (h *Handle) PushComponents(name string, components ...Components[string]) { h.components[name] = append(h.components[name], components...) } -func (h *Handle) PushGroupComponentStrs(name string, order int, fns ...string) { - var calls []Components - for _, fn := range fns { - calls = append(calls, Components{ - Str: fn, +func (h *Handle) PushGroupComponentStrs(name string, order int, str ...string) { + var calls []Components[string] + for _, fn := range str { + calls = append(calls, Components[string]{ + Val: fn, Order: order, }) } h.components[name] = append(h.components[name], calls...) } func (h *Handle) PushGroupComponentFns(name string, order int, fns ...func(*Handle) string) { - var calls []Components + var calls []Components[string] for _, fn := range fns { - calls = append(calls, Components{ + calls = append(calls, Components[string]{ Fn: fn, Order: order, }) @@ -326,13 +326,13 @@ func (h *Handle) PushGroupComponentFns(name string, order int, fns ...func(*Hand func CalComponents(h *Handle) { for k, ss := range h.components { - slice.Sort(ss, func(i, j Components) bool { + slice.Sort(ss, func(i, j Components[string]) bool { return i.Order > j.Order }) var s []string for _, component := range ss { - if component.Str != "" { - s = append(s, component.Str) + if component.Val != "" { + s = append(s, component.Val) continue } if component.Fn != nil { diff --git a/plugin/digest/digest.go b/plugin/digest/digest.go index d9913fe..cec63b5 100644 --- a/plugin/digest/digest.go +++ b/plugin/digest/digest.go @@ -57,7 +57,7 @@ func Raw(str string, limit int, u string) string { } } - if end >= limit || i >= total-1 { + if end >= limit || i >= total { break } if ru[i] == l { @@ -71,8 +71,8 @@ func Raw(str string, limit int, u string) string { end++ } } - if i > total-1 { - i = total - 1 + if i > total { + i = total } content = string(ru[:i])