优化代码
This commit is contained in:
parent
3f96c09d36
commit
14aad9e15b
@ -1,15 +1,22 @@
|
||||
package actions
|
||||
|
||||
import (
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
|
||||
"github.com/fthvgb1/wp-go/internal/theme"
|
||||
"github.com/fthvgb1/wp-go/internal/theme/wp"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func ThemeHook(scene int) func(*gin.Context) {
|
||||
return func(ctx *gin.Context) {
|
||||
return func(c *gin.Context) {
|
||||
s := scene
|
||||
if scene == constraints.Home {
|
||||
if _, ok := c.GetQuery("s"); ok {
|
||||
s = constraints.Search
|
||||
}
|
||||
}
|
||||
t := theme.GetCurrentTemplateName()
|
||||
h := wp.NewHandle(ctx, scene, t)
|
||||
h := wp.NewHandle(c, s, t)
|
||||
h.Index = wp.NewIndexHandle(h)
|
||||
h.Detail = wp.NewDetailHandle(h)
|
||||
theme.Hook(t, h)
|
||||
|
@ -7,4 +7,6 @@ const (
|
||||
Archive = "archives"
|
||||
Categories = "categories"
|
||||
Meta = "meta"
|
||||
|
||||
Widget = "widget"
|
||||
)
|
||||
|
@ -41,7 +41,6 @@ func Hook(h *wp.Handle) {
|
||||
}
|
||||
|
||||
func ready(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
|
||||
components.WidgetArea(h)
|
||||
h.GetPassword()
|
||||
h.PushComponentFilterFn(widgets.Search, func(h *wp.Handle, s string) string {
|
||||
return strings.ReplaceAll(s, `class="search-submit"`, `class="search-submit screen-reader-text"`)
|
||||
@ -49,6 +48,7 @@ func ready(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
|
||||
wphandle.RegisterPlugins(h, config.GetConfig().Plugins...)
|
||||
|
||||
h.PushCacheGroupHeadScript("CalCustomBackGround", 10, CalCustomBackGround, colorSchemeCss)
|
||||
h.PushHandleFn(constraints.Ok, wp.NewHandleFn(components.WidgetArea, 20))
|
||||
h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(customHeader, 10))
|
||||
h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(wp.Indexs, 100))
|
||||
h.PushHandleFn(constraints.Detail, wp.NewHandleFn(wp.Details, 100))
|
||||
|
@ -43,34 +43,29 @@ var paginate = func() plugins.PageEle {
|
||||
return p
|
||||
}()
|
||||
|
||||
var pipe = wp.HandlePipe(wp.Render, widget.MiddleWare(ready, dispatch)...)
|
||||
var pipe = wp.HandlePipe(wp.Render, widget.MiddleWare(ready)...)
|
||||
|
||||
func Hook(h *wp.Handle) {
|
||||
pipe(h)
|
||||
}
|
||||
|
||||
func ready(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
|
||||
components.WidgetArea(h)
|
||||
h.GetPassword()
|
||||
wphandle.RegisterPlugins(h, config.GetConfig().Plugins...)
|
||||
h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(calClass, 20))
|
||||
errHandle := wp.NewHandleFn(errorsHandle, 100)
|
||||
h.PushHandleFn(constraints.Error404, errHandle)
|
||||
h.PushHandleFn(constraints.ParamError, errHandle)
|
||||
h.PushHandleFn(constraints.InternalErr, errHandle)
|
||||
h.PushHandleFn(constraints.AllStats, wp.NewHandleFn(index, 100))
|
||||
h.PushHandleFn(constraints.Detail, wp.NewHandleFn(detail, 100))
|
||||
h.PushGroupHandleFn(constraints.AllStats, 99, wp.PreCodeAndStats, wp.PreTemplate, errorsHandle)
|
||||
h.PushCacheGroupHeadScript("colorScheme-customHeader", 10, colorScheme, customHeader)
|
||||
h.PushHandleFn(constraints.Ok, wp.NewHandleFn(components.WidgetArea, 20))
|
||||
pushScripts(h)
|
||||
h.SetData("HeaderImage", getHeaderImage(h))
|
||||
h.SetData("scene", h.Scene())
|
||||
for _, s := range []string{widgets.Meta, widgets.Categories, widgets.Archive, widgets.Search, widgets.RecentComments, widgets.RecentPosts} {
|
||||
ss := strings.ReplaceAll(s, "-", "_")
|
||||
if s == widgets.RecentPosts {
|
||||
ss = "recent_entries"
|
||||
}
|
||||
wp.SetComponentsArgsForMap(h, s, "{$before_widget}", fmt.Sprintf(`<section id="%s-2" class="widget widget_%s">`, s, ss))
|
||||
wp.SetComponentsArgsForMap(h, s, "{$after_widget}", "</section>")
|
||||
}
|
||||
h.SetComponentsArgs(widgets.Widget, map[string]string{
|
||||
"{$before_widget}": `<section id="%s" class="widget widget_%s">`,
|
||||
"{$after_widget}": `</section>`,
|
||||
})
|
||||
wp.SetComponentsArgsForMap(h, widgets.Search, "{$form}", searchForm)
|
||||
|
||||
next(h)
|
||||
}
|
||||
|
||||
@ -85,15 +80,6 @@ var searchForm = `<form role="search" method="get" class="search-form" action="/
|
||||
</button>
|
||||
</form>`
|
||||
|
||||
func dispatch(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
|
||||
switch h.Scene() {
|
||||
case constraints.Detail:
|
||||
detail(next, h.Detail)
|
||||
default:
|
||||
index(next, h.Index)
|
||||
}
|
||||
}
|
||||
|
||||
var listPostsPlugins = func() map[string]wp.Plugin[models.Posts, *wp.Handle] {
|
||||
return maps.Merge(wp.ListPostPlugins(), map[string]wp.Plugin[models.Posts, *wp.Handle]{
|
||||
"twentyseventeen_postThumbnail": postThumbnail,
|
||||
@ -101,24 +87,29 @@ var listPostsPlugins = func() map[string]wp.Plugin[models.Posts, *wp.Handle] {
|
||||
}()
|
||||
|
||||
func errorsHandle(h *wp.Handle) {
|
||||
h.SetTempl("twentyseventeen/posts/error.gohtml")
|
||||
switch h.Stats {
|
||||
case constraints.Error404, constraints.InternalErr:
|
||||
case constraints.Error404, constraints.InternalErr, constraints.ParamError:
|
||||
logs.ErrPrintln(h.Err(), "报错:")
|
||||
h.SetTempl("twentyseventeen/posts/error.gohtml")
|
||||
}
|
||||
}
|
||||
|
||||
func index(next wp.HandleFn[*wp.Handle], i *wp.IndexHandle) {
|
||||
func index(h *wp.Handle) {
|
||||
if h.Scene() == constraints.Detail {
|
||||
return
|
||||
}
|
||||
i := h.Index
|
||||
err := i.BuildIndexData(wp.NewIndexParams(i.C))
|
||||
if err != nil {
|
||||
i.SetErr(err)
|
||||
}
|
||||
h.SetData("scene", h.Scene())
|
||||
i.SetPageEle(paginate)
|
||||
i.SetPostsPlugins(listPostsPlugins)
|
||||
next(i.Handle)
|
||||
}
|
||||
|
||||
func detail(next wp.HandleFn[*wp.Handle], d *wp.DetailHandle) {
|
||||
func detail(h *wp.Handle) {
|
||||
d := h.Detail
|
||||
err := d.BuildDetailData()
|
||||
if err != nil {
|
||||
d.SetErr(err)
|
||||
@ -129,10 +120,7 @@ func detail(next wp.HandleFn[*wp.Handle], d *wp.DetailHandle) {
|
||||
img.Srcset = fmt.Sprintf("%s %dw, %s", img.Path, img.Width, img.Srcset)
|
||||
d.Post.Thumbnail = img
|
||||
}
|
||||
|
||||
d.CommentRender = commentFormat
|
||||
|
||||
next(d.Handle)
|
||||
}
|
||||
|
||||
var commentFormat = comment{}
|
||||
|
@ -11,7 +11,7 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (h *Handle) CalBodyClass() {
|
||||
func CalBodyClass(h *Handle) {
|
||||
h.ginH["bodyClass"] = h.BodyClass(h.class...)
|
||||
}
|
||||
|
||||
|
@ -1,16 +1,18 @@
|
||||
package components
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/fthvgb1/wp-go/helper/slice"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/cache"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/constraints/widgets"
|
||||
"github.com/fthvgb1/wp-go/internal/theme/wp"
|
||||
"github.com/fthvgb1/wp-go/internal/theme/wp/components/widget"
|
||||
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var widgets = map[string]func(*wp.Handle) string{
|
||||
var widgetFn = map[string]func(*wp.Handle) string{
|
||||
"search": widget.SearchForm,
|
||||
"recent-posts": widget.RecentPosts,
|
||||
"recent-comments": widget.RecentComments,
|
||||
@ -20,23 +22,40 @@ var widgets = map[string]func(*wp.Handle) string{
|
||||
}
|
||||
|
||||
func WidgetArea(h *wp.Handle) {
|
||||
args := wp.GetComponentsArgs(h, widgets.Widget, map[string]string{})
|
||||
beforeWidget, ok := args["{$before_widget}"]
|
||||
if !ok {
|
||||
beforeWidget = ""
|
||||
} else {
|
||||
delete(args, "{$before_widget}")
|
||||
}
|
||||
v := wpconfig.GetPHPArrayVal("sidebars_widgets", []any{}, "sidebar-1")
|
||||
sidebar := slice.FilterAndMap(v, func(t any) (func(*wp.Handle) string, bool) {
|
||||
vv := t.(string)
|
||||
ss := strings.Split(vv, "-")
|
||||
id := ss[len(ss)-1]
|
||||
name := strings.Join(ss[0:len(ss)-1], "-")
|
||||
fn, ok := widgets[name]
|
||||
fn, ok := widgetFn[name]
|
||||
if ok {
|
||||
if id != "2" {
|
||||
wp.SetComponentsArgsForMap(h, name, "{$id}", id)
|
||||
}
|
||||
if beforeWidget != "" {
|
||||
n := strings.ReplaceAll(name, "-", "_")
|
||||
if name == "recent-posts" {
|
||||
n = "recent_entries"
|
||||
}
|
||||
wp.SetComponentsArgsForMap(h, name, "{$before_widget}", fmt.Sprintf(beforeWidget, vv, n))
|
||||
}
|
||||
if len(args) > 0 {
|
||||
for k, val := range args {
|
||||
wp.SetComponentsArgsForMap(h, name, k, val)
|
||||
}
|
||||
}
|
||||
return fn, true
|
||||
}
|
||||
return nil, false
|
||||
})
|
||||
h.PushHandleFn(constraints.Ok, wp.NewHandleFn(func(h *wp.Handle) {
|
||||
h.PushGroupComponentFns(constraints.SidebarsWidgets, 10, sidebar...)
|
||||
}, 30))
|
||||
h.SetData("categories", cache.CategoriesTags(h.C, constraints.Category))
|
||||
}
|
||||
|
@ -47,11 +47,7 @@ func NewIndexHandle(handle *Handle) *IndexHandle {
|
||||
func (i *IndexHandle) ParseIndex(parm *IndexParams) (err error) {
|
||||
i.Param = parm
|
||||
switch i.scene {
|
||||
case constraints.Home, constraints.Search:
|
||||
s := i.C.Query("s")
|
||||
if s != "" && strings.Replace(s, " ", "", -1) != "" {
|
||||
i.scene = constraints.Search
|
||||
}
|
||||
case constraints.Search:
|
||||
i.Param.ParseSearch()
|
||||
case constraints.Category:
|
||||
err = i.Param.ParseCategory()
|
||||
@ -158,10 +154,11 @@ func (i *IndexHandle) Render() {
|
||||
}
|
||||
|
||||
func Indexs(h *Handle) {
|
||||
if h.Scene() != constraints.Detail {
|
||||
if h.Scene() == constraints.Detail {
|
||||
return
|
||||
}
|
||||
i := h.Index
|
||||
_ = i.BuildIndexData(NewIndexParams(i.C))
|
||||
PreCodeAndStats(h)
|
||||
PreTemplate(h)
|
||||
}
|
||||
}
|
||||
|
@ -298,10 +298,8 @@ func (h *Handle) Render() {
|
||||
func (h *Handle) CommonComponents() {
|
||||
h.AddCacheComponent("customLogo", CalCustomLogo)
|
||||
h.PushCacheGroupHeadScript("siteIconAndCustomCss", 0, CalSiteIcon, CalCustomCss)
|
||||
h.PushGroupHandleFn(constraints.AllStats, 10, CalMultipleComponents, CalBodyClass)
|
||||
h.PushHandleFn(constraints.AllStats, NewHandleFn(func(h *Handle) {
|
||||
h.CalMultipleComponents()
|
||||
h.CalBodyClass()
|
||||
}, 10), NewHandleFn(func(h *Handle) {
|
||||
h.C.HTML(h.Code, h.templ, h.ginH)
|
||||
}, 0))
|
||||
}
|
||||
@ -331,7 +329,7 @@ func (h *Handle) PushGroupComponentFns(name string, order int, fns ...func(*Hand
|
||||
h.components[name] = append(h.components[name], calls...)
|
||||
}
|
||||
|
||||
func (h *Handle) CalMultipleComponents() {
|
||||
func CalMultipleComponents(h *Handle) {
|
||||
for k, ss := range h.components {
|
||||
slice.Sort(ss, func(i, j Components) bool {
|
||||
return i.Order > j.Order
|
||||
|
Loading…
Reference in New Issue
Block a user