mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-20 00:20:12 +01:00
♻️ Refactor av render
This commit is contained in:
parent
34647490f2
commit
f4699c9fad
3 changed files with 175 additions and 164 deletions
|
|
@ -285,7 +285,11 @@ 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) {
|
||||||
|
avCache := map[string]*av.AttributeView{}
|
||||||
|
avCache[attrView.ID] = attrView
|
||||||
|
for _, item := range collection.GetItems() {
|
||||||
|
for _, value := range item.GetValues() {
|
||||||
itemID := item.GetID()
|
itemID := item.GetID()
|
||||||
|
|
||||||
switch value.Type {
|
switch value.Type {
|
||||||
|
|
@ -314,11 +318,11 @@ func fillAttributeViewAutoGeneratedValues(attrView *av.AttributeView, ials map[s
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
destAv := (*avCache)[relKey.Relation.AvID]
|
destAv := avCache[relKey.Relation.AvID]
|
||||||
if nil == destAv {
|
if nil == destAv {
|
||||||
destAv, _ = av.ParseAttributeView(relKey.Relation.AvID)
|
destAv, _ = av.ParseAttributeView(relKey.Relation.AvID)
|
||||||
if nil != destAv {
|
if nil != destAv {
|
||||||
(*avCache)[relKey.Relation.AvID] = destAv
|
avCache[relKey.Relation.AvID] = destAv
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if nil == destAv {
|
if nil == destAv {
|
||||||
|
|
@ -358,11 +362,11 @@ func fillAttributeViewAutoGeneratedValues(attrView *av.AttributeView, ials map[s
|
||||||
case av.KeyTypeRelation: // 渲染关联列
|
case av.KeyTypeRelation: // 渲染关联列
|
||||||
relKey, _ := attrView.GetKey(value.KeyID)
|
relKey, _ := attrView.GetKey(value.KeyID)
|
||||||
if nil != relKey && nil != relKey.Relation {
|
if nil != relKey && nil != relKey.Relation {
|
||||||
destAv := (*avCache)[relKey.Relation.AvID]
|
destAv := avCache[relKey.Relation.AvID]
|
||||||
if nil == destAv {
|
if nil == destAv {
|
||||||
destAv, _ = av.ParseAttributeView(relKey.Relation.AvID)
|
destAv, _ = av.ParseAttributeView(relKey.Relation.AvID)
|
||||||
if nil != destAv {
|
if nil != destAv {
|
||||||
(*avCache)[relKey.Relation.AvID] = destAv
|
avCache[relKey.Relation.AvID] = destAv
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if nil != destAv {
|
if nil != destAv {
|
||||||
|
|
@ -424,6 +428,42 @@ func fillAttributeViewAutoGeneratedValues(attrView *av.AttributeView, ials map[s
|
||||||
items[itemID] = keyValues
|
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