mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-17 23:20:13 +01:00
♻️ Refactor av render
This commit is contained in:
parent
34647490f2
commit
f4699c9fad
3 changed files with 175 additions and 164 deletions
302
kernel/sql/av.go
302
kernel/sql/av.go
|
|
@ -285,146 +285,186 @@ 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) {
|
func fillAttributeViewAutoGeneratedValues(attrView *av.AttributeView, collection av.Collection, ials map[string]map[string]string, items map[string][]*av.KeyValues) {
|
||||||
itemID := item.GetID()
|
avCache := map[string]*av.AttributeView{}
|
||||||
|
avCache[attrView.ID] = attrView
|
||||||
|
for _, item := range collection.GetItems() {
|
||||||
|
for _, value := range item.GetValues() {
|
||||||
|
itemID := item.GetID()
|
||||||
|
|
||||||
switch value.Type {
|
switch value.Type {
|
||||||
case av.KeyTypeBlock: // 对于主键可能需要填充静态锚文本 Database-bound block primary key supports setting static anchor text https://github.com/siyuan-note/siyuan/issues/10049
|
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 {
|
if nil != value.Block {
|
||||||
for k, v := range ials[itemID] {
|
for k, v := range ials[itemID] {
|
||||||
if k == av.NodeAttrViewStaticText+"-"+attrView.ID {
|
if k == av.NodeAttrViewStaticText+"-"+attrView.ID {
|
||||||
value.Block.Content = v
|
value.Block.Content = v
|
||||||
break
|
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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 fillAttributeViewTemplateValues(attrView *av.AttributeView, collection av.Collection, ials map[string]map[string]string, items map[string][]*av.KeyValues) (err error) {
|
||||||
|
var renderTemplateErr error
|
||||||
|
for _, item := range collection.GetItems() {
|
||||||
|
for _, value := range item.GetValues() {
|
||||||
|
itemID := item.GetID()
|
||||||
|
|
||||||
|
switch value.Type {
|
||||||
|
case av.KeyTypeTemplate: // 渲染模板字段
|
||||||
|
keyValues := items[itemID]
|
||||||
|
ial := ials[itemID]
|
||||||
|
if nil == ial {
|
||||||
|
ial = map[string]string{}
|
||||||
|
}
|
||||||
|
content, renderErr := RenderTemplateField(ial, keyValues, value.Template.Content)
|
||||||
|
value.Template.Content = content
|
||||||
|
if nil != renderErr {
|
||||||
|
key, _ := attrView.GetKey(value.KeyID)
|
||||||
|
keyName := ""
|
||||||
|
if nil != key {
|
||||||
|
keyName = key.Name
|
||||||
|
}
|
||||||
|
err = fmt.Errorf("database [%s] template field [%s] rendering failed: %s", getAttrViewName(attrView), keyName, renderErr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if nil != err {
|
||||||
|
renderTemplateErr = err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
err = renderTemplateErr
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func fillAttributeViewTemplateValue(value *av.Value, item av.Item, attrView *av.AttributeView, ials map[string]map[string]string, items map[string][]*av.KeyValues) (err error) {
|
func fillAttributeViewTemplateValue(value *av.Value, item av.Item, attrView *av.AttributeView, ials map[string]map[string]string, items map[string][]*av.KeyValues) (err error) {
|
||||||
itemID := item.GetID()
|
itemID := item.GetID()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -114,24 +114,10 @@ func RenderAttributeViewGallery(attrView *av.AttributeView, view *av.View, query
|
||||||
ials := BatchGetBlockAttrsWitTrees(ialIDs, boundTrees)
|
ials := BatchGetBlockAttrsWitTrees(ialIDs, boundTrees)
|
||||||
|
|
||||||
// 渲染自动生成的字段值,比如关联字段、汇总字段、创建时间字段和更新时间字段
|
// 渲染自动生成的字段值,比如关联字段、汇总字段、创建时间字段和更新时间字段
|
||||||
avCache := map[string]*av.AttributeView{}
|
fillAttributeViewAutoGeneratedValues(attrView, ret, ials, cardsValues)
|
||||||
avCache[attrView.ID] = attrView
|
|
||||||
for _, card := range ret.Cards {
|
|
||||||
for _, value := range card.Values {
|
|
||||||
fillAttributeViewAutoGeneratedValues(attrView, ials, value.Value, card, cardsValues, &avCache)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 最后单独渲染模板字段,这样模板字段就可以使用汇总、关联、创建时间和更新时间字段的值了
|
// 最后单独渲染模板字段,这样模板字段就可以使用汇总、关联、创建时间和更新时间字段的值了
|
||||||
var renderTemplateErr error
|
renderTemplateErr := fillAttributeViewTemplateValues(attrView, ret, ials, cardsValues)
|
||||||
for _, card := range ret.Cards {
|
|
||||||
for _, value := range card.Values {
|
|
||||||
err := fillAttributeViewTemplateValue(value.Value, card, attrView, ials, cardsValues)
|
|
||||||
if nil != err {
|
|
||||||
renderTemplateErr = err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if nil != renderTemplateErr {
|
if nil != renderTemplateErr {
|
||||||
util.PushErrMsg(fmt.Sprintf(util.Langs[util.Lang][44], util.EscapeHTML(renderTemplateErr.Error())), 30000)
|
util.PushErrMsg(fmt.Sprintf(util.Langs[util.Lang][44], util.EscapeHTML(renderTemplateErr.Error())), 30000)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -107,25 +107,10 @@ func RenderAttributeViewTable(attrView *av.AttributeView, view *av.View, query s
|
||||||
ials := BatchGetBlockAttrs(ialIDs)
|
ials := BatchGetBlockAttrs(ialIDs)
|
||||||
|
|
||||||
// 渲染自动生成的列值,比如关联列、汇总列、创建时间列和更新时间列
|
// 渲染自动生成的列值,比如关联列、汇总列、创建时间列和更新时间列
|
||||||
avCache := map[string]*av.AttributeView{}
|
fillAttributeViewAutoGeneratedValues(attrView, ret, ials, rowsValues)
|
||||||
avCache[attrView.ID] = attrView
|
|
||||||
for _, row := range ret.Rows {
|
|
||||||
for _, cell := range row.Cells {
|
|
||||||
fillAttributeViewAutoGeneratedValues(attrView, ials, cell.Value, row, rowsValues, &avCache)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 最后单独渲染模板列,这样模板列就可以使用汇总、关联、创建时间和更新时间列的值了
|
// 最后单独渲染模板列,这样模板列就可以使用汇总、关联、创建时间和更新时间列的值了
|
||||||
// Database table view template columns support reading relation, rollup, created and updated columns https://github.com/siyuan-note/siyuan/issues/10442
|
renderTemplateErr := fillAttributeViewTemplateValues(attrView, ret, ials, rowsValues)
|
||||||
var renderTemplateErr error
|
|
||||||
for _, row := range ret.Rows {
|
|
||||||
for _, cell := range row.Cells {
|
|
||||||
err := fillAttributeViewTemplateValue(cell.Value, row, attrView, ials, rowsValues)
|
|
||||||
if nil != err {
|
|
||||||
renderTemplateErr = err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if nil != renderTemplateErr {
|
if nil != renderTemplateErr {
|
||||||
util.PushErrMsg(fmt.Sprintf(util.Langs[util.Lang][44], util.EscapeHTML(renderTemplateErr.Error())), 30000)
|
util.PushErrMsg(fmt.Sprintf(util.Langs[util.Lang][44], util.EscapeHTML(renderTemplateErr.Error())), 30000)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue