Refactor component attribute handling to support spread operator and real-time rendering mode

This commit is contained in:
Max 2024-09-10 09:23:10 +08:00
parent 72542dab79
commit 0a21780473
3 changed files with 85 additions and 62 deletions

File diff suppressed because one or more lines are too long

View file

@ -69,7 +69,19 @@ func Render(process *process.Process) interface{} {
return fmt.Sprintf("<span class='sui-render-error'> Name not found </span>") return fmt.Sprintf("<span class='sui-render-error'> Name not found </span>")
} }
html, err := r.renderHTML(c, name, c.HTML, core.Data(data)) // Get the render option
option := map[string]interface{}{}
if v, ok := payload["option"].(map[string]interface{}); ok {
option = v
}
// Get the component name (optional)
comp := ""
if v, ok := option["component"].(string); ok {
comp = v
}
html, err := r.renderHTML(c, name, comp, c.HTML, core.Data(data))
if err != nil { if err != nil {
return fmt.Sprintf("<span class='sui-render-error'> %s </span>", err.Error()) return fmt.Sprintf("<span class='sui-render-error'> %s </span>", err.Error())
} }
@ -77,7 +89,7 @@ func Render(process *process.Process) interface{} {
return html return html
} }
func (r *Request) renderHTML(c *core.Cache, name string, html string, data core.Data) (string, error) { func (r *Request) renderHTML(c *core.Cache, name string, comp string, html string, data core.Data) (string, error) {
doc, err := core.NewDocument([]byte(html)) doc, err := core.NewDocument([]byte(html))
if err != nil { if err != nil {
@ -111,6 +123,15 @@ func (r *Request) renderHTML(c *core.Cache, name string, html string, data core.
sel.Find("[sui-hide]").Remove() sel.Find("[sui-hide]").Remove()
parser.Tidy(sel) parser.Tidy(sel)
// **** warning ****
// Fix s:event-cn="__page" to s:event-cn="component" for component
// The following code is the temporary solution for the component event
// will be removed in the sui v2 release
if comp != "" {
sel.Find("[s\\:event-cn='__page']").SetAttr("s:event-cn", comp)
}
html, err = sel.Html() html, err = sel.Html()
if err != nil { if err != nil {
return "", fmt.Errorf("Html error: %w", err) return "", fmt.Errorf("Html error: %w", err)

View file

@ -353,6 +353,7 @@ async function __sui_render(
const routeAttr = elm ? elm.getAttribute("s:route") : false; const routeAttr = elm ? elm.getAttribute("s:route") : false;
const root = document.body.getAttribute("s:public") || ""; const root = document.body.getAttribute("s:public") || "";
const route = routeAttr ? `${root}${routeAttr}` : window.location.pathname; const route = routeAttr ? `${root}${routeAttr}` : window.location.pathname;
option.component = (routeAttr && comp.root.getAttribute("s:cn")) || "";
const url = `/api/__yao/sui/v1/render${route}`; const url = `/api/__yao/sui/v1/render${route}`;
const payload = { name, data: _data, option }; const payload = { name, data: _data, option };
@ -409,6 +410,7 @@ export type RenderOption = {
showLoader?: HTMLElement | string | boolean; // default is false showLoader?: HTMLElement | string | boolean; // default is false
replace?: boolean; // default is true replace?: boolean; // default is true
withPageData?: boolean; // default is false withPageData?: boolean; // default is false
component?: string; // default is empty
}; };
export type ComponentState = { export type ComponentState = {