From b4ed405e4ee802acba21990dbb7ad88c2e326a73 Mon Sep 17 00:00:00 2001 From: Max Date: Sun, 7 Jul 2024 22:34:10 +0800 Subject: [PATCH] optimize sui page injection script --- sui/core/injections.go | 45 ++++++++++++++++++++++++++ sui/core/page_test.go | 2 +- sui/core/parser.go | 26 ++++++--------- sui/storages/local/page_render_test.go | 2 +- 4 files changed, 56 insertions(+), 19 deletions(-) create mode 100644 sui/core/injections.go diff --git a/sui/core/injections.go b/sui/core/injections.go new file mode 100644 index 00000000..1549c867 --- /dev/null +++ b/sui/core/injections.go @@ -0,0 +1,45 @@ +package core + +import "fmt" + +const initScriptTmpl = ` + try { + var __sui_data = %s; + } catch (e) { console.log('init data error:', e); } + + document.addEventListener("DOMContentLoaded", function () { + try { + document.querySelectorAll("[s\\:ready]").forEach(function (element) { + const method = element.getAttribute("s:ready"); + const cn = element.getAttribute("s:cn"); + if (method && typeof window[cn] === "function") { + try { + window[cn](element); + } catch (e) { + const message = e.message || e || "An error occurred"; + console.error(` + "`[SUI] ${cn} Error: ${message}`" + `); + } + } + }); + } catch (e) {} + }); + %s +` + +const i118nScriptTmpl = ` + function L(key) { + return key; + } +` + +func bodyInjectionScript(jsonRaw string, debug bool) string { + jsPrintData := "" + if debug { + jsPrintData = `console.log(__sui_data);` + } + return fmt.Sprintf(``, jsonRaw, jsPrintData) +} + +func headInjectionScript() string { + return fmt.Sprintf(``) +} diff --git a/sui/core/page_test.go b/sui/core/page_test.go index 867cde21..232d5ca6 100644 --- a/sui/core/page_test.go +++ b/sui/core/page_test.go @@ -28,6 +28,6 @@ func TestPageExec(t *testing.T) { res := any.Of(data).Map().Dot() assert.Equal(t, "yes", res.Get("array[3][0].query")) - assert.Equal(t, "Article Search", res.Get("articles.data[0].description")) + assert.Equal(t, "Article Search 1", res.Get("articles.data[0].description")) assert.Equal(t, "/test/path", res.Get("url.path")) } diff --git a/sui/core/parser.go b/sui/core/parser.go index d2e10780..7e6e2d54 100644 --- a/sui/core/parser.go +++ b/sui/core/parser.go @@ -199,10 +199,10 @@ func (parser *TemplateParser) Render(html string) (string, error) { delete(parser.replace, sel) } - // Print the data - jsPrintData := "" - if parser.option != nil && parser.option.Debug { - jsPrintData = "console.log(__sui_data);\n" + // Append the head + head := doc.Find("head") + if head.Length() > 0 { + head.AppendHtml(headInjectionScript()) } // Append the data to the body @@ -212,19 +212,7 @@ func (parser *TemplateParser) Render(html string) (string, error) { if err != nil { data, _ = jsoniter.MarshalToString(map[string]string{"error": err.Error()}) } - body.AppendHtml("\n", - ) + body.AppendHtml(bodyInjectionScript(data, parser.debug())) } // For editor @@ -756,6 +744,10 @@ func (parser *TemplateParser) hasParsed(sel *goquery.Selection) bool { return false } +func (parser *TemplateParser) debug() bool { + return parser.option != nil && parser.option.Debug +} + func (parser *TemplateParser) toArray(value interface{}) ([]interface{}, error) { switch values := value.(type) { diff --git a/sui/storages/local/page_render_test.go b/sui/storages/local/page_render_test.go index 6b9b83d2..20170fe7 100644 --- a/sui/storages/local/page_render_test.go +++ b/sui/storages/local/page_render_test.go @@ -31,7 +31,7 @@ func TestPageEditorRender(t *testing.T) { assert.NotEmpty(t, res.Scripts) assert.NotEmpty(t, res.Styles) assert.GreaterOrEqual(t, len(res.Styles), 1) - assert.GreaterOrEqual(t, len(res.Scripts), 2) + assert.GreaterOrEqual(t, len(res.Scripts), 1) }