调整优化修复bug

This commit is contained in:
xing 2023-03-04 14:44:51 +08:00
parent 997839e98a
commit a905f59eae
7 changed files with 72 additions and 53 deletions

View File

@ -1,6 +1,10 @@
package reload package reload
import "github.com/fthvgb1/wp-go/safety" import (
"github.com/fthvgb1/wp-go/helper/slice"
"github.com/fthvgb1/wp-go/safety"
"strings"
)
var calls []func() var calls []func()
@ -13,6 +17,18 @@ func SetStr(name, val string) {
str.Store(name, val) str.Store(name, val)
} }
func GetStrBy[T any](key, delimiter string, t T, fn ...func(T) string) string {
v, ok := str.Load(key)
if ok {
return v
}
v = strings.Join(slice.Map(fn, func(vv func(T) string) string {
return vv(t)
}), delimiter)
str.Store(key, v)
return v
}
func Vars[T any](defaults T) *safety.Var[T] { func Vars[T any](defaults T) *safety.Var[T] {
ss := safety.NewVar(defaults) ss := safety.NewVar(defaults)
calls = append(calls, func() { calls = append(calls, func() {

View File

@ -6,13 +6,9 @@ import (
) )
func EnlighterJS(h *wp.Handle) { func EnlighterJS(h *wp.Handle) {
h.PushGroupHeadScript(20, func(h *wp.Handle) string { h.PushGroupHeadScript(20, `<link rel='stylesheet' id='enlighterjs-css' href='/wp-content/plugins/enlighter/cache/enlighterjs.min.css' media='all' />`)
return `<link rel='stylesheet' id='enlighterjs-css' href='/wp-content/plugins/enlighter/cache/enlighterjs.min.css' media='all' />`
})
h.PushGroupFooterScript(10, func(h *wp.Handle) string { h.PushGroupFooterScript(10, str.Join(`<script src='/wp-content/plugins/enlighter/cache/enlighterjs.min.js?ver=0A0B0C' id='enlighterjs-js'></script>`, "\n", enlighterjs))
return str.Join(`<script src='/wp-content/plugins/enlighter/cache/enlighterjs.min.js?ver=0A0B0C' id='enlighterjs-js'></script>`, "\n", enlighterjs)
})
} }
var enlighterjs = `<script id='enlighterjs-js-after'> var enlighterjs = `<script id='enlighterjs-js-after'>

View File

@ -41,7 +41,7 @@ func dispatch(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
h.GetPassword() h.GetPassword()
wphandle.RegisterPlugins(h, config.GetConfig().Plugins...) wphandle.RegisterPlugins(h, config.GetConfig().Plugins...)
h.PushGroupHeadScript(10, CalCustomBackGround, colorSchemeCss) h.PushCacheGroupHeadScript("CalCustomBackGround", 10, CalCustomBackGround, colorSchemeCss)
h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(customHeader, 10)) h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(customHeader, 10))
switch h.Scene() { switch h.Scene() {
case constraints.Detail: case constraints.Detail:

View File

@ -6,21 +6,16 @@ import (
"github.com/fthvgb1/wp-go/internal/wpconfig" "github.com/fthvgb1/wp-go/internal/wpconfig"
) )
func pushHeadScripts(h *wp.Handle) { func pushScripts(h *wp.Handle) {
var head = headScript h.PushCacheGroupFooterScript("head", 30, func(h *wp.Handle) string {
h.PushGroupHeadScript(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 return head
}) })
} h.PushGroupFooterScript(20, footerScript)
func pushFooterScripts(h *wp.Handle) {
var footer = footerScript
h.PushGroupFooterScript(20, func(h *wp.Handle) string {
if "dark" == wpconfig.GetThemeModsVal(ThemeName, "colorscheme", "light") {
footer = fmt.Sprintf("%s\n%s", footerScript, ` <link rel="stylesheet" id="twentyseventeen-colors-dark-css" href="/wp-content/themes/twentyseventeen/assets/css/colors-dark.css?ver=20191025" media="all">`)
}
return footer
})
} }
var headScript = `<link rel='stylesheet' id='twentyseventeen-style-css' href='/wp-content/themes/twentyseventeen/style.css?ver=20221101' media='all' /> var headScript = `<link rel='stylesheet' id='twentyseventeen-style-css' href='/wp-content/themes/twentyseventeen/style.css?ver=20221101' media='all' />

View File

@ -55,9 +55,8 @@ func ready(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
h.PushHandleFn(constraints.Error404, errHandle) h.PushHandleFn(constraints.Error404, errHandle)
h.PushHandleFn(constraints.ParamError, errHandle) h.PushHandleFn(constraints.ParamError, errHandle)
h.PushHandleFn(constraints.InternalErr, errHandle) h.PushHandleFn(constraints.InternalErr, errHandle)
h.PushGroupHeadScript(10, colorScheme, customHeader) h.PushCacheGroupHeadScript("colorScheme-customHeader", 10, colorScheme, customHeader)
pushHeadScripts(h) pushScripts(h)
pushFooterScripts(h)
h.SetData("HeaderImage", getHeaderImage(h)) h.SetData("HeaderImage", getHeaderImage(h))
h.SetData("scene", h.Scene()) h.SetData("scene", h.Scene())
next(h) next(h)

View File

@ -103,11 +103,11 @@ func (d *DetailHandle) ContextPost() {
func (d *DetailHandle) Render() { func (d *DetailHandle) Render() {
d.PreCodeAndStats() d.PreCodeAndStats()
d.PreTemplate() d.PreTemplate()
reply := ""
if d.Post.CommentStatus == "open" && wpconfig.GetOption("thread_comments") == "1" { if d.Post.CommentStatus == "open" && wpconfig.GetOption("thread_comments") == "1" {
d.PushGroupFooterScript(10, func(h *Handle) string { reply = `<script src='/wp-includes/js/comment-reply.min.js' id='comment-reply-js'></script>`
return `<script src='/wp-includes/js/comment-reply.min.js' id='comment-reply-js'></script>`
})
} }
d.PushGroupFooterScript(10, reply)
d.PushHandleFn(constraints.Ok, NewHandleFn(func(h *Handle) { d.PushHandleFn(constraints.Ok, NewHandleFn(func(h *Handle) {
d.PasswordProject() d.PasswordProject()

View File

@ -39,7 +39,7 @@ type HandlePlugins map[string]HandleFn[*Handle]
// Components Order 为执行顺序,降序执行 // Components Order 为执行顺序,降序执行
type Components struct { type Components struct {
Fn func(*Handle) string str string
Order int Order int
} }
@ -107,8 +107,8 @@ func NewHandle(c *gin.Context, scene int, theme string) *Handle {
} }
} }
func NewComponents(fn func(*Handle) string, order int) Components { func (h *Handle) NewCacheComponent(name string, order int, fn func(handle *Handle) string) Components {
return Components{Fn: fn, Order: order} return Components{str: h.CacheStr(name, fn), Order: order}
} }
func (h *Handle) PushHandleFn(statsOrScene int, fns ...HandleCall) { func (h *Handle) PushHandleFn(statsOrScene int, fns ...HandleCall) {
@ -123,29 +123,49 @@ func (h *Handle) PushGroupHandleFn(statsOrScene, order int, fns ...HandleFn[*Han
h.handleFns[statsOrScene] = append(h.handleFns[statsOrScene], calls...) h.handleFns[statsOrScene] = append(h.handleFns[statsOrScene], calls...)
} }
func (h *Handle) AddComponent(name string, fn func(*Handle) string) { func (h *Handle) AddCacheComponent(name string, fn func(*Handle) string) {
h.ginH[name] = h.CacheStr(name, fn)
}
func (h *Handle) CacheStr(name string, fn func(*Handle) string) string {
v, ok := reload.GetStr(name) v, ok := reload.GetStr(name)
if !ok { if !ok {
v = fn(h) v = fn(h)
reload.SetStr(name, v) reload.SetStr(name, v)
} }
h.ginH[name] = v return v
} }
func (h *Handle) PushHeadScript(fn ...Components) { func (h *Handle) PushHeadScript(fn ...Components) {
h.PushComponents(constraints.HeadScript, fn...) h.PushComponents(constraints.HeadScript, fn...)
} }
func (h *Handle) PushGroupHeadScript(order int, fns ...func(*Handle) string) { func (h *Handle) PushGroupHeadScript(order int, str ...string) {
h.PushGroupComponents(constraints.HeadScript, order, fns...) h.PushGroupComponents(constraints.HeadScript, order, str...)
} }
func (h *Handle) PushCacheGroupHeadScript(key string, order int, fns ...func(*Handle) string) {
h.PushGroupCacheScript(constraints.HeadScript, key, order, fns...)
}
func (h *Handle) PushFooterScript(fn ...Components) { func (h *Handle) PushFooterScript(fn ...Components) {
h.PushComponents(constraints.FooterScript, fn...) h.PushComponents(constraints.FooterScript, fn...)
} }
func (h *Handle) PushGroupFooterScript(order int, fns ...func(*Handle) string) { func (h *Handle) PushGroupFooterScript(order int, fns ...string) {
h.PushGroupComponents(constraints.FooterScript, order, fns...) h.PushGroupComponents(constraints.FooterScript, order, fns...)
} }
func (h *Handle) componentKey(name string) string {
return fmt.Sprintf("theme_%d_%s", h.scene, name)
}
func (h *Handle) PushCacheGroupFooterScript(key string, order int, fns ...func(*Handle) string) {
h.PushGroupCacheScript(constraints.FooterScript, key, order, fns...)
}
func (h *Handle) PushGroupCacheScript(name, key string, order int, fns ...func(*Handle) string) {
v := reload.GetStrBy(key, "\n", h, fns...)
h.PushGroupComponents(name, order, v)
}
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 {
@ -206,8 +226,8 @@ func (h *Handle) Render() {
} }
func (h *Handle) CommonComponents() { func (h *Handle) CommonComponents() {
h.AddComponent("customLogo", CalCustomLogo) h.AddCacheComponent("customLogo", CalCustomLogo)
h.PushGroupHeadScript(0, CalSiteIcon, CalCustomCss) h.PushCacheGroupHeadScript("siteIconAndCustomCss", 0, CalSiteIcon, CalCustomCss)
h.PushHandleFn(constraints.AllStats, NewHandleFn(func(h *Handle) { h.PushHandleFn(constraints.AllStats, NewHandleFn(func(h *Handle) {
h.CalMultipleComponents() h.CalMultipleComponents()
h.CalBodyClass() h.CalBodyClass()
@ -221,7 +241,7 @@ func (h *Handle) PushComponents(name string, components ...Components) {
h.components[k] = append(h.components[k], components...) h.components[k] = append(h.components[k], components...)
} }
func (h *Handle) PushGroupComponents(name string, order int, fns ...func(*Handle) string) { func (h *Handle) PushGroupComponents(name string, order int, fns ...string) {
var calls []Components var calls []Components
for _, fn := range fns { for _, fn := range fns {
calls = append(calls, Components{fn, order}) calls = append(calls, Components{fn, order})
@ -230,24 +250,17 @@ func (h *Handle) PushGroupComponents(name string, order int, fns ...func(*Handle
h.components[k] = append(h.components[k], calls...) h.components[k] = append(h.components[k], calls...)
} }
func (h *Handle) componentKey(name string) string {
return fmt.Sprintf("%d_%s", h.scene, name)
}
func (h *Handle) CalMultipleComponents() { func (h *Handle) CalMultipleComponents() {
for k, ss := range h.components { for k, ss := range h.components {
v, ok := reload.GetStr(k)
if !ok {
slice.Sort(ss, func(i, j Components) bool { slice.Sort(ss, func(i, j Components) bool {
return i.Order > j.Order return i.Order > j.Order
}) })
v = strings.Join(slice.FilterAndMap(ss, func(t Components) (string, bool) { v := strings.Join(slice.FilterAndMap(ss, func(t Components) (string, bool) {
s := t.Fn(h) s := t.str
return s, s != "" return s, s != ""
}), "\n") }), "\n")
reload.SetStr(k, v) kk := strings.Split(k, "_")
} key := kk[len(kk)-1]
key := strings.Split(k, "_")[1]
h.ginH[key] = v h.ginH[key] = v
} }
} }