From 4fdd0ddef046cefd8f0149e7198020d2677e9af2 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Sun, 1 Oct 2023 18:37:39 +0800 Subject: [PATCH] :art: Add template type column to Attribute View https://github.com/siyuan-note/siyuan/issues/8766 --- kernel/av/av.go | 4 +-- kernel/model/attribute_view.go | 55 +++++++++++++++++++--------------- 2 files changed, 33 insertions(+), 26 deletions(-) diff --git a/kernel/av/av.go b/kernel/av/av.go index 08e370de17f..3ee8d7186c6 100644 --- a/kernel/av/av.go +++ b/kernel/av/av.go @@ -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 描述了视图的结构。 diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go index d5814b7c50d..e2902272061 100644 --- a/kernel/model/attribute_view.go +++ b/kernel/model/attribute_view.go @@ -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 "" != 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)