优化代码 components hook
This commit is contained in:
parent
43754377e8
commit
43ec093e4c
@ -37,7 +37,7 @@ type Selectors struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func EnlighterJS(h *wp.Handle) {
|
func EnlighterJS(h *wp.Handle) {
|
||||||
h.PushGroupHeadScript(20, `<link rel='stylesheet' id='enlighterjs-css' href='/wp-content/plugins/enlighter/cache/enlighterjs.min.css' media='all' />`)
|
h.PushGroupHeadScript("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("enlighterJs", 10, func(h *wp.Handle) string {
|
||||||
op := wpconfig.GetOption("enlighter-options")
|
op := wpconfig.GetOption("enlighter-options")
|
||||||
|
@ -67,6 +67,7 @@ func configs(h *wp.Handle) {
|
|||||||
h.CommonComponents()
|
h.CommonComponents()
|
||||||
h.Index.SetListPlugin(wp.PostsPlugins(wp.PostPlugin(), wp.GetListPostPlugins(conf.ListPagePlugins, wp.ListPostPlugins())...))
|
h.Index.SetListPlugin(wp.PostsPlugins(wp.PostPlugin(), wp.GetListPostPlugins(conf.ListPagePlugins, wp.ListPostPlugins())...))
|
||||||
components.WidgetArea(h)
|
components.WidgetArea(h)
|
||||||
|
wp.ReplyCommentJs(h)
|
||||||
h.SetData("customHeader", customHeader(h))
|
h.SetData("customHeader", customHeader(h))
|
||||||
h.PushRender(constraints.AllStats, wp.NewHandleFn(wp.IndexRender, 50, "wp.IndexRender"))
|
h.PushRender(constraints.AllStats, wp.NewHandleFn(wp.IndexRender, 50, "wp.IndexRender"))
|
||||||
h.PushRender(constraints.Detail, wp.NewHandleFn(wp.DetailRender, 50, "wp.DetailRender"))
|
h.PushRender(constraints.Detail, wp.NewHandleFn(wp.DetailRender, 50, "wp.DetailRender"))
|
||||||
|
@ -7,14 +7,14 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func pushScripts(h *wp.Handle) {
|
func pushScripts(h *wp.Handle) {
|
||||||
h.PushCacheGroupHeadScript("head", 30, func(h *wp.Handle) string {
|
h.PushCacheGroupHeadScript("{theme}.head", 30, func(h *wp.Handle) string {
|
||||||
head := headScript
|
head := headScript
|
||||||
if "dark" == wpconfig.GetThemeModsVal(ThemeName, "colorscheme", "light") {
|
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">`)
|
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
|
return head
|
||||||
})
|
})
|
||||||
h.PushGroupFooterScript(20, footerScript)
|
h.PushGroupFooterScript("{theme}.footer", 20, footerScript)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
200
internal/theme/wp/components.go
Normal file
200
internal/theme/wp/components.go
Normal file
@ -0,0 +1,200 @@
|
|||||||
|
package wp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/fthvgb1/wp-go/helper/maps"
|
||||||
|
"github.com/fthvgb1/wp-go/helper/slice"
|
||||||
|
str "github.com/fthvgb1/wp-go/helper/strings"
|
||||||
|
"github.com/fthvgb1/wp-go/internal/cmd/reload"
|
||||||
|
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (h *Handle) DeleteComponents(scene, name string) {
|
||||||
|
h.componentHook[scene] = append(h.componentHook[scene], func(c Components[string]) (Components[string], bool) {
|
||||||
|
return c, c.Name != name
|
||||||
|
})
|
||||||
|
}
|
||||||
|
func (h *Handle) ReplaceComponents(scene, name string, components Components[string]) {
|
||||||
|
h.componentHook[scene] = append(h.componentHook[scene], func(c Components[string]) (Components[string], bool) {
|
||||||
|
if c.Name == name {
|
||||||
|
c = components
|
||||||
|
}
|
||||||
|
return c, true
|
||||||
|
})
|
||||||
|
}
|
||||||
|
func (h *Handle) HookComponents(scene string, fn func(Components[string]) (Components[string], bool)) {
|
||||||
|
h.componentHook[scene] = append(h.componentHook[scene], fn)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CalComponents(h *Handle) {
|
||||||
|
for k, components := range h.components {
|
||||||
|
key := str.Join("calComponents-", k)
|
||||||
|
key = h.ComponentFilterFnHook("calComponents", key, k)
|
||||||
|
ss := reload.GetAnyValMapBy("calComponents", key, h, func(h *Handle) []Components[string] {
|
||||||
|
r := slice.FilterAndMap(components, func(t Components[string]) (Components[string], bool) {
|
||||||
|
fns, ok := h.componentHook[k]
|
||||||
|
if !ok {
|
||||||
|
return t, true
|
||||||
|
}
|
||||||
|
for _, fn := range fns {
|
||||||
|
c, ok := fn(t)
|
||||||
|
if !ok {
|
||||||
|
return c, false
|
||||||
|
}
|
||||||
|
t = c
|
||||||
|
}
|
||||||
|
return t, true
|
||||||
|
})
|
||||||
|
slice.Sort(r, func(i, j Components[string]) bool {
|
||||||
|
return i.Order > j.Order
|
||||||
|
})
|
||||||
|
return r
|
||||||
|
})
|
||||||
|
var s = make([]string, 0, len(ss))
|
||||||
|
for _, component := range ss {
|
||||||
|
if component.Val != "" {
|
||||||
|
s = append(s, component.Val)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if component.Fn != nil {
|
||||||
|
v := ""
|
||||||
|
if component.Cached {
|
||||||
|
v = reload.GetAnyValMapBy("cacheComponents", component.Name, h, component.Fn)
|
||||||
|
} else {
|
||||||
|
v = component.Fn(h)
|
||||||
|
}
|
||||||
|
if v != "" {
|
||||||
|
s = append(s, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
h.ginH[k] = strings.Join(s, "\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handle) PushComponents(name string, components ...Components[string]) {
|
||||||
|
h.components[name] = append(h.components[name], components...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handle) PushGroupComponentStr(componentType, name string, order int, strs ...string) {
|
||||||
|
var calls []Components[string]
|
||||||
|
for _, val := range strs {
|
||||||
|
calls = append(calls, Components[string]{
|
||||||
|
Val: val,
|
||||||
|
Order: order,
|
||||||
|
Name: name,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
h.components[componentType] = append(h.components[componentType], calls...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handle) PushCacheGroupHeadScript(key string, order int, fns ...func(*Handle) string) {
|
||||||
|
h.PushGroupCacheComponentFn(constraints.HeadScript, key, order, fns...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handle) PushFooterScript(components ...Components[string]) {
|
||||||
|
h.PushComponents(constraints.FooterScript, components...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handle) PushGroupFooterScript(name string, order int, strs ...string) {
|
||||||
|
h.PushGroupComponentStr(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) PushGroupCacheComponentFn(componentType, name string, order int, fns ...func(*Handle) string) {
|
||||||
|
h.PushComponents(componentType, h.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 (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) PushHeadScript(components ...Components[string]) {
|
||||||
|
h.PushComponents(constraints.HeadScript, components...)
|
||||||
|
}
|
||||||
|
func (h *Handle) PushGroupHeadScript(name string, order int, str ...string) {
|
||||||
|
h.PushGroupComponentStr(constraints.HeadScript, name, order, str...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetComponentsArgs[T any](h *Handle, k string, defaults T) T {
|
||||||
|
v, ok := h.componentsArgs[k]
|
||||||
|
if ok {
|
||||||
|
vv, ok := v.(T)
|
||||||
|
if ok {
|
||||||
|
return vv
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return defaults
|
||||||
|
}
|
||||||
|
|
||||||
|
func PushComponentsArgsForSlice[T any](h *Handle, name string, v ...T) {
|
||||||
|
val, ok := h.componentsArgs[name]
|
||||||
|
if !ok {
|
||||||
|
var vv []T
|
||||||
|
vv = append(vv, v...)
|
||||||
|
h.componentsArgs[name] = vv
|
||||||
|
return
|
||||||
|
}
|
||||||
|
vv, ok := val.([]T)
|
||||||
|
if ok {
|
||||||
|
vv = append(vv, v...)
|
||||||
|
h.componentsArgs[name] = vv
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func SetComponentsArgsForMap[K comparable, V any](h *Handle, name string, key K, v V) {
|
||||||
|
val, ok := h.componentsArgs[name]
|
||||||
|
if !ok {
|
||||||
|
vv := make(map[K]V)
|
||||||
|
vv[key] = v
|
||||||
|
h.componentsArgs[name] = vv
|
||||||
|
return
|
||||||
|
}
|
||||||
|
vv, ok := val.(map[K]V)
|
||||||
|
if ok {
|
||||||
|
vv[key] = v
|
||||||
|
h.componentsArgs[name] = vv
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func MergeComponentsArgsForMap[K comparable, V any](h *Handle, name string, m map[K]V) {
|
||||||
|
val, ok := h.componentsArgs[name]
|
||||||
|
if !ok {
|
||||||
|
h.componentsArgs[name] = m
|
||||||
|
return
|
||||||
|
}
|
||||||
|
vv, ok := val.(map[K]V)
|
||||||
|
if ok {
|
||||||
|
h.componentsArgs[name] = maps.Merge(vv, m)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handle) SetComponentsArgs(key string, value any) {
|
||||||
|
h.componentsArgs[key] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handle) ComponentFilterFn(name string) ([]func(*Handle, string, ...any) string, bool) {
|
||||||
|
fn, ok := h.componentFilterFn[name]
|
||||||
|
return fn, ok
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handle) PushComponentFilterFn(name string, fns ...func(*Handle, string, ...any) string) {
|
||||||
|
h.componentFilterFn[name] = append(h.componentFilterFn[name], fns...)
|
||||||
|
}
|
||||||
|
func (h *Handle) ComponentFilterFnHook(name, s string, args ...any) string {
|
||||||
|
calls, ok := h.componentFilterFn[name]
|
||||||
|
if ok {
|
||||||
|
return slice.Reduce(calls, func(fn func(*Handle, string, ...any) string, r string) string {
|
||||||
|
return fn(h, r, args...)
|
||||||
|
}, s)
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
@ -10,17 +10,18 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var widgetFn = map[string]widgetComponent{
|
var widgetFn = map[string]widgetComponent{
|
||||||
"search": {fn: widget.Search},
|
"search": {fn: widget.Search, name: "search"},
|
||||||
"recent-posts": {fn: widget.RecentPosts},
|
"recent-posts": {fn: widget.RecentPosts, name: "recent-posts"},
|
||||||
"recent-comments": {fn: widget.RecentComments},
|
"recent-comments": {fn: widget.RecentComments, name: "recent-comments"},
|
||||||
"archives": {fn: widget.Archive},
|
"archives": {fn: widget.Archive, name: "archives"},
|
||||||
"categories": {fn: widget.Category},
|
"categories": {fn: widget.Category, name: "categories"},
|
||||||
"meta": {fn: widget.Meta, cacheKey: "widgetMeta"},
|
"meta": {fn: widget.Meta, name: "meta", cached: true},
|
||||||
}
|
}
|
||||||
|
|
||||||
type widgetComponent struct {
|
type widgetComponent struct {
|
||||||
fn func(h *wp.Handle, id string) string
|
fn func(h *wp.Handle, id string) string
|
||||||
cacheKey string
|
cached bool
|
||||||
|
name string
|
||||||
}
|
}
|
||||||
|
|
||||||
func WidgetArea(h *wp.Handle) {
|
func WidgetArea(h *wp.Handle) {
|
||||||
@ -47,7 +48,8 @@ func sidebars() []wp.Components[string] {
|
|||||||
component.Fn = fn
|
component.Fn = fn
|
||||||
} else {
|
} else {
|
||||||
component.Fn = widget.Fn(id, widgetComponents.fn)
|
component.Fn = widget.Fn(id, widgetComponents.fn)
|
||||||
component.CacheKey = widgetComponents.cacheKey
|
component.Name = widgetComponents.name
|
||||||
|
component.Cached = widgetComponents.cached
|
||||||
}
|
}
|
||||||
component.Order = 10
|
component.Order = 10
|
||||||
return component, true
|
return component, true
|
||||||
|
@ -107,13 +107,18 @@ func DetailRender(h *Handle) {
|
|||||||
d.PasswordProject()
|
d.PasswordProject()
|
||||||
d.RenderComment()
|
d.RenderComment()
|
||||||
d.ginH["post"] = d.Post
|
d.ginH["post"] = d.Post
|
||||||
reply := ""
|
|
||||||
if d.Post.CommentStatus == "open" && wpconfig.GetOption("thread_comments") == "1" {
|
|
||||||
reply = `<script src='/wp-includes/js/comment-reply.min.js' id='comment-reply-js'></script>`
|
|
||||||
}
|
|
||||||
d.PushGroupFooterScript(10, reply)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Details(h *Handle) {
|
func Details(h *Handle) {
|
||||||
_ = h.Detail.BuildDetailData()
|
_ = h.Detail.BuildDetailData()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ReplyCommentJs(h *Handle) {
|
||||||
|
h.PushFooterScript(h.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>`
|
||||||
|
}
|
||||||
|
return reply
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
@ -93,17 +93,17 @@ func NewIndexParams(ctx *gin.Context) *IndexParams {
|
|||||||
PostStatus: []any{"publish"},
|
PostStatus: []any{"publish"},
|
||||||
BlogName: wpconfig.GetOption("blogname"),
|
BlogName: wpconfig.GetOption("blogname"),
|
||||||
}
|
}
|
||||||
i.ParseSearch = i.parseSearch
|
i.ParseSearch = i.ParseSearchs
|
||||||
i.ParseArchive = i.parseArchive
|
i.ParseArchive = i.ParseArchives
|
||||||
i.ParseCategory = i.parseCategory
|
i.ParseCategory = i.ParseCategorys
|
||||||
i.ParseTag = i.parseTag
|
i.ParseTag = i.ParseTags
|
||||||
i.CategoryCondition = i.categoryCondition
|
i.CategoryCondition = i.CategoryConditions
|
||||||
i.ParseAuthor = i.parseAuthor
|
i.ParseAuthor = i.ParseAuthors
|
||||||
i.ParseParams = i.parseParams
|
i.ParseParams = i.ParseParamss
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *IndexParams) parseSearch() {
|
func (i *IndexParams) ParseSearchs() {
|
||||||
s := i.Ctx.Query("s")
|
s := i.Ctx.Query("s")
|
||||||
if s != "" {
|
if s != "" {
|
||||||
q := str.Join("%", s, "%")
|
q := str.Join("%", s, "%")
|
||||||
@ -118,7 +118,7 @@ func (i *IndexParams) parseSearch() {
|
|||||||
i.Search = s
|
i.Search = s
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func (i *IndexParams) parseArchive() error {
|
func (i *IndexParams) ParseArchives() error {
|
||||||
year := i.Ctx.Param("year")
|
year := i.Ctx.Param("year")
|
||||||
if year != "" {
|
if year != "" {
|
||||||
y := str.ToInteger(year, -1)
|
y := str.ToInteger(year, -1)
|
||||||
@ -148,7 +148,7 @@ func (i *IndexParams) parseArchive() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *IndexParams) parseCategory() error {
|
func (i *IndexParams) ParseCategorys() error {
|
||||||
category := i.Ctx.Param("category")
|
category := i.Ctx.Param("category")
|
||||||
if category != "" {
|
if category != "" {
|
||||||
if !maps.IsExists(cache.AllCategoryTagsNames(i.Ctx, constraints.Category), category) {
|
if !maps.IsExists(cache.AllCategoryTagsNames(i.Ctx, constraints.Category), category) {
|
||||||
@ -161,7 +161,7 @@ func (i *IndexParams) parseCategory() error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func (i *IndexParams) parseTag() error {
|
func (i *IndexParams) ParseTags() error {
|
||||||
tag := i.Ctx.Param("tag")
|
tag := i.Ctx.Param("tag")
|
||||||
if tag != "" {
|
if tag != "" {
|
||||||
if !maps.IsExists(cache.AllCategoryTagsNames(i.Ctx, constraints.Tag), tag) {
|
if !maps.IsExists(cache.AllCategoryTagsNames(i.Ctx, constraints.Tag), tag) {
|
||||||
@ -175,7 +175,7 @@ func (i *IndexParams) parseTag() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *IndexParams) categoryCondition() {
|
func (i *IndexParams) CategoryConditions() {
|
||||||
if i.Category != "" {
|
if i.Category != "" {
|
||||||
i.Where = append(i.Where, []string{
|
i.Where = append(i.Where, []string{
|
||||||
"d.name", i.Category,
|
"d.name", i.Category,
|
||||||
@ -190,7 +190,7 @@ func (i *IndexParams) categoryCondition() {
|
|||||||
i.setTitleLR(i.Category, i.BlogName)
|
i.setTitleLR(i.Category, i.BlogName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func (i *IndexParams) parseAuthor() (err error) {
|
func (i *IndexParams) ParseAuthors() (err error) {
|
||||||
username := i.Ctx.Param("author")
|
username := i.Ctx.Param("author")
|
||||||
if username != "" {
|
if username != "" {
|
||||||
allUsername, er := cache.GetAllUsername(i.Ctx)
|
allUsername, er := cache.GetAllUsername(i.Ctx)
|
||||||
@ -215,7 +215,7 @@ func (i *IndexParams) parseAuthor() (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *IndexParams) parseParams() {
|
func (i *IndexParams) ParseParamss() {
|
||||||
i.Order = i.Ctx.Query("Order")
|
i.Order = i.Ctx.Query("Order")
|
||||||
if !maps.IsExists(orders, i.Order) {
|
if !maps.IsExists(orders, i.Order) {
|
||||||
order := config.GetConfig().PostOrder
|
order := config.GetConfig().PostOrder
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
package wp
|
package wp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"github.com/fthvgb1/wp-go/helper/slice"
|
"github.com/fthvgb1/wp-go/helper/slice"
|
||||||
|
str "github.com/fthvgb1/wp-go/helper/strings"
|
||||||
"github.com/fthvgb1/wp-go/internal/cmd/reload"
|
"github.com/fthvgb1/wp-go/internal/cmd/reload"
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
|
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
|
||||||
)
|
)
|
||||||
@ -65,7 +65,8 @@ func PipeHandle(pipeScene string, keyFn func(*Handle, string) string, fn func(*H
|
|||||||
}
|
}
|
||||||
|
|
||||||
func PipeKey(h *Handle, pipScene string) string {
|
func PipeKey(h *Handle, pipScene string) string {
|
||||||
return fmt.Sprintf("pipekey-%s-%s-%s", pipScene, h.scene, h.scene)
|
key := str.Join("pipekey", "-", pipScene, "-", h.scene, "-", h.Stats)
|
||||||
|
return h.ComponentFilterFnHook("pipeKey", key, pipScene)
|
||||||
}
|
}
|
||||||
|
|
||||||
func PipeDataHandle(h *Handle, dataHandlers map[string][]HandleCall) (handlers []HandleCall) {
|
func PipeDataHandle(h *Handle, dataHandlers map[string][]HandleCall) (handlers []HandleCall) {
|
||||||
|
@ -2,7 +2,6 @@ package wp
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/fthvgb1/wp-go/helper/maps"
|
"github.com/fthvgb1/wp-go/helper/maps"
|
||||||
"github.com/fthvgb1/wp-go/helper/slice"
|
|
||||||
str "github.com/fthvgb1/wp-go/helper/strings"
|
str "github.com/fthvgb1/wp-go/helper/strings"
|
||||||
"github.com/fthvgb1/wp-go/internal/cmd/reload"
|
"github.com/fthvgb1/wp-go/internal/cmd/reload"
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
|
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
|
||||||
@ -12,7 +11,6 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Handle struct {
|
type Handle struct {
|
||||||
@ -28,6 +26,7 @@ type Handle struct {
|
|||||||
Stats string
|
Stats string
|
||||||
templ string
|
templ string
|
||||||
components map[string][]Components[string]
|
components map[string][]Components[string]
|
||||||
|
componentHook map[string][]func(Components[string]) (Components[string], bool)
|
||||||
themeMods wpconfig.ThemeMods
|
themeMods wpconfig.ThemeMods
|
||||||
handlers map[string]map[string][]HandleCall
|
handlers map[string]map[string][]HandleCall
|
||||||
handleHook map[string][]func(HandleCall) (HandleCall, bool)
|
handleHook map[string][]func(HandleCall) (HandleCall, bool)
|
||||||
@ -39,6 +38,22 @@ type Handle struct {
|
|||||||
template *template.Template
|
template *template.Template
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *Handle) Components() map[string][]Components[string] {
|
||||||
|
return h.components
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handle) ComponentHook() map[string][]func(Components[string]) (Components[string], bool) {
|
||||||
|
return h.componentHook
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handle) Handlers() map[string]map[string][]HandleCall {
|
||||||
|
return h.handlers
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handle) HandleHook() map[string][]func(HandleCall) (HandleCall, bool) {
|
||||||
|
return h.handleHook
|
||||||
|
}
|
||||||
|
|
||||||
func (h *Handle) SetTemplate(template *template.Template) {
|
func (h *Handle) SetTemplate(template *template.Template) {
|
||||||
h.template = template
|
h.template = template
|
||||||
}
|
}
|
||||||
@ -51,10 +66,11 @@ type HandlePlugins map[string]HandleFn[*Handle]
|
|||||||
|
|
||||||
// Components Order 为执行顺序,降序执行
|
// Components Order 为执行顺序,降序执行
|
||||||
type Components[T any] struct {
|
type Components[T any] struct {
|
||||||
|
Name string
|
||||||
Val T
|
Val T
|
||||||
Fn func(*Handle) T
|
Fn func(*Handle) T
|
||||||
Order int
|
Order int
|
||||||
CacheKey string
|
Cached bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type HandleFn[T any] func(T)
|
type HandleFn[T any] func(T)
|
||||||
@ -78,13 +94,6 @@ func InitThemeArgAndConfig(fn func(*Handle), h *Handle) {
|
|||||||
inited = true
|
inited = true
|
||||||
return *h
|
return *h
|
||||||
})
|
})
|
||||||
m := make(map[string][]Components[string])
|
|
||||||
for k, v := range hh.components {
|
|
||||||
vv := make([]Components[string], len(v))
|
|
||||||
copy(vv, v)
|
|
||||||
m[k] = vv
|
|
||||||
}
|
|
||||||
h.components = m
|
|
||||||
h.ginH = maps.Copy(hh.ginH)
|
h.ginH = maps.Copy(hh.ginH)
|
||||||
h.ginH["calPostClass"] = postClass(h)
|
h.ginH["calPostClass"] = postClass(h)
|
||||||
h.ginH["calBodyClass"] = bodyClass(h)
|
h.ginH["calBodyClass"] = bodyClass(h)
|
||||||
@ -92,33 +101,17 @@ func InitThemeArgAndConfig(fn func(*Handle), h *Handle) {
|
|||||||
if inited {
|
if inited {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
h.components = hh.components
|
||||||
h.Index.postsPlugin = hh.Index.postsPlugin
|
h.Index.postsPlugin = hh.Index.postsPlugin
|
||||||
h.Index.pageEle = hh.Index.pageEle
|
h.Index.pageEle = hh.Index.pageEle
|
||||||
h.Detail.CommentRender = hh.Detail.CommentRender
|
h.Detail.CommentRender = hh.Detail.CommentRender
|
||||||
h.handlers = hh.handlers
|
h.handlers = hh.handlers
|
||||||
h.handleHook = hh.handleHook
|
h.handleHook = hh.handleHook
|
||||||
|
h.componentHook = hh.componentHook
|
||||||
h.componentsArgs = hh.componentsArgs
|
h.componentsArgs = hh.componentsArgs
|
||||||
h.componentFilterFn = hh.componentFilterFn
|
h.componentFilterFn = hh.componentFilterFn
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handle) ComponentFilterFn(name string) ([]func(*Handle, string, ...any) string, bool) {
|
|
||||||
fn, ok := h.componentFilterFn[name]
|
|
||||||
return fn, ok
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *Handle) PushComponentFilterFn(name string, fns ...func(*Handle, string, ...any) string) {
|
|
||||||
h.componentFilterFn[name] = append(h.componentFilterFn[name], fns...)
|
|
||||||
}
|
|
||||||
func (h *Handle) ComponentFilterFnHook(name, s string, args ...any) string {
|
|
||||||
calls, ok := h.componentFilterFn[name]
|
|
||||||
if ok {
|
|
||||||
return slice.Reduce(calls, func(fn func(*Handle, string, ...any) string, r string) string {
|
|
||||||
return fn(h, r, args...)
|
|
||||||
}, s)
|
|
||||||
}
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *Handle) Abort() {
|
func (h *Handle) Abort() {
|
||||||
h.abort = true
|
h.abort = true
|
||||||
}
|
}
|
||||||
@ -157,61 +150,6 @@ func (h *Handle) SetData(k string, v any) {
|
|||||||
h.ginH[k] = v
|
h.ginH[k] = v
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetComponentsArgs[T any](h *Handle, k string, defaults T) T {
|
|
||||||
v, ok := h.componentsArgs[k]
|
|
||||||
if ok {
|
|
||||||
vv, ok := v.(T)
|
|
||||||
if ok {
|
|
||||||
return vv
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return defaults
|
|
||||||
}
|
|
||||||
|
|
||||||
func PushComponentsArgsForSlice[T any](h *Handle, name string, v ...T) {
|
|
||||||
val, ok := h.componentsArgs[name]
|
|
||||||
if !ok {
|
|
||||||
var vv []T
|
|
||||||
vv = append(vv, v...)
|
|
||||||
h.componentsArgs[name] = vv
|
|
||||||
return
|
|
||||||
}
|
|
||||||
vv, ok := val.([]T)
|
|
||||||
if ok {
|
|
||||||
vv = append(vv, v...)
|
|
||||||
h.componentsArgs[name] = vv
|
|
||||||
}
|
|
||||||
}
|
|
||||||
func SetComponentsArgsForMap[K comparable, V any](h *Handle, name string, key K, v V) {
|
|
||||||
val, ok := h.componentsArgs[name]
|
|
||||||
if !ok {
|
|
||||||
vv := make(map[K]V)
|
|
||||||
vv[key] = v
|
|
||||||
h.componentsArgs[name] = vv
|
|
||||||
return
|
|
||||||
}
|
|
||||||
vv, ok := val.(map[K]V)
|
|
||||||
if ok {
|
|
||||||
vv[key] = v
|
|
||||||
h.componentsArgs[name] = vv
|
|
||||||
}
|
|
||||||
}
|
|
||||||
func MergeComponentsArgsForMap[K comparable, V any](h *Handle, name string, m map[K]V) {
|
|
||||||
val, ok := h.componentsArgs[name]
|
|
||||||
if !ok {
|
|
||||||
h.componentsArgs[name] = m
|
|
||||||
return
|
|
||||||
}
|
|
||||||
vv, ok := val.(map[K]V)
|
|
||||||
if ok {
|
|
||||||
h.componentsArgs[name] = maps.Merge(vv, m)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *Handle) SetComponentsArgs(key string, value any) {
|
|
||||||
h.componentsArgs[key] = value
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewHandle(c *gin.Context, scene string, theme string) *Handle {
|
func NewHandle(c *gin.Context, scene string, theme string) *Handle {
|
||||||
mods, err := wpconfig.GetThemeMods(theme)
|
mods, err := wpconfig.GetThemeMods(theme)
|
||||||
logs.IfError(err, "获取mods失败")
|
logs.IfError(err, "获取mods失败")
|
||||||
@ -225,43 +163,6 @@ func NewHandle(c *gin.Context, scene string, theme string) *Handle {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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) AddCacheComponent(name string, fn func(*Handle) string) {
|
|
||||||
h.components[name] = append(h.components[name], h.NewCacheComponent(name, 10, fn))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *Handle) PushHeadScript(fn ...Components[string]) {
|
|
||||||
h.PushComponents(constraints.HeadScript, fn...)
|
|
||||||
}
|
|
||||||
func (h *Handle) PushGroupHeadScript(order int, str ...string) {
|
|
||||||
h.PushGroupComponentStrs(constraints.HeadScript, order, str...)
|
|
||||||
}
|
|
||||||
func (h *Handle) PushCacheGroupHeadScript(key string, order int, fns ...func(*Handle) string) {
|
|
||||||
h.PushGroupCacheComponentFn(constraints.HeadScript, key, order, fns...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *Handle) PushFooterScript(fn ...Components[string]) {
|
|
||||||
h.PushComponents(constraints.FooterScript, fn...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *Handle) PushGroupFooterScript(order int, fns ...string) {
|
|
||||||
h.PushGroupComponentStrs(constraints.FooterScript, order, fns...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *Handle) PushCacheGroupFooterScript(key string, order int, fns ...func(*Handle) string) {
|
|
||||||
h.PushGroupCacheComponentFn(constraints.FooterScript, key, order, fns...)
|
|
||||||
}
|
|
||||||
func (h *Handle) PushGroupCacheComponentFn(name, key string, order int, fns ...func(*Handle) string) {
|
|
||||||
h.PushComponents(name, h.NewCacheComponent(key, order, func(h *Handle) string {
|
|
||||||
return strings.Join(slice.Map(fns, func(t func(*Handle) string) string {
|
|
||||||
return t(h)
|
|
||||||
}), "\n")
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *Handle) GetPassword() {
|
func (h *Handle) GetPassword() {
|
||||||
pw := h.Session.Get("post_password")
|
pw := h.Session.Get("post_password")
|
||||||
if pw != nil {
|
if pw != nil {
|
||||||
@ -302,58 +203,6 @@ func RenderTemplate(h *Handle) {
|
|||||||
h.StopPipe()
|
h.StopPipe()
|
||||||
}
|
}
|
||||||
|
|
||||||
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, 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[string]
|
|
||||||
for _, fn := range fns {
|
|
||||||
calls = append(calls, Components[string]{
|
|
||||||
Fn: fn,
|
|
||||||
Order: order,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
h.components[name] = append(h.components[name], calls...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func CalComponents(h *Handle) {
|
|
||||||
for k, ss := range h.components {
|
|
||||||
slice.Sort(ss, func(i, j Components[string]) bool {
|
|
||||||
return i.Order > j.Order
|
|
||||||
})
|
|
||||||
var s []string
|
|
||||||
for _, component := range ss {
|
|
||||||
if component.Val != "" {
|
|
||||||
s = append(s, component.Val)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if component.Fn != nil {
|
|
||||||
v := ""
|
|
||||||
if component.CacheKey != "" {
|
|
||||||
v = reload.GetAnyValMapBy("calComponent", component.CacheKey, h, component.Fn)
|
|
||||||
} else {
|
|
||||||
v = component.Fn(h)
|
|
||||||
}
|
|
||||||
if v != "" {
|
|
||||||
s = append(s, v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
h.ginH[k] = strings.Join(s, "\n")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewHandleFn(fn HandleFn[*Handle], order int, name string) HandleCall {
|
func NewHandleFn(fn HandleFn[*Handle], order int, name string) HandleCall {
|
||||||
return HandleCall{Fn: fn, Order: order, Name: name}
|
return HandleCall{Fn: fn, Order: order, Name: name}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user