From 6137502ef9f5f525f68271e8a214d2bb9e8bc4e6 Mon Sep 17 00:00:00 2001 From: xing Date: Thu, 13 Jul 2023 22:59:06 +0800 Subject: [PATCH] MaybeInlineStyles --- app/theme/twentyseventeen/script.go | 58 ++++++++++++++++++-- app/theme/wp/scriptloader/defaultstyles.go | 7 +++ app/theme/wp/scriptloader/head.go | 62 ++++++++++++++++++++++ app/theme/wp/scriptloader/scriptloader.go | 56 ++++++++++++------- 4 files changed, 161 insertions(+), 22 deletions(-) create mode 100644 app/theme/wp/scriptloader/defaultstyles.go create mode 100644 app/theme/wp/scriptloader/head.go diff --git a/app/theme/twentyseventeen/script.go b/app/theme/twentyseventeen/script.go index 8b02f7f..3f98a62 100644 --- a/app/theme/twentyseventeen/script.go +++ b/app/theme/twentyseventeen/script.go @@ -6,6 +6,10 @@ import ( "github.com/fthvgb1/wp-go/app/theme/wp" "github.com/fthvgb1/wp-go/app/theme/wp/scriptloader" "github.com/fthvgb1/wp-go/app/wpconfig" + "github.com/fthvgb1/wp-go/helper" + "github.com/fthvgb1/wp-go/helper/maps" + "github.com/fthvgb1/wp-go/helper/number" + str "github.com/fthvgb1/wp-go/helper/strings" ) func pushScripts(h *wp.Handle) { @@ -21,11 +25,23 @@ func pushScripts(h *wp.Handle) { scriptloader.EnqueueScripts("html5", "/assets/js/html5.js", nil, "20161020", false) scriptloader.AddData("html5", "conditional", "lt IE 9") - scriptloader.EnqueueScripts("twentyseventeen-skip-link-focus-fix", "/assets/js/skip-link-focus-fix.js", nil, "20161114", true) + scriptloader.EnqueueScripts("twentyseventeen-skip-link-focus-fix", "/assets/js/skip-link-focus-fix.js", + nil, "20161114", true) - scriptloader.AddStaticLocalize("twentyseventeen-skip-link-focus-fix", "twentyseventeenScreenReaderText", map[string]any{ - "quote": ``, - }) + l10n := map[string]any{ + "quote": svg(h, map[string]string{"icon": "quote-right"}), + } + + scriptloader.EnqueueScripts("twentyseventeen-global", "/assets/js/global.js", + []string{"jquery"}, "20211130", true) + + scriptloader.EnqueueScripts("jquery-scrollto", "/assets/js/jquery.scrollTo.js", + []string{"jquery"}, "2.1.3", true) + scriptloader.EnqueueScripts("comment-reply", "", nil, "", false) + + //todo menu top + + scriptloader.AddStaticLocalize("twentyseventeen-skip-link-focus-fix", "twentyseventeenScreenReaderText", l10n) scriptloader.AddStaticLocalize("wp-custom-header", "_wpCustomHeaderSettings", map[string]any{ "mimeType": `video/mp4`, "posterUrl": `/wp-content/uploads/2023/01/cropped-wallhaven-9dm7dd-1.png`, @@ -71,3 +87,37 @@ var footerScript = ` ` + +func svg(h *wp.Handle, m map[string]string) string { + if !maps.IsExists(m, "icon") { + return "" + } + ariaHidden := ` aria-hidden="true"` + ariaLabelledby := "" + uniqueId := "" + if m["title"] != "" { + ariaHidden = "" + id := helper.GetContextVal(h.C, "svg", 0) + uniqueId = number.IntToString(id) + id++ + h.C.Set("svg", id) + ariaLabelledby = str.Join(" aria-labelledby=\"title-", uniqueId, "\"") + if m["desc"] != "" { + ariaLabelledby = str.Join(" aria-labelledby=\"title-", uniqueId, " desc-", uniqueId, "\"") + } + } + s := str.NewBuilder() + s.WriteString("") + if m["title"] != "" { + s.WriteString(``, m["title"], "") + if m["desc"] != "" { + s.WriteString(``, m["desc"], ``) + } + } + s.WriteString(` `) + if m["fallback"] != "" { + s.WriteString(``) + } + s.WriteString(``) + return s.String() +} diff --git a/app/theme/wp/scriptloader/defaultstyles.go b/app/theme/wp/scriptloader/defaultstyles.go new file mode 100644 index 0000000..ca2e026 --- /dev/null +++ b/app/theme/wp/scriptloader/defaultstyles.go @@ -0,0 +1,7 @@ +package scriptloader + +import "github.com/fthvgb1/wp-go/safety" + +func defaultStyles(m *safety.Map[string, *Script], suffix string) { + +} diff --git a/app/theme/wp/scriptloader/head.go b/app/theme/wp/scriptloader/head.go new file mode 100644 index 0000000..8dca1e2 --- /dev/null +++ b/app/theme/wp/scriptloader/head.go @@ -0,0 +1,62 @@ +package scriptloader + +import ( + "github.com/fthvgb1/wp-go/app/pkg/logs" + "github.com/fthvgb1/wp-go/app/theme/wp" + "github.com/fthvgb1/wp-go/helper/slice" + "os" +) + +type _style struct { + handle string + src string + path string + size int64 +} + +func MaybeInlineStyles(h *wp.Handle) { + totalInlineLimit := int64(0) + var styles []_style + ss := styleQueues.Load() + for _, que := range ss.Queue { + p, ok := __styles.Load(que) + if !ok { + continue + } + f, ok := p.Extra["path"] + if !ok || f == nil { + continue + } + ff := f[0] + stat, err := os.Stat(ff) + if err != nil { + return + } + styles = append(styles, _style{ + handle: que, + src: p.Src, + path: ff, + size: stat.Size(), + }) + } + if len(styles) < 1 { + return + } + slice.Sort(styles, func(i, j _style) bool { + return i.size > j.size + }) + totalInlineSize := int64(0) + for _, i := range styles { + if totalInlineSize+i.size > totalInlineLimit { + break + } + css, err := os.ReadFile(i.path) + if err != nil { + logs.Error(err, "read file ", i.path) + continue + } + s, _ := __styles.Load(i.handle) + s.Src = "" + s.Extra["after"] = append(s.Extra["after"], string(css)) + } +} diff --git a/app/theme/wp/scriptloader/scriptloader.go b/app/theme/wp/scriptloader/scriptloader.go index d5adf1f..93974a9 100644 --- a/app/theme/wp/scriptloader/scriptloader.go +++ b/app/theme/wp/scriptloader/scriptloader.go @@ -21,14 +21,18 @@ import ( "strings" ) -var scripts = reload.MapBy[string, *Script](func(m *safety.Map[string, *Script]) { +var __styles = reload.MapBy(func(m *safety.Map[string, *Script]) { + defaultStyles(m, ".css") +}) +var __scripts = reload.MapBy[string, *Script](func(m *safety.Map[string, *Script]) { suffix := ".min" defaultScripts(m, suffix) + }) func addScript(handle string, src string, deps []string, ver string, args any) { script := NewScript(handle, src, deps, ver, args) - scripts.Store(handle, script) + __scripts.Store(handle, script) } func localize(handle, objectname string, l10n map[string]any) string { @@ -55,14 +59,14 @@ func AddDynamicLocalize(h *wp.Handle, handle, objectname string, l10n map[string } func getData(handle, key string) string { - h, ok := scripts.Load(handle) + h, ok := __scripts.Load(handle) if !ok { return "" } return strings.Join(h.Extra[key], "\n") } func GetData(h *wp.Handle, handle, key string) string { - hh, ok := scripts.Load(handle) + hh, ok := __scripts.Load(handle) if !ok { return "" } @@ -71,8 +75,14 @@ func GetData(h *wp.Handle, handle, key string) string { return strings.Join(d, "\n") } -func AddData(handle, key, data string) { - s, ok := scripts.Load(handle) +func AddData(handle, key, data string, t ...int) { + var s *Script + var ok bool + if t != nil { + s, ok = __styles.Load(handle) + } else { + s, ok = __scripts.Load(handle) + } if !ok { s = NewScript(handle, "", nil, "", nil) } @@ -96,7 +106,7 @@ func AddInlineStyle(handle, data string) { if handle == "" || data == "" { return } - AddData(handle, "after", data) + AddData(handle, "after", data, style) } func InlineScripts(handle, position string, display bool) string { @@ -113,10 +123,16 @@ func InlineScripts(handle, position string, display bool) string { func AddScript(handle string, src string, deps []string, ver string, args any) { script := NewScript(handle, src, deps, ver, args) - scripts.Store(handle, script) + __scripts.Store(handle, script) } -var scriptQueues = scriptQueue{} +const ( + style = iota + script +) + +var scriptQueues = reload.Vars(scriptQueue{}) +var styleQueues = reload.Vars(scriptQueue{}) type scriptQueue struct { Register map[string]struct{} @@ -134,7 +150,7 @@ func EnqueueStyle(handle, src string, deps []string, ver, media string) { if src != "" { AddScript(h[0], src, deps, ver, media) } - enqueue(handle) + enqueue(handle, style) } func EnqueueStyles(handle, src string, deps []string, ver, media string) { if src != "" { @@ -150,7 +166,7 @@ func EnqueueScript(handle, src string, deps []string, ver string, inFooter bool) if inFooter { AddData(h[0], "group", "1") } - enqueue(handle) + enqueue(handle, script) } func EnqueueScripts(handle, src string, deps []string, ver string, inFooter bool) { if src != "" { @@ -159,14 +175,18 @@ func EnqueueScripts(handle, src string, deps []string, ver string, inFooter bool EnqueueScript(handle, src, deps, ver, inFooter) } -func enqueue(handle string) { +func enqueue(handle string, t int) { h := strings.Split(handle, "?") - if slice.IsContained(scriptQueues.Queue, h[0]) && maps.IsExists(scriptQueues.Register, h[0]) { - scriptQueues.Queue = append(scriptQueues.Queue, h[0]) - } else if maps.IsExists(scriptQueues.Register, h[0]) { - scriptQueues.queuedBeforeRegister[h[0]] = "" + ss := styleQueues.Load() + if t == 1 { + ss = scriptQueues.Load() + } + if slice.IsContained(ss.Queue, h[0]) && maps.IsExists(ss.Register, h[0]) { + ss.Queue = append(ss.Queue, h[0]) + } else if maps.IsExists(ss.Register, h[0]) { + ss.queuedBeforeRegister[h[0]] = "" if len(h) > 1 { - scriptQueues.queuedBeforeRegister[h[0]] = h[1] + ss.queuedBeforeRegister[h[0]] = h[1] } } } @@ -221,7 +241,7 @@ func GetDynamicData(h *wp.Handle, handle, key string) string { } func SetTranslation(handle, domain, path string) { - hh, ok := scripts.Load(handle) + hh, ok := __scripts.Load(handle) if !ok { return }