Skip to content

Commit

Permalink
Merge pull request #667 from trheyi/main
Browse files Browse the repository at this point in the history
refactor: Update namespace handling in SUI page builder
  • Loading branch information
trheyi authored Jul 7, 2024
2 parents a8d676e + 78d8d9f commit 44a1774
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 16 deletions.
9 changes: 8 additions & 1 deletion sui/core/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ func (page *Page) Build(ctx *BuildContext, option *BuildOption) (*goquery.Docume
}

ctx.sequence++

namespace := Namespace(page.Name, ctx.sequence, option.ScriptMinify)
page.namespace = namespace

html, err := page.BuildHTML(option)
if err != nil {
ctx.warnings = append(ctx.warnings, err.Error())
Expand All @@ -48,14 +52,14 @@ func (page *Page) Build(ctx *BuildContext, option *BuildOption) (*goquery.Docume
if err != nil {
return nil, ctx.warnings, err
}
doc.Find("body").SetAttr("s:ns", namespace)

err = page.buildComponents(doc, ctx, option)
if err != nil {
return nil, ctx.warnings, err
}

// Scripts
namespace := Namespace(page.Name, ctx.sequence, option.ScriptMinify)
scripts, err := page.BuildScripts(ctx, option, "__page", namespace)
if err != nil {
return nil, ctx.warnings, err
Expand Down Expand Up @@ -101,10 +105,12 @@ func (page *Page) BuildAsComponent(sel *goquery.Selection, ctx *BuildContext, op

namespace := Namespace(name, ctx.sequence, option.ScriptMinify)
component := ComponentName(name, option.ScriptMinify)
page.namespace = namespace
attrs := []html.Attribute{
{Key: "s:ns", Val: namespace},
{Key: "s:cn", Val: component},
{Key: "s:ready", Val: component + "()"},
{Key: "s:parent", Val: page.parent.namespace},
}

ctx.sequence++
Expand Down Expand Up @@ -264,6 +270,7 @@ func (page *Page) buildComponents(doc *goquery.Document, ctx *BuildContext, opti
// Check if Just-In-Time Component ( "is" has variable )
if ctx.isJitComponent(name) {
sel.SetAttr("s:jit", "true")
sel.SetAttr("s:parent", page.namespace)
sel.SetAttr("s:root", public.Root)
ctx.addJitComponent(name)
return
Expand Down
66 changes: 51 additions & 15 deletions sui/core/jit.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,47 @@ func (parser *TemplateParser) isComponent(sel *goquery.Selection) bool {
}

func (parser *TemplateParser) componentProps(sel *goquery.Selection) (map[string]interface{}, error) {

parentProps := map[string]string{}
props := map[string]string{}
parentSel := sel.Parent()
if parentSel == nil {
return map[string]interface{}{}, nil
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 {

if !strings.HasPrefix(attr.Key, "s:prop") {
continue
}
key := strings.TrimPrefix(attr.Key, "s:prop:")
parentProps[key] = attr.Val
}
}

for _, attr := range parentSel.Nodes[0].Attr {
if !strings.HasPrefix(attr.Key, "s:prop") {
for _, attr := range sel.Nodes[0].Attr {
if strings.HasPrefix(attr.Key, "s:") || attr.Key == "is" {
continue
}
key := strings.TrimPrefix(attr.Key, "s:prop:")
props[key] = attr.Val

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)
Expand All @@ -164,7 +193,7 @@ func (parser *TemplateParser) parseComponentProps(props map[string]string) (map[
result := map[string]interface{}{}
for key, val := range props {
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 {
return nil, err
}
Expand All @@ -173,17 +202,24 @@ func (parser *TemplateParser) parseComponentProps(props map[string]string) (map[
continue
}

for k, v := range values.(map[string]interface{}) {
result[k] = v
if _, ok := values.(map[string]interface{}); ok {
for k, v := range values.(map[string]interface{}) {
result[k] = v
}
}

continue
}
value, err := parser.data.Exec(val)
if err != nil {
return nil, err

if strings.HasPrefix(val, "{{") && strings.HasSuffix(val, "}}") {
value, err := parser.data.Exec(val)
if err != nil {
return map[string]interface{}{}, err
}
result[key] = value
continue
}
result[key] = value

result[key] = val
}
return result, nil
}
Expand Down
1 change: 1 addition & 0 deletions sui/core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type Page struct {
Attrs map[string]string `json:"-"`
Attributes []html.Attribute `json:"-"`
Translations []Translation `json:"-"` // will be deprecated
namespace string `json:"-"`
parent *Page `json:"-"`
}

Expand Down

0 comments on commit 44a1774

Please sign in to comment.