优化 完善
This commit is contained in:
parent
e1c1da6083
commit
6f800230b0
|
@ -96,6 +96,13 @@ func ToBool[T comparable](t T) bool {
|
||||||
return vv != t
|
return vv != t
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ToBoolInt(t any) int8 {
|
||||||
|
if IsZeros(t) {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
func GetContextVal[V, K any](ctx context.Context, k K, defaults V) V {
|
func GetContextVal[V, K any](ctx context.Context, k K, defaults V) V {
|
||||||
v := ctx.Value(k)
|
v := ctx.Value(k)
|
||||||
if v == nil {
|
if v == nil {
|
||||||
|
|
|
@ -51,6 +51,19 @@ func AnyAnyToStrAny(m map[any]any) (r map[string]any) {
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
func StrAnyToAnyAny(m map[string]any) (r map[any]any) {
|
||||||
|
r = make(map[any]any)
|
||||||
|
for kk, v := range m {
|
||||||
|
vv, ok := v.(map[string]any)
|
||||||
|
if ok {
|
||||||
|
r[kk] = StrAnyToAnyAny(vv)
|
||||||
|
} else {
|
||||||
|
r[kk] = v
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func IsExists[K comparable, V any](m map[K]V, k K) bool {
|
func IsExists[K comparable, V any](m map[K]V, k K) bool {
|
||||||
_, ok := m[k]
|
_, ok := m[k]
|
||||||
|
@ -135,3 +148,14 @@ func WithDefaultVal[K comparable, V any](m map[K]V, k K, defaults V) V {
|
||||||
}
|
}
|
||||||
return defaults
|
return defaults
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func AnyAnyMap[K comparable, V any](m map[any]any, fn func(k, v any) (K, V, bool)) map[K]V {
|
||||||
|
mm := make(map[K]V, 0)
|
||||||
|
for k, v := range m {
|
||||||
|
key, val, ok := fn(k, v)
|
||||||
|
if ok {
|
||||||
|
mm[key] = val
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return mm
|
||||||
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"unicode"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Join(s ...string) (str string) {
|
func Join(s ...string) (str string) {
|
||||||
|
@ -94,3 +95,19 @@ func (b *Builder) Sprintf(format string, a ...any) int {
|
||||||
func CutSpecialDuplicate(s, char string) string {
|
func CutSpecialDuplicate(s, char string) string {
|
||||||
return strings.Join(strings.Fields(s), char)
|
return strings.Join(strings.Fields(s), char)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CamelCaseTo 驼峰单词转下划线或横杠单词 //分隔符
|
||||||
|
func CamelCaseTo(s string, delimiter rune) string {
|
||||||
|
var output []rune
|
||||||
|
for i, r := range s {
|
||||||
|
if i == 0 {
|
||||||
|
output = append(output, unicode.ToLower(r))
|
||||||
|
} else {
|
||||||
|
if unicode.IsUpper(r) {
|
||||||
|
output = append(output, delimiter)
|
||||||
|
}
|
||||||
|
output = append(output, unicode.ToLower(r))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return string(output)
|
||||||
|
}
|
||||||
|
|
|
@ -73,13 +73,25 @@ func Roots[T any, K comparable](a []T, top K, fn func(T) (child, parent K)) map[
|
||||||
return root(a, top, fn)
|
return root(a, top, fn)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type nod[K comparable] struct {
|
||||||
|
current K
|
||||||
|
parent K
|
||||||
|
}
|
||||||
|
|
||||||
func root[T any, K comparable](a []T, top K, fn func(T) (child, parent K)) map[K]*Node[T, K] {
|
func root[T any, K comparable](a []T, top K, fn func(T) (child, parent K)) map[K]*Node[T, K] {
|
||||||
m := make(map[K]*Node[T, K])
|
m := make(map[K]*Node[T, K])
|
||||||
|
mm := make(map[int]nod[K])
|
||||||
m[top] = &Node[T, K]{Children: new([]Node[T, K])}
|
m[top] = &Node[T, K]{Children: new([]Node[T, K])}
|
||||||
for _, t := range a {
|
for i, t := range a {
|
||||||
c, p := fn(t)
|
c, p := fn(t)
|
||||||
node := Node[T, K]{Parent: p, Data: t, Children: new([]Node[T, K])}
|
node := Node[T, K]{Parent: p, Data: t, Children: new([]Node[T, K])}
|
||||||
m[c] = &node
|
m[c] = &node
|
||||||
|
mm[i] = nod[K]{c, p}
|
||||||
|
}
|
||||||
|
for i := range a {
|
||||||
|
c := mm[i].current
|
||||||
|
p := mm[i].parent
|
||||||
|
node := *m[c]
|
||||||
parent, ok := m[p]
|
parent, ok := m[p]
|
||||||
if !ok {
|
if !ok {
|
||||||
m[p] = &Node[T, K]{Children: new([]Node[T, K])}
|
m[p] = &Node[T, K]{Children: new([]Node[T, K])}
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
package widgets
|
package widgets
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Search = "search"
|
Search = "widget-search"
|
||||||
RecentPosts = "recent-posts"
|
RecentPosts = "widget-recent-posts"
|
||||||
RecentComments = "recent-comments"
|
RecentComments = "widget-recent-comments"
|
||||||
Archive = "archives"
|
Archive = "widget-archives"
|
||||||
Categories = "categories"
|
Categories = "widget-categories"
|
||||||
Meta = "meta"
|
Meta = "widget-meta"
|
||||||
|
|
||||||
Widget = "widget"
|
Widget = "widget"
|
||||||
)
|
)
|
||||||
|
|
|
@ -36,10 +36,10 @@ func CategoriesAndTags(a ...any) (terms []models.TermsMy, err error) {
|
||||||
w := model.SqlBuilder{
|
w := model.SqlBuilder{
|
||||||
{"tt.taxonomy", "in", ""},
|
{"tt.taxonomy", "in", ""},
|
||||||
}
|
}
|
||||||
if helper.GetContextVal(ctx, "onlyTop", false) {
|
if helper.GetContextVal(ctx, "showOnlyTopLevel", false) {
|
||||||
w = append(w, []string{"tt.parent", "=", "0", "int"})
|
w = append(w, []string{"tt.parent", "=", "0", "int"})
|
||||||
}
|
}
|
||||||
if !helper.GetContextVal(ctx, "showCountZero", false) {
|
if !helper.GetContextVal(ctx, "showEmpty", false) {
|
||||||
w = append(w, []string{"tt.count", ">", "0", "int"})
|
w = append(w, []string{"tt.count", ">", "0", "int"})
|
||||||
}
|
}
|
||||||
order := []string{"name", "asc"}
|
order := []string{"name", "asc"}
|
||||||
|
|
|
@ -7,7 +7,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func HiddenLogin(h *wp.Handle) {
|
func HiddenLogin(h *wp.Handle) {
|
||||||
h.PushComponentFilterFn(widgets.Meta, func(h *wp.Handle, s string) string {
|
h.PushComponentFilterFn(widgets.Meta, func(h *wp.Handle, s string, args ...any) string {
|
||||||
return str.Replace(s, map[string]string{
|
return str.Replace(s, map[string]string{
|
||||||
`<li><a href="/wp-login.php">登录</a></li>`: "",
|
`<li><a href="/wp-login.php">登录</a></li>`: "",
|
||||||
`<li><a href="/feed">登录</a></li>`: "",
|
`<li><a href="/feed">登录</a></li>`: "",
|
||||||
|
|
741
internal/static/wp-includes/css/dist/block-library/style.css
vendored
Executable file → Normal file
741
internal/static/wp-includes/css/dist/block-library/style.css
vendored
Executable file → Normal file
File diff suppressed because it is too large
Load Diff
6
internal/static/wp-includes/css/dist/block-library/style.min.css
vendored
Executable file → Normal file
6
internal/static/wp-includes/css/dist/block-library/style.min.css
vendored
Executable file → Normal file
File diff suppressed because one or more lines are too long
|
@ -42,7 +42,7 @@ func Hook(h *wp.Handle) {
|
||||||
|
|
||||||
func ready(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
|
func ready(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
|
||||||
h.GetPassword()
|
h.GetPassword()
|
||||||
h.PushComponentFilterFn(widgets.Search, func(h *wp.Handle, s string) string {
|
h.PushComponentFilterFn(widgets.Search, func(h *wp.Handle, s string, args ...any) string {
|
||||||
return strings.ReplaceAll(s, `class="search-submit"`, `class="search-submit screen-reader-text"`)
|
return strings.ReplaceAll(s, `class="search-submit"`, `class="search-submit screen-reader-text"`)
|
||||||
})
|
})
|
||||||
wphandle.RegisterPlugins(h, config.GetConfig().Plugins...)
|
wphandle.RegisterPlugins(h, config.GetConfig().Plugins...)
|
||||||
|
|
|
@ -49,9 +49,6 @@ func categoryArgs() map[string]string {
|
||||||
"{$nav}": "",
|
"{$nav}": "",
|
||||||
"{$navCloser}": "",
|
"{$navCloser}": "",
|
||||||
"{$title}": "",
|
"{$title}": "",
|
||||||
"{$dropdown_id}": "archives-dropdown-2",
|
|
||||||
"{$dropdown_type}": "monthly",
|
|
||||||
"{$dropdown_label}": "选择月份",
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,7 +65,7 @@ func Category(h *wp.Handle) string {
|
||||||
}
|
}
|
||||||
categories := cache.CategoriesTags(h.C, constraints.Category)
|
categories := cache.CategoriesTags(h.C, constraints.Category)
|
||||||
if dropdown == 1 {
|
if dropdown == 1 {
|
||||||
t = strings.ReplaceAll(t, "{$html}", categoryDropdown(h, args, conf, categories))
|
t = strings.ReplaceAll(t, "{$html}", CategoryDropdown(h, args, conf, categories))
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
t = strings.ReplaceAll(t, "{$html}", categoryUL(h, args, conf, categories))
|
t = strings.ReplaceAll(t, "{$html}", categoryUL(h, args, conf, categories))
|
||||||
|
@ -181,16 +178,33 @@ var categoryDropdownJs = `/* <![CDATA[ */
|
||||||
/* ]]> */
|
/* ]]> */
|
||||||
`
|
`
|
||||||
|
|
||||||
func categoryDropdown(h *wp.Handle, args map[string]string, conf map[any]any, categories []models.TermsMy) string {
|
func CategoryDropdown(h *wp.Handle, args map[string]string, conf map[any]any, categories []models.TermsMy) string {
|
||||||
s := str.NewBuilder()
|
s := str.NewBuilder()
|
||||||
s.WriteString(`<form action="/" method="get">
|
s.WriteString(`<form action="/" method="get">
|
||||||
`)
|
`)
|
||||||
s.Sprintf(` <label class="screen-reader-text" for="%s">%s</label>
|
s.Sprintf(` <label class="screen-reader-text" for="%s">%s</label>
|
||||||
`, args["{$selectId}"], args["{$title}"])
|
`, args["{$selectId}"], args["{$title}"])
|
||||||
if len(categories) > 0 {
|
s.WriteString(DropdownCategories(h, args, conf, categories))
|
||||||
|
s.WriteString("</form>\n")
|
||||||
|
attr := ""
|
||||||
|
if !slice.IsContained(h.CommonThemeMods().ThemeSupport.HTML5, "script") {
|
||||||
|
attr = ` type="text/javascript"`
|
||||||
|
}
|
||||||
|
s.Sprintf(`<script%s>
|
||||||
|
`, attr)
|
||||||
|
s.Sprintf(categoryDropdownJs, args["{$selectId}"])
|
||||||
|
s.WriteString("</script>\n")
|
||||||
|
return s.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func DropdownCategories(h *wp.Handle, args map[string]string, conf map[any]any, categories []models.TermsMy) string {
|
||||||
|
if len(categories) < 1 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
s := str.NewBuilder()
|
||||||
s.Sprintf(` <select %s name="%s" id="%s" class="%s">
|
s.Sprintf(` <select %s name="%s" id="%s" class="%s">
|
||||||
`, args["{$required}"], args["{$name}"], args["{$selectId}"], args["{$class}"])
|
`, args["{$required}"], args["{$name}"], args["{$selectId}"], args["{$class}"])
|
||||||
s.Sprintf(` <option value="%[1]s">%[1]s</option>
|
s.Sprintf(` <option value="-1">%s</option>
|
||||||
`, args["{$show_option_none}"])
|
`, args["{$show_option_none}"])
|
||||||
currentCategory := ""
|
currentCategory := ""
|
||||||
if h.Scene() == constraints.Category {
|
if h.Scene() == constraints.Category {
|
||||||
|
@ -223,17 +237,7 @@ func categoryDropdown(h *wp.Handle, args map[string]string, conf map[any]any, ca
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
s.WriteString(" </select>\n")
|
s.WriteString(" </select>\n")
|
||||||
}
|
return h.ComponentFilterFnHook("wp_dropdown_cats", s.String())
|
||||||
s.WriteString("</form>\n")
|
|
||||||
attr := ""
|
|
||||||
if !slice.IsContained(h.CommonThemeMods().ThemeSupport.HTML5, "script") {
|
|
||||||
attr = ` type="text/javascript"`
|
|
||||||
}
|
|
||||||
s.Sprintf(`<script%s>
|
|
||||||
`, attr)
|
|
||||||
s.Sprintf(categoryDropdownJs, args["{$selectId}"])
|
|
||||||
s.WriteString("</script>\n")
|
|
||||||
return s.String()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsCategory(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
|
func IsCategory(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
|
||||||
|
|
|
@ -3,8 +3,8 @@ package components
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"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/cmd/reload"
|
"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/constraints/widgets"
|
"github.com/fthvgb1/wp-go/internal/pkg/constraints/widgets"
|
||||||
"github.com/fthvgb1/wp-go/internal/theme/wp"
|
"github.com/fthvgb1/wp-go/internal/theme/wp"
|
||||||
|
@ -25,7 +25,6 @@ var widgetFn = map[string]wp.Components[string]{
|
||||||
func WidgetArea(h *wp.Handle) {
|
func WidgetArea(h *wp.Handle) {
|
||||||
sidebar := reload.GetAnyValBys("sidebarWidgets", h, sidebars)
|
sidebar := reload.GetAnyValBys("sidebarWidgets", h, sidebars)
|
||||||
h.PushComponents(constraints.SidebarsWidgets, sidebar...)
|
h.PushComponents(constraints.SidebarsWidgets, sidebar...)
|
||||||
h.SetData("categories", cache.CategoriesTags(h.C, constraints.Category))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func sidebars(h *wp.Handle) []wp.Components[string] {
|
func sidebars(h *wp.Handle) []wp.Components[string] {
|
||||||
|
@ -43,21 +42,29 @@ func sidebars(h *wp.Handle) []wp.Components[string] {
|
||||||
id := ss[len(ss)-1]
|
id := ss[len(ss)-1]
|
||||||
name := strings.Join(ss[0:len(ss)-1], "-")
|
name := strings.Join(ss[0:len(ss)-1], "-")
|
||||||
components, ok := widgetFn[name]
|
components, ok := widgetFn[name]
|
||||||
if !ok {
|
if name != "block" && !ok {
|
||||||
return components, false
|
return components, false
|
||||||
}
|
}
|
||||||
if id != "2" {
|
if id != "2" {
|
||||||
wp.SetComponentsArgsForMap(h, name, "{$id}", id)
|
wp.SetComponentsArgsForMap(h, name, "{$id}", id)
|
||||||
}
|
}
|
||||||
|
names := str.Join("widget-", name)
|
||||||
if beforeWidget != "" {
|
if beforeWidget != "" {
|
||||||
n := strings.ReplaceAll(name, "-", "_")
|
n := strings.ReplaceAll(name, "-", "_")
|
||||||
if name == "recent-posts" {
|
if name == "recent-posts" {
|
||||||
n = "recent_entries"
|
n = "recent_entries"
|
||||||
}
|
}
|
||||||
wp.SetComponentsArgsForMap(h, name, "{$before_widget}", fmt.Sprintf(beforeWidget, vv, n))
|
wp.SetComponentsArgsForMap(h, names, "{$before_widget}", fmt.Sprintf(beforeWidget, vv, n))
|
||||||
}
|
}
|
||||||
for k, val := range args {
|
for k, val := range args {
|
||||||
wp.SetComponentsArgsForMap(h, name, k, val)
|
wp.SetComponentsArgsForMap(h, names, k, val)
|
||||||
|
}
|
||||||
|
if name == "block" {
|
||||||
|
fn := Block(id)
|
||||||
|
if fn == nil {
|
||||||
|
return wp.Components[string]{}, false
|
||||||
|
}
|
||||||
|
components = wp.Components[string]{Fn: fn, Order: 10}
|
||||||
}
|
}
|
||||||
components.Order = 10
|
components.Order = 10
|
||||||
return components, true
|
return components, true
|
||||||
|
|
|
@ -33,7 +33,7 @@ type Handle struct {
|
||||||
err error
|
err error
|
||||||
abort bool
|
abort bool
|
||||||
componentsArgs map[string]any
|
componentsArgs map[string]any
|
||||||
componentFilterFn map[string][]func(*Handle, string) string
|
componentFilterFn map[string][]func(*Handle, string, ...any) string
|
||||||
}
|
}
|
||||||
|
|
||||||
type HandlePlugins map[string]HandleFn[*Handle]
|
type HandlePlugins map[string]HandleFn[*Handle]
|
||||||
|
@ -55,19 +55,19 @@ type HandleCall struct {
|
||||||
Order int
|
Order int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handle) ComponentFilterFn(name string) ([]func(*Handle, string) string, bool) {
|
func (h *Handle) ComponentFilterFn(name string) ([]func(*Handle, string, ...any) string, bool) {
|
||||||
fn, ok := h.componentFilterFn[name]
|
fn, ok := h.componentFilterFn[name]
|
||||||
return fn, ok
|
return fn, ok
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handle) PushComponentFilterFn(name string, fns ...func(*Handle, string) string) {
|
func (h *Handle) PushComponentFilterFn(name string, fns ...func(*Handle, string, ...any) string) {
|
||||||
h.componentFilterFn[name] = append(h.componentFilterFn[name], fns...)
|
h.componentFilterFn[name] = append(h.componentFilterFn[name], fns...)
|
||||||
}
|
}
|
||||||
func (h *Handle) ComponentFilterFnHook(name, s string) string {
|
func (h *Handle) ComponentFilterFnHook(name, s string, args ...any) string {
|
||||||
calls, ok := h.componentFilterFn[name]
|
calls, ok := h.componentFilterFn[name]
|
||||||
if ok {
|
if ok {
|
||||||
return slice.Reduce(calls, func(fn func(*Handle, string) string, r string) string {
|
return slice.Reduce(calls, func(fn func(*Handle, string, ...any) string, r string) string {
|
||||||
return fn(h, r)
|
return fn(h, r, args...)
|
||||||
}, s)
|
}, s)
|
||||||
}
|
}
|
||||||
return s
|
return s
|
||||||
|
@ -181,7 +181,7 @@ func NewHandle(c *gin.Context, scene int, theme string) *Handle {
|
||||||
components: make(map[string][]Components[string]),
|
components: make(map[string][]Components[string]),
|
||||||
handleFns: make(map[int][]HandleCall),
|
handleFns: make(map[int][]HandleCall),
|
||||||
componentsArgs: make(map[string]any),
|
componentsArgs: make(map[string]any),
|
||||||
componentFilterFn: make(map[string][]func(*Handle, string) string),
|
componentFilterFn: make(map[string][]func(*Handle, string, ...any) string),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user