Compare commits

...

2 Commits

Author SHA1 Message Date
00c16c03a4 小优化 2023-03-18 22:26:01 +08:00
ec4dfad86a 优化 fix bug 2023-03-18 21:02:24 +08:00
4 changed files with 49 additions and 49 deletions

View File

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

View File

@ -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},
@ -21,12 +22,13 @@ var widgetFn = map[string]wp.Components{
"meta": {Fn: widget.Meta, CacheKey: "widgetMeta"},
}
type Widget struct {
Fn func(*wp.Handle) string
CacheKey string
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 WidgetArea(h *wp.Handle) {
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,13 +37,15 @@ 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]
name := strings.Join(ss[0:len(ss)-1], "-")
components, ok := widgetFn[name]
if ok {
if !ok {
return components, false
}
if id != "2" {
wp.SetComponentsArgsForMap(h, name, "{$id}", id)
}
@ -57,9 +61,5 @@ func WidgetArea(h *wp.Handle) {
}
components.Order = 10
return components, true
}
return components, false
})
h.PushComponents(constraints.SidebarsWidgets, sidebar...)
h.SetData("categories", cache.CategoriesTags(h.C, constraints.Category))
}

View File

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

View File

@ -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])