🎨 Database table view template columns support reading relation, rollup, created and updated columns https://github.com/siyuan-note/siyuan/issues/10442

This commit is contained in:
Daniel 2024-02-26 09:18:44 +08:00
parent eb5124b122
commit 45bfaefb18
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
2 changed files with 79 additions and 25 deletions

View file

@ -860,19 +860,10 @@ 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 {
switch cell.ValueType {
case av.KeyTypeTemplate: // 渲染模板列
keyValues := rows[row.ID]
ial := map[string]string{}
block := row.GetBlockValue()
if nil != block && !block.IsDetached {
ial = GetBlockAttrsWithoutWaitWriting(row.ID)
}
content := renderTemplateCol(ial, cell.Value.Template.Content, keyValues)
cell.Value.Template.Content = content
case av.KeyTypeRollup: // 渲染汇总列
rollupKey, _ := attrView.GetKey(cell.Value.KeyID)
if nil == rollupKey || nil == rollupKey.Rollup {
@ -913,6 +904,12 @@ func renderAttributeViewTable(attrView *av.AttributeView, view *av.View) (ret *a
}
cell.Value.Rollup.RenderContents(rollupKey.Rollup.Calc, destKey)
// 将汇总列的值保存到 rows 中,后续渲染模板列的时候会用到,下同
// Database table view template columns support reading relation, rollup, created and updated columns https://github.com/siyuan-note/siyuan/issues/10442
keyValues := rows[row.ID]
keyValues = append(keyValues, &av.KeyValues{Key: rollupKey, Values: []*av.Value{{ID: cell.Value.ID, KeyID: rollupKey.ID, BlockID: row.ID, Type: av.KeyTypeRollup, Rollup: cell.Value.Rollup}}})
rows[row.ID] = keyValues
case av.KeyTypeRelation: // 渲染关联列
relKey, _ := attrView.GetKey(cell.Value.KeyID)
if nil != relKey && nil != relKey.Relation {
@ -927,6 +924,10 @@ func renderAttributeViewTable(attrView *av.AttributeView, view *av.View) (ret *a
}
}
}
keyValues := rows[row.ID]
keyValues = append(keyValues, &av.KeyValues{Key: relKey, Values: []*av.Value{{ID: cell.Value.ID, KeyID: relKey.ID, BlockID: row.ID, Type: av.KeyTypeRelation, Relation: cell.Value.Relation}}})
rows[row.ID] = keyValues
case av.KeyTypeCreated: // 渲染创建时间
createdStr := row.ID[:len("20060102150405")]
created, parseErr := time.ParseInLocation("20060102150405", createdStr, time.Local)
@ -936,6 +937,11 @@ func renderAttributeViewTable(attrView *av.AttributeView, view *av.View) (ret *a
} else {
cell.Value.Created = av.NewFormattedValueCreated(time.Now().UnixMilli(), 0, av.CreatedFormatNone)
}
keyValues := rows[row.ID]
createdKey, _ := attrView.GetKey(cell.Value.KeyID)
keyValues = append(keyValues, &av.KeyValues{Key: createdKey, Values: []*av.Value{{ID: cell.Value.ID, KeyID: createdKey.ID, BlockID: row.ID, Type: av.KeyTypeCreated, Created: cell.Value.Created}}})
rows[row.ID] = keyValues
case av.KeyTypeUpdated: // 渲染更新时间
ial := map[string]string{}
block := row.GetBlockValue()
@ -955,6 +961,29 @@ func renderAttributeViewTable(attrView *av.AttributeView, view *av.View) (ret *a
cell.Value.Updated = av.NewFormattedValueUpdated(time.Now().UnixMilli(), 0, av.UpdatedFormatNone)
}
}
keyValues := rows[row.ID]
updatedKey, _ := attrView.GetKey(cell.Value.KeyID)
keyValues = append(keyValues, &av.KeyValues{Key: updatedKey, Values: []*av.Value{{ID: cell.Value.ID, KeyID: updatedKey.ID, BlockID: row.ID, Type: av.KeyTypeUpdated, Updated: cell.Value.Updated}}})
rows[row.ID] = keyValues
}
}
}
// 最后单独渲染模板列,这样模板列就可以使用汇总、关联、创建时间和更新时间列的值了
// Database table view template columns support reading relation, rollup, created and updated columns https://github.com/siyuan-note/siyuan/issues/10442
for _, row := range ret.Rows {
for _, cell := range row.Cells {
switch cell.ValueType {
case av.KeyTypeTemplate: // 渲染模板列
keyValues := rows[row.ID]
ial := map[string]string{}
block := row.GetBlockValue()
if nil != block && !block.IsDetached {
ial = GetBlockAttrsWithoutWaitWriting(row.ID)
}
content := renderTemplateCol(ial, cell.Value.Template.Content, keyValues)
cell.Value.Template.Content = content
}
}
}
@ -1658,7 +1687,7 @@ func addAttributeViewBlock(avID, previousBlockID, blockID string, isDetached boo
if 0 < len(viewable.Rows) {
row := GetLastSortRow(viewable.Rows)
if nil != row {
for affectKeyID, _ := range affectKeyIDs {
for affectKeyID := range affectKeyIDs {
for _, cell := range row.Cells {
if nil != cell.Value && cell.Value.KeyID == affectKeyID {
if av.KeyTypeBlock == cell.ValueType {
@ -1681,7 +1710,7 @@ func addAttributeViewBlock(avID, previousBlockID, blockID string, isDetached boo
}
notAddedValues := map[string]bool{}
for affectKeyID, _ := range affectKeyIDs {
for affectKeyID := range affectKeyIDs {
if !addedValues[affectKeyID] {
notAddedValues[affectKeyID] = true
break

View file

@ -740,22 +740,10 @@ 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 {
switch cell.ValueType {
case av.KeyTypeTemplate: // 渲染模板列
keyValues := rows[row.ID]
ial := map[string]string{}
block := row.GetBlockValue()
if nil != block && !block.IsDetached {
ial = cache.GetBlockIAL(row.ID)
if nil == ial {
ial = map[string]string{}
}
}
content := renderTemplateCol(ial, cell.Value.Template.Content, keyValues)
cell.Value.Template.Content = content
case av.KeyTypeRollup: // 渲染汇总列
rollupKey, _ := attrView.GetKey(cell.Value.KeyID)
if nil == rollupKey || nil == rollupKey.Rollup {
@ -796,6 +784,12 @@ func renderAttributeViewTable(attrView *av.AttributeView, view *av.View) (ret *a
}
cell.Value.Rollup.RenderContents(rollupKey.Rollup.Calc, destKey)
// 将汇总列的值保存到 rows 中,后续渲染模板列的时候会用到,下同
// Database table view template columns support reading relation, rollup, created and updated columns https://github.com/siyuan-note/siyuan/issues/10442
keyValues := rows[row.ID]
keyValues = append(keyValues, &av.KeyValues{Key: rollupKey, Values: []*av.Value{{ID: cell.Value.ID, KeyID: rollupKey.ID, BlockID: row.ID, Type: av.KeyTypeRollup, Rollup: cell.Value.Rollup}}})
rows[row.ID] = keyValues
case av.KeyTypeRelation: // 渲染关联列
relKey, _ := attrView.GetKey(cell.Value.KeyID)
if nil != relKey && nil != relKey.Relation {
@ -819,6 +813,11 @@ func renderAttributeViewTable(attrView *av.AttributeView, view *av.View) (ret *a
} else {
cell.Value.Created = av.NewFormattedValueCreated(time.Now().UnixMilli(), 0, av.CreatedFormatNone)
}
keyValues := rows[row.ID]
createdKey, _ := attrView.GetKey(cell.Value.KeyID)
keyValues = append(keyValues, &av.KeyValues{Key: createdKey, Values: []*av.Value{{ID: cell.Value.ID, KeyID: createdKey.ID, BlockID: row.ID, Type: av.KeyTypeCreated, Created: cell.Value.Created}}})
rows[row.ID] = keyValues
case av.KeyTypeUpdated: // 渲染更新时间
ial := map[string]string{}
block := row.GetBlockValue()
@ -841,6 +840,32 @@ func renderAttributeViewTable(attrView *av.AttributeView, view *av.View) (ret *a
cell.Value.Updated = av.NewFormattedValueUpdated(time.Now().UnixMilli(), 0, av.UpdatedFormatNone)
}
}
keyValues := rows[row.ID]
updatedKey, _ := attrView.GetKey(cell.Value.KeyID)
keyValues = append(keyValues, &av.KeyValues{Key: updatedKey, Values: []*av.Value{{ID: cell.Value.ID, KeyID: updatedKey.ID, BlockID: row.ID, Type: av.KeyTypeUpdated, Updated: cell.Value.Updated}}})
rows[row.ID] = keyValues
}
}
}
// 最后单独渲染模板列,这样模板列就可以使用汇总、关联、创建时间和更新时间列的值了
// Database table view template columns support reading relation, rollup, created and updated columns https://github.com/siyuan-note/siyuan/issues/10442
for _, row := range ret.Rows {
for _, cell := range row.Cells {
switch cell.ValueType {
case av.KeyTypeTemplate: // 渲染模板列
keyValues := rows[row.ID]
ial := map[string]string{}
block := row.GetBlockValue()
if nil != block && !block.IsDetached {
ial = cache.GetBlockIAL(row.ID)
if nil == ial {
ial = map[string]string{}
}
}
content := renderTemplateCol(ial, cell.Value.Template.Content, keyValues)
cell.Value.Template.Content = content
}
}
}