优化文档列表的插件机制

This commit is contained in:
xing 2023-05-09 13:44:20 +08:00
parent 21d0b8c041
commit b0618cf800
5 changed files with 42 additions and 14 deletions

View File

@ -1,7 +1,6 @@
package twentyfifteen
import (
"github.com/fthvgb1/wp-go/app/pkg/config"
"github.com/fthvgb1/wp-go/app/pkg/constraints"
"github.com/fthvgb1/wp-go/app/pkg/constraints/widgets"
"github.com/fthvgb1/wp-go/app/plugins"
@ -19,7 +18,6 @@ func Hook(h *wp.Handle) {
}
func configs(h *wp.Handle) {
conf := config.GetConfig()
h.PushComponentFilterFn(widgets.Search, func(h *wp.Handle, s string, args ...any) string {
return strings.ReplaceAll(s, `class="search-submit"`, `class="search-submit screen-reader-text"`)
})
@ -31,7 +29,6 @@ func configs(h *wp.Handle) {
h.PushCacheGroupHeadScript(constraints.AllScene, "CalCustomBackGround", 10, CalCustomBackGround)
h.PushCacheGroupHeadScript(constraints.AllScene, "colorSchemeCss", 10, colorSchemeCss)
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))

View File

@ -3,7 +3,6 @@ package twentyseventeen
import (
"fmt"
"github.com/fthvgb1/wp-go/app/cmd/reload"
"github.com/fthvgb1/wp-go/app/pkg/config"
"github.com/fthvgb1/wp-go/app/pkg/constraints"
"github.com/fthvgb1/wp-go/app/pkg/constraints/widgets"
"github.com/fthvgb1/wp-go/app/pkg/logs"
@ -36,7 +35,6 @@ func Hook(h *wp.Handle) {
}
func configs(h *wp.Handle) {
conf := config.GetConfig()
wp.InitPipe(h)
h.PushHandler(constraints.PipeMiddleware, constraints.Home,
wp.NewHandleFn(widget.IsCategory, 100, "widget.IsCategory"))
@ -57,7 +55,7 @@ func configs(h *wp.Handle) {
h.CommonComponents()
h.Index.SetPageEle(paginate)
wp.ReplyCommentJs(h)
h.Index.SetListPlugin(wp.PostsPlugins(wp.PostPlugin(postThumbnail), wp.GetListPostPlugins(conf.ListPagePlugins, wp.ListPostPlugins())...))
h.PushPostPlugin(postThumbnail)
wp.SetComponentsArgsForMap(h, widgets.Search, "{$form}", searchForm)
wp.PushIndexHandler(constraints.PipeRender, h, wp.NewHandleFn(wp.IndexRender, 10, "wp.IndexRender"))
h.PushRender(constraints.Detail, wp.NewHandleFn(wp.DetailRender, 10, "wp.DetailRender"))

View File

@ -45,8 +45,9 @@ func searchArgs() map[string]string {
}
}
var form = html5SearchForm
func Search(h *wp.Handle, id string) string {
form := html5SearchForm
args := reload.GetAnyValBys("widget-search-args", h, func(h *wp.Handle) map[string]string {
search := searchArgs()
commonArgs := wp.GetComponentsArgs(h, widgets.Widget, map[string]string{})

View File

@ -3,6 +3,7 @@ package wp
import (
"database/sql"
"fmt"
"github.com/fthvgb1/wp-go/app/cmd/reload"
"github.com/fthvgb1/wp-go/app/pkg/cache"
"github.com/fthvgb1/wp-go/app/pkg/constraints"
"github.com/fthvgb1/wp-go/app/pkg/models"
@ -137,10 +138,14 @@ func (i *IndexHandle) BuildIndexData(parm *IndexParams) (err error) {
}
func (i *IndexHandle) ExecPostsPlugin() {
if i.postsPlugin != nil {
for j := range i.Posts {
i.postsPlugin(i.Handle, &i.Posts[j])
fn := i.postsPlugin
if fn == nil {
fn = reload.GetAnyValBys("postPlugins", i, func(a *IndexHandle) PostsPlugin {
return UsePostsPlugins()
})
}
for j := range i.Posts {
fn(i.Handle, &i.Posts[j])
}
}

View File

@ -1,6 +1,7 @@
package wp
import (
"github.com/fthvgb1/wp-go/app/cmd/reload"
"github.com/fthvgb1/wp-go/app/pkg/config"
"github.com/fthvgb1/wp-go/app/pkg/models"
"github.com/fthvgb1/wp-go/app/plugins"
@ -19,9 +20,14 @@ func PostsPlugins(initial PostsPlugin, calls ...func(PostsPlugin, *Handle, *mode
}, initial)
}
var pluginFns = map[string]func(PostsPlugin, *Handle, *models.Posts){
var pluginFns = reload.Vars(map[string]func(PostsPlugin, *Handle, *models.Posts){
"passwordProject": PasswordProject,
"digest": Digest,
})
func (h *Handle) PushPostsPlugin(name string, fn func(PostsPlugin, *Handle, *models.Posts)) {
m := pluginFns.Load()
m[name] = fn
}
// PasswordProject 标题和内容密码保护
@ -47,7 +53,15 @@ func Digest(next PostsPlugin, h *Handle, post *models.Posts) {
next(h, post)
}
func PostPlugin(calls ...func(h *Handle, posts *models.Posts)) PostsPlugin {
var ordinaryPlugin = reload.Vars([]PostsPlugin{})
func (h *Handle) PushPostPlugin(plugin ...PostsPlugin) {
p := ordinaryPlugin.Load()
p = append(p, plugin...)
ordinaryPlugin.Store(p)
}
func PostPlugin(calls ...PostsPlugin) PostsPlugin {
return func(h *Handle, posts *models.Posts) {
for _, call := range calls {
call(h, posts)
@ -55,8 +69,21 @@ func PostPlugin(calls ...func(h *Handle, posts *models.Posts)) PostsPlugin {
}
}
func UsePostsPlugins() PostsPlugin {
p := config.GetConfig().ListPagePlugins
var pluginss []func(PostsPlugin, *Handle, *models.Posts)
m := pluginFns.Load()
for _, s := range p {
f, ok := m[s]
if ok {
pluginss = append(pluginss, f)
}
}
return PostsPlugins(PostPlugin(ordinaryPlugin.Load()...), pluginss...)
}
func ListPostPlugins() map[string]func(PostsPlugin, *Handle, *models.Posts) {
return maps.Copy(pluginFns)
return maps.Copy(pluginFns.Load())
}
func ProjectTitle(t models.Posts) models.Posts {