完善 components

This commit is contained in:
xing 2023-04-30 21:17:33 +08:00
parent da7dd410cd
commit 120c874a8d
10 changed files with 100 additions and 48 deletions

View File

@ -110,11 +110,14 @@ func Copy[K comparable, V any](m map[K]V) map[K]V {
func Merge[K comparable, V any](m ...map[K]V) map[K]V {
if len(m) < 1 {
panic("no map")
return nil
} else if len(m) < 2 {
return m[0]
}
mm := m[0]
if mm == nil {
mm = make(map[K]V)
}
for _, m2 := range m[1:] {
for k, v := range m2 {
mm[k] = v
@ -123,6 +126,31 @@ func Merge[K comparable, V any](m ...map[K]V) map[K]V {
return mm
}
func MergeBy[K comparable, V any](fn func(k K, v1, v2 V) (V, bool), m ...map[K]V) map[K]V {
if len(m) < 1 {
return nil
} else if len(m) < 2 {
return m[0]
}
mm := m[0]
if mm == nil {
mm = make(map[K]V)
}
for _, m2 := range m[1:] {
for k, v := range m2 {
vv, ok := mm[k]
if ok {
vvv, ok := fn(k, vv, v)
if ok {
v = vvv
}
}
mm[k] = v
}
}
return mm
}
func FilterZeroMerge[K comparable, V any](m ...map[K]V) map[K]V {
if len(m) < 1 {
panic("no map")
@ -130,6 +158,9 @@ func FilterZeroMerge[K comparable, V any](m ...map[K]V) map[K]V {
return m[0]
}
mm := m[0]
if mm == nil {
mm = make(map[K]V)
}
for _, m2 := range m[1:] {
for k, v := range m2 {
if helper.IsZeros(v) {

View File

@ -4,6 +4,7 @@ import (
"fmt"
"github.com/fthvgb1/wp-go/helper/maps"
"github.com/fthvgb1/wp-go/internal/phphelper"
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
"github.com/fthvgb1/wp-go/internal/pkg/logs"
"github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/fthvgb1/wp-go/internal/wpconfig"
@ -37,9 +38,9 @@ type Selectors struct {
}
func EnlighterJS(h *wp.Handle) {
h.PushGroupHeadScript("enlighterjs-css", 20, `<link rel='stylesheet' id='enlighterjs-css' href='/wp-content/plugins/enlighter/cache/enlighterjs.min.css' media='all' />`)
h.PushGroupHeadScript(constraints.AllScene, "enlighterjs-css", 20, `<link rel='stylesheet' id='enlighterjs-css' href='/wp-content/plugins/enlighter/cache/enlighterjs.min.css' media='all' />`)
h.PushCacheGroupFooterScript("enlighterJs", 10, func(h *wp.Handle) string {
h.PushCacheGroupFooterScript(constraints.AllScene, "enlighterJs", 10, func(h *wp.Handle) string {
op := wpconfig.GetOption("enlighter-options")
opp, err := phphelper.UnPHPSerializeToStrAnyMap(op)
if err != nil {

View File

@ -63,7 +63,8 @@ func configs(h *wp.Handle) {
})
h.Index.SetPageEle(plugins.TwentyFifteenPagination())
wphandle.UsePlugins(h)
h.PushCacheGroupHeadScript("CalCustomBackGround", 10, CalCustomBackGround, colorSchemeCss)
h.PushCacheGroupHeadScript(constraints.AllScene, "CalCustomBackGround", 10, CalCustomBackGround)
h.PushCacheGroupHeadScript(constraints.AllScene, "colorSchemeCss", 10, colorSchemeCss)
h.CommonComponents()
h.Index.SetListPlugin(wp.PostsPlugins(wp.PostPlugin(), wp.GetListPostPlugins(conf.ListPagePlugins, wp.ListPostPlugins())...))
components.WidgetArea(h)

View File

@ -2,19 +2,20 @@ package twentyseventeen
import (
"fmt"
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
"github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/fthvgb1/wp-go/internal/wpconfig"
)
func pushScripts(h *wp.Handle) {
h.PushCacheGroupHeadScript("{theme}.head", 30, func(h *wp.Handle) string {
h.PushCacheGroupHeadScript(constraints.AllScene, "{theme}.head", 30, func(h *wp.Handle) string {
head := headScript
if "dark" == wpconfig.GetThemeModsVal(ThemeName, "colorscheme", "light") {
head = fmt.Sprintf("%s\n%s", headScript, ` <link rel="stylesheet" id="twentyseventeen-colors-dark-css" href="/wp-content/themes/twentyseventeen/assets/css/colors-dark.css?ver=20191025" media="all">`)
}
return head
})
h.PushGroupFooterScript("{theme}.footer", 20, footerScript)
h.PushGroupFooterScript(constraints.AllScene, "{theme}.footer", 20, footerScript)
}

View File

@ -55,7 +55,7 @@ func configs(h *wp.Handle) {
conf := config.GetConfig()
wphandle.UsePlugins(h)
h.PushComponentFilterFn("bodyClass", calClass)
h.PushCacheGroupHeadScript("colorScheme-customHeader", 10, colorScheme, customHeader)
h.PushCacheGroupHeadScript(constraints.AllScene, "colorScheme-customHeader", 10, colorScheme, customHeader)
components.WidgetArea(h)
pushScripts(h)
h.PushRender(constraints.AllStats, wp.NewHandleFn(headerImage, 10, "headerImage"))

View File

@ -27,7 +27,13 @@ func (h *Handle) HookComponents(scene string, fn func(Components[string]) (Compo
}
func CalComponents(h *Handle) {
for k, components := range h.components {
componentss := reload.GetAnyValMapBy("scene-components", str.Join("allScene-", h.scene), h, func(h *Handle) map[string][]Components[string] {
return maps.MergeBy(func(k string, v1, v2 []Components[string]) ([]Components[string], bool) {
vv := append(v1, v2...)
return vv, vv != nil
}, nil, h.components[h.scene], h.components[constraints.AllScene])
})
for k, components := range componentss {
key := str.Join("calComponents-", k)
key = h.ComponentFilterFnHook("calComponents", key, k)
ss := reload.GetAnyValMapBy("calComponents", key, h, func(h *Handle) []Components[string] {
@ -72,58 +78,63 @@ func CalComponents(h *Handle) {
}
}
func (h *Handle) PushComponents(name string, components ...Components[string]) {
h.components[name] = append(h.components[name], components...)
func (h *Handle) PushComponents(scene, componentType string, components ...Components[string]) {
c, ok := h.components[scene]
if !ok {
c = make(map[string][]Components[string])
h.components[scene] = c
}
c[componentType] = append(c[componentType], components...)
}
func (h *Handle) PushGroupComponentStr(componentType, name string, order int, strs ...string) {
var calls []Components[string]
func (h *Handle) PushGroupComponentStr(scene, componentType, name string, order int, strs ...string) {
var components []Components[string]
for _, val := range strs {
calls = append(calls, Components[string]{
components = append(components, Components[string]{
Val: val,
Order: order,
Name: name,
})
}
h.components[componentType] = append(h.components[componentType], calls...)
h.PushComponents(scene, componentType, components...)
}
func (h *Handle) PushCacheGroupHeadScript(key string, order int, fns ...func(*Handle) string) {
h.PushGroupCacheComponentFn(constraints.HeadScript, key, order, fns...)
func (h *Handle) PushCacheGroupHeadScript(scene, name string, order int, fns ...func(*Handle) string) {
h.PushGroupCacheComponentFn(scene, constraints.HeadScript, name, order, fns...)
}
func (h *Handle) PushFooterScript(components ...Components[string]) {
h.PushComponents(constraints.FooterScript, components...)
func (h *Handle) PushFooterScript(scene string, components ...Components[string]) {
h.PushComponents(scene, constraints.FooterScript, components...)
}
func (h *Handle) PushGroupFooterScript(name string, order int, strs ...string) {
h.PushGroupComponentStr(constraints.FooterScript, name, order, strs...)
func (h *Handle) PushGroupFooterScript(scene, name string, order int, strs ...string) {
h.PushGroupComponentStr(scene, constraints.FooterScript, name, order, strs...)
}
func (h *Handle) PushCacheGroupFooterScript(name string, order int, fns ...func(*Handle) string) {
h.PushGroupCacheComponentFn(constraints.FooterScript, name, order, fns...)
func (h *Handle) PushCacheGroupFooterScript(scene, name string, order int, fns ...func(*Handle) string) {
h.PushGroupCacheComponentFn(scene, constraints.FooterScript, name, order, fns...)
}
func (h *Handle) PushGroupCacheComponentFn(componentType, name string, order int, fns ...func(*Handle) string) {
h.PushComponents(componentType, h.NewComponent(name, true, order, func(h *Handle) string {
func (h *Handle) PushGroupCacheComponentFn(scene, componentType, name string, order int, fns ...func(*Handle) string) {
h.PushComponents(scene, componentType, NewComponent(name, "", true, order, func(h *Handle) string {
return strings.Join(slice.Map(fns, func(t func(*Handle) string) string {
return t(h)
}), "\n")
}))
}
func (h *Handle) NewComponent(name string, cached bool, order int, fn func(handle *Handle) string) Components[string] {
return Components[string]{Fn: fn, Name: name, Cached: cached, Order: order}
func NewComponent(name, val string, cached bool, order int, fn func(handle *Handle) string) Components[string] {
return Components[string]{Fn: fn, Name: name, Cached: cached, Order: order, Val: val}
}
func (h *Handle) AddCacheComponent(componentType, name string, order int, fn func(*Handle) string) {
h.components[componentType] = append(h.components[componentType], h.NewComponent(name, true, order, fn))
func (h *Handle) AddCacheComponent(scene, componentType, name string, order int, fn func(*Handle) string) {
h.PushComponents(scene, componentType, NewComponent(name, "", true, order, fn))
}
func (h *Handle) PushHeadScript(components ...Components[string]) {
h.PushComponents(constraints.HeadScript, components...)
func (h *Handle) PushHeadScript(scene string, components ...Components[string]) {
h.PushComponents(scene, constraints.HeadScript, components...)
}
func (h *Handle) PushGroupHeadScript(name string, order int, str ...string) {
h.PushGroupComponentStr(constraints.HeadScript, name, order, str...)
func (h *Handle) PushGroupHeadScript(scene, name string, order int, str ...string) {
h.PushGroupComponentStr(scene, constraints.HeadScript, name, order, str...)
}
func GetComponentsArgs[T any](h *Handle, k string, defaults T) T {

View File

@ -12,12 +12,16 @@ var blockFn = map[string]func(*wp.Handle, string, block.ParserBlock) (func() str
"core/categories": block.Category,
}
func Block(id string) func(*wp.Handle) string {
func Block(id string) (func(*wp.Handle) string, string) {
content := wpconfig.GetPHPArrayVal("widget_block", "", str.ToInteger[int64](id, 0), "content")
if content == "" {
return nil
return nil, ""
}
var name string
v := block.ParseBlock(content)
if len(v.Output) > 0 {
name = v.Output[0].Name
}
return func(h *wp.Handle) string {
var out []string
for _, parserBlock := range v.Output {
@ -28,8 +32,9 @@ func Block(id string) func(*wp.Handle) string {
continue
}
out = append(out, s())
}
}
return strings.Join(out, "\n")
}
}, name
}

View File

@ -2,6 +2,7 @@ package components
import (
"github.com/fthvgb1/wp-go/helper/slice"
str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
"github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/fthvgb1/wp-go/internal/theme/wp/components/widget"
@ -10,12 +11,12 @@ import (
)
var widgetFn = map[string]widgetComponent{
"search": {fn: widget.Search, name: "search"},
"recent-posts": {fn: widget.RecentPosts, name: "recent-posts"},
"recent-comments": {fn: widget.RecentComments, name: "recent-comments"},
"archives": {fn: widget.Archive, name: "archives"},
"categories": {fn: widget.Category, name: "categories"},
"meta": {fn: widget.Meta, name: "meta", cached: true},
"search": {fn: widget.Search, name: "widget.search"},
"recent-posts": {fn: widget.RecentPosts, name: "widget.recent-posts"},
"recent-comments": {fn: widget.RecentComments, name: "widget.recent-comments"},
"archives": {fn: widget.Archive, name: "widget.archives"},
"categories": {fn: widget.Category, name: "widget.categories"},
"meta": {fn: widget.Meta, name: "widget.meta", cached: true},
}
type widgetComponent struct {
@ -25,7 +26,7 @@ type widgetComponent struct {
}
func WidgetArea(h *wp.Handle) {
h.PushComponents(constraints.SidebarsWidgets, sidebars()...)
h.PushComponents(constraints.AllScene, constraints.SidebarsWidgets, sidebars()...)
}
func sidebars() []wp.Components[string] {
@ -41,11 +42,12 @@ func sidebars() []wp.Components[string] {
}
var component wp.Components[string]
if name == "block" {
fn := Block(id)
fn, fnName := Block(id)
if fn == nil {
return component, false
}
component.Fn = fn
component.Name = str.Join("block.", fnName)
} else {
component.Fn = widget.Fn(id, widgetComponents.fn)
component.Name = widgetComponents.name

View File

@ -114,7 +114,7 @@ func Details(h *Handle) {
}
func ReplyCommentJs(h *Handle) {
h.PushFooterScript(h.NewComponent("comment-reply.js", false, 10, func(h *Handle) string {
h.PushFooterScript(constraints.Detail, NewComponent("comment-reply.js", "", false, 10, func(h *Handle) string {
reply := ""
if h.Detail.Post.CommentStatus == "open" && wpconfig.GetOption("thread_comments") == "1" {
reply = `<script src='/wp-includes/js/comment-reply.min.js' id='comment-reply-js'></script>`

View File

@ -25,7 +25,7 @@ type Handle struct {
Code int
Stats string
templ string
components map[string][]Components[string]
components map[string]map[string][]Components[string]
componentHook map[string][]func(Components[string]) (Components[string], bool)
themeMods wpconfig.ThemeMods
handlers map[string]map[string][]HandleCall
@ -38,7 +38,7 @@ type Handle struct {
template *template.Template
}
func (h *Handle) Components() map[string][]Components[string] {
func (h *Handle) Components() map[string]map[string][]Components[string] {
return h.components
}
@ -84,7 +84,7 @@ type HandleCall struct {
func InitThemeArgAndConfig(fn func(*Handle), h *Handle) {
var inited = false
hh := reload.GetAnyValBys("themeArgAndConfig", h, func(h *Handle) Handle {
h.components = make(map[string][]Components[string])
h.components = make(map[string]map[string][]Components[string])
h.componentsArgs = make(map[string]any)
h.componentFilterFn = make(map[string][]func(*Handle, string, ...any) string)
h.handlers = make(map[string]map[string][]HandleCall)
@ -193,7 +193,7 @@ func PreCodeAndStats(h *Handle) {
}
func (h *Handle) CommonComponents() {
h.PushCacheGroupHeadScript("siteIconAndCustomCss", 0, CalSiteIcon, CalCustomCss)
h.PushCacheGroupHeadScript(constraints.AllScene, "siteIconAndCustomCss", 0, CalSiteIcon, CalCustomCss)
h.PushRender(constraints.AllStats, NewHandleFn(CalComponents, 10, "wp.CalComponents"))
h.PushRender(constraints.AllStats, NewHandleFn(RenderTemplate, 0, "wp.RenderTemplate"))
}