组合优于继承。。。。。。。

This commit is contained in:
xing 2023-02-23 23:42:51 +08:00
parent bd7d20160a
commit 865b37cf89
10 changed files with 99 additions and 84 deletions

View File

@ -42,10 +42,14 @@ func NewHandle(c *gin.Context, scene int, theme string) *Handle {
} }
} }
func (h *Handle) AutoCal(name string, fn func() string) { func (h *Handle) PushHandleFn(fns ...func(*Handle)) {
h.HandleFns = append(h.HandleFns, fns...)
}
func (h *Handle) AutoCal(name string, fn func(*Handle) string) {
v, ok := reload.GetStr(name) v, ok := reload.GetStr(name)
if !ok { if !ok {
v = fn() v = fn(h)
reload.SetStr(name, v) reload.SetStr(name, v)
} }
h.GinH[name] = v h.GinH[name] = v
@ -72,9 +76,9 @@ func (h *Handle) Render() {
for _, fn := range h.HandleFns { for _, fn := range h.HandleFns {
fn(h) fn(h)
} }
h.AutoCal("siteIcon", h.CalSiteIcon) h.AutoCal("siteIcon", CalSiteIcon)
h.AutoCal("customLogo", h.CalCustomLogo) h.AutoCal("customLogo", CalCustomLogo)
h.AutoCal("customCss", h.CalCustomCss) h.AutoCal("customCss", CalCustomCss)
h.CalBodyClass() h.CalBodyClass()
h.C.HTML(h.Code, h.Templ, h.GinH) h.C.HTML(h.Code, h.Templ, h.GinH)

View File

@ -6,7 +6,7 @@ import (
"github.com/fthvgb1/wp-go/internal/pkg/cache" "github.com/fthvgb1/wp-go/internal/pkg/cache"
) )
func (h *Handle) CalCustomCss() (r string) { func CalCustomCss(h *Handle) (r string) {
if h.ThemeMods.CustomCssPostId < 1 { if h.ThemeMods.CustomCssPostId < 1 {
return return
} }

View File

@ -8,7 +8,7 @@ import (
"github.com/fthvgb1/wp-go/internal/wpconfig" "github.com/fthvgb1/wp-go/internal/wpconfig"
) )
func (h *Handle) CalCustomLogo() (r string) { func CalCustomLogo(h *Handle) (r string) {
id := uint64(h.ThemeMods.CustomLogo) id := uint64(h.ThemeMods.CustomLogo)
if id < 1 { if id < 1 {
id = str.ToInteger[uint64](wpconfig.GetOption("site_logo"), 0) id = str.ToInteger[uint64](wpconfig.GetOption("site_logo"), 0)

View File

@ -11,7 +11,7 @@ import (
var sizes = []string{"site_icon-270", "site_icon-32", "site_icon-192", "site_icon-180"} var sizes = []string{"site_icon-270", "site_icon-32", "site_icon-192", "site_icon-180"}
func (h *Handle) CalSiteIcon() (r string) { func CalSiteIcon(h *Handle) (r string) {
id := str.ToInteger[uint64](wpconfig.GetOption("site_icon"), 0) id := str.ToInteger[uint64](wpconfig.GetOption("site_icon"), 0)
if id < 1 { if id < 1 {
return return

View File

@ -3,12 +3,13 @@ package twentyfifteen
import ( import (
"fmt" "fmt"
"github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/helper/slice"
"github.com/fthvgb1/wp-go/internal/theme/common"
"strconv" "strconv"
"strings" "strings"
) )
func (h *handle) colorSchemeCss() string { func colorSchemeCss(h *common.Handle) string {
s := slice.Filter([]string{h.calColorSchemeCss(), h.calSidebarTextColorCss(), h.calHeaderBackgroundColorCss()}, func(s string) bool { s := slice.Filter([]string{calColorSchemeCss(h), calSidebarTextColorCss(h), calHeaderBackgroundColorCss(h)}, func(s string) bool {
return s != "" return s != ""
}) })
if len(s) < 1 { if len(s) < 1 {
@ -16,9 +17,9 @@ func (h *handle) colorSchemeCss() string {
} }
return fmt.Sprintf(`<style id='%s-inline-css'%s>\n%s\n</style>`, "twentyfifteen-style", "", strings.Join(s, "\n")) return fmt.Sprintf(`<style id='%s-inline-css'%s>\n%s\n</style>`, "twentyfifteen-style", "", strings.Join(s, "\n"))
} }
func (h *handle) calColorSchemeCss() (r string) { func calColorSchemeCss(h *common.Handle) (r string) {
color := h.getColorScheme() color := getColorScheme(h)
if "default" == h.IndexHandle.ThemeMods.ColorScheme || len(color) < 1 { if "default" == h.ThemeMods.ColorScheme || len(color) < 1 {
return return
} }
textColorRgb := slice.ToAnySlice(Hex2RgbUint8(color[3])) textColorRgb := slice.ToAnySlice(Hex2RgbUint8(color[3]))
@ -45,31 +46,31 @@ func (h *handle) calColorSchemeCss() (r string) {
return return
} }
func (h *handle) calSidebarTextColorCss() (r string) { func calSidebarTextColorCss(h *common.Handle) (r string) {
colors := h.getColorScheme() colors := getColorScheme(h)
if h.IndexHandle.ThemeMods.SidebarTextcolor == "" || h.IndexHandle.ThemeMods.SidebarTextcolor == colors[4] { if h.ThemeMods.SidebarTextcolor == "" || h.ThemeMods.SidebarTextcolor == colors[4] {
return return
} }
linkColorRgb := Hex2RgbUint8(h.IndexHandle.ThemeMods.SidebarTextcolor) linkColorRgb := Hex2RgbUint8(h.ThemeMods.SidebarTextcolor)
color := slice.ToAnySlice(linkColorRgb) color := slice.ToAnySlice(linkColorRgb)
textColor := fmt.Sprintf(`rgba( %[1]v, %[2]v, %[3]v, 0.7)`, color...) textColor := fmt.Sprintf(`rgba( %[1]v, %[2]v, %[3]v, 0.7)`, color...)
borderColor := fmt.Sprintf(`rgba( %[1]v, %[2]v, %[3]v, 0.1)`, color...) borderColor := fmt.Sprintf(`rgba( %[1]v, %[2]v, %[3]v, 0.1)`, color...)
borderFocusColor := fmt.Sprintf(`rgba( %[1]v, %[2]v, %[3]v, 0.3)`, color...) borderFocusColor := fmt.Sprintf(`rgba( %[1]v, %[2]v, %[3]v, 0.3)`, color...)
r = fmt.Sprintf(sidebarTextColorTemplate, h.IndexHandle.ThemeMods.SidebarTextcolor, textColor, borderColor, borderFocusColor) r = fmt.Sprintf(sidebarTextColorTemplate, h.ThemeMods.SidebarTextcolor, textColor, borderColor, borderFocusColor)
return return
} }
func (h *handle) calHeaderBackgroundColorCss() (r string) { func calHeaderBackgroundColorCss(h *common.Handle) (r string) {
colors := h.getColorScheme() colors := getColorScheme(h)
if h.IndexHandle.ThemeMods.HeaderBackgroundColor == "" || h.IndexHandle.ThemeMods.HeaderBackgroundColor == colors[1] { if h.ThemeMods.HeaderBackgroundColor == "" || h.ThemeMods.HeaderBackgroundColor == colors[1] {
return return
} }
r = fmt.Sprintf(headerBackgroundColorCssTemplate, h.IndexHandle.ThemeMods.HeaderBackgroundColor, h.IndexHandle.ThemeMods.HeaderBackgroundColor) r = fmt.Sprintf(headerBackgroundColorCssTemplate, h.ThemeMods.HeaderBackgroundColor, h.ThemeMods.HeaderBackgroundColor)
return return
} }
func (h *handle) getColorScheme() (r []string) { func getColorScheme(h *common.Handle) (r []string) {
x, ok := colorscheme[h.IndexHandle.ThemeMods.ColorScheme] x, ok := colorscheme[h.ThemeMods.ColorScheme]
if ok { if ok {
r = x.Colors r = x.Colors
} }

View File

@ -5,6 +5,7 @@ import (
"github.com/fthvgb1/wp-go/helper" "github.com/fthvgb1/wp-go/helper"
"github.com/fthvgb1/wp-go/helper/maps" "github.com/fthvgb1/wp-go/helper/maps"
str "github.com/fthvgb1/wp-go/helper/strings" str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/internal/theme/common"
) )
var postx = map[string]string{ var postx = map[string]string{
@ -29,30 +30,30 @@ var repeat = map[string]string{
"no-repeat": "no-repeat", "no-repeat": "no-repeat",
} }
func (h *handle) CalCustomBackGround() (r string) { func CalCustomBackGround(h *common.Handle) (r string) {
if h.IndexHandle.ThemeMods.BackgroundImage == "" && h.IndexHandle.ThemeMods.BackgroundColor == themesupport.CustomBackground.DefaultColor { if h.ThemeMods.BackgroundImage == "" && h.ThemeMods.BackgroundColor == themesupport.CustomBackground.DefaultColor {
return return
} }
s := str.NewBuilder() s := str.NewBuilder()
if h.IndexHandle.ThemeMods.BackgroundImage != "" { if h.ThemeMods.BackgroundImage != "" {
s.Sprintf(` background-image: url("%s");`, helper.CutUrlHost(h.IndexHandle.ThemeMods.BackgroundImage)) s.Sprintf(` background-image: url("%s");`, helper.CutUrlHost(h.ThemeMods.BackgroundImage))
} }
backgroundPositionX := helper.Defaults(h.IndexHandle.ThemeMods.BackgroundPositionX, themesupport.CustomBackground.DefaultPositionX) backgroundPositionX := helper.Defaults(h.ThemeMods.BackgroundPositionX, themesupport.CustomBackground.DefaultPositionX)
backgroundPositionX = maps.WithDefaultVal(postx, backgroundPositionX, "left") backgroundPositionX = maps.WithDefaultVal(postx, backgroundPositionX, "left")
backgroundPositionY := helper.Defaults(h.IndexHandle.ThemeMods.BackgroundPositionY, themesupport.CustomBackground.DefaultPositionY) backgroundPositionY := helper.Defaults(h.ThemeMods.BackgroundPositionY, themesupport.CustomBackground.DefaultPositionY)
backgroundPositionY = maps.WithDefaultVal(posty, backgroundPositionY, "top") backgroundPositionY = maps.WithDefaultVal(posty, backgroundPositionY, "top")
positon := fmt.Sprintf(" background-position: %s %s;", backgroundPositionX, backgroundPositionY) positon := fmt.Sprintf(" background-position: %s %s;", backgroundPositionX, backgroundPositionY)
siz := helper.DefaultVal(h.IndexHandle.ThemeMods.BackgroundSize, themesupport.CustomBackground.DefaultSize) siz := helper.DefaultVal(h.ThemeMods.BackgroundSize, themesupport.CustomBackground.DefaultSize)
siz = maps.WithDefaultVal(size, siz, "auto") siz = maps.WithDefaultVal(size, siz, "auto")
siz = fmt.Sprintf(" background-size: %s;", siz) siz = fmt.Sprintf(" background-size: %s;", siz)
repeats := helper.Defaults(h.IndexHandle.ThemeMods.BackgroundRepeat, themesupport.CustomBackground.DefaultRepeat) repeats := helper.Defaults(h.ThemeMods.BackgroundRepeat, themesupport.CustomBackground.DefaultRepeat)
repeats = maps.WithDefaultVal(repeat, repeats, "repeat") repeats = maps.WithDefaultVal(repeat, repeats, "repeat")
repeats = fmt.Sprintf(" background-repeat: %s;", repeats) repeats = fmt.Sprintf(" background-repeat: %s;", repeats)
attachment := helper.Defaults(h.IndexHandle.ThemeMods.BackgroundAttachment, themesupport.CustomBackground.DefaultAttachment) attachment := helper.Defaults(h.ThemeMods.BackgroundAttachment, themesupport.CustomBackground.DefaultAttachment)
attachment = helper.Or(attachment == "fixed", "fixed", "scroll") attachment = helper.Or(attachment == "fixed", "fixed", "scroll")
attachment = fmt.Sprintf(" background-attachment: %s;", attachment) attachment = fmt.Sprintf(" background-attachment: %s;", attachment)
s.WriteString(positon, siz, repeats, attachment) s.WriteString(positon, siz, repeats, attachment)

View File

@ -4,6 +4,7 @@ import (
str "github.com/fthvgb1/wp-go/helper/strings" str "github.com/fthvgb1/wp-go/helper/strings"
"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/theme/common"
) )
var style = `<style type="text/css" id="twentyfifteen-header-css">` var style = `<style type="text/css" id="twentyfifteen-header-css">`
@ -82,20 +83,20 @@ var imgStyle = `.site-header {
var header = reload.Vars(constraints.Defaults) var header = reload.Vars(constraints.Defaults)
func (h *handle) CalCustomHeader() (r string, rand bool) { func calCustomHeader(h *common.Handle) (r string, rand bool) {
img, rand := h.IndexHandle.GetCustomHeader() img, rand := h.GetCustomHeader()
if img.Path == "" && h.IndexHandle.DisplayHeaderText() { if img.Path == "" && h.DisplayHeaderText() {
return return
} }
ss := str.NewBuilder() ss := str.NewBuilder()
ss.WriteString(style) ss.WriteString(style)
if img.Path == "" && !h.IndexHandle.DisplayHeaderText() { if img.Path == "" && !h.DisplayHeaderText() {
ss.WriteString(defaultTextStyle) ss.WriteString(defaultTextStyle)
} }
if img.Path != "" { if img.Path != "" {
ss.Sprintf(imgStyle, img.Path, img.Path) ss.Sprintf(imgStyle, img.Path, img.Path)
} }
if !h.IndexHandle.DisplayHeaderText() { if !h.DisplayHeaderText() {
ss.WriteString(`.site-title, ss.WriteString(`.site-title,
.site-description { .site-description {
clip: rect(1px, 1px, 1px, 1px); clip: rect(1px, 1px, 1px, 1px);
@ -107,15 +108,15 @@ func (h *handle) CalCustomHeader() (r string, rand bool) {
return return
} }
func (h *handle) CustomHeader() { func customHeader(h *common.Handle) {
headers := header.Load() headers := header.Load()
if headers == constraints.Defaults { if headers == constraints.Defaults {
headerss, rand := h.CalCustomHeader() headerss, rand := calCustomHeader(h)
headers = headerss headers = headerss
if !rand { if !rand {
header.Store(headers) header.Store(headers)
} }
} }
h.IndexHandle.GinH["customHeader"] = headers h.GinH["customHeader"] = headers
return return
} }

View File

@ -1,7 +1,7 @@
{{template "layout/base" .}} {{template "layout/base" .}}
{{define "content"}} {{define "content"}}
{{ if .post.PostContent}} {{ if 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 id="post-{{.post.Id}}"

View File

@ -28,36 +28,31 @@ func Init(fs embed.FS) {
logs.ErrPrintln(err, "解析colorscheme失败") logs.ErrPrintln(err, "解析colorscheme失败")
} }
type handle struct { var detailPipe = common.HandlePipe(func(d *common.DetailHandle) {
*common.IndexHandle d.Render()
*common.DetailHandle }, detail)
} var indexPipe = common.HandlePipe(func(i *common.IndexHandle) {
i.Render()
func newHandle(iHandle *common.IndexHandle, dHandle *common.DetailHandle) *handle { }, index)
return &handle{iHandle, dHandle}
}
func Hook(h *common.Handle) { func Hook(h *common.Handle) {
h.WidgetAreaData() h.WidgetAreaData()
h.GetPassword() h.GetPassword()
handle := newHandle(common.NewIndexHandle(h), common.NewDetailHandle(h)) h.AutoCal("colorScheme", colorSchemeCss)
if h.Scene == constraints.Detail { h.AutoCal("customBackground", CalCustomBackGround)
handle.Detail() h.PushHandleFn(customHeader)
return switch h.Scene {
case constraints.Detail:
detailPipe(common.NewDetailHandle(h))
default:
indexPipe(common.NewIndexHandle(h))
} }
handle.Index()
} }
func (h *handle) Index() { func index(next common.HandleFn[*common.IndexHandle], i *common.IndexHandle) {
h.CustomHeader() i.Indexs()
h.IndexHandle.AutoCal("colorScheme", h.colorSchemeCss)
h.IndexHandle.AutoCal("customBackground", h.CalCustomBackGround)
h.Indexs()
} }
func (h *handle) Detail() { func detail(fn common.HandleFn[*common.DetailHandle], d *common.DetailHandle) {
h.CustomHeader() d.Details()
h.IndexHandle.AutoCal("colorScheme", h.colorSchemeCss)
h.IndexHandle.AutoCal("customBackground", h.CalCustomBackGround)
h.Details()
} }

View File

@ -2,11 +2,10 @@ package twentyseventeen
import ( import (
"fmt" "fmt"
"github.com/fthvgb1/wp-go/helper"
"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/internal/cmd/reload"
"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/models" "github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/internal/plugins" "github.com/fthvgb1/wp-go/internal/plugins"
"github.com/fthvgb1/wp-go/internal/theme/common" "github.com/fthvgb1/wp-go/internal/theme/common"
@ -38,16 +37,17 @@ var indexPipe = common.HandlePipe(func(i *common.IndexHandle) {
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.PushHandleFn(calClass)
h.GinH["HeaderImage"] = getHeaderImage(h.C) h.GinH["HeaderImage"] = getHeaderImage(h)
if h.Scene == constraints.Detail { switch h.Scene {
case constraints.Detail:
detailPipe(common.NewDetailHandle(h)) detailPipe(common.NewDetailHandle(h))
return default:
}
indexPipe(common.NewIndexHandle(h)) indexPipe(common.NewIndexHandle(h))
}
} }
var pluginFns = func() map[string]common.Plugin[models.Posts, *common.Handle] { var listPostsPlugins = func() map[string]common.Plugin[models.Posts, *common.Handle] {
return maps.Merge(common.ListPostPlugins(), map[string]common.Plugin[models.Posts, *common.Handle]{ return maps.Merge(common.ListPostPlugins(), map[string]common.Plugin[models.Posts, *common.Handle]{
"twentyseventeen_postThumbnail": postThumbnail, "twentyseventeen_postThumbnail": postThumbnail,
}) })
@ -62,7 +62,7 @@ func index(next common.HandleFn[*common.IndexHandle], i *common.IndexHandle) {
i.C.HTML(i.Code, i.Templ, i.GinH) i.C.HTML(i.Code, i.Templ, i.GinH)
return return
} }
i.PostsPlugins = pluginFns i.PostsPlugins = listPostsPlugins
i.PageEle = paginate i.PageEle = paginate
next(i) next(i)
} }
@ -118,16 +118,29 @@ func postThumbnail(next common.Fn[models.Posts], h *common.Handle, t models.Post
return next(t) return next(t)
} }
func getHeaderImage(c *gin.Context) (r models.PostThumbnail) { var header = reload.Vars(models.PostThumbnail{})
r.Path = "/wp-content/themes/twentyseventeen/assets/images/header.jpg"
func getHeaderImage(h *common.Handle) (r models.PostThumbnail) {
img := header.Load()
r.Sizes = "100vw"
if img.Path == "" {
image, rand := h.GetCustomHeader()
if !rand {
if image.Path == "" {
r.Path = helper.CutUrlHost(h.ThemeMods.ThemeSupport.CustomHeader.DefaultImage)
r.Width = 2000 r.Width = 2000
r.Height = 1200 r.Height = 1200
hs, err := cache.GetHeaderImages(c, ThemeName) header.Store(r)
if err != nil { return
logs.ErrPrintln(err, "获取页眉背景图失败")
} else if len(hs) > 0 && err == nil {
_, r = slice.Rand(hs)
} }
r = image
r.Sizes = "100vw"
header.Store(r)
return
}
img = image
}
r = img
r.Sizes = "100vw" r.Sizes = "100vw"
return return
} }