优化代码

This commit is contained in:
xing 2023-02-23 18:21:51 +08:00
parent 6d1fde655d
commit 2a87ebdfd6
6 changed files with 34 additions and 80 deletions

View File

@ -69,13 +69,14 @@ func (h *Handle) Render() {
h.Templ = str.Join(h.Theme, "/posts/detail.gohtml") h.Templ = str.Join(h.Theme, "/posts/detail.gohtml")
} }
} }
for _, fn := range h.HandleFns {
fn(h)
}
h.AutoCal("siteIcon", h.CalSiteIcon) h.AutoCal("siteIcon", h.CalSiteIcon)
h.AutoCal("customLogo", h.CalCustomLogo) h.AutoCal("customLogo", h.CalCustomLogo)
h.AutoCal("customCss", h.CalCustomCss) h.AutoCal("customCss", h.CalCustomCss)
h.CalBodyClass() h.CalBodyClass()
for _, fn := range h.HandleFns {
fn(h)
}
h.C.HTML(h.Code, h.Templ, h.GinH) h.C.HTML(h.Code, h.Templ, h.GinH)
} }

View File

@ -17,20 +17,25 @@ type DetailHandle struct {
CommentRender plugins.CommentHtml CommentRender plugins.CommentHtml
Comments []models.Comments Comments []models.Comments
Post models.Posts Post models.Posts
Pipes []HandlePipeFn[*DetailHandle]
} }
func NewDetailHandle(handle *Handle) *DetailHandle { func NewDetailHandle(handle *Handle) *DetailHandle {
return &DetailHandle{Handle: handle} return &DetailHandle{Handle: handle}
} }
func (d *DetailHandle) Pipe(calls ...HandlePipeFn[*DetailHandle]) {
HandlePipe[*DetailHandle](append(calls, d.Pipes...), func(d *DetailHandle) {
d.Render()
})(d)
}
func (d *DetailHandle) BuildDetailData() (err error) { func (d *DetailHandle) BuildDetailData() (err error) {
d.GinH["title"] = wpconfig.GetOption("blogname") d.GinH["title"] = wpconfig.GetOption("blogname")
err = d.CheckAndGetPost() err = d.CheckAndGetPost()
if err != nil { if err != nil {
return return
} }
d.WidgetAreaData()
d.GetPassword()
d.Comment() d.Comment()
d.ContextPost() d.ContextPost()
return return
@ -73,6 +78,9 @@ func (d *DetailHandle) Comment() {
} }
func (d *DetailHandle) RenderComment() { func (d *DetailHandle) RenderComment() {
if d.CommentRender == nil {
d.CommentRender = plugins.CommentRender()
}
ableComment := true ableComment := true
if d.Post.CommentStatus != "open" || if d.Post.CommentStatus != "open" ||
(d.Post.PostPassword != "" && d.Password != d.Post.PostPassword) { (d.Post.PostPassword != "" && d.Password != d.Post.PostPassword) {
@ -94,19 +102,8 @@ func (d *DetailHandle) ContextPost() {
func (d *DetailHandle) Render() { func (d *DetailHandle) Render() {
d.PasswordProject() d.PasswordProject()
if d.CommentRender == nil {
d.CommentRender = plugins.CommentRender()
}
d.CalBodyClass()
d.AutoCal("siteIcon", d.CalSiteIcon)
d.AutoCal("customLogo", d.CalCustomLogo)
d.AutoCal("customCss", d.CalCustomCss)
d.RenderComment() d.RenderComment()
d.CalBodyClass() d.Handle.Render()
if d.Templ == "" {
d.Templ = fmt.Sprintf("%s/posts/detail.gohtml", d.Theme)
}
d.C.HTML(d.Code, d.Templ, d.GinH)
} }
func (d *DetailHandle) Details() { func (d *DetailHandle) Details() {
@ -117,6 +114,5 @@ func (d *DetailHandle) Details() {
d.C.HTML(d.Code, d.Templ, d.GinH) d.C.HTML(d.Code, d.Templ, d.GinH)
return return
} }
d.ExecHandlePlugin()
d.Render() d.Render()
} }

View File

@ -6,7 +6,7 @@
{{block "head" .}} {{block "head" .}}
{{end}} {{end}}
</head> </head>
<body class="{{.bodyClass}} wp-embed-responsive hfeed has-header-image has-sidebar colors-light"> <body class="{{.bodyClass}} wp-embed-responsive hfeed has-sidebar colors-light">
{{template "svg"}} {{template "svg"}}
<div id="page" class="site"> <div id="page" class="site">
<a class="skip-link screen-reader-text" href="#content">跳至内容</a> <a class="skip-link screen-reader-text" href="#content">跳至内容</a>

View File

@ -1,7 +1,7 @@
{{template "layout/base" .}} {{template "layout/base" .}}
{{define "content"}} {{define "content"}}
{{ if .post.PostContent}} {{ if gt .post.Id 0}}
{{if .post.Thumbnail.Path}} {{if .post.Thumbnail.Path}}
<div class="single-featured-image-header"> <div class="single-featured-image-header">
<img width="{{.post.Thumbnail.OriginAttachmentData.Width}}" height="{{.post.Thumbnail.OriginAttachmentData.Height}}" src="{{.post.Thumbnail.Path}}" class="attachment-twentyseventeen-featured-image size-twentyseventeen-featured-image wp-post-image" alt="{{.post.PostTitle}}" decoding="async" loading="lazy" srcset="{{.post.Thumbnail.Srcset}}" sizes="{{.post.Thumbnail.Sizes}}"> <img width="{{.post.Thumbnail.OriginAttachmentData.Width}}" height="{{.post.Thumbnail.OriginAttachmentData.Height}}" src="{{.post.Thumbnail.Path}}" class="attachment-twentyseventeen-featured-image size-twentyseventeen-featured-image wp-post-image" alt="{{.post.PostTitle}}" decoding="async" loading="lazy" srcset="{{.post.Thumbnail.Srcset}}" sizes="{{.post.Thumbnail.Sizes}}">

View File

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"github.com/fthvgb1/wp-go/helper/maps" "github.com/fthvgb1/wp-go/helper/maps"
"github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/helper/slice"
str "github.com/fthvgb1/wp-go/helper/strings"
"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/logs" "github.com/fthvgb1/wp-go/internal/pkg/logs"
@ -29,29 +28,13 @@ var paginate = func() plugins.PageEle {
return p return p
}() }()
type handle struct {
*common.Handle
}
func newHandle(h *common.Handle) *handle {
return &handle{Handle: h}
}
type detailHandle struct {
*common.DetailHandle
h *handle
}
func newDetailHandle(dHandle *common.DetailHandle) *detailHandle {
return &detailHandle{DetailHandle: dHandle, h: newHandle(dHandle.Handle)}
}
func Hook(h *common.Handle) { func Hook(h *common.Handle) {
h.WidgetAreaData() h.WidgetAreaData()
h.GetPassword() h.GetPassword()
h.HandleFns = append(h.HandleFns, calClass)
h.GinH["HeaderImage"] = getHeaderImage(h.C) h.GinH["HeaderImage"] = getHeaderImage(h.C)
if h.Scene == constraints.Detail { if h.Scene == constraints.Detail {
newDetailHandle(common.NewDetailHandle(h)).Detail() common.NewDetailHandle(h).Pipe(detail)
return return
} }
common.NewIndexHandle(h).Pipe(index) common.NewIndexHandle(h).Pipe(index)
@ -77,16 +60,15 @@ func index(next common.HandleFn[*common.IndexHandle], i *common.IndexHandle) {
next(i) next(i)
} }
func (d *detailHandle) Detail() { func detail(next common.HandleFn[*common.DetailHandle], d *common.DetailHandle) {
err := d.BuildDetailData() err := d.BuildDetailData()
if err != nil { if err != nil {
d.Code = http.StatusNotFound d.Code = http.StatusNotFound
d.Stats = constraints.Error404 d.Stats = constraints.Error404
d.GinH["bodyClass"] = d.h.bodyClass() d.GinH["bodyClass"] = d.BodyClass()
d.C.HTML(d.Code, d.Templ, d.GinH) d.C.HTML(d.Code, d.Templ, d.GinH)
return return
} }
d.GinH["bodyClass"] = d.h.bodyClass()
img := wpconfig.Thumbnail(d.Post.Thumbnail.OriginAttachmentData, "thumbnail", "", "thumbnail", "post-thumbnail") img := wpconfig.Thumbnail(d.Post.Thumbnail.OriginAttachmentData, "thumbnail", "", "thumbnail", "post-thumbnail")
img.Width = img.OriginAttachmentData.Width img.Width = img.OriginAttachmentData.Width
img.Height = img.OriginAttachmentData.Height img.Height = img.OriginAttachmentData.Height
@ -95,8 +77,8 @@ func (d *detailHandle) Detail() {
d.Post.Thumbnail = img d.Post.Thumbnail = img
d.CommentRender = commentFormat d.CommentRender = commentFormat
d.GinH["post"] = d.Post d.GinH["post"] = d.Post
d.Render()
next(d)
} }
var commentFormat = comment{} var commentFormat = comment{}
@ -138,46 +120,21 @@ func getHeaderImage(c *gin.Context) (r models.PostThumbnail) {
logs.ErrPrintln(err, "获取页眉背景图失败") logs.ErrPrintln(err, "获取页眉背景图失败")
} else if len(hs) > 0 && err == nil { } else if len(hs) > 0 && err == nil {
_, r = slice.Rand(hs) _, r = slice.Rand(hs)
} }
r.Sizes = "100vw" r.Sizes = "100vw"
return return
} }
func (i *handle) bodyClass() string { func calClass(h *common.Handle) {
s := "" u := wpconfig.GetThemeModsVal(h.Theme, "header_image", h.ThemeMods.ThemeSupport.CustomHeader.DefaultImage)
if constraints.Ok != i.Stats { if u != "" && u != "remove-header" {
return "error404" h.Class = append(h.Class, "has-header-image")
} }
switch i.Scene { if h.Scene == constraints.Archive {
case constraints.Search: if "one-column" == wpconfig.GetThemeModsVal(h.Theme, "page_layout", "") {
s = "search-no-results" h.Class = append(h.Class, "page-one-column")
if len(i.GinH["posts"].([]models.Posts)) > 0 { } else {
s = "search-results" h.Class = append(h.Class, "page-two-column")
} }
case constraints.Category, constraints.Tag:
cat := i.C.Param("category")
if cat == "" {
cat = i.C.Param("tag")
} }
_, cate := slice.SearchFirst(cache.CategoriesTags(i.C, i.Scene), func(my models.TermsMy) bool {
return my.Name == cat
})
if cate.Slug[0] != '%' {
s = cate.Slug
}
s = fmt.Sprintf("category-%d %v", cate.Terms.TermId, s)
case constraints.Detail:
s = fmt.Sprintf("postid-%d", i.GinH["post"].(models.Posts).Id)
}
return str.Join(class[i.Scene], s)
}
var class = map[int]string{
constraints.Home: "home blog ",
constraints.Archive: "archive date page-two-column",
constraints.Category: "archive category page-two-column",
constraints.Tag: "archive category page-two-column ",
constraints.Search: "search ",
constraints.Detail: "post-template-default single single-post single-format-standard ",
} }