优化 完善
This commit is contained in:
parent
e1c1da6083
commit
6f800230b0
|
@ -96,6 +96,13 @@ func ToBool[T comparable](t T) bool {
|
|||
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 {
|
||||
v := ctx.Value(k)
|
||||
if v == nil {
|
||||
|
|
|
@ -51,6 +51,19 @@ func AnyAnyToStrAny(m map[any]any) (r map[string]any) {
|
|||
}
|
||||
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 {
|
||||
_, ok := m[k]
|
||||
|
@ -135,3 +148,14 @@ func WithDefaultVal[K comparable, V any](m map[K]V, k K, defaults V) V {
|
|||
}
|
||||
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"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
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 {
|
||||
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)
|
||||
}
|
||||
|
||||
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] {
|
||||
m := make(map[K]*Node[T, K])
|
||||
mm := make(map[int]nod[K])
|
||||
m[top] = &Node[T, K]{Children: new([]Node[T, K])}
|
||||
for _, t := range a {
|
||||
for i, t := range a {
|
||||
c, p := fn(t)
|
||||
node := Node[T, K]{Parent: p, Data: t, Children: new([]Node[T, K])}
|
||||
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]
|
||||
if !ok {
|
||||
m[p] = &Node[T, K]{Children: new([]Node[T, K])}
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
package widgets
|
||||
|
||||
const (
|
||||
Search = "search"
|
||||
RecentPosts = "recent-posts"
|
||||
RecentComments = "recent-comments"
|
||||
Archive = "archives"
|
||||
Categories = "categories"
|
||||
Meta = "meta"
|
||||
Search = "widget-search"
|
||||
RecentPosts = "widget-recent-posts"
|
||||
RecentComments = "widget-recent-comments"
|
||||
Archive = "widget-archives"
|
||||
Categories = "widget-categories"
|
||||
Meta = "widget-meta"
|
||||
|
||||
Widget = "widget"
|
||||
)
|
||||
|
|
|
@ -36,10 +36,10 @@ func CategoriesAndTags(a ...any) (terms []models.TermsMy, err error) {
|
|||
w := model.SqlBuilder{
|
||||
{"tt.taxonomy", "in", ""},
|
||||
}
|
||||
if helper.GetContextVal(ctx, "onlyTop", false) {
|
||||
if helper.GetContextVal(ctx, "showOnlyTopLevel", false) {
|
||||
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"})
|
||||
}
|
||||
order := []string{"name", "asc"}
|
||||
|
|
|
@ -7,7 +7,7 @@ import (
|
|||
)
|
||||
|
||||
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{
|
||||
`<li><a href="/wp-login.php">登录</a></li>`: "",
|
||||
`<li><a href="/feed">登录</a></li>`: "",
|
||||
|
|
751
internal/static/wp-includes/css/dist/block-library/style.css
vendored
Executable file → Normal file
751
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) {
|
||||
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"`)
|
||||
})
|
||||
wphandle.RegisterPlugins(h, config.GetConfig().Plugins...)
|
||||
|
|
|
@ -49,9 +49,6 @@ func categoryArgs() map[string]string {
|
|||
"{$nav}": "",
|
||||
"{$navCloser}": "",
|
||||
"{$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)
|
||||
if dropdown == 1 {
|
||||
t = strings.ReplaceAll(t, "{$html}", categoryDropdown(h, args, conf, categories))
|
||||
t = strings.ReplaceAll(t, "{$html}", CategoryDropdown(h, args, conf, categories))
|
||||
|
||||
} else {
|
||||
t = strings.ReplaceAll(t, "{$html}", categoryUL(h, args, conf, categories))
|
||||
|
@ -181,49 +178,13 @@ 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.WriteString(`<form action="/" method="get">
|
||||
`)
|
||||
s.Sprintf(` <label class="screen-reader-text" for="%s">%s</label>
|
||||
`, args["{$selectId}"], args["{$title}"])
|
||||
if len(categories) > 0 {
|
||||
s.Sprintf(` <select %s name="%s" id="%s" class="%s">
|
||||
`, args["{$required}"], args["{$name}"], args["{$selectId}"], args["{$class}"])
|
||||
s.Sprintf(` <option value="%[1]s">%[1]s</option>
|
||||
`, args["{$show_option_none}"])
|
||||
currentCategory := ""
|
||||
if h.Scene() == constraints.Category {
|
||||
currentCategory = h.Index.Param.Category
|
||||
}
|
||||
showCount := conf["count"].(int64)
|
||||
fn := func(category models.TermsMy, deep int) {
|
||||
lv := fmt.Sprintf("level-%d", deep+1)
|
||||
sep := strings.Repeat(" ", deep*2)
|
||||
selected := ""
|
||||
if category.Name == currentCategory {
|
||||
selected = "selected"
|
||||
}
|
||||
count := ""
|
||||
if showCount != 0 {
|
||||
count = fmt.Sprintf("(%d)", category.Count)
|
||||
}
|
||||
s.Sprintf(` <option class="%s" %s value="%d">%s%s %s</option>
|
||||
`, lv, selected, category.Terms.TermId, sep, category.Name, count)
|
||||
}
|
||||
if conf["hierarchical"].(int64) == 0 {
|
||||
for _, category := range categories {
|
||||
fn(category, 0)
|
||||
}
|
||||
} else {
|
||||
tree.Root(categories, 0, func(t models.TermsMy) (child, parent uint64) {
|
||||
return t.TermTaxonomyId, t.Parent
|
||||
}).Loop(func(category models.TermsMy, deep int) {
|
||||
fn(category, deep)
|
||||
})
|
||||
}
|
||||
s.WriteString(" </select>\n")
|
||||
}
|
||||
s.WriteString(DropdownCategories(h, args, conf, categories))
|
||||
s.WriteString("</form>\n")
|
||||
attr := ""
|
||||
if !slice.IsContained(h.CommonThemeMods().ThemeSupport.HTML5, "script") {
|
||||
|
@ -236,6 +197,49 @@ func categoryDropdown(h *wp.Handle, args map[string]string, conf map[any]any, ca
|
|||
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">
|
||||
`, args["{$required}"], args["{$name}"], args["{$selectId}"], args["{$class}"])
|
||||
s.Sprintf(` <option value="-1">%s</option>
|
||||
`, args["{$show_option_none}"])
|
||||
currentCategory := ""
|
||||
if h.Scene() == constraints.Category {
|
||||
currentCategory = h.Index.Param.Category
|
||||
}
|
||||
showCount := conf["count"].(int64)
|
||||
fn := func(category models.TermsMy, deep int) {
|
||||
lv := fmt.Sprintf("level-%d", deep+1)
|
||||
sep := strings.Repeat(" ", deep*2)
|
||||
selected := ""
|
||||
if category.Name == currentCategory {
|
||||
selected = "selected"
|
||||
}
|
||||
count := ""
|
||||
if showCount != 0 {
|
||||
count = fmt.Sprintf("(%d)", category.Count)
|
||||
}
|
||||
s.Sprintf(` <option class="%s" %s value="%d">%s%s %s</option>
|
||||
`, lv, selected, category.Terms.TermId, sep, category.Name, count)
|
||||
}
|
||||
if conf["hierarchical"].(int64) == 0 {
|
||||
for _, category := range categories {
|
||||
fn(category, 0)
|
||||
}
|
||||
} else {
|
||||
tree.Root(categories, 0, func(t models.TermsMy) (child, parent uint64) {
|
||||
return t.TermTaxonomyId, t.Parent
|
||||
}).Loop(func(category models.TermsMy, deep int) {
|
||||
fn(category, deep)
|
||||
})
|
||||
}
|
||||
s.WriteString(" </select>\n")
|
||||
return h.ComponentFilterFnHook("wp_dropdown_cats", s.String())
|
||||
}
|
||||
|
||||
func IsCategory(next wp.HandleFn[*wp.Handle], h *wp.Handle) {
|
||||
if h.Scene() != constraints.Home {
|
||||
next(h)
|
||||
|
|
|
@ -3,8 +3,8 @@ package components
|
|||
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/pkg/constraints/widgets"
|
||||
"github.com/fthvgb1/wp-go/internal/theme/wp"
|
||||
|
@ -25,7 +25,6 @@ var widgetFn = map[string]wp.Components[string]{
|
|||
func WidgetArea(h *wp.Handle) {
|
||||
sidebar := reload.GetAnyValBys("sidebarWidgets", h, sidebars)
|
||||
h.PushComponents(constraints.SidebarsWidgets, sidebar...)
|
||||
h.SetData("categories", cache.CategoriesTags(h.C, constraints.Category))
|
||||
}
|
||||
|
||||
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]
|
||||
name := strings.Join(ss[0:len(ss)-1], "-")
|
||||
components, ok := widgetFn[name]
|
||||
if !ok {
|
||||
if name != "block" && !ok {
|
||||
return components, false
|
||||
}
|
||||
if id != "2" {
|
||||
wp.SetComponentsArgsForMap(h, name, "{$id}", id)
|
||||
}
|
||||
names := str.Join("widget-", name)
|
||||
if beforeWidget != "" {
|
||||
n := strings.ReplaceAll(name, "-", "_")
|
||||
if name == "recent-posts" {
|
||||
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 {
|
||||
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
|
||||
return components, true
|
||||
|
|
|
@ -33,7 +33,7 @@ type Handle struct {
|
|||
err error
|
||||
abort bool
|
||||
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]
|
||||
|
@ -55,19 +55,19 @@ type HandleCall struct {
|
|||
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]
|
||||
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...)
|
||||
}
|
||||
func (h *Handle) ComponentFilterFnHook(name, s string) string {
|
||||
func (h *Handle) ComponentFilterFnHook(name, s string, args ...any) string {
|
||||
calls, ok := h.componentFilterFn[name]
|
||||
if ok {
|
||||
return slice.Reduce(calls, func(fn func(*Handle, string) string, r string) string {
|
||||
return fn(h, r)
|
||||
return slice.Reduce(calls, func(fn func(*Handle, string, ...any) string, r string) string {
|
||||
return fn(h, r, args...)
|
||||
}, s)
|
||||
}
|
||||
return s
|
||||
|
@ -181,7 +181,7 @@ func NewHandle(c *gin.Context, scene int, theme string) *Handle {
|
|||
components: make(map[string][]Components[string]),
|
||||
handleFns: make(map[int][]HandleCall),
|
||||
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