body class

This commit is contained in:
xing 2023-04-16 00:57:50 +08:00
parent b53819f733
commit 98cee2f18b
11 changed files with 101 additions and 16 deletions

View File

@ -7,6 +7,7 @@ import (
"golang.org/x/exp/constraints" "golang.org/x/exp/constraints"
"math" "math"
"math/rand" "math/rand"
"strconv"
) )
func Range[T constraints.Integer](start, end, step T) []T { func Range[T constraints.Integer](start, end, step T) []T {
@ -73,6 +74,10 @@ func ToString[T constraints.Integer | constraints.Float](n T) string {
return fmt.Sprintf("%v", n) return fmt.Sprintf("%v", n)
} }
func IntToString[T constraints.Integer](i T) string {
return strconv.FormatInt(int64(i), 10)
}
func Abs[T constraints.Integer | constraints.Float](n T) T { func Abs[T constraints.Integer | constraints.Float](n T) T {
if n >= 0 { if n >= 0 {
return n return n

View File

@ -57,10 +57,10 @@ func CategoriesAndTags(a ...any) (terms []models.TermsMy, err error) {
model.In(in), model.In(in),
)) ))
for i := 0; i < len(terms); i++ { for i := 0; i < len(terms); i++ {
if v, ok := wpconfig.Terms.Load(terms[i].Terms.TermId); ok { if v, ok := wpconfig.GetTerm(terms[i].Terms.TermId); ok {
terms[i].Terms = v terms[i].Terms = v
} }
if v, ok := wpconfig.TermTaxonomies.Load(terms[i].Terms.TermId); ok { if v, ok := wpconfig.GetTermTaxonomy(terms[i].Terms.TermId); ok {
terms[i].TermTaxonomy = v terms[i].TermTaxonomy = v
} }
} }

View File

@ -24,7 +24,7 @@ func GetPostsByIds(a ...any) (m map[uint64]models.Posts, err error) {
{"left join", "wp_term_taxonomy c", "b.term_taxonomy_id=c.term_taxonomy_id"}, {"left join", "wp_term_taxonomy c", "b.term_taxonomy_id=c.term_taxonomy_id"},
{"left join", "wp_terms d", "c.term_id=d.term_id"}, {"left join", "wp_terms d", "c.term_id=d.term_id"},
}), }),
model.Fields("a.*,ifnull(d.name,'') category_name,ifnull(taxonomy,'') `taxonomy`"), model.Fields("a.*,ifnull(d.name,'') category_name,ifnull(c.term_id,0) terms_id,ifnull(taxonomy,'') `taxonomy`"),
model.In(slice.ToAnySlice(ids)), model.In(slice.ToAnySlice(ids)),
)) ))
@ -42,6 +42,9 @@ func GetPostsByIds(a ...any) (m map[uint64]models.Posts, err error) {
} else if post.Taxonomy == "post_tag" { } else if post.Taxonomy == "post_tag" {
v.Tags = append(v.Tags, post.CategoryName) v.Tags = append(v.Tags, post.CategoryName)
} }
if post.TermsId > 0 {
v.TermIds = append(v.TermIds, post.TermsId)
}
postsMap[post.Id] = v postsMap[post.Id] = v
} }
//host, _ := wpconfig.Options.Load("siteurl") //host, _ := wpconfig.Options.Load("siteurl")

View File

@ -29,6 +29,8 @@ type Posts struct {
CommentCount int64 `gorm:"column:comment_count" db:"comment_count" json:"comment_count" form:"comment_count"` CommentCount int64 `gorm:"column:comment_count" db:"comment_count" json:"comment_count" form:"comment_count"`
//扩展字段 //扩展字段
TermsId uint64 `db:"terms_id" json:"terms_id"`
TermIds []uint64 `db:"term_ids" json:"term_ids"`
Taxonomy string `db:"taxonomy" json:"taxonomy"` Taxonomy string `db:"taxonomy" json:"taxonomy"`
CategoryName string `db:"category_name" json:"category_name"` CategoryName string `db:"category_name" json:"category_name"`
Categories []string `json:"categories"` Categories []string `json:"categories"`

View File

@ -1,6 +1,7 @@
package theme package theme
import ( import (
"github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/internal/wpconfig" "github.com/fthvgb1/wp-go/internal/wpconfig"
"html/template" "html/template"
"time" "time"
@ -20,6 +21,14 @@ var comFn = template.FuncMap{
return wpconfig.GetOption(k) return wpconfig.GetOption(k)
}, },
"getLang": wpconfig.GetLang, "getLang": wpconfig.GetLang,
"postsFn": postsFn,
"exec": func(fn func() string) string {
return fn()
},
}
func postsFn(fn func(models.Posts) string, a models.Posts) string {
return fn(a)
} }
func FuncMap() template.FuncMap { func FuncMap() template.FuncMap {

View File

@ -3,7 +3,7 @@
<html lang="{{getLang}}" class="no-js"> <html lang="{{getLang}}" class="no-js">
{{template "layout/head" .}} {{template "layout/head" .}}
<body class="{{.bodyClass}}"> <body class="{{.calBodyClass|exec}}">
{{template "svg"}} {{template "svg"}}
<div id="page" class="hfeed site"> <div id="page" class="hfeed site">
<a class="skip-link screen-reader-text" href="#content"> <a class="skip-link screen-reader-text" href="#content">

View File

@ -4,8 +4,7 @@
{{ if and (.post) (gt .post.Id 0)}} {{ if and (.post) (gt .post.Id 0)}}
<div id="primary" class="content-area"> <div id="primary" class="content-area">
<main id="main" class="site-main"> <main id="main" class="site-main">
<article id="post-{{.post.Id}}" <article class="{{ .post|postsFn .calPostClass}}">
class="post-{{.post.Id}} post type-post status-publish format-standard hentry category-uncategorized">
<header class="entry-header"> <header class="entry-header">
<h1 class="entry-title">{{.post.PostTitle}}</h1></header><!-- .entry-header --> <h1 class="entry-title">{{.post.PostTitle}}</h1></header><!-- .entry-header -->

View File

@ -12,8 +12,7 @@
</header> </header>
{{end}} {{end}}
{{ range $k,$v:=.posts}} {{ range $k,$v:=.posts}}
<article id="post-{{$v.Id}}" <article class="{{ $v|postsFn $.calPostClass}}">
class="post-{{$v.Id}} post {{if $v.IsSticky}}sticky{{end}} {{if $v.Thumbnail.Path}}has-post-thumbnail{{end}} type-post status-publish format-standard hentry category">
{{if $v.Thumbnail.Path}} {{if $v.Thumbnail.Path}}
<a class="post-thumbnail" href="/p/{{$v.Id}}" aria-hidden="true"> <a class="post-thumbnail" href="/p/{{$v.Id}}" aria-hidden="true">
<img width="{{$v.Thumbnail.Width}}" height="{{$v.Thumbnail.Height}}" src="{{$v.Thumbnail.Path}}" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="{{$v.PostTitle}}" decoding="async"> <img width="{{$v.Thumbnail.Width}}" height="{{$v.Thumbnail.Height}}" src="{{$v.Thumbnail.Path}}" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="{{$v.PostTitle}}" decoding="async">

View File

@ -1,6 +1,7 @@
package wp package wp
import ( import (
"fmt"
"github.com/fthvgb1/wp-go/helper/number" "github.com/fthvgb1/wp-go/helper/number"
"github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/helper/slice"
str "github.com/fthvgb1/wp-go/helper/strings" str "github.com/fthvgb1/wp-go/helper/strings"
@ -75,3 +76,44 @@ func (h *Handle) BodyClass() string {
} }
return h.ComponentFilterFnHook("bodyClass", strings.Join(class, " ")) return h.ComponentFilterFnHook("bodyClass", strings.Join(class, " "))
} }
func (h *Handle) PostClass(posts models.Posts) string {
var class []string
class = append(class, fmt.Sprintf("post-%d", posts.Id), posts.PostType,
str.Join("type-", posts.PostType), str.Join("status-", posts.PostStatus),
"hentry", "format-standard")
if h.CommonThemeMods().ThemeSupport.PostThumbnails && posts.Thumbnail.Path != "" {
class = append(class, "has-post-thumbnail")
}
if posts.PostPassword != "" {
if h.password != posts.PostPassword {
class = append(class, "post-password-required")
} else {
class = append(class, "post-password-projected")
}
}
if h.scene == constraints.Home && h.IsStick(posts.Id) {
class = append(class, "sticky")
}
for _, id := range posts.TermIds {
term, ok := wpconfig.GetTermMy(id)
if !ok || term.Slug == "" {
continue
}
termClass := term.Slug
if termClass[0] == '%' {
termClass = number.ToString(term.Terms.TermId)
}
switch term.Taxonomy {
case "category":
class = append(class, str.Join("category-", termClass))
case "post_tag":
class = append(class, str.Join("tag-", termClass))
case "post_format":
class = append(class, fmt.Sprintf("format-%s", strings.ReplaceAll(term.Slug, "post-format-", "")))
}
}
return h.ComponentFilterFnHook("postClass", strings.Join(class, " "))
}

View File

@ -8,6 +8,7 @@ import (
"github.com/fthvgb1/wp-go/internal/cmd/reload" "github.com/fthvgb1/wp-go/internal/cmd/reload"
"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"
"github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/internal/wpconfig" "github.com/fthvgb1/wp-go/internal/wpconfig"
"github.com/gin-contrib/sessions" "github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -84,6 +85,12 @@ func InitThemeArgAndConfig(fn func(*Handle), h *Handle) {
} }
h.components = m h.components = m
h.ginH = maps.Copy(hh.ginH) h.ginH = maps.Copy(hh.ginH)
h.ginH["calPostClass"] = func(posts models.Posts) string {
return h.PostClass(posts)
}
h.ginH["calBodyClass"] = func() string {
return h.BodyClass()
}
if inited { if inited {
return return
} }

View File

@ -1,30 +1,49 @@
package wpconfig package wpconfig
import ( import (
"context"
"github.com/fthvgb1/wp-go/internal/pkg/models" "github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/model" "github.com/fthvgb1/wp-go/model"
"github.com/fthvgb1/wp-go/safety" "github.com/fthvgb1/wp-go/safety"
) )
var Terms safety.Map[uint64, models.Terms] var terms = safety.NewMap[uint64, models.Terms]()
var TermTaxonomies safety.Map[uint64, models.TermTaxonomy] var termTaxonomies = safety.NewMap[uint64, models.TermTaxonomy]()
var my = safety.NewMap[uint64, models.TermsMy]()
func GetTerm(termId uint64) (models.Terms, bool) {
return terms.Load(termId)
}
func GetTermTaxonomy(termId uint64) (models.TermTaxonomy, bool) {
return termTaxonomies.Load(termId)
}
func GetTermMy(termId uint64) (models.TermsMy, bool) {
return my.Load(termId)
}
func InitTerms() (err error) { func InitTerms() (err error) {
ctx := context.Background() terms.Flush()
terms, err := model.SimpleFind[models.Terms](ctx, nil, "*") termTaxonomies.Flush()
term, err := model.SimpleFind[models.Terms](ctx, nil, "*")
if err != nil { if err != nil {
return err return err
} }
for _, wpTerms := range terms { for _, wpTerms := range term {
Terms.Store(wpTerms.TermId, wpTerms) terms.Store(wpTerms.TermId, wpTerms)
} }
termTax, err := model.SimpleFind[models.TermTaxonomy](ctx, nil, "*") termTax, err := model.SimpleFind[models.TermTaxonomy](ctx, nil, "*")
if err != nil { if err != nil {
return err return err
} }
for _, taxonomy := range termTax { for _, taxonomy := range termTax {
TermTaxonomies.Store(taxonomy.TermTaxonomyId, taxonomy) termTaxonomies.Store(taxonomy.TermTaxonomyId, taxonomy)
if term, ok := terms.Load(taxonomy.TermId); ok {
my.Store(taxonomy.TermId, models.TermsMy{
Terms: term,
TermTaxonomy: taxonomy,
})
}
} }
return return
} }