refactor: Update namespace handling in SUI page builder

This commit is contained in:
Max 2024-07-08 02:02:03 +08:00
parent 7ea9953b69
commit 78d8d9fbc2
3 changed files with 60 additions and 16 deletions

View file

@ -39,6 +39,10 @@ func (page *Page) Build(ctx *BuildContext, option *BuildOption) (*goquery.Docume
} }
ctx.sequence++ ctx.sequence++
namespace := Namespace(page.Name, ctx.sequence, option.ScriptMinify)
page.namespace = namespace
html, err := page.BuildHTML(option) html, err := page.BuildHTML(option)
if err != nil { if err != nil {
ctx.warnings = append(ctx.warnings, err.Error()) ctx.warnings = append(ctx.warnings, err.Error())
@ -48,6 +52,7 @@ func (page *Page) Build(ctx *BuildContext, option *BuildOption) (*goquery.Docume
if err != nil { if err != nil {
return nil, ctx.warnings, err return nil, ctx.warnings, err
} }
doc.Find("body").SetAttr("s:ns", namespace)
err = page.buildComponents(doc, ctx, option) err = page.buildComponents(doc, ctx, option)
if err != nil { if err != nil {
@ -55,7 +60,6 @@ func (page *Page) Build(ctx *BuildContext, option *BuildOption) (*goquery.Docume
} }
// Scripts // Scripts
namespace := Namespace(page.Name, ctx.sequence, option.ScriptMinify)
scripts, err := page.BuildScripts(ctx, option, "__page", namespace) scripts, err := page.BuildScripts(ctx, option, "__page", namespace)
if err != nil { if err != nil {
return nil, ctx.warnings, err return nil, ctx.warnings, err
@ -101,10 +105,12 @@ func (page *Page) BuildAsComponent(sel *goquery.Selection, ctx *BuildContext, op
namespace := Namespace(name, ctx.sequence, option.ScriptMinify) namespace := Namespace(name, ctx.sequence, option.ScriptMinify)
component := ComponentName(name, option.ScriptMinify) component := ComponentName(name, option.ScriptMinify)
page.namespace = namespace
attrs := []html.Attribute{ attrs := []html.Attribute{
{Key: "s:ns", Val: namespace}, {Key: "s:ns", Val: namespace},
{Key: "s:cn", Val: component}, {Key: "s:cn", Val: component},
{Key: "s:ready", Val: component + "()"}, {Key: "s:ready", Val: component + "()"},
{Key: "s:parent", Val: page.parent.namespace},
} }
ctx.sequence++ ctx.sequence++
@ -264,6 +270,7 @@ func (page *Page) buildComponents(doc *goquery.Document, ctx *BuildContext, opti
// Check if Just-In-Time Component ( "is" has variable ) // Check if Just-In-Time Component ( "is" has variable )
if ctx.isJitComponent(name) { if ctx.isJitComponent(name) {
sel.SetAttr("s:jit", "true") sel.SetAttr("s:jit", "true")
sel.SetAttr("s:parent", page.namespace)
sel.SetAttr("s:root", public.Root) sel.SetAttr("s:root", public.Root)
ctx.addJitComponent(name) ctx.addJitComponent(name)
return return

View file

@ -143,18 +143,47 @@ func (parser *TemplateParser) isComponent(sel *goquery.Selection) bool {
} }
func (parser *TemplateParser) componentProps(sel *goquery.Selection) (map[string]interface{}, error) { func (parser *TemplateParser) componentProps(sel *goquery.Selection) (map[string]interface{}, error) {
props := map[string]string{}
parentSel := sel.Parent()
if parentSel == nil {
return map[string]interface{}{}, nil
}
parentProps := map[string]string{}
props := map[string]string{}
parent := sel.AttrOr("s:parent", "")
parentSel := sel.Parents().Find(fmt.Sprintf(`[s\:ns="%s"]`, parent))
if parentSel != nil && parentSel.Length() > 0 {
for _, attr := range parentSel.Nodes[0].Attr { for _, attr := range parentSel.Nodes[0].Attr {
if !strings.HasPrefix(attr.Key, "s:prop") { if !strings.HasPrefix(attr.Key, "s:prop") {
continue continue
} }
key := strings.TrimPrefix(attr.Key, "s:prop:") key := strings.TrimPrefix(attr.Key, "s:prop:")
props[key] = attr.Val parentProps[key] = attr.Val
}
}
for _, attr := range sel.Nodes[0].Attr {
if strings.HasPrefix(attr.Key, "s:") || attr.Key == "is" {
continue
}
if strings.HasPrefix(attr.Key, "...$props") {
data := Data{"$props": parentProps}
values, err := data.Exec(fmt.Sprintf("{{ %s }}", strings.TrimPrefix(attr.Key, "...")))
if err != nil {
return map[string]interface{}{}, err
}
if values == nil {
continue
}
if _, ok := values.(map[string]string); ok {
for key, val := range values.(map[string]string) {
props[key] = val
}
}
continue
}
props[attr.Key] = attr.Val
} }
return parser.parseComponentProps(props) return parser.parseComponentProps(props)
@ -164,7 +193,7 @@ func (parser *TemplateParser) parseComponentProps(props map[string]string) (map[
result := map[string]interface{}{} result := map[string]interface{}{}
for key, val := range props { for key, val := range props {
if strings.HasPrefix(key, "...") { if strings.HasPrefix(key, "...") {
values, err := parser.data.Exec(fmt.Sprintf("{{ %s }}", val)) values, err := parser.data.Exec(fmt.Sprintf("{{ %s }}", strings.TrimPrefix(key, "...")))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -173,17 +202,24 @@ func (parser *TemplateParser) parseComponentProps(props map[string]string) (map[
continue continue
} }
if _, ok := values.(map[string]interface{}); ok {
for k, v := range values.(map[string]interface{}) { for k, v := range values.(map[string]interface{}) {
result[k] = v result[k] = v
} }
}
continue continue
} }
if strings.HasPrefix(val, "{{") && strings.HasSuffix(val, "}}") {
value, err := parser.data.Exec(val) value, err := parser.data.Exec(val)
if err != nil { if err != nil {
return nil, err return map[string]interface{}{}, err
} }
result[key] = value result[key] = value
continue
}
result[key] = val
} }
return result, nil return result, nil
} }

View file

@ -42,6 +42,7 @@ type Page struct {
Attrs map[string]string `json:"-"` Attrs map[string]string `json:"-"`
Attributes []html.Attribute `json:"-"` Attributes []html.Attribute `json:"-"`
Translations []Translation `json:"-"` // will be deprecated Translations []Translation `json:"-"` // will be deprecated
namespace string `json:"-"`
parent *Page `json:"-"` parent *Page `json:"-"`
} }