调整优化修复bug
This commit is contained in:
parent
997839e98a
commit
a905f59eae
|
@ -1,6 +1,10 @@
|
|||
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()
|
||||
|
||||
|
@ -13,6 +17,18 @@ func SetStr(name, val string) {
|
|||
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] {
|
||||
ss := safety.NewVar(defaults)
|
||||
calls = append(calls, func() {
|
||||
|
|
|
@ -6,13 +6,9 @@ import (
|
|||
)
|
||||
|
||||
func EnlighterJS(h *wp.Handle) {
|
||||
h.PushGroupHeadScript(20, func(h *wp.Handle) string {
|
||||
return `<link rel='stylesheet' id='enlighterjs-css' href='/wp-content/plugins/enlighter/cache/enlighterjs.min.css' media='all' />`
|
||||
})
|
||||
h.PushGroupHeadScript(20, `<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 {
|
||||
return str.Join(`<script src='/wp-content/plugins/enlighter/cache/enlighterjs.min.js?ver=0A0B0C' id='enlighterjs-js'></script>`, "\n", enlighterjs)
|
||||
})
|
||||
h.PushGroupFooterScript(10, 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'>
|
||||
|
|
|
@ -41,7 +41,7 @@ func dispatch(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
|
|||
h.GetPassword()
|
||||
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))
|
||||
switch h.Scene() {
|
||||
case constraints.Detail:
|
||||
|
|
|
@ -6,21 +6,16 @@ import (
|
|||
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
||||
)
|
||||
|
||||
func pushHeadScripts(h *wp.Handle) {
|
||||
var head = headScript
|
||||
h.PushGroupHeadScript(30, func(h *wp.Handle) string {
|
||||
func pushScripts(h *wp.Handle) {
|
||||
h.PushCacheGroupFooterScript("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)
|
||||
|
||||
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' />
|
||||
|
|
|
@ -55,9 +55,8 @@ func ready(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
|
|||
h.PushHandleFn(constraints.Error404, errHandle)
|
||||
h.PushHandleFn(constraints.ParamError, errHandle)
|
||||
h.PushHandleFn(constraints.InternalErr, errHandle)
|
||||
h.PushGroupHeadScript(10, colorScheme, customHeader)
|
||||
pushHeadScripts(h)
|
||||
pushFooterScripts(h)
|
||||
h.PushCacheGroupHeadScript("colorScheme-customHeader", 10, colorScheme, customHeader)
|
||||
pushScripts(h)
|
||||
h.SetData("HeaderImage", getHeaderImage(h))
|
||||
h.SetData("scene", h.Scene())
|
||||
next(h)
|
||||
|
|
|
@ -103,11 +103,11 @@ func (d *DetailHandle) ContextPost() {
|
|||
func (d *DetailHandle) Render() {
|
||||
d.PreCodeAndStats()
|
||||
d.PreTemplate()
|
||||
reply := ""
|
||||
if d.Post.CommentStatus == "open" && wpconfig.GetOption("thread_comments") == "1" {
|
||||
d.PushGroupFooterScript(10, func(h *Handle) string {
|
||||
return `<script src='/wp-includes/js/comment-reply.min.js' id='comment-reply-js'></script>`
|
||||
})
|
||||
reply = `<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.PasswordProject()
|
||||
|
|
|
@ -39,7 +39,7 @@ type HandlePlugins map[string]HandleFn[*Handle]
|
|||
|
||||
// Components Order 为执行顺序,降序执行
|
||||
type Components struct {
|
||||
Fn func(*Handle) string
|
||||
str string
|
||||
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 {
|
||||
return Components{Fn: fn, Order: order}
|
||||
func (h *Handle) NewCacheComponent(name string, order int, fn func(handle *Handle) string) Components {
|
||||
return Components{str: h.CacheStr(name, fn), Order: order}
|
||||
}
|
||||
|
||||
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...)
|
||||
}
|
||||
|
||||
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)
|
||||
if !ok {
|
||||
v = fn(h)
|
||||
reload.SetStr(name, v)
|
||||
}
|
||||
h.ginH[name] = v
|
||||
return v
|
||||
}
|
||||
|
||||
func (h *Handle) PushHeadScript(fn ...Components) {
|
||||
h.PushComponents(constraints.HeadScript, fn...)
|
||||
}
|
||||
func (h *Handle) PushGroupHeadScript(order int, fns ...func(*Handle) string) {
|
||||
h.PushGroupComponents(constraints.HeadScript, order, fns...)
|
||||
func (h *Handle) PushGroupHeadScript(order int, str ...string) {
|
||||
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) {
|
||||
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...)
|
||||
}
|
||||
|
||||
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() {
|
||||
pw := h.Session.Get("post_password")
|
||||
if pw != nil {
|
||||
|
@ -206,8 +226,8 @@ func (h *Handle) Render() {
|
|||
}
|
||||
|
||||
func (h *Handle) CommonComponents() {
|
||||
h.AddComponent("customLogo", CalCustomLogo)
|
||||
h.PushGroupHeadScript(0, CalSiteIcon, CalCustomCss)
|
||||
h.AddCacheComponent("customLogo", CalCustomLogo)
|
||||
h.PushCacheGroupHeadScript("siteIconAndCustomCss", 0, CalSiteIcon, CalCustomCss)
|
||||
h.PushHandleFn(constraints.AllStats, NewHandleFn(func(h *Handle) {
|
||||
h.CalMultipleComponents()
|
||||
h.CalBodyClass()
|
||||
|
@ -221,7 +241,7 @@ func (h *Handle) PushComponents(name string, components ...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
|
||||
for _, fn := range fns {
|
||||
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...)
|
||||
}
|
||||
|
||||
func (h *Handle) componentKey(name string) string {
|
||||
return fmt.Sprintf("%d_%s", h.scene, name)
|
||||
}
|
||||
|
||||
func (h *Handle) CalMultipleComponents() {
|
||||
for k, ss := range h.components {
|
||||
v, ok := reload.GetStr(k)
|
||||
if !ok {
|
||||
slice.Sort(ss, func(i, j Components) bool {
|
||||
return i.Order > j.Order
|
||||
})
|
||||
v = strings.Join(slice.FilterAndMap(ss, func(t Components) (string, bool) {
|
||||
s := t.Fn(h)
|
||||
return s, s != ""
|
||||
}), "\n")
|
||||
reload.SetStr(k, v)
|
||||
}
|
||||
key := strings.Split(k, "_")[1]
|
||||
slice.Sort(ss, func(i, j Components) bool {
|
||||
return i.Order > j.Order
|
||||
})
|
||||
v := strings.Join(slice.FilterAndMap(ss, func(t Components) (string, bool) {
|
||||
s := t.str
|
||||
return s, s != ""
|
||||
}), "\n")
|
||||
kk := strings.Split(k, "_")
|
||||
key := kk[len(kk)-1]
|
||||
h.ginH[key] = v
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user