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