This commit is contained in:
xing 2023-02-12 19:19:53 +08:00
parent bfd4e4ac4d
commit b1e824b27d
4 changed files with 12 additions and 12 deletions

View File

@ -45,17 +45,17 @@ func (h *Handle) GetPassword() {
} }
} }
func (i *IndexHandle) ExecListPagePlugin(m map[string]Plugin[models.Posts], calls ...func(*models.Posts)) { func (i *IndexHandle) ExecListPagePlugin(m map[string]Plugin[models.Posts, *Handle], calls ...func(*models.Posts)) {
pluginConf := config.GetConfig().ListPagePlugins pluginConf := config.GetConfig().ListPagePlugins
plugin := GetListPostPlugins(pluginConf, m) plugin := GetListPostPlugins(pluginConf, m)
i.GinH["posts"] = slice.Map(i.Posts, PluginFn[models.Posts](plugin, i.Handle, Defaults(calls...))) i.GinH["posts"] = slice.Map(i.Posts, PluginFn[models.Posts, *Handle](plugin, i.Handle, Defaults(calls...)))
} }
func ListPostPlugins() map[string]Plugin[models.Posts] { func ListPostPlugins() map[string]Plugin[models.Posts, *Handle] {
return maps.Copy(pluginFns) return maps.Copy(pluginFns)
} }
@ -79,8 +79,8 @@ func ProjectTitle(t models.Posts) models.Posts {
return t return t
} }
func GetListPostPlugins(name []string, m map[string]Plugin[models.Posts]) []Plugin[models.Posts] { func GetListPostPlugins(name []string, m map[string]Plugin[models.Posts, *Handle]) []Plugin[models.Posts, *Handle] {
return slice.FilterAndMap(name, func(t string) (Plugin[models.Posts], bool) { return slice.FilterAndMap(name, func(t string) (Plugin[models.Posts, *Handle], bool) {
v, ok := m[t] v, ok := m[t]
if ok { if ok {
return v, true return v, true

View File

@ -61,7 +61,7 @@ type IndexHandle struct {
Posts []models.Posts Posts []models.Posts
PageEle pagination.Elements PageEle pagination.Elements
TotalRows int TotalRows int
PostsPlugins map[string]Plugin[models.Posts] PostsPlugins map[string]Plugin[models.Posts, *Handle]
} }
func NewIndexHandle(handle *Handle) *IndexHandle { func NewIndexHandle(handle *Handle) *IndexHandle {

View File

@ -8,17 +8,17 @@ import (
) )
type Fn[T any] func(T) T type Fn[T any] func(T) T
type Plugin[T any] func(initialFn Fn[T], h *Handle, t T) T type Plugin[T, H any] func(initialFn Fn[T], h H, t T) T
func PluginFn[T any](a []Plugin[T], h *Handle, fn Fn[T]) Fn[T] { func PluginFn[T, H any](a []Plugin[T, H], h H, fn Fn[T]) Fn[T] {
return slice.ReverseReduce(a, func(next Plugin[T], fn Fn[T]) Fn[T] { return slice.ReverseReduce(a, func(next Plugin[T, H], fn Fn[T]) Fn[T] {
return func(t T) T { return func(t T) T {
return next(fn, h, t) return next(fn, h, t)
} }
}, fn) }, fn)
} }
var pluginFns = map[string]Plugin[models.Posts]{ var pluginFns = map[string]Plugin[models.Posts, *Handle]{
"passwordProject": PasswordProject, "passwordProject": PasswordProject,
"digest": Digest, "digest": Digest,
} }

View File

@ -65,8 +65,8 @@ func Hook(h *common.Handle) {
newIndexHandle(common.NewIndexHandle(h)).Index() newIndexHandle(common.NewIndexHandle(h)).Index()
} }
var pluginFns = func() map[string]common.Plugin[models.Posts] { var pluginFns = func() map[string]common.Plugin[models.Posts, *common.Handle] {
return maps.Merge(common.ListPostPlugins(), map[string]common.Plugin[models.Posts]{ return maps.Merge(common.ListPostPlugins(), map[string]common.Plugin[models.Posts, *common.Handle]{
"twentyseventeen_postThumbnail": postThumbnail, "twentyseventeen_postThumbnail": postThumbnail,
}) })
}() }()