Skip to content

Commit

Permalink
🎨 Add template type column to Attribute View #8766
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Oct 1, 2023
1 parent b4bded4 commit 4fdd0dd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 26 deletions.
4 changes: 2 additions & 2 deletions kernel/av/av.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,8 @@ type ValueTemplate struct {
Content string `json:"content"`
}

func (t *ValueTemplate) Render(blockID string, r func(blockID string) string) {
t.Content = r(blockID)
func (t *ValueTemplate) Render(blockID, tplContent string, r func(blockID, tplContent string) string) {
t.Content = r(blockID, tplContent)
}

// View 描述了视图的结构。
Expand Down
55 changes: 31 additions & 24 deletions kernel/model/attribute_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,28 @@ type BlockAttributeViewKeys struct {
KeyValues []*av.KeyValues `json:"keyValues"`
}

func renderTemplateCol(blockID, tplContent string) string {
funcMap := sprig.TxtFuncMap()
goTpl := template.New("").Delims(".action{", "}")
tplContent = strings.ReplaceAll(tplContent, ".custom-", ".custom_") // 模板中的属性名不允许包含 - 字符,因此这里需要替换
tpl, tplErr := goTpl.Funcs(funcMap).Parse(tplContent)
if nil != tplErr {
logging.LogWarnf("parse template [%s] failed: %s", tplContent, tplErr)
return ""
}

buf := &bytes.Buffer{}
ial := GetBlockAttrs(blockID)
dataModel := map[string]string{} // 复制一份 IAL 以避免修改原始数据
for k, v := range ial {
dataModel[strings.ReplaceAll(k, "custom-", "custom_")] = v
}
if err := tpl.Execute(buf, dataModel); nil != err {
logging.LogWarnf("execute template [%s] failed: %s", tplContent, err)
}
return buf.String()
}

func GetBlockAttributeViewKeys(blockID string) (ret []*BlockAttributeViewKeys) {
waitForSyncingStorages()

Expand Down Expand Up @@ -74,6 +96,14 @@ func GetBlockAttributeViewKeys(blockID string) (ret []*BlockAttributeViewKeys) {
}
}

if av.KeyTypeTemplate == kValues.Key.Type {
// 渲染模板列
content := renderTemplateCol(blockID, kValues.Key.Template)
if "<no value>" != content {
kValues.Values = append(kValues.Values, &av.Value{ID: ast.NewNodeID(), KeyID: kValues.Key.ID, BlockID: blockID, Type: av.KeyTypeTemplate, Template: &av.ValueTemplate{Content: content}})
}
}

if 0 < len(kValues.Values) {
keyValues = append(keyValues, kValues)
}
Expand Down Expand Up @@ -233,31 +263,8 @@ func renderAttributeViewTable(attrView *av.AttributeView, view *av.View) (ret *a

// 渲染模板列
if av.KeyTypeTemplate == tableCell.ValueType {
render := func(blockID string) string {
funcMap := sprig.TxtFuncMap()
goTpl := template.New("").Delims(".action{", "}")
tplContent := col.Template
tplContent = strings.ReplaceAll(tplContent, ".custom-", ".custom_") // 模板中的属性名不允许包含 - 字符,因此这里需要替换
tpl, tplErr := goTpl.Funcs(funcMap).Parse(tplContent)
if nil != tplErr {
logging.LogWarnf("parse template [%s] failed: %s", tplContent, tplErr)
return ""
}

buf := &bytes.Buffer{}
ial := GetBlockAttrs(blockID)
dataModel := map[string]string{} // 复制一份 IAL 以避免修改原始数据
for k, v := range ial {
dataModel[strings.ReplaceAll(k, "custom-", "custom_")] = v
}
if err = tpl.Execute(buf, dataModel); nil != err {
logging.LogWarnf("execute template [%s] failed: %s", tplContent, err)
}
return buf.String()
}

tableCell.Value = &av.Value{ID: tableCell.ID, KeyID: col.ID, BlockID: rowID, Type: av.KeyTypeTemplate, Template: &av.ValueTemplate{}}
tableCell.Value.Template.Render(tableCell.Value.BlockID, render)
tableCell.Value.Template.Render(tableCell.Value.BlockID, col.Template, renderTemplateCol)
}

tableRow.Cells = append(tableRow.Cells, tableCell)
Expand Down

0 comments on commit 4fdd0dd

Please sign in to comment.