From cbf3cabb24a9c8ebe07f5c2bf0d60030ee827798 Mon Sep 17 00:00:00 2001 From: xing Date: Tue, 28 Feb 2023 20:51:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/theme/common/common.go | 28 +++++++++++++++---- internal/theme/twentyfifteen/twentyfifteen.go | 2 +- .../theme/twentyseventeen/twentyseventeen.go | 2 +- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/internal/theme/common/common.go b/internal/theme/common/common.go index 86f7f4a..94ae61e 100644 --- a/internal/theme/common/common.go +++ b/internal/theme/common/common.go @@ -28,7 +28,7 @@ type Handle struct { Class []string Components map[string][]Components ThemeMods wpconfig.ThemeMods - HandleFns map[int][]HandleFn[*Handle] + HandleFns map[int][]HandleCall Error error } @@ -44,7 +44,7 @@ func NewHandle(c *gin.Context, scene int, theme string) *Handle { Stats: constraints.Ok, ThemeMods: mods, Components: make(map[string][]Components), - HandleFns: make(map[int][]HandleFn[*Handle]), + HandleFns: make(map[int][]HandleCall), } } @@ -58,7 +58,7 @@ func NewComponents(fn func(*Handle) string, order int) Components { return Components{Fn: fn, Order: order} } -func (h *Handle) PushHandleFn(stats int, fns ...HandleFn[*Handle]) { +func (h *Handle) PushHandleFn(stats int, fns ...HandleCall) { h.HandleFns[stats] = append(h.HandleFns[stats], fns...) } @@ -88,13 +88,20 @@ func (h *Handle) GetPassword() { func (h *Handle) ExecHandleFns() { calls, ok := h.HandleFns[h.Stats] if ok { + slice.SortSelf(calls, func(i, j HandleCall) bool { + return i.Order > j.Order + }) for _, call := range calls { - call(h) + call.Fn(h) } } - for _, fn := range h.HandleFns[constraints.AllStats] { - fn(h) + fns, ok := h.HandleFns[constraints.AllStats] + if ok { + for _, fn := range fns { + fn.Fn(h) + } } + } func (h *Handle) PreTemplate() { @@ -155,6 +162,15 @@ type HandleFn[T any] func(T) type HandlePipeFn[T any] func(HandleFn[T], T) +type HandleCall struct { + Fn HandleFn[*Handle] + Order int +} + +func NewHandleFn(fn HandleFn[*Handle], order int) HandleCall { + return HandleCall{Fn: fn, Order: order} +} + // HandlePipe 方便把功能写在其它包里 func HandlePipe[T any](initial func(T), fns ...HandlePipeFn[T]) HandleFn[T] { return slice.ReverseReduce(fns, func(next HandlePipeFn[T], f func(t T)) func(t T) { diff --git a/internal/theme/twentyfifteen/twentyfifteen.go b/internal/theme/twentyfifteen/twentyfifteen.go index 227510f..bae98a3 100644 --- a/internal/theme/twentyfifteen/twentyfifteen.go +++ b/internal/theme/twentyfifteen/twentyfifteen.go @@ -41,7 +41,7 @@ func dispatch(next common.HandleFn[*common.Handle], h *common.Handle) { common.NewComponents(CalCustomBackGround, 10), common.NewComponents(colorSchemeCss, 10), ) - h.PushHandleFn(constraints.AllStats, customHeader) + h.PushHandleFn(constraints.AllStats, common.NewHandleFn(customHeader, 10)) switch h.Scene { case constraints.Detail: detail(next, h.Detail) diff --git a/internal/theme/twentyseventeen/twentyseventeen.go b/internal/theme/twentyseventeen/twentyseventeen.go index 46e18d9..ebe8698 100644 --- a/internal/theme/twentyseventeen/twentyseventeen.go +++ b/internal/theme/twentyseventeen/twentyseventeen.go @@ -36,7 +36,7 @@ func Hook(h *common.Handle) { func ready(next common.HandleFn[*common.Handle], h *common.Handle) { h.WidgetAreaData() h.GetPassword() - h.PushHandleFn(constraints.AllStats, calClass) + h.PushHandleFn(constraints.AllStats, common.NewHandleFn(calClass, 10)) h.PushHeadScript( common.NewComponents(colorScheme, 10), common.NewComponents(customHeader, 10),