optimize sui page injection script
This commit is contained in:
parent
265188bed1
commit
b4ed405e4e
4 changed files with 56 additions and 19 deletions
45
sui/core/injections.go
Normal file
45
sui/core/injections.go
Normal file
|
|
@ -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(`<script type="text/javascript">`+initScriptTmpl+`</script>`, jsonRaw, jsPrintData)
|
||||
}
|
||||
|
||||
func headInjectionScript() string {
|
||||
return fmt.Sprintf(`<script type="text/javascript">` + i118nScriptTmpl + `</script>`)
|
||||
}
|
||||
|
|
@ -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"))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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("<script>\n" +
|
||||
"try { " +
|
||||
`var __sui_data = ` + data + ";\n" +
|
||||
"} catch (e) { console.log('init data error:', e); }\n" +
|
||||
|
||||
`document.addEventListener("DOMContentLoaded", function () {` + "\n" +
|
||||
` try {` + "\n" +
|
||||
` __sui_data_ready( __sui_data );` + "\n" +
|
||||
` } catch(e) {}` + "\n" +
|
||||
`});` + "\n" + jsPrintData +
|
||||
|
||||
"</script>\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) {
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue