优化代码 components hook
This commit is contained in:
parent
43754377e8
commit
43ec093e4c
@ -37,7 +37,7 @@ type Selectors struct {
|
||||
}
|
||||
|
||||
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 {
|
||||
op := wpconfig.GetOption("enlighter-options")
|
||||
|
@ -67,6 +67,7 @@ func configs(h *wp.Handle) {
|
||||
h.CommonComponents()
|
||||
h.Index.SetListPlugin(wp.PostsPlugins(wp.PostPlugin(), wp.GetListPostPlugins(conf.ListPagePlugins, wp.ListPostPlugins())...))
|
||||
components.WidgetArea(h)
|
||||
wp.ReplyCommentJs(h)
|
||||
h.SetData("customHeader", customHeader(h))
|
||||
h.PushRender(constraints.AllStats, wp.NewHandleFn(wp.IndexRender, 50, "wp.IndexRender"))
|
||||
h.PushRender(constraints.Detail, wp.NewHandleFn(wp.DetailRender, 50, "wp.DetailRender"))
|
||||
|
@ -7,14 +7,14 @@ import (
|
||||
)
|
||||
|
||||
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
|
||||
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(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{
|
||||
"search": {fn: widget.Search},
|
||||
"recent-posts": {fn: widget.RecentPosts},
|
||||
"recent-comments": {fn: widget.RecentComments},
|
||||
"archives": {fn: widget.Archive},
|
||||
"categories": {fn: widget.Category},
|
||||
"meta": {fn: widget.Meta, cacheKey: "widgetMeta"},
|
||||
"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},
|
||||
}
|
||||
|
||||
type widgetComponent struct {
|
||||
fn func(h *wp.Handle, id string) string
|
||||
cacheKey string
|
||||
cached bool
|
||||
name string
|
||||
}
|
||||
|
||||
func WidgetArea(h *wp.Handle) {
|
||||
@ -47,7 +48,8 @@ func sidebars() []wp.Components[string] {
|
||||
component.Fn = fn
|
||||
} else {
|
||||
component.Fn = widget.Fn(id, widgetComponents.fn)
|
||||
component.CacheKey = widgetComponents.cacheKey
|
||||
component.Name = widgetComponents.name
|
||||
component.Cached = widgetComponents.cached
|
||||
}
|
||||
component.Order = 10
|
||||
return component, true
|
||||
|
@ -107,13 +107,18 @@ func DetailRender(h *Handle) {
|
||||
d.PasswordProject()
|
||||
d.RenderComment()
|
||||
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) {
|
||||
_ = 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"},
|
||||
BlogName: wpconfig.GetOption("blogname"),
|
||||
}
|
||||
i.ParseSearch = i.parseSearch
|
||||
i.ParseArchive = i.parseArchive
|
||||
i.ParseCategory = i.parseCategory
|
||||
i.ParseTag = i.parseTag
|
||||
i.CategoryCondition = i.categoryCondition
|
||||
i.ParseAuthor = i.parseAuthor
|
||||
i.ParseParams = i.parseParams
|
||||
i.ParseSearch = i.ParseSearchs
|
||||
i.ParseArchive = i.ParseArchives
|
||||
i.ParseCategory = i.ParseCategorys
|
||||
i.ParseTag = i.ParseTags
|
||||
i.CategoryCondition = i.CategoryConditions
|
||||
i.ParseAuthor = i.ParseAuthors
|
||||
i.ParseParams = i.ParseParamss
|
||||
return i
|
||||
}
|
||||
|
||||
func (i *IndexParams) parseSearch() {
|
||||
func (i *IndexParams) ParseSearchs() {
|
||||
s := i.Ctx.Query("s")
|
||||
if s != "" {
|
||||
q := str.Join("%", s, "%")
|
||||
@ -118,7 +118,7 @@ func (i *IndexParams) parseSearch() {
|
||||
i.Search = s
|
||||
}
|
||||
}
|
||||
func (i *IndexParams) parseArchive() error {
|
||||
func (i *IndexParams) ParseArchives() error {
|
||||
year := i.Ctx.Param("year")
|
||||
if year != "" {
|
||||
y := str.ToInteger(year, -1)
|
||||
@ -148,7 +148,7 @@ func (i *IndexParams) parseArchive() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *IndexParams) parseCategory() error {
|
||||
func (i *IndexParams) ParseCategorys() error {
|
||||
category := i.Ctx.Param("category")
|
||||
if category != "" {
|
||||
if !maps.IsExists(cache.AllCategoryTagsNames(i.Ctx, constraints.Category), category) {
|
||||
@ -161,7 +161,7 @@ func (i *IndexParams) parseCategory() error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (i *IndexParams) parseTag() error {
|
||||
func (i *IndexParams) ParseTags() error {
|
||||
tag := i.Ctx.Param("tag")
|
||||
if tag != "" {
|
||||
if !maps.IsExists(cache.AllCategoryTagsNames(i.Ctx, constraints.Tag), tag) {
|
||||
@ -175,7 +175,7 @@ func (i *IndexParams) parseTag() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *IndexParams) categoryCondition() {
|
||||
func (i *IndexParams) CategoryConditions() {
|
||||
if i.Category != "" {
|
||||
i.Where = append(i.Where, []string{
|
||||
"d.name", i.Category,
|
||||
@ -190,7 +190,7 @@ func (i *IndexParams) categoryCondition() {
|
||||
i.setTitleLR(i.Category, i.BlogName)
|
||||
}
|
||||
}
|
||||
func (i *IndexParams) parseAuthor() (err error) {
|
||||
func (i *IndexParams) ParseAuthors() (err error) {
|
||||
username := i.Ctx.Param("author")
|
||||
if username != "" {
|
||||
allUsername, er := cache.GetAllUsername(i.Ctx)
|
||||
@ -215,7 +215,7 @@ func (i *IndexParams) parseAuthor() (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (i *IndexParams) parseParams() {
|
||||
func (i *IndexParams) ParseParamss() {
|
||||
i.Order = i.Ctx.Query("Order")
|
||||
if !maps.IsExists(orders, i.Order) {
|
||||
order := config.GetConfig().PostOrder
|
||||
|
@ -1,8 +1,8 @@
|
||||
package wp
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"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"
|
||||
)
|
||||
@ -65,7 +65,8 @@ func PipeHandle(pipeScene string, keyFn func(*Handle, string) string, fn func(*H
|
||||
}
|
||||
|
||||
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) {
|
||||
|
@ -2,7 +2,6 @@ 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"
|
||||
@ -12,7 +11,6 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Handle struct {
|
||||
@ -28,6 +26,7 @@ type Handle struct {
|
||||
Stats string
|
||||
templ string
|
||||
components map[string][]Components[string]
|
||||
componentHook map[string][]func(Components[string]) (Components[string], bool)
|
||||
themeMods wpconfig.ThemeMods
|
||||
handlers map[string]map[string][]HandleCall
|
||||
handleHook map[string][]func(HandleCall) (HandleCall, bool)
|
||||
@ -39,6 +38,22 @@ type Handle struct {
|
||||
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) {
|
||||
h.template = template
|
||||
}
|
||||
@ -51,10 +66,11 @@ type HandlePlugins map[string]HandleFn[*Handle]
|
||||
|
||||
// Components Order 为执行顺序,降序执行
|
||||
type Components[T any] struct {
|
||||
Name string
|
||||
Val T
|
||||
Fn func(*Handle) T
|
||||
Order int
|
||||
CacheKey string
|
||||
Cached bool
|
||||
}
|
||||
|
||||
type HandleFn[T any] func(T)
|
||||
@ -78,13 +94,6 @@ func InitThemeArgAndConfig(fn func(*Handle), h *Handle) {
|
||||
inited = true
|
||||
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["calPostClass"] = postClass(h)
|
||||
h.ginH["calBodyClass"] = bodyClass(h)
|
||||
@ -92,33 +101,17 @@ func InitThemeArgAndConfig(fn func(*Handle), h *Handle) {
|
||||
if inited {
|
||||
return
|
||||
}
|
||||
h.components = hh.components
|
||||
h.Index.postsPlugin = hh.Index.postsPlugin
|
||||
h.Index.pageEle = hh.Index.pageEle
|
||||
h.Detail.CommentRender = hh.Detail.CommentRender
|
||||
h.handlers = hh.handlers
|
||||
h.handleHook = hh.handleHook
|
||||
h.componentHook = hh.componentHook
|
||||
h.componentsArgs = hh.componentsArgs
|
||||
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() {
|
||||
h.abort = true
|
||||
}
|
||||
@ -157,61 +150,6 @@ func (h *Handle) SetData(k string, v any) {
|
||||
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 {
|
||||
mods, err := wpconfig.GetThemeMods(theme)
|
||||
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() {
|
||||
pw := h.Session.Get("post_password")
|
||||
if pw != nil {
|
||||
@ -302,58 +203,6 @@ func RenderTemplate(h *Handle) {
|
||||
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 {
|
||||
return HandleCall{Fn: fn, Order: order, Name: name}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user