Compare commits

...

2 Commits

Author SHA1 Message Date
1ecb338af5 主题配置复制 2023-04-02 23:37:12 +08:00
7e55253126 小优化 2023-03-31 14:33:09 +08:00
3 changed files with 26 additions and 5 deletions

View File

@ -12,7 +12,7 @@ import (
) )
func CalBodyClass(h *Handle) { func CalBodyClass(h *Handle) {
h.ginH["bodyClass"] = h.BodyClass(h.class...) h.ginH["bodyClass"] = h.BodyClass(h.bodyClass...)
} }
func (h *Handle) BodyClass(class ...string) string { func (h *Handle) BodyClass(class ...string) string {

View File

@ -70,7 +70,6 @@ func Search(h *wp.Handle, id string) string {
return args return args
}) })
args = maps.Copy(args)
s := strings.ReplaceAll(searchTemplate, "{$form}", form) s := strings.ReplaceAll(searchTemplate, "{$form}", form)
val := "" val := ""
if h.Scene() == constraints.Search { if h.Scene() == constraints.Search {

View File

@ -12,6 +12,7 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"net/http" "net/http"
"strings" "strings"
"sync"
) )
type Handle struct { type Handle struct {
@ -26,7 +27,7 @@ type Handle struct {
Code int Code int
Stats int Stats int
templ string templ string
class []string bodyClass []string
components map[string][]Components[string] components map[string][]Components[string]
themeMods wpconfig.ThemeMods themeMods wpconfig.ThemeMods
handleFns map[int][]HandleCall handleFns map[int][]HandleCall
@ -36,6 +37,9 @@ type Handle struct {
componentFilterFn map[string][]func(*Handle, string, ...any) string componentFilterFn map[string][]func(*Handle, string, ...any) string
} }
var configHandle = reload.Vars(&Handle{})
var configHandleMux = sync.Mutex{}
type HandlePlugins map[string]HandleFn[*Handle] type HandlePlugins map[string]HandleFn[*Handle]
// Components Order 为执行顺序,降序执行 // Components Order 为执行顺序,降序执行
@ -55,6 +59,24 @@ type HandleCall struct {
Order int Order int
} }
func InitThemeArgAndConfig(fn func(*Handle) *Handle, h *Handle) {
hh := configHandle.Load()
if len(hh.handleFns) < 1 {
configHandleMux.Lock()
hh = configHandle.Load()
if len(hh.handleFns) < 1 {
hh = fn(h)
configHandle.Store(hh)
}
configHandleMux.Unlock()
}
h.components = hh.components
h.handleFns = hh.handleFns
h.componentsArgs = hh.componentsArgs
h.componentFilterFn = hh.componentFilterFn
h.ginH = maps.Copy(hh.ginH)
}
func (h *Handle) ComponentFilterFn(name string) ([]func(*Handle, string, ...any) string, bool) { func (h *Handle) ComponentFilterFn(name string) ([]func(*Handle, string, ...any) string, bool) {
fn, ok := h.componentFilterFn[name] fn, ok := h.componentFilterFn[name]
return fn, ok return fn, ok
@ -109,7 +131,7 @@ func (h *Handle) SetData(k string, v any) {
} }
func (h *Handle) PushClass(class ...string) { func (h *Handle) PushClass(class ...string) {
h.class = append(h.class, class...) h.bodyClass = append(h.bodyClass, class...)
} }
func GetComponentsArgs[T any](h *Handle, k string, defaults T) T { func GetComponentsArgs[T any](h *Handle, k string, defaults T) T {
@ -202,7 +224,7 @@ func (h *Handle) PushGroupHandleFn(statsOrScene, order int, fns ...HandleFn[*Han
} }
func (h *Handle) AddCacheComponent(name string, fn func(*Handle) string) { func (h *Handle) AddCacheComponent(name string, fn func(*Handle) string) {
h.ginH[name] = reload.GetAnyValBys(name, h, fn) h.components[name] = append(h.components[name], h.NewCacheComponent(name, 10, fn))
} }
func (h *Handle) PushHeadScript(fn ...Components[string]) { func (h *Handle) PushHeadScript(fn ...Components[string]) {