Merge pull request #749 from trheyi/main

Refactor component attribute handling to support spread operator and real-time rendering mode
This commit is contained in:
Max 2024-09-10 09:47:08 +08:00 committed by GitHub
commit 0fc136a883
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
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>")
}
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 {
return fmt.Sprintf("<span class='sui-render-error'> %s </span>", err.Error())
}
@ -77,7 +89,7 @@ func Render(process *process.Process) interface{} {
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))
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()
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()
if err != nil {
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 root = document.body.getAttribute("s:public") || "";
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 payload = { name, data: _data, option };
@ -409,6 +410,7 @@ export type RenderOption = {
showLoader?: HTMLElement | string | boolean; // default is false
replace?: boolean; // default is true
withPageData?: boolean; // default is false
component?: string; // default is empty
};
export type ComponentState = {