优化完善

This commit is contained in:
xing 2023-04-19 15:27:58 +08:00
parent 8da369b166
commit 8a9209196e
7 changed files with 71 additions and 52 deletions

View File

@ -14,6 +14,7 @@ const (
ParamError ParamError
InternalErr InternalErr
AllStats AllStats
AllScene
Defaults = "default" Defaults = "default"

View File

@ -41,7 +41,7 @@ func Init(fs embed.FS) {
} }
} }
var pipe = wp.HandlePipe(wp.ExecuteHandleFn, widget.MiddleWare(ready, data)...) var pipe = wp.HandlePipe(wp.ExecuteHandleFn, widget.MiddleWare(ready, wp.DataHandle)...)
func Hook(h *wp.Handle) { func Hook(h *wp.Handle) {
pipe(h) pipe(h)
@ -53,16 +53,6 @@ func ready(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
next(h) next(h)
} }
func data(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
if h.Scene() == constraints.Detail {
wp.Details(h)
} else {
wp.Indexs(h)
}
h.DetermineHandleFns()
next(h)
}
func configs(h *wp.Handle) { func configs(h *wp.Handle) {
conf := config.GetConfig() conf := config.GetConfig()
h.PushComponentFilterFn(widgets.Search, func(h *wp.Handle, s string, args ...any) string { h.PushComponentFilterFn(widgets.Search, func(h *wp.Handle, s string, args ...any) string {
@ -73,11 +63,15 @@ func configs(h *wp.Handle) {
h.PushCacheGroupHeadScript("CalCustomBackGround", 10, CalCustomBackGround, colorSchemeCss) h.PushCacheGroupHeadScript("CalCustomBackGround", 10, CalCustomBackGround, colorSchemeCss)
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())...))
h.PushHandleFn(constraints.Ok, wp.NewHandleFn(components.WidgetArea, 20)) components.WidgetArea(h)
h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(customHeader, 10)) h.PushRender(constraints.AllStats, wp.NewHandleFn(customHeader, 10))
h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(wp.IndexRender, 50)) h.PushRender(constraints.AllStats, wp.NewHandleFn(wp.IndexRender, 50))
h.PushHandleFn(constraints.Detail, wp.NewHandleFn(wp.DetailRender, 50)) h.PushRender(constraints.Detail, wp.NewHandleFn(wp.DetailRender, 50))
h.PushHandleFn(constraints.Detail, wp.NewHandleFn(postThumb, 60)) h.PushRender(constraints.Detail, wp.NewHandleFn(postThumb, 60))
h.PushDataHandler(constraints.Detail, wp.NewHandleFn(wp.Details, 100))
h.PushDataHandler(constraints.AllScene, wp.NewHandleFn(wp.Indexs, 100))
h.PushDataHandler(constraints.AllScene, wp.NewHandleFn(wp.PreCodeAndStats, 80))
h.PushDataHandler(constraints.AllScene, wp.NewHandleFn(wp.PreTemplate, 70))
} }
func postThumb(h *wp.Handle) { func postThumb(h *wp.Handle) {

View File

@ -42,7 +42,7 @@ var paginate = func() plugins.PageEle {
return p return p
}() }()
var pipe = wp.HandlePipe(wp.ExecuteHandleFn, widget.MiddleWare(ready, data)...) var pipe = wp.HandlePipe(wp.ExecuteHandleFn, widget.MiddleWare(ready, wp.DataHandle)...)
func Hook(h *wp.Handle) { func Hook(h *wp.Handle) {
pipe(h) pipe(h)
@ -55,20 +55,23 @@ func configs(h *wp.Handle) {
h.PushCacheGroupHeadScript("colorScheme-customHeader", 10, colorScheme, customHeader) h.PushCacheGroupHeadScript("colorScheme-customHeader", 10, colorScheme, customHeader)
components.WidgetArea(h) components.WidgetArea(h)
pushScripts(h) pushScripts(h)
h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(func(h *wp.Handle) { h.PushRender(constraints.AllStats, wp.NewHandleFn(func(h *wp.Handle) {
h.SetData("HeaderImage", getHeaderImage(h)) h.SetData("HeaderImage", getHeaderImage(h))
}, 10)) }, 10))
h.SetComponentsArgs(widgets.Widget, map[string]string{ h.SetComponentsArgs(widgets.Widget, map[string]string{
"{$before_widget}": `<section id="%s" class="%s">`, "{$before_widget}": `<section id="%s" class="%s">`,
"{$after_widget}": `</section>`, "{$after_widget}": `</section>`,
}) })
h.PushGroupHandleFn(constraints.AllStats, 90, wp.PreTemplate, errorsHandle) h.PushGroupRender(constraints.AllStats, 90, wp.PreTemplate, errorsHandle)
h.CommonComponents() h.CommonComponents()
h.Index.SetPageEle(paginate) h.Index.SetPageEle(paginate)
h.Index.SetListPlugin(wp.PostsPlugins(wp.PostPlugin(postThumbnail), wp.GetListPostPlugins(conf.ListPagePlugins, wp.ListPostPlugins())...)) h.Index.SetListPlugin(wp.PostsPlugins(wp.PostPlugin(postThumbnail), wp.GetListPostPlugins(conf.ListPagePlugins, wp.ListPostPlugins())...))
wp.SetComponentsArgsForMap(h, widgets.Search, "{$form}", searchForm) wp.SetComponentsArgsForMap(h, widgets.Search, "{$form}", searchForm)
h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(wp.IndexRender, 10)) h.PushRender(constraints.AllStats, wp.NewHandleFn(wp.IndexRender, 10))
h.PushHandleFn(constraints.Detail, wp.NewHandleFn(wp.DetailRender, 10)) h.PushRender(constraints.Detail, wp.NewHandleFn(wp.DetailRender, 10))
h.PushDataHandler(constraints.Detail, wp.NewHandleFn(detail, 100))
h.PushDataHandler(constraints.AllScene, wp.NewHandleFn(index, 100))
h.PushDataHandler(constraints.AllScene, wp.NewHandleFn(wp.PreCodeAndStats, 90))
} }
func ready(next wp.HandleFn[*wp.Handle], h *wp.Handle) { func ready(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
wp.InitThemeArgAndConfig(configs, h) wp.InitThemeArgAndConfig(configs, h)
@ -95,17 +98,6 @@ func errorsHandle(h *wp.Handle) {
} }
} }
func data(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
if h.Scene() == constraints.Detail {
detail(h)
} else {
index(h)
}
wp.PreCodeAndStats(h)
h.DetermineHandleFns()
next(h)
}
func index(h *wp.Handle) { func index(h *wp.Handle) {
if h.Scene() == constraints.Detail { if h.Scene() == constraints.Detail {
return return

View File

@ -93,7 +93,7 @@ func (h *Handle) GetHeaderImages(theme string) (r []models.PostThumbnail, err er
} }
func thumb(m models.Posts, theme string) models.PostThumbnail { func thumb(m models.Posts, theme string) models.PostThumbnail {
m.Thumbnail = wpconfig.Thumbnail(m.AttachmentMetadata, "thumbnail", "", "thumbnail", "post-thumbnail", fmt.Sprintf("%s-thumbnail-avatar", theme)) m.Thumbnail = wpconfig.Thumbnail(m.AttachmentMetadata, "full", "", "thumbnail", "post-thumbnail", fmt.Sprintf("%s-thumbnail-avatar", theme))
m.Thumbnail.Width = m.AttachmentMetadata.Width m.Thumbnail.Width = m.AttachmentMetadata.Width
m.Thumbnail.Height = m.AttachmentMetadata.Height m.Thumbnail.Height = m.AttachmentMetadata.Height
if m.Thumbnail.Path != "" { if m.Thumbnail.Path != "" {

View File

@ -116,6 +116,4 @@ func DetailRender(h *Handle) {
func Details(h *Handle) { func Details(h *Handle) {
_ = h.Detail.BuildDetailData() _ = h.Detail.BuildDetailData()
PreCodeAndStats(h)
PreTemplate(h)
} }

View File

@ -153,8 +153,6 @@ func Indexs(h *Handle) {
} }
i := h.Index i := h.Index
_ = i.BuildIndexData(NewIndexParams(i.C)) _ = i.BuildIndexData(NewIndexParams(i.C))
PreCodeAndStats(h)
PreTemplate(h)
} }
func (i *IndexHandle) MarkSticky(posts *[]models.Posts) { func (i *IndexHandle) MarkSticky(posts *[]models.Posts) {

View File

@ -30,7 +30,8 @@ type Handle struct {
templ string templ string
components map[string][]Components[string] components map[string][]Components[string]
themeMods wpconfig.ThemeMods themeMods wpconfig.ThemeMods
handleFns map[int][]HandleCall renders map[int][]HandleCall
dataHandler map[int][]HandleCall
err error err error
abort bool abort bool
componentsArgs map[string]any componentsArgs map[string]any
@ -69,7 +70,8 @@ func InitThemeArgAndConfig(fn func(*Handle), h *Handle) {
var inited = false var inited = false
hh := reload.GetAnyValBys("themeArgAndConfig", h, func(h *Handle) Handle { hh := reload.GetAnyValBys("themeArgAndConfig", h, func(h *Handle) Handle {
h.components = make(map[string][]Components[string]) h.components = make(map[string][]Components[string])
h.handleFns = make(map[int][]HandleCall) h.renders = make(map[int][]HandleCall)
h.dataHandler = make(map[int][]HandleCall)
h.componentsArgs = make(map[string]any) h.componentsArgs = make(map[string]any)
h.componentFilterFn = make(map[string][]func(*Handle, string, ...any) string) h.componentFilterFn = make(map[string][]func(*Handle, string, ...any) string)
h.ginH = gin.H{} h.ginH = gin.H{}
@ -97,7 +99,8 @@ func InitThemeArgAndConfig(fn func(*Handle), h *Handle) {
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.handleFns = hh.handleFns h.renders = hh.renders
h.dataHandler = hh.dataHandler
h.componentsArgs = hh.componentsArgs h.componentsArgs = hh.componentsArgs
h.componentFilterFn = hh.componentFilterFn h.componentFilterFn = hh.componentFilterFn
} }
@ -227,16 +230,45 @@ func (h *Handle) NewCacheComponent(name string, order int, fn func(handle *Handl
return Components[string]{Fn: fn, CacheKey: name, Order: order} return Components[string]{Fn: fn, CacheKey: name, Order: order}
} }
func (h *Handle) PushHandleFn(statsOrScene int, fns ...HandleCall) { func (h *Handle) PushRender(statsOrScene int, fns ...HandleCall) {
h.handleFns[statsOrScene] = append(h.handleFns[statsOrScene], fns...) h.renders[statsOrScene] = append(h.renders[statsOrScene], fns...)
}
func (h *Handle) PushDataHandler(Scene int, fns ...HandleCall) {
h.dataHandler[Scene] = append(h.dataHandler[Scene], fns...)
} }
func (h *Handle) PushGroupHandleFn(statsOrScene, order int, fns ...HandleFn[*Handle]) { func (h *Handle) PushGroupRender(statsOrScene, order int, fns ...HandleFn[*Handle]) {
var calls []HandleCall var calls []HandleCall
for _, fn := range fns { for _, fn := range fns {
calls = append(calls, HandleCall{fn, order}) calls = append(calls, HandleCall{fn, order})
} }
h.handleFns[statsOrScene] = append(h.handleFns[statsOrScene], calls...) h.renders[statsOrScene] = append(h.renders[statsOrScene], calls...)
}
func (h *Handle) PushGroupDataHandler(scene, order int, fns ...HandleFn[*Handle]) {
var calls []HandleCall
for _, fn := range fns {
calls = append(calls, HandleCall{fn, order})
}
h.dataHandler[scene] = append(h.dataHandler[scene], calls...)
}
func DataHandle(next HandleFn[*Handle], h *Handle) {
handlers := reload.SafetyMapBy("dataHandle", h.scene, h, func(h *Handle) []HandleCall {
a := h.dataHandler[h.scene]
aa, ok := h.dataHandler[constraints.AllScene]
if ok {
a = append(a, aa...)
}
slice.Sort(a, func(i, j HandleCall) bool {
return i.Order > j.Order
})
return a
})
for _, handler := range handlers {
handler.Fn(h)
}
h.DetermineHandleFns()
next(h)
} }
func (h *Handle) AddCacheComponent(name string, fn func(*Handle) string) { func (h *Handle) AddCacheComponent(name string, fn func(*Handle) string) {
@ -280,16 +312,16 @@ func (h *Handle) GetPassword() {
} }
func (h *Handle) ExecHandleFns() { func (h *Handle) ExecHandleFns() {
calls, ok := h.handleFns[h.Stats] calls, ok := h.renders[h.Stats]
var fns []HandleCall var fns []HandleCall
if ok { if ok {
fns = append(fns, calls...) fns = append(fns, calls...)
} }
calls, ok = h.handleFns[h.scene] calls, ok = h.renders[h.scene]
if ok { if ok {
fns = append(fns, calls...) fns = append(fns, calls...)
} }
calls, ok = h.handleFns[constraints.AllStats] calls, ok = h.renders[constraints.AllStats]
if ok { if ok {
fns = append(fns, calls...) fns = append(fns, calls...)
} }
@ -334,8 +366,8 @@ func (h *Handle) Render() {
func (h *Handle) CommonComponents() { func (h *Handle) CommonComponents() {
h.AddCacheComponent("customLogo", CalCustomLogo) h.AddCacheComponent("customLogo", CalCustomLogo)
h.PushCacheGroupHeadScript("siteIconAndCustomCss", 0, CalSiteIcon, CalCustomCss) h.PushCacheGroupHeadScript("siteIconAndCustomCss", 0, CalSiteIcon, CalCustomCss)
h.PushGroupHandleFn(constraints.AllStats, 10, CalComponents) h.PushGroupRender(constraints.AllStats, 10, CalComponents)
h.PushHandleFn(constraints.AllStats, NewHandleFn(func(h *Handle) { h.PushRender(constraints.AllStats, NewHandleFn(func(h *Handle) {
h.C.HTML(h.Code, h.templ, h.ginH) h.C.HTML(h.Code, h.templ, h.ginH)
}, 0)) }, 0))
} }
@ -406,16 +438,20 @@ func HandlePipe[T any](initial func(T), fns ...HandlePipeFn[T]) HandleFn[T] {
} }
func DetermineHandleFn(h *Handle) []HandleCall { func DetermineHandleFn(h *Handle) []HandleCall {
calls, ok := h.handleFns[h.Stats] calls, ok := h.renders[h.Stats]
var fns []HandleCall var fns []HandleCall
if ok { if ok {
fns = append(fns, calls...) fns = append(fns, calls...)
} }
calls, ok = h.handleFns[h.scene] calls, ok = h.renders[h.scene]
if ok { if ok {
fns = append(fns, calls...) fns = append(fns, calls...)
} }
calls, ok = h.handleFns[constraints.AllStats] calls, ok = h.renders[constraints.AllStats]
if ok {
fns = append(fns, calls...)
}
calls, ok = h.renders[constraints.AllScene]
if ok { if ok {
fns = append(fns, calls...) fns = append(fns, calls...)
} }