customlogo

This commit is contained in:
xing 2023-02-16 20:48:08 +08:00
parent 3c21187ee3
commit a635b9a8ea
7 changed files with 84 additions and 21 deletions

View File

@ -71,14 +71,18 @@ func (h *Handle) bodyClass(class ...string) string {
} }
} }
class = append(class, s) class = append(class, s)
if h.IsSupport("custom-background") && wpconfig.IsCustomBackground(h.Theme) { mods, err := wpconfig.GetThemeMods(h.Theme)
if err == nil {
if wpconfig.IsCustomBackground(h.Theme) {
class = append(class, "custom-background") class = append(class, "custom-background")
} }
if h.IsSupport("custom-logo") && wpconfig.IsCustomLogo(h.Theme) { if mods.CustomLogo > 0 {
class = append(class, "wp-custom-logo") class = append(class, "wp-custom-logo")
} }
if h.IsSupport("responsive-embeds") { if mods.ThemeSupport.ResponsiveEmbeds {
class = append(class, "wp-embed-responsive") class = append(class, "wp-embed-responsive")
} }
}
return str.Join(commonClass[h.Scene], strings.Join(class, " ")) return str.Join(commonClass[h.Scene], strings.Join(class, " "))
} }

View File

@ -0,0 +1,54 @@
package common
import (
"fmt"
"github.com/fthvgb1/wp-go/helper/maps"
"github.com/fthvgb1/wp-go/internal/pkg/cache"
"github.com/fthvgb1/wp-go/internal/wpconfig"
"github.com/fthvgb1/wp-go/safety"
)
var logo = safety.NewVar("default")
func (h *Handle) CalCustomLogo() (r string) {
mods, err := wpconfig.GetThemeMods(h.Theme)
if err != nil {
return
}
id := uint64(mods.CustomLogo)
if id < 1 {
return
}
logo, err := cache.GetPostById(h.C, id)
if err != nil || logo.AttachmentMetadata.File == "" {
return
}
siz := "full"
meta, _ := cache.GetPostMetaByPostId(h.C, id)
alt := maps.WithDefaultVal(meta, "_wp_attachment_image_alt", any(wpconfig.Options.Value("blogname")))
desc := alt.(string)
imgx := map[string]string{
"class": "custom-logo",
"alt": desc,
"decoding": "async",
//"loading":"lazy",
}
img := wpconfig.Thumbnail(logo.AttachmentMetadata, siz, "", "")
imgx["srcset"] = img.Srcset
imgx["sizes"] = img.Sizes
imgx["src"] = img.Path
r = fmt.Sprintf("%s />", maps.Reduce(imgx, func(k string, v string, t string) string {
return fmt.Sprintf(`%s %s="%s"`, t, k, v)
}, fmt.Sprintf(`<img wight="%v" height="%v"`, img.Width, img.Height)))
r = fmt.Sprintf(`<a href="%s" class="custom-logo-link" rel="home"%s>%s</a>`, "/", ` aria-current="page"`, r)
return
}
func (h *Handle) CustomLogo() {
s := logo.Load()
if s == "default" {
s = h.CalCustomLogo()
logo.Store(s)
}
h.GinH["customLogo"] = s
}

View File

@ -98,6 +98,7 @@ func (d *DetailHandle) Render() {
d.CommentRender = plugins.CommentRender() d.CommentRender = plugins.CommentRender()
} }
d.SiteIcon() d.SiteIcon()
d.CustomLogo()
d.RenderComment() d.RenderComment()
d.CalBodyClass() d.CalBodyClass()
if d.Templ == "" { if d.Templ == "" {

View File

@ -113,6 +113,7 @@ func (i *IndexHandle) Render() {
} }
i.Pagination() i.Pagination()
i.SiteIcon() i.SiteIcon()
i.CustomLogo()
i.CalBodyClass() i.CalBodyClass()
if i.Templ == "" { if i.Templ == "" {
i.Templ = fmt.Sprintf("%s/posts/index.gohtml", i.Theme) i.Templ = fmt.Sprintf("%s/posts/index.gohtml", i.Theme)

View File

@ -2,4 +2,5 @@ package common
func Reload() { func Reload() {
backgroud.Store("default") backgroud.Store("default")
icon.Store("default")
} }

View File

@ -16,6 +16,9 @@
<div id="sidebar" class="sidebar" style="position: relative; "> <div id="sidebar" class="sidebar" style="position: relative; ">
<header id="masthead" class="site-header"> <header id="masthead" class="site-header">
<div class="site-branding"> <div class="site-branding">
{{if .customLogo}}
{{.customLogo|unescaped}}
{{end}}
<h1 class="site-title"> <h1 class="site-title">
<a href="/" rel="home">{{ "blogname"| getOption }}</a> <a href="/" rel="home">{{ "blogname"| getOption }}</a>
</h1> </h1>

View File

@ -66,13 +66,23 @@ type ImageData struct {
} }
func Thumbnail(metadata models.WpAttachmentMetadata, Type, host string, except ...string) (r models.PostThumbnail) { func Thumbnail(metadata models.WpAttachmentMetadata, Type, host string, except ...string) (r models.PostThumbnail) {
up := strings.Split(metadata.File, "/")
if Type == "full" {
metadata.Sizes["full"] = models.MetaDataFileSize{
File: filepath.Base(metadata.File),
Width: metadata.Width,
Height: metadata.Height,
MimeType: metadata.Sizes["thumbnail"].MimeType,
FileSize: metadata.FileSize,
}
}
if _, ok := metadata.Sizes[Type]; ok { if _, ok := metadata.Sizes[Type]; ok {
r.Path = fmt.Sprintf("%s/wp-content/uploads/%s", host, metadata.File) r.Path = fmt.Sprintf("%s/wp-content/uploads/%s", host, metadata.File)
r.Width = metadata.Sizes[Type].Width r.Width = metadata.Sizes[Type].Width
r.Height = metadata.Sizes[Type].Height r.Height = metadata.Sizes[Type].Height
up := strings.Split(metadata.File, "/")
r.Srcset = strings.Join(maps.FilterToSlice[string](metadata.Sizes, func(s string, size models.MetaDataFileSize) (r string, ok bool) { r.Srcset = strings.Join(maps.FilterToSlice[string](metadata.Sizes, func(s string, size models.MetaDataFileSize) (r string, ok bool) {
up[2] = size.File up[len(up)-1] = size.File
for _, s2 := range except { for _, s2 := range except {
if s == s2 { if s == s2 {
return return
@ -130,17 +140,6 @@ func IsCustomBackground(theme string) bool {
return false return false
} }
func IsCustomLogo(theme string) bool {
mods, err := GetThemeMods(theme)
if err != nil {
return false
}
if mods.CustomLogo > 0 {
return true
}
return false
}
func (m *ThemeMods) setThemeColorScheme(themeName string) { func (m *ThemeMods) setThemeColorScheme(themeName string) {
bytes, err := templateFs.ReadFile(filepath.Join(themeName, "colorscheme.json")) bytes, err := templateFs.ReadFile(filepath.Join(themeName, "colorscheme.json"))