🎨 Add created and updated type column to database https://github.com/siyuan-note/siyuan/issues/9371

This commit is contained in:
Daniel 2023-10-08 12:32:37 +08:00
parent 0b6b729ab5
commit b1cf004958
No known key found for this signature in database
GPG Key ID: 86211BA83DF03017

View File

@ -39,7 +39,7 @@ type BlockAttributeViewKeys struct {
KeyValues []*av.KeyValues `json:"keyValues"` 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() funcMap := sprig.TxtFuncMap()
goTpl := template.New("").Delims(".action{", "}") goTpl := template.New("").Delims(".action{", "}")
tplContent = strings.ReplaceAll(tplContent, ".custom-", ".custom_") // 模板中的属性名不允许包含 - 字符,因此这里需要替换 tplContent = strings.ReplaceAll(tplContent, ".custom-", ".custom_") // 模板中的属性名不允许包含 - 字符,因此这里需要替换
@ -50,7 +50,6 @@ func renderTemplateCol(blockID, tplContent string, rowValues []*av.KeyValues) st
} }
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
ial := GetBlockAttrs(blockID)
dataModel := map[string]interface{}{} // 复制一份 IAL 以避免修改原始数据 dataModel := map[string]interface{}{} // 复制一份 IAL 以避免修改原始数据
for k, v := range ial { for k, v := range ial {
dataModel[strings.ReplaceAll(k, "custom-", "custom_")] = v dataModel[strings.ReplaceAll(k, "custom-", "custom_")] = v
@ -141,7 +140,8 @@ func GetBlockAttributeViewKeys(blockID string) (ret []*BlockAttributeViewKeys) {
for _, kv := range keyValues { for _, kv := range keyValues {
if av.KeyTypeTemplate == kv.Key.Type { if av.KeyTypeTemplate == kv.Key.Type {
if 0 < len(kv.Values) { 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 tableRow.ID = rowID
// 格式化数字 switch tableCell.ValueType {
if av.KeyTypeNumber == tableCell.ValueType && nil != tableCell.Value && nil != tableCell.Value.Number && tableCell.Value.Number.IsNotEmpty { case av.KeyTypeNumber: // 格式化数字
tableCell.Value.Number.Format = col.NumberFormat if nil != tableCell.Value && nil != tableCell.Value.Number && tableCell.Value.Number.IsNotEmpty {
tableCell.Value.Number.FormatNumber() tableCell.Value.Number.Format = col.NumberFormat
} tableCell.Value.Number.FormatNumber()
}
// 渲染模板列 case av.KeyTypeTemplate: // 渲染模板列
if av.KeyTypeTemplate == tableCell.ValueType {
tableCell.Value = &av.Value{ID: tableCell.ID, KeyID: col.ID, BlockID: rowID, Type: av.KeyTypeTemplate, Template: &av.ValueTemplate{Content: col.Template}} 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) 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) ret.Rows = append(ret.Rows, &tableRow)
} }
// 渲染模板列 // 渲染自动生成的列值,比如模板列、创建时间列和更新时间
for _, row := range ret.Rows { for _, row := range ret.Rows {
for _, cell := range row.Cells { for _, cell := range row.Cells {
if av.KeyTypeTemplate == cell.ValueType { switch cell.ValueType {
case av.KeyTypeTemplate: // 渲染模板列
keyValues := rows[row.ID] 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 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)
}
} }
} }
} }