mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-16 22:50:13 +01:00
🎨 Database gallery view https://github.com/siyuan-note/siyuan/issues/10414
This commit is contained in:
parent
23c01d0d3f
commit
a72c257227
6 changed files with 163 additions and 272 deletions
|
|
@ -16,6 +16,17 @@
|
||||||
|
|
||||||
package av
|
package av
|
||||||
|
|
||||||
|
// Item 描述了一个项目的接口。
|
||||||
|
// 项目可以表格行、画廊卡片或其他视图类型的实体。
|
||||||
|
type Item interface {
|
||||||
|
|
||||||
|
// GetBlockValue 返回主键的值。
|
||||||
|
GetBlockValue() *Value
|
||||||
|
|
||||||
|
// GetID 返回项目的 ID。
|
||||||
|
GetID() string
|
||||||
|
}
|
||||||
|
|
||||||
// BaseLayout 描述了布局的基础结构。
|
// BaseLayout 描述了布局的基础结构。
|
||||||
type BaseLayout struct {
|
type BaseLayout struct {
|
||||||
Spec int `json:"spec"` // 布局格式版本
|
Spec int `json:"spec"` // 布局格式版本
|
||||||
|
|
|
||||||
|
|
@ -112,6 +112,10 @@ type GalleryFieldValue struct {
|
||||||
*BaseValue
|
*BaseValue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (card *GalleryCard) GetID() string {
|
||||||
|
return card.ID
|
||||||
|
}
|
||||||
|
|
||||||
func (card *GalleryCard) GetBlockValue() (ret *Value) {
|
func (card *GalleryCard) GetBlockValue() (ret *Value) {
|
||||||
for _, v := range card.Values {
|
for _, v := range card.Values {
|
||||||
if KeyTypeBlock == v.ValueType {
|
if KeyTypeBlock == v.ValueType {
|
||||||
|
|
|
||||||
|
|
@ -87,6 +87,10 @@ type TableCell struct {
|
||||||
BgColor string `json:"bgColor"` // 单元格背景颜色
|
BgColor string `json:"bgColor"` // 单元格背景颜色
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (row *TableRow) GetID() string {
|
||||||
|
return row.ID
|
||||||
|
}
|
||||||
|
|
||||||
func (row *TableRow) GetBlockValue() (ret *Value) {
|
func (row *TableRow) GetBlockValue() (ret *Value) {
|
||||||
for _, cell := range row.Cells {
|
for _, cell := range row.Cells {
|
||||||
if KeyTypeBlock == cell.ValueType {
|
if KeyTypeBlock == cell.ValueType {
|
||||||
|
|
|
||||||
141
kernel/sql/av.go
141
kernel/sql/av.go
|
|
@ -22,6 +22,7 @@ import (
|
||||||
"text/template"
|
"text/template"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/88250/lute/ast"
|
||||||
"github.com/siyuan-note/logging"
|
"github.com/siyuan-note/logging"
|
||||||
"github.com/siyuan-note/siyuan/kernel/av"
|
"github.com/siyuan-note/siyuan/kernel/av"
|
||||||
"github.com/siyuan-note/siyuan/kernel/filesys"
|
"github.com/siyuan-note/siyuan/kernel/filesys"
|
||||||
|
|
@ -248,6 +249,146 @@ func fillAttributeViewBaseValue(baseValue *av.BaseValue, fieldID, itemID string,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func fillAttributeViewAutoGeneratedValues(attrView *av.AttributeView, ials map[string]map[string]string, value *av.Value, item av.Item, items map[string][]*av.KeyValues, avCache *map[string]*av.AttributeView) {
|
||||||
|
itemID := item.GetID()
|
||||||
|
|
||||||
|
switch value.Type {
|
||||||
|
case av.KeyTypeBlock: // 对于主键可能需要填充静态锚文本 Database-bound block primary key supports setting static anchor text https://github.com/siyuan-note/siyuan/issues/10049
|
||||||
|
if nil != value.Block {
|
||||||
|
for k, v := range ials[itemID] {
|
||||||
|
if k == av.NodeAttrViewStaticText+"-"+attrView.ID {
|
||||||
|
value.Block.Content = v
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case av.KeyTypeRollup: // 渲染汇总列
|
||||||
|
rollupKey, _ := attrView.GetKey(value.KeyID)
|
||||||
|
if nil == rollupKey || nil == rollupKey.Rollup {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
relKey, _ := attrView.GetKey(rollupKey.Rollup.RelationKeyID)
|
||||||
|
if nil == relKey || nil == relKey.Relation {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
relVal := attrView.GetValue(relKey.ID, itemID)
|
||||||
|
if nil == relVal || nil == relVal.Relation {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
destAv := (*avCache)[relKey.Relation.AvID]
|
||||||
|
if nil == destAv {
|
||||||
|
destAv, _ = av.ParseAttributeView(relKey.Relation.AvID)
|
||||||
|
if nil != destAv {
|
||||||
|
(*avCache)[relKey.Relation.AvID] = destAv
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if nil == destAv {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
destKey, _ := destAv.GetKey(rollupKey.Rollup.KeyID)
|
||||||
|
if nil == destKey {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, blockID := range relVal.Relation.BlockIDs {
|
||||||
|
destVal := destAv.GetValue(rollupKey.Rollup.KeyID, blockID)
|
||||||
|
if nil == destVal {
|
||||||
|
if destAv.ExistBlock(blockID) { // 数据库中存在行但是列值不存在是数据未初始化,这里补一个默认值
|
||||||
|
destVal = av.GetAttributeViewDefaultValue(ast.NewNodeID(), rollupKey.Rollup.KeyID, blockID, destKey.Type)
|
||||||
|
}
|
||||||
|
if nil == destVal {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if av.KeyTypeNumber == destKey.Type {
|
||||||
|
destVal.Number.Format = destKey.NumberFormat
|
||||||
|
destVal.Number.FormatNumber()
|
||||||
|
}
|
||||||
|
|
||||||
|
value.Rollup.Contents = append(value.Rollup.Contents, destVal.Clone())
|
||||||
|
}
|
||||||
|
|
||||||
|
value.Rollup.RenderContents(rollupKey.Rollup.Calc, destKey)
|
||||||
|
|
||||||
|
// 将汇总列的值保存到 rowsValues 中,后续渲染模板列的时候会用到,下同
|
||||||
|
// Database table view template columns support reading relation, rollup, created and updated columns https://github.com/siyuan-note/siyuan/issues/10442
|
||||||
|
keyValues := items[itemID]
|
||||||
|
keyValues = append(keyValues, &av.KeyValues{Key: rollupKey, Values: []*av.Value{{ID: value.ID, KeyID: rollupKey.ID, BlockID: itemID, Type: av.KeyTypeRollup, Rollup: value.Rollup}}})
|
||||||
|
items[itemID] = keyValues
|
||||||
|
case av.KeyTypeRelation: // 渲染关联列
|
||||||
|
relKey, _ := attrView.GetKey(value.KeyID)
|
||||||
|
if nil != relKey && nil != relKey.Relation {
|
||||||
|
destAv := (*avCache)[relKey.Relation.AvID]
|
||||||
|
if nil == destAv {
|
||||||
|
destAv, _ = av.ParseAttributeView(relKey.Relation.AvID)
|
||||||
|
if nil != destAv {
|
||||||
|
(*avCache)[relKey.Relation.AvID] = destAv
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if nil != destAv {
|
||||||
|
blocks := map[string]*av.Value{}
|
||||||
|
blockValues := destAv.GetBlockKeyValues()
|
||||||
|
if nil != blockValues {
|
||||||
|
for _, blockValue := range blockValues.Values {
|
||||||
|
blocks[blockValue.BlockID] = blockValue
|
||||||
|
}
|
||||||
|
for _, blockID := range value.Relation.BlockIDs {
|
||||||
|
if val := blocks[blockID]; nil != val {
|
||||||
|
value.Relation.Contents = append(value.Relation.Contents, val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
keyValues := items[itemID]
|
||||||
|
keyValues = append(keyValues, &av.KeyValues{Key: relKey, Values: []*av.Value{{ID: value.ID, KeyID: relKey.ID, BlockID: itemID, Type: av.KeyTypeRelation, Relation: value.Relation}}})
|
||||||
|
items[itemID] = keyValues
|
||||||
|
case av.KeyTypeCreated: // 渲染创建时间
|
||||||
|
createdStr := itemID[:len("20060102150405")]
|
||||||
|
created, parseErr := time.ParseInLocation("20060102150405", createdStr, time.Local)
|
||||||
|
if nil == parseErr {
|
||||||
|
value.Created = av.NewFormattedValueCreated(created.UnixMilli(), 0, av.CreatedFormatNone)
|
||||||
|
value.Created.IsNotEmpty = true
|
||||||
|
} else {
|
||||||
|
value.Created = av.NewFormattedValueCreated(time.Now().UnixMilli(), 0, av.CreatedFormatNone)
|
||||||
|
}
|
||||||
|
|
||||||
|
keyValues := items[itemID]
|
||||||
|
createdKey, _ := attrView.GetKey(value.KeyID)
|
||||||
|
keyValues = append(keyValues, &av.KeyValues{Key: createdKey, Values: []*av.Value{{ID: value.ID, KeyID: createdKey.ID, BlockID: itemID, Type: av.KeyTypeCreated, Created: value.Created}}})
|
||||||
|
items[itemID] = keyValues
|
||||||
|
case av.KeyTypeUpdated: // 渲染更新时间
|
||||||
|
ial := ials[itemID]
|
||||||
|
if nil == ial {
|
||||||
|
ial = map[string]string{}
|
||||||
|
}
|
||||||
|
block := item.GetBlockValue()
|
||||||
|
updatedStr := ial["updated"]
|
||||||
|
if "" == updatedStr && nil != block {
|
||||||
|
value.Updated = av.NewFormattedValueUpdated(block.Block.Updated, 0, av.UpdatedFormatNone)
|
||||||
|
value.Updated.IsNotEmpty = true
|
||||||
|
} else {
|
||||||
|
updated, parseErr := time.ParseInLocation("20060102150405", updatedStr, time.Local)
|
||||||
|
if nil == parseErr {
|
||||||
|
value.Updated = av.NewFormattedValueUpdated(updated.UnixMilli(), 0, av.UpdatedFormatNone)
|
||||||
|
value.Updated.IsNotEmpty = true
|
||||||
|
} else {
|
||||||
|
value.Updated = av.NewFormattedValueUpdated(time.Now().UnixMilli(), 0, av.UpdatedFormatNone)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
keyValues := items[itemID]
|
||||||
|
updatedKey, _ := attrView.GetKey(value.KeyID)
|
||||||
|
keyValues = append(keyValues, &av.KeyValues{Key: updatedKey, Values: []*av.Value{{ID: value.ID, KeyID: updatedKey.ID, BlockID: itemID, Type: av.KeyTypeUpdated, Updated: value.Updated}}})
|
||||||
|
items[itemID] = keyValues
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func fillAttributeViewNilValue(value *av.Value, typ av.KeyType) {
|
func fillAttributeViewNilValue(value *av.Value, typ av.KeyType) {
|
||||||
value.Type = typ
|
value.Type = typ
|
||||||
switch typ {
|
switch typ {
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/88250/lute"
|
"github.com/88250/lute"
|
||||||
"github.com/88250/lute/ast"
|
"github.com/88250/lute/ast"
|
||||||
|
|
@ -117,140 +116,7 @@ func RenderAttributeViewGallery(attrView *av.AttributeView, view *av.View, query
|
||||||
avCache[attrView.ID] = attrView
|
avCache[attrView.ID] = attrView
|
||||||
for _, card := range ret.Cards {
|
for _, card := range ret.Cards {
|
||||||
for _, value := range card.Values {
|
for _, value := range card.Values {
|
||||||
switch value.ValueType {
|
fillAttributeViewAutoGeneratedValues(attrView, ials, value.Value, card, cardsValues, &avCache)
|
||||||
case av.KeyTypeBlock: // 对于主键可能需要填充静态锚文本 Database-bound block primary key supports setting static anchor text https://github.com/siyuan-note/siyuan/issues/10049
|
|
||||||
if nil != value.Value.Block {
|
|
||||||
for k, v := range ials[card.ID] {
|
|
||||||
if k == av.NodeAttrViewStaticText+"-"+attrView.ID {
|
|
||||||
value.Value.Block.Content = v
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case av.KeyTypeRollup: // 渲染汇总字段
|
|
||||||
rollupKey, _ := attrView.GetKey(value.Value.KeyID)
|
|
||||||
if nil == rollupKey || nil == rollupKey.Rollup {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
relKey, _ := attrView.GetKey(rollupKey.Rollup.RelationKeyID)
|
|
||||||
if nil == relKey || nil == relKey.Relation {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
relVal := attrView.GetValue(relKey.ID, card.ID)
|
|
||||||
if nil == relVal || nil == relVal.Relation {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
destAv := avCache[relKey.Relation.AvID]
|
|
||||||
if nil == destAv {
|
|
||||||
destAv, _ = av.ParseAttributeView(relKey.Relation.AvID)
|
|
||||||
if nil != destAv {
|
|
||||||
avCache[relKey.Relation.AvID] = destAv
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if nil == destAv {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
destKey, _ := destAv.GetKey(rollupKey.Rollup.KeyID)
|
|
||||||
if nil == destKey {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, blockID := range relVal.Relation.BlockIDs {
|
|
||||||
destVal := destAv.GetValue(rollupKey.Rollup.KeyID, blockID)
|
|
||||||
if nil == destVal {
|
|
||||||
if destAv.ExistBlock(blockID) { // 数据库中存在但是值不存在是数据未初始化,这里补一个默认值
|
|
||||||
destVal = av.GetAttributeViewDefaultValue(ast.NewNodeID(), rollupKey.Rollup.KeyID, blockID, destKey.Type)
|
|
||||||
}
|
|
||||||
if nil == destVal {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if av.KeyTypeNumber == destKey.Type {
|
|
||||||
destVal.Number.Format = destKey.NumberFormat
|
|
||||||
destVal.Number.FormatNumber()
|
|
||||||
}
|
|
||||||
|
|
||||||
value.Value.Rollup.Contents = append(value.Value.Rollup.Contents, destVal.Clone())
|
|
||||||
}
|
|
||||||
|
|
||||||
value.Value.Rollup.RenderContents(rollupKey.Rollup.Calc, destKey)
|
|
||||||
|
|
||||||
// 将汇总字段的值保存到 cardsValues 中,后续渲染模板字段的时候会用到,下同
|
|
||||||
keyValues := cardsValues[card.ID]
|
|
||||||
keyValues = append(keyValues, &av.KeyValues{Key: rollupKey, Values: []*av.Value{{ID: value.Value.ID, KeyID: rollupKey.ID, BlockID: card.ID, Type: av.KeyTypeRollup, Rollup: value.Value.Rollup}}})
|
|
||||||
cardsValues[card.ID] = keyValues
|
|
||||||
case av.KeyTypeRelation: // 渲染关联字段
|
|
||||||
relKey, _ := attrView.GetKey(value.Value.KeyID)
|
|
||||||
if nil != relKey && nil != relKey.Relation {
|
|
||||||
destAv := avCache[relKey.Relation.AvID]
|
|
||||||
if nil == destAv {
|
|
||||||
destAv, _ = av.ParseAttributeView(relKey.Relation.AvID)
|
|
||||||
if nil != destAv {
|
|
||||||
avCache[relKey.Relation.AvID] = destAv
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if nil != destAv {
|
|
||||||
blocks := map[string]*av.Value{}
|
|
||||||
blockValues := destAv.GetBlockKeyValues()
|
|
||||||
if nil != blockValues {
|
|
||||||
for _, blockValue := range blockValues.Values {
|
|
||||||
blocks[blockValue.BlockID] = blockValue
|
|
||||||
}
|
|
||||||
for _, blockID := range value.Value.Relation.BlockIDs {
|
|
||||||
if val := blocks[blockID]; nil != val {
|
|
||||||
value.Value.Relation.Contents = append(value.Value.Relation.Contents, val)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
keyValues := cardsValues[card.ID]
|
|
||||||
keyValues = append(keyValues, &av.KeyValues{Key: relKey, Values: []*av.Value{{ID: value.Value.ID, KeyID: relKey.ID, BlockID: card.ID, Type: av.KeyTypeRelation, Relation: value.Value.Relation}}})
|
|
||||||
cardsValues[card.ID] = keyValues
|
|
||||||
case av.KeyTypeCreated: // 渲染创建时间
|
|
||||||
createdStr := card.ID[:len("20060102150405")]
|
|
||||||
created, parseErr := time.ParseInLocation("20060102150405", createdStr, time.Local)
|
|
||||||
if nil == parseErr {
|
|
||||||
value.Value.Created = av.NewFormattedValueCreated(created.UnixMilli(), 0, av.CreatedFormatNone)
|
|
||||||
value.Value.Created.IsNotEmpty = true
|
|
||||||
} else {
|
|
||||||
value.Value.Created = av.NewFormattedValueCreated(time.Now().UnixMilli(), 0, av.CreatedFormatNone)
|
|
||||||
}
|
|
||||||
|
|
||||||
keyValues := cardsValues[card.ID]
|
|
||||||
createdKey, _ := attrView.GetKey(value.Value.KeyID)
|
|
||||||
keyValues = append(keyValues, &av.KeyValues{Key: createdKey, Values: []*av.Value{{ID: value.Value.ID, KeyID: createdKey.ID, BlockID: card.ID, Type: av.KeyTypeCreated, Created: value.Value.Created}}})
|
|
||||||
cardsValues[card.ID] = keyValues
|
|
||||||
case av.KeyTypeUpdated: // 渲染更新时间
|
|
||||||
ial := ials[card.ID]
|
|
||||||
if nil == ial {
|
|
||||||
ial = map[string]string{}
|
|
||||||
}
|
|
||||||
block := card.GetBlockValue()
|
|
||||||
updatedStr := ial["updated"]
|
|
||||||
if "" == updatedStr && nil != block {
|
|
||||||
value.Value.Updated = av.NewFormattedValueUpdated(block.Block.Updated, 0, av.UpdatedFormatNone)
|
|
||||||
value.Value.Updated.IsNotEmpty = true
|
|
||||||
} else {
|
|
||||||
updated, parseErr := time.ParseInLocation("20060102150405", updatedStr, time.Local)
|
|
||||||
if nil == parseErr {
|
|
||||||
value.Value.Updated = av.NewFormattedValueUpdated(updated.UnixMilli(), 0, av.UpdatedFormatNone)
|
|
||||||
value.Value.Updated.IsNotEmpty = true
|
|
||||||
} else {
|
|
||||||
value.Value.Updated = av.NewFormattedValueUpdated(time.Now().UnixMilli(), 0, av.UpdatedFormatNone)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
keyValues := cardsValues[card.ID]
|
|
||||||
updatedKey, _ := attrView.GetKey(value.Value.KeyID)
|
|
||||||
keyValues = append(keyValues, &av.KeyValues{Key: updatedKey, Values: []*av.Value{{ID: value.Value.ID, KeyID: updatedKey.ID, BlockID: card.ID, Type: av.KeyTypeUpdated, Updated: value.Value.Updated}}})
|
|
||||||
cardsValues[card.ID] = keyValues
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/88250/lute/ast"
|
"github.com/88250/lute/ast"
|
||||||
"github.com/siyuan-note/siyuan/kernel/av"
|
"github.com/siyuan-note/siyuan/kernel/av"
|
||||||
|
|
@ -121,142 +120,8 @@ func RenderAttributeViewTable(attrView *av.AttributeView, view *av.View, query s
|
||||||
avCache := map[string]*av.AttributeView{}
|
avCache := map[string]*av.AttributeView{}
|
||||||
avCache[attrView.ID] = attrView
|
avCache[attrView.ID] = attrView
|
||||||
for _, row := range ret.Rows {
|
for _, row := range ret.Rows {
|
||||||
for _, value := range row.Cells {
|
for _, cell := range row.Cells {
|
||||||
switch value.ValueType {
|
fillAttributeViewAutoGeneratedValues(attrView, ials, cell.Value, row, rowsValues, &avCache)
|
||||||
case av.KeyTypeBlock: // 对于主键可能需要填充静态锚文本 Database-bound block primary key supports setting static anchor text https://github.com/siyuan-note/siyuan/issues/10049
|
|
||||||
if nil != value.Value.Block {
|
|
||||||
for k, v := range ials[row.ID] {
|
|
||||||
if k == av.NodeAttrViewStaticText+"-"+attrView.ID {
|
|
||||||
value.Value.Block.Content = v
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case av.KeyTypeRollup: // 渲染汇总列
|
|
||||||
rollupKey, _ := attrView.GetKey(value.Value.KeyID)
|
|
||||||
if nil == rollupKey || nil == rollupKey.Rollup {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
relKey, _ := attrView.GetKey(rollupKey.Rollup.RelationKeyID)
|
|
||||||
if nil == relKey || nil == relKey.Relation {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
relVal := attrView.GetValue(relKey.ID, row.ID)
|
|
||||||
if nil == relVal || nil == relVal.Relation {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
destAv := avCache[relKey.Relation.AvID]
|
|
||||||
if nil == destAv {
|
|
||||||
destAv, _ = av.ParseAttributeView(relKey.Relation.AvID)
|
|
||||||
if nil != destAv {
|
|
||||||
avCache[relKey.Relation.AvID] = destAv
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if nil == destAv {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
destKey, _ := destAv.GetKey(rollupKey.Rollup.KeyID)
|
|
||||||
if nil == destKey {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, blockID := range relVal.Relation.BlockIDs {
|
|
||||||
destVal := destAv.GetValue(rollupKey.Rollup.KeyID, blockID)
|
|
||||||
if nil == destVal {
|
|
||||||
if destAv.ExistBlock(blockID) { // 数据库中存在行但是列值不存在是数据未初始化,这里补一个默认值
|
|
||||||
destVal = av.GetAttributeViewDefaultValue(ast.NewNodeID(), rollupKey.Rollup.KeyID, blockID, destKey.Type)
|
|
||||||
}
|
|
||||||
if nil == destVal {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if av.KeyTypeNumber == destKey.Type {
|
|
||||||
destVal.Number.Format = destKey.NumberFormat
|
|
||||||
destVal.Number.FormatNumber()
|
|
||||||
}
|
|
||||||
|
|
||||||
value.Value.Rollup.Contents = append(value.Value.Rollup.Contents, destVal.Clone())
|
|
||||||
}
|
|
||||||
|
|
||||||
value.Value.Rollup.RenderContents(rollupKey.Rollup.Calc, destKey)
|
|
||||||
|
|
||||||
// 将汇总列的值保存到 rowsValues 中,后续渲染模板列的时候会用到,下同
|
|
||||||
// Database table view template columns support reading relation, rollup, created and updated columns https://github.com/siyuan-note/siyuan/issues/10442
|
|
||||||
keyValues := rowsValues[row.ID]
|
|
||||||
keyValues = append(keyValues, &av.KeyValues{Key: rollupKey, Values: []*av.Value{{ID: value.Value.ID, KeyID: rollupKey.ID, BlockID: row.ID, Type: av.KeyTypeRollup, Rollup: value.Value.Rollup}}})
|
|
||||||
rowsValues[row.ID] = keyValues
|
|
||||||
case av.KeyTypeRelation: // 渲染关联列
|
|
||||||
relKey, _ := attrView.GetKey(value.Value.KeyID)
|
|
||||||
if nil != relKey && nil != relKey.Relation {
|
|
||||||
destAv := avCache[relKey.Relation.AvID]
|
|
||||||
if nil == destAv {
|
|
||||||
destAv, _ = av.ParseAttributeView(relKey.Relation.AvID)
|
|
||||||
if nil != destAv {
|
|
||||||
avCache[relKey.Relation.AvID] = destAv
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if nil != destAv {
|
|
||||||
blocks := map[string]*av.Value{}
|
|
||||||
blockValues := destAv.GetBlockKeyValues()
|
|
||||||
if nil != blockValues {
|
|
||||||
for _, blockValue := range blockValues.Values {
|
|
||||||
blocks[blockValue.BlockID] = blockValue
|
|
||||||
}
|
|
||||||
for _, blockID := range value.Value.Relation.BlockIDs {
|
|
||||||
if val := blocks[blockID]; nil != val {
|
|
||||||
value.Value.Relation.Contents = append(value.Value.Relation.Contents, val)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
keyValues := rowsValues[row.ID]
|
|
||||||
keyValues = append(keyValues, &av.KeyValues{Key: relKey, Values: []*av.Value{{ID: value.Value.ID, KeyID: relKey.ID, BlockID: row.ID, Type: av.KeyTypeRelation, Relation: value.Value.Relation}}})
|
|
||||||
rowsValues[row.ID] = keyValues
|
|
||||||
case av.KeyTypeCreated: // 渲染创建时间
|
|
||||||
createdStr := row.ID[:len("20060102150405")]
|
|
||||||
created, parseErr := time.ParseInLocation("20060102150405", createdStr, time.Local)
|
|
||||||
if nil == parseErr {
|
|
||||||
value.Value.Created = av.NewFormattedValueCreated(created.UnixMilli(), 0, av.CreatedFormatNone)
|
|
||||||
value.Value.Created.IsNotEmpty = true
|
|
||||||
} else {
|
|
||||||
value.Value.Created = av.NewFormattedValueCreated(time.Now().UnixMilli(), 0, av.CreatedFormatNone)
|
|
||||||
}
|
|
||||||
|
|
||||||
keyValues := rowsValues[row.ID]
|
|
||||||
createdKey, _ := attrView.GetKey(value.Value.KeyID)
|
|
||||||
keyValues = append(keyValues, &av.KeyValues{Key: createdKey, Values: []*av.Value{{ID: value.Value.ID, KeyID: createdKey.ID, BlockID: row.ID, Type: av.KeyTypeCreated, Created: value.Value.Created}}})
|
|
||||||
rowsValues[row.ID] = keyValues
|
|
||||||
case av.KeyTypeUpdated: // 渲染更新时间
|
|
||||||
ial := ials[row.ID]
|
|
||||||
if nil == ial {
|
|
||||||
ial = map[string]string{}
|
|
||||||
}
|
|
||||||
block := row.GetBlockValue()
|
|
||||||
updatedStr := ial["updated"]
|
|
||||||
if "" == updatedStr && nil != block {
|
|
||||||
value.Value.Updated = av.NewFormattedValueUpdated(block.Block.Updated, 0, av.UpdatedFormatNone)
|
|
||||||
value.Value.Updated.IsNotEmpty = true
|
|
||||||
} else {
|
|
||||||
updated, parseErr := time.ParseInLocation("20060102150405", updatedStr, time.Local)
|
|
||||||
if nil == parseErr {
|
|
||||||
value.Value.Updated = av.NewFormattedValueUpdated(updated.UnixMilli(), 0, av.UpdatedFormatNone)
|
|
||||||
value.Value.Updated.IsNotEmpty = true
|
|
||||||
} else {
|
|
||||||
value.Value.Updated = av.NewFormattedValueUpdated(time.Now().UnixMilli(), 0, av.UpdatedFormatNone)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
keyValues := rowsValues[row.ID]
|
|
||||||
updatedKey, _ := attrView.GetKey(value.Value.KeyID)
|
|
||||||
keyValues = append(keyValues, &av.KeyValues{Key: updatedKey, Values: []*av.Value{{ID: value.Value.ID, KeyID: updatedKey.ID, BlockID: row.ID, Type: av.KeyTypeUpdated, Updated: value.Value.Updated}}})
|
|
||||||
rowsValues[row.ID] = keyValues
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue