From dc3c637c5ed3b5b41b2f6f10d36ec42216eec2e3 Mon Sep 17 00:00:00 2001 From: Max Date: Mon, 29 Jul 2024 16:46:21 +0800 Subject: [PATCH] Refactor component event injection to handle missing event selectors --- sui/core/build.go | 4 ++-- sui/core/injections.go | 16 +++++++++------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/sui/core/build.go b/sui/core/build.go index 8f76f8a0..181e0e8b 100644 --- a/sui/core/build.go +++ b/sui/core/build.go @@ -684,7 +684,7 @@ func (page *Page) BuildScripts(ctx *BuildContext, option *BuildOption, component arguments := "document.body" if !ispage { - arguments = "arguments[0]" + arguments = "component" } injectScript := componentInitScript(arguments) @@ -754,7 +754,7 @@ func (page *Page) BuildScripts(ctx *BuildContext, option *BuildOption, component parent := "body" if !ispage { parent = "head" - code = fmt.Sprintf("function %s(){\n%s\n}\n", component, addTabToEachLine(code)) + code = fmt.Sprintf("function %s( component ){\n%s\n}\n", component, addTabToEachLine(code)) } scripts = append(scripts, ScriptNode{ diff --git a/sui/core/injections.go b/sui/core/injections.go index 3b3cd5cc..c532e2c8 100644 --- a/sui/core/injections.go +++ b/sui/core/injections.go @@ -216,12 +216,14 @@ const pageEventScriptTmpl = ` ` const compEventScriptTmpl = ` - document.querySelector("[s\\:event=%s]").addEventListener("%s", function (event) { - const dataKeys = %s; - const jsonKeys = %s; - handler = new %s(this).%s; - __sui_event_handler(event, dataKeys, jsonKeys, this, handler); - }); + if (document.querySelector("[s\\:event=%s]")) { + document.querySelector("[s\\:event=%s]").addEventListener("%s", function (event) { + const dataKeys = %s; + const jsonKeys = %s; + handler = new %s(this).%s; + __sui_event_handler(event, dataKeys, jsonKeys, this, handler); + }); + } ` const componentInitScriptTmpl = ` @@ -262,7 +264,7 @@ func pageEventInjectScript(eventID, eventName, dataKeys, jsonKeys, handler strin } func compEventInjectScript(eventID, eventName, component, dataKeys, jsonKeys, handler string) string { - return fmt.Sprintf(compEventScriptTmpl, eventID, eventName, dataKeys, jsonKeys, component, handler) + return fmt.Sprintf(compEventScriptTmpl, eventID, eventID, eventName, dataKeys, jsonKeys, component, handler) } func componentInitScript(root string) string {