From b1cf004958e5c99c82d02a503ad65ef5a25241bd Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Sun, 8 Oct 2023 12:32:37 +0800 Subject: [PATCH] :art: Add created and updated type column to database https://github.com/siyuan-note/siyuan/issues/9371 --- kernel/model/attribute_view.go | 52 +++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go index c4ed522be..ecc752f26 100644 --- a/kernel/model/attribute_view.go +++ b/kernel/model/attribute_view.go @@ -39,7 +39,7 @@ type BlockAttributeViewKeys struct { KeyValues []*av.KeyValues `json:"keyValues"` } -func renderTemplateCol(blockID, tplContent string, rowValues []*av.KeyValues) string { +func renderTemplateCol(ial map[string]string, tplContent string, rowValues []*av.KeyValues) string { funcMap := sprig.TxtFuncMap() goTpl := template.New("").Delims(".action{", "}") tplContent = strings.ReplaceAll(tplContent, ".custom-", ".custom_") // 模板中的属性名不允许包含 - 字符,因此这里需要替换 @@ -50,7 +50,6 @@ func renderTemplateCol(blockID, tplContent string, rowValues []*av.KeyValues) st } buf := &bytes.Buffer{} - ial := GetBlockAttrs(blockID) dataModel := map[string]interface{}{} // 复制一份 IAL 以避免修改原始数据 for k, v := range ial { dataModel[strings.ReplaceAll(k, "custom-", "custom_")] = v @@ -141,7 +140,8 @@ func GetBlockAttributeViewKeys(blockID string) (ret []*BlockAttributeViewKeys) { for _, kv := range keyValues { if av.KeyTypeTemplate == kv.Key.Type { if 0 < len(kv.Values) { - kv.Values[0].Template.Content = renderTemplateCol(blockID, kv.Key.Template, keyValues) + ial := GetBlockAttrs(blockID) + kv.Values[0].Template.Content = renderTemplateCol(ial, kv.Key.Template, keyValues) } } } @@ -309,15 +309,18 @@ func renderAttributeViewTable(attrView *av.AttributeView, view *av.View) (ret *a } tableRow.ID = rowID - // 格式化数字 - if av.KeyTypeNumber == tableCell.ValueType && nil != tableCell.Value && nil != tableCell.Value.Number && tableCell.Value.Number.IsNotEmpty { - tableCell.Value.Number.Format = col.NumberFormat - tableCell.Value.Number.FormatNumber() - } - - // 渲染模板列 - if av.KeyTypeTemplate == tableCell.ValueType { + switch tableCell.ValueType { + case av.KeyTypeNumber: // 格式化数字 + if nil != tableCell.Value && nil != tableCell.Value.Number && tableCell.Value.Number.IsNotEmpty { + tableCell.Value.Number.Format = col.NumberFormat + tableCell.Value.Number.FormatNumber() + } + case av.KeyTypeTemplate: // 渲染模板列 tableCell.Value = &av.Value{ID: tableCell.ID, KeyID: col.ID, BlockID: rowID, Type: av.KeyTypeTemplate, Template: &av.ValueTemplate{Content: col.Template}} + case av.KeyTypeCreated: // 填充创建时间列值,后面再渲染 + tableCell.Value = &av.Value{ID: tableCell.ID, KeyID: col.ID, BlockID: rowID, Type: av.KeyTypeCreated} + case av.KeyTypeUpdated: // 填充更新时间列值,后面再渲染 + tableCell.Value = &av.Value{ID: tableCell.ID, KeyID: col.ID, BlockID: rowID, Type: av.KeyTypeUpdated} } tableRow.Cells = append(tableRow.Cells, tableCell) @@ -325,13 +328,34 @@ func renderAttributeViewTable(attrView *av.AttributeView, view *av.View) (ret *a ret.Rows = append(ret.Rows, &tableRow) } - // 渲染模板列 + // 渲染自动生成的列值,比如模板列、创建时间列和更新时间列 for _, row := range ret.Rows { for _, cell := range row.Cells { - if av.KeyTypeTemplate == cell.ValueType { + switch cell.ValueType { + case av.KeyTypeTemplate: // 渲染模板列 keyValues := rows[row.ID] - content := renderTemplateCol(row.ID, cell.Value.Template.Content, keyValues) + ial := GetBlockAttrs(row.ID) + content := renderTemplateCol(ial, cell.Value.Template.Content, keyValues) cell.Value.Template.Content = content + case av.KeyTypeCreated: // 渲染创建时间 + createdStr := row.ID[:len("20060102150405")] + created, parseErr := time.Parse("20060102150405", createdStr) + if nil == parseErr { + cell.Value.Created = av.NewFormattedValueCreated(created.Unix(), 0, av.CreatedFormatNone) + } else { + logging.LogWarnf("parse created [%s] failed: %s", createdStr, parseErr) + cell.Value.Created = av.NewFormattedValueCreated(time.Now().Unix(), 0, av.CreatedFormatNone) + } + case av.KeyTypeUpdated: // 渲染更新时间 + ial := GetBlockAttrs(row.ID) + updatedStr := ial["updated"] + updated, parseErr := time.Parse("20060102150405", updatedStr) + if nil == parseErr { + cell.Value.Created = av.NewFormattedValueCreated(updated.Unix(), 0, av.CreatedFormatNone) + } else { + logging.LogWarnf("parse updated [%s] failed: %s", updatedStr, parseErr) + cell.Value.Created = av.NewFormattedValueCreated(time.Now().Unix(), 0, av.CreatedFormatNone) + } } } }