MaybeInlineStyles
This commit is contained in:
parent
25bf1dd1f7
commit
6137502ef9
@ -6,6 +6,10 @@ import (
|
|||||||
"github.com/fthvgb1/wp-go/app/theme/wp"
|
"github.com/fthvgb1/wp-go/app/theme/wp"
|
||||||
"github.com/fthvgb1/wp-go/app/theme/wp/scriptloader"
|
"github.com/fthvgb1/wp-go/app/theme/wp/scriptloader"
|
||||||
"github.com/fthvgb1/wp-go/app/wpconfig"
|
"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) {
|
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.EnqueueScripts("html5", "/assets/js/html5.js", nil, "20161020", false)
|
||||||
scriptloader.AddData("html5", "conditional", "lt IE 9")
|
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{
|
l10n := map[string]any{
|
||||||
"quote": `<svg class="icon icon-quote-right" aria-hidden="true" role="img"> <use href="#icon-quote-right" xlink:href="#icon-quote-right"></use> </svg>`,
|
"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{
|
scriptloader.AddStaticLocalize("wp-custom-header", "_wpCustomHeaderSettings", map[string]any{
|
||||||
"mimeType": `video/mp4`,
|
"mimeType": `video/mp4`,
|
||||||
"posterUrl": `/wp-content/uploads/2023/01/cropped-wallhaven-9dm7dd-1.png`,
|
"posterUrl": `/wp-content/uploads/2023/01/cropped-wallhaven-9dm7dd-1.png`,
|
||||||
@ -71,3 +87,37 @@ var footerScript = `<script id="twentyseventeen-skip-link-focus-fix-js-extra">
|
|||||||
<script src="/wp-content/themes/twentyseventeen/assets/js/skip-link-focus-fix.js?ver=20161114" id="twentyseventeen-skip-link-focus-fix-js"></script>
|
<script src="/wp-content/themes/twentyseventeen/assets/js/skip-link-focus-fix.js?ver=20161114" id="twentyseventeen-skip-link-focus-fix-js"></script>
|
||||||
<script src="/wp-content/themes/twentyseventeen/assets/js/global.js?ver=20211130" id="twentyseventeen-global-js"></script>
|
<script src="/wp-content/themes/twentyseventeen/assets/js/global.js?ver=20211130" id="twentyseventeen-global-js"></script>
|
||||||
<script src="/wp-content/themes/twentyseventeen/assets/js/jquery.scrollTo.js?ver=2.1.3" id="jquery-scrollto-js"></script>`
|
<script src="/wp-content/themes/twentyseventeen/assets/js/jquery.scrollTo.js?ver=2.1.3" id="jquery-scrollto-js"></script>`
|
||||||
|
|
||||||
|
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("<svg class=\"icon icon-", m["icon"], "\"", ariaHidden, ariaLabelledby, " role=\"img\">")
|
||||||
|
if m["title"] != "" {
|
||||||
|
s.WriteString(`<title id="title-`, uniqueId, `">`, m["title"], "</title>")
|
||||||
|
if m["desc"] != "" {
|
||||||
|
s.WriteString(`<desc id="desc-`, uniqueId, `">`, m["desc"], `</desc>`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s.WriteString(` <use href="#icon-`, m["icon"], `" xlink:href="#icon-`, m["icon"], `"></use> `)
|
||||||
|
if m["fallback"] != "" {
|
||||||
|
s.WriteString(`<span class="svg-fallback icon-' . esc_attr( $args['icon'] ) . '"></span>`)
|
||||||
|
}
|
||||||
|
s.WriteString(`<span class="svg-fallback icon-`, m["icon"], `"></span></svg>`)
|
||||||
|
return s.String()
|
||||||
|
}
|
||||||
|
7
app/theme/wp/scriptloader/defaultstyles.go
Normal file
7
app/theme/wp/scriptloader/defaultstyles.go
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package scriptloader
|
||||||
|
|
||||||
|
import "github.com/fthvgb1/wp-go/safety"
|
||||||
|
|
||||||
|
func defaultStyles(m *safety.Map[string, *Script], suffix string) {
|
||||||
|
|
||||||
|
}
|
62
app/theme/wp/scriptloader/head.go
Normal file
62
app/theme/wp/scriptloader/head.go
Normal file
@ -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))
|
||||||
|
}
|
||||||
|
}
|
@ -21,14 +21,18 @@ import (
|
|||||||
"strings"
|
"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"
|
suffix := ".min"
|
||||||
defaultScripts(m, suffix)
|
defaultScripts(m, suffix)
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
func addScript(handle string, src string, deps []string, ver string, args any) {
|
func addScript(handle string, src string, deps []string, ver string, args any) {
|
||||||
script := NewScript(handle, src, deps, ver, args)
|
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 {
|
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 {
|
func getData(handle, key string) string {
|
||||||
h, ok := scripts.Load(handle)
|
h, ok := __scripts.Load(handle)
|
||||||
if !ok {
|
if !ok {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
return strings.Join(h.Extra[key], "\n")
|
return strings.Join(h.Extra[key], "\n")
|
||||||
}
|
}
|
||||||
func GetData(h *wp.Handle, handle, key string) string {
|
func GetData(h *wp.Handle, handle, key string) string {
|
||||||
hh, ok := scripts.Load(handle)
|
hh, ok := __scripts.Load(handle)
|
||||||
if !ok {
|
if !ok {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
@ -71,8 +75,14 @@ func GetData(h *wp.Handle, handle, key string) string {
|
|||||||
return strings.Join(d, "\n")
|
return strings.Join(d, "\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
func AddData(handle, key, data string) {
|
func AddData(handle, key, data string, t ...int) {
|
||||||
s, ok := scripts.Load(handle)
|
var s *Script
|
||||||
|
var ok bool
|
||||||
|
if t != nil {
|
||||||
|
s, ok = __styles.Load(handle)
|
||||||
|
} else {
|
||||||
|
s, ok = __scripts.Load(handle)
|
||||||
|
}
|
||||||
if !ok {
|
if !ok {
|
||||||
s = NewScript(handle, "", nil, "", nil)
|
s = NewScript(handle, "", nil, "", nil)
|
||||||
}
|
}
|
||||||
@ -96,7 +106,7 @@ func AddInlineStyle(handle, data string) {
|
|||||||
if handle == "" || data == "" {
|
if handle == "" || data == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
AddData(handle, "after", data)
|
AddData(handle, "after", data, style)
|
||||||
}
|
}
|
||||||
|
|
||||||
func InlineScripts(handle, position string, display bool) string {
|
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) {
|
func AddScript(handle string, src string, deps []string, ver string, args any) {
|
||||||
script := NewScript(handle, src, deps, ver, args)
|
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 {
|
type scriptQueue struct {
|
||||||
Register map[string]struct{}
|
Register map[string]struct{}
|
||||||
@ -134,7 +150,7 @@ func EnqueueStyle(handle, src string, deps []string, ver, media string) {
|
|||||||
if src != "" {
|
if src != "" {
|
||||||
AddScript(h[0], src, deps, ver, media)
|
AddScript(h[0], src, deps, ver, media)
|
||||||
}
|
}
|
||||||
enqueue(handle)
|
enqueue(handle, style)
|
||||||
}
|
}
|
||||||
func EnqueueStyles(handle, src string, deps []string, ver, media string) {
|
func EnqueueStyles(handle, src string, deps []string, ver, media string) {
|
||||||
if src != "" {
|
if src != "" {
|
||||||
@ -150,7 +166,7 @@ func EnqueueScript(handle, src string, deps []string, ver string, inFooter bool)
|
|||||||
if inFooter {
|
if inFooter {
|
||||||
AddData(h[0], "group", "1")
|
AddData(h[0], "group", "1")
|
||||||
}
|
}
|
||||||
enqueue(handle)
|
enqueue(handle, script)
|
||||||
}
|
}
|
||||||
func EnqueueScripts(handle, src string, deps []string, ver string, inFooter bool) {
|
func EnqueueScripts(handle, src string, deps []string, ver string, inFooter bool) {
|
||||||
if src != "" {
|
if src != "" {
|
||||||
@ -159,14 +175,18 @@ func EnqueueScripts(handle, src string, deps []string, ver string, inFooter bool
|
|||||||
EnqueueScript(handle, src, deps, ver, inFooter)
|
EnqueueScript(handle, src, deps, ver, inFooter)
|
||||||
}
|
}
|
||||||
|
|
||||||
func enqueue(handle string) {
|
func enqueue(handle string, t int) {
|
||||||
h := strings.Split(handle, "?")
|
h := strings.Split(handle, "?")
|
||||||
if slice.IsContained(scriptQueues.Queue, h[0]) && maps.IsExists(scriptQueues.Register, h[0]) {
|
ss := styleQueues.Load()
|
||||||
scriptQueues.Queue = append(scriptQueues.Queue, h[0])
|
if t == 1 {
|
||||||
} else if maps.IsExists(scriptQueues.Register, h[0]) {
|
ss = scriptQueues.Load()
|
||||||
scriptQueues.queuedBeforeRegister[h[0]] = ""
|
}
|
||||||
|
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 {
|
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) {
|
func SetTranslation(handle, domain, path string) {
|
||||||
hh, ok := scripts.Load(handle)
|
hh, ok := __scripts.Load(handle)
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user