🎨 Add template type column to Attribute View https://github.com/siyuan-note/siyuan/issues/8766

This commit is contained in:
Daniel 2023-10-01 18:37:39 +08:00
parent b4bded40e3
commit 4fdd0ddef0
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
2 changed files with 33 additions and 26 deletions

View file

@ -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 描述了视图的结构。

View file

@ -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()
@ -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)
}
@ -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)