diff --git a/internal/cmd/reload/reload.go b/internal/cmd/reload/reload.go index 429045e..c1ff6b7 100644 --- a/internal/cmd/reload/reload.go +++ b/internal/cmd/reload/reload.go @@ -4,6 +4,15 @@ import "github.com/fthvgb1/wp-go/safety" var calls []func() +var str = safety.NewMap[string, string]() + +func GetStr(name string) (string, bool) { + return str.Load(name) +} +func SetStr(name, val string) { + str.Store(name, val) +} + func Vars[T any](defaults T) *safety.Var[T] { ss := safety.NewVar(defaults) calls = append(calls, func() { @@ -27,4 +36,5 @@ func Reload() { for _, call := range calls { call() } + str.Flush() } diff --git a/internal/theme/common/common.go b/internal/theme/common/common.go index 1fb2e50..c759be9 100644 --- a/internal/theme/common/common.go +++ b/internal/theme/common/common.go @@ -6,6 +6,7 @@ import ( "github.com/fthvgb1/wp-go/helper/maps" "github.com/fthvgb1/wp-go/helper/slice" str "github.com/fthvgb1/wp-go/helper/strings" + "github.com/fthvgb1/wp-go/internal/cmd/reload" "github.com/fthvgb1/wp-go/internal/pkg/config" "github.com/fthvgb1/wp-go/internal/pkg/constraints" "github.com/fthvgb1/wp-go/internal/pkg/logs" @@ -53,6 +54,15 @@ func (h *Handle) GetPassword() { } } +func (h *Handle) AutoCal(name string, fn func() string) { + v, ok := reload.GetStr(name) + if !ok { + v = fn() + reload.SetStr(name, v) + } + h.GinH[name] = v +} + func (i *IndexHandle) ExecListPagePlugin(m map[string]Plugin[models.Posts, *Handle], calls ...func(*models.Posts)) { pluginConf := config.GetConfig().ListPagePlugins diff --git a/internal/theme/common/customcss.go b/internal/theme/common/customcss.go index b886609..3dce6ed 100644 --- a/internal/theme/common/customcss.go +++ b/internal/theme/common/customcss.go @@ -3,13 +3,9 @@ package common import ( "fmt" "github.com/fthvgb1/wp-go/helper/html" - "github.com/fthvgb1/wp-go/internal/cmd/reload" "github.com/fthvgb1/wp-go/internal/pkg/cache" - "github.com/fthvgb1/wp-go/internal/pkg/constraints" ) -var css = reload.Vars(constraints.Defaults) - func (h *Handle) CalCustomCss() (r string) { if h.ThemeMods.CustomCssPostId < 1 { return @@ -21,12 +17,3 @@ func (h *Handle) CalCustomCss() (r string) { r = fmt.Sprintf(``, html.StripTags(post.PostContent, "")) return } - -func (h *Handle) CustomCss() { - cs := css.Load() - if cs == constraints.Defaults { - cs = h.CalCustomCss() - css.Store(cs) - } - h.GinH["customCss"] = cs -} diff --git a/internal/theme/common/customlogo.go b/internal/theme/common/customlogo.go index c1a2282..1fd6f99 100644 --- a/internal/theme/common/customlogo.go +++ b/internal/theme/common/customlogo.go @@ -4,14 +4,10 @@ import ( "fmt" "github.com/fthvgb1/wp-go/helper/maps" str "github.com/fthvgb1/wp-go/helper/strings" - "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/wpconfig" ) -var logo = reload.Vars(constraints.Defaults) - func (h *Handle) CalCustomLogo() (r string) { id := uint64(h.ThemeMods.CustomLogo) if id < 1 { @@ -44,12 +40,3 @@ func (h *Handle) CalCustomLogo() (r string) { r = fmt.Sprintf(`%s`, "/", ` aria-current="page"`, r) return } - -func (h *Handle) CustomLogo() { - s := logo.Load() - if s == constraints.Defaults { - s = h.CalCustomLogo() - logo.Store(s) - } - h.GinH["customLogo"] = s -} diff --git a/internal/theme/common/detail.go b/internal/theme/common/detail.go index 9ec9ad1..dc72420 100644 --- a/internal/theme/common/detail.go +++ b/internal/theme/common/detail.go @@ -97,9 +97,10 @@ func (d *DetailHandle) Render() { if d.CommentRender == nil { d.CommentRender = plugins.CommentRender() } - d.SiteIcon() - d.CustomLogo() - d.CustomCss() + d.CalBodyClass() + d.AutoCal("siteIcon", d.CalSiteIcon) + d.AutoCal("customLogo", d.CalCustomLogo) + d.AutoCal("customCss", d.CalCustomCss) d.RenderComment() d.CalBodyClass() if d.Templ == "" { diff --git a/internal/theme/common/index.go b/internal/theme/common/index.go index 26948e6..b9060fa 100644 --- a/internal/theme/common/index.go +++ b/internal/theme/common/index.go @@ -112,9 +112,9 @@ func (i *IndexHandle) Render() { i.PageEle = plugins.TwentyFifteenPagination() } i.Pagination() - i.SiteIcon() - i.CustomLogo() - i.CustomCss() + i.AutoCal("siteIcon", i.CalSiteIcon) + i.AutoCal("customLogo", i.CalCustomLogo) + i.AutoCal("customCss", i.CalCustomCss) i.CalBodyClass() if i.Templ == "" { i.Templ = fmt.Sprintf("%s/posts/index.gohtml", i.Theme) diff --git a/internal/theme/common/siteicon.go b/internal/theme/common/siteicon.go index 830fdc2..ebbb19b 100644 --- a/internal/theme/common/siteicon.go +++ b/internal/theme/common/siteicon.go @@ -4,14 +4,11 @@ import ( "fmt" "github.com/fthvgb1/wp-go/helper/slice" str "github.com/fthvgb1/wp-go/helper/strings" - "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/wpconfig" "strings" ) -var icon = reload.Vars(constraints.Defaults) var sizes = []string{"site_icon-270", "site_icon-32", "site_icon-192", "site_icon-180"} func (h *Handle) CalSiteIcon() (r string) { @@ -42,12 +39,3 @@ func (h *Handle) CalSiteIcon() (r string) { r = strings.Join(size, "\n") return } - -func (h *Handle) SiteIcon() { - s := icon.Load() - if s == constraints.Defaults { - s = h.CalSiteIcon() - icon.Store(s) - } - h.GinH["siteIcon"] = s -} diff --git a/internal/theme/twentyfifteen/colorschemecss.go b/internal/theme/twentyfifteen/colorschemecss.go index be1f9aa..c60ec3a 100644 --- a/internal/theme/twentyfifteen/colorschemecss.go +++ b/internal/theme/twentyfifteen/colorschemecss.go @@ -3,24 +3,15 @@ package twentyfifteen import ( "fmt" "github.com/fthvgb1/wp-go/helper/slice" - "github.com/fthvgb1/wp-go/internal/cmd/reload" - "github.com/fthvgb1/wp-go/internal/pkg/constraints" "strconv" "strings" ) -var colorCss = reload.Vars("default") - -func (h *handle) colorSchemeCss() { - color := colorCss.Load() - if color == constraints.Defaults { - s := slice.Filter([]string{h.calColorSchemeCss(), h.calSidebarTextColorCss(), h.calHeaderBackgroundColorCss()}, func(s string) bool { - return s != "" - }) - color = fmt.Sprintf(`\n`, "twentyfifteen-style", "", strings.Join(s, "\n")) - colorCss.Store(color) - } - h.IndexHandle.GinH["colorScheme"] = color +func (h *handle) colorSchemeCss() string { + s := slice.Filter([]string{h.calColorSchemeCss(), h.calSidebarTextColorCss(), h.calHeaderBackgroundColorCss()}, func(s string) bool { + return s != "" + }) + return fmt.Sprintf(`\n`, "twentyfifteen-style", "", strings.Join(s, "\n")) } func (h *handle) calColorSchemeCss() (r string) { color := h.getColorScheme() diff --git a/internal/theme/twentyfifteen/custombackground.go b/internal/theme/twentyfifteen/custombackground.go index b64b455..68a141e 100644 --- a/internal/theme/twentyfifteen/custombackground.go +++ b/internal/theme/twentyfifteen/custombackground.go @@ -5,8 +5,6 @@ import ( "github.com/fthvgb1/wp-go/helper" "github.com/fthvgb1/wp-go/helper/maps" str "github.com/fthvgb1/wp-go/helper/strings" - "github.com/fthvgb1/wp-go/internal/cmd/reload" - "github.com/fthvgb1/wp-go/internal/pkg/constraints" ) var postx = map[string]string{ @@ -31,17 +29,6 @@ var repeat = map[string]string{ "no-repeat": "no-repeat", } -var background = reload.Vars(constraints.Defaults) - -func (h *handle) CustomBackGround() { - b := background.Load() - if b == constraints.Defaults { - b = h.CalCustomBackGround() - background.Store(b) - } - h.IndexHandle.GinH["customBackground"] = b -} - func (h *handle) CalCustomBackGround() (r string) { if h.IndexHandle.ThemeMods.BackgroundImage == "" && h.IndexHandle.ThemeMods.BackgroundColor == themesupport.CustomBackground.DefaultColor { return diff --git a/internal/theme/twentyfifteen/twentyfifteen.go b/internal/theme/twentyfifteen/twentyfifteen.go index afacb64..618ed2f 100644 --- a/internal/theme/twentyfifteen/twentyfifteen.go +++ b/internal/theme/twentyfifteen/twentyfifteen.go @@ -37,14 +37,6 @@ func newHandle(iHandle *common.IndexHandle, dHandle *common.DetailHandle) *handl return &handle{iHandle, dHandle} } -type detailHandle struct { - *common.DetailHandle -} - -func newDetailHandle(dHandle *common.DetailHandle) *detailHandle { - return &detailHandle{DetailHandle: dHandle} -} - func Hook(h *common.Handle) { h.WidgetAreaData() h.GetPassword() @@ -58,14 +50,14 @@ func Hook(h *common.Handle) { func (h *handle) Index() { h.CustomHeader() - h.colorSchemeCss() - h.CustomBackGround() + h.IndexHandle.AutoCal("colorScheme", h.colorSchemeCss) + h.IndexHandle.AutoCal("customBackground", h.CalCustomBackGround) h.Indexs() } func (h *handle) Detail() { h.CustomHeader() - h.colorSchemeCss() - h.CustomBackGround() + h.IndexHandle.AutoCal("colorScheme", h.colorSchemeCss) + h.IndexHandle.AutoCal("customBackground", h.CalCustomBackGround) h.Details() }