From 51766ed0c0f722ecac761fc13eb8b5714bf00d04 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Sat, 9 Aug 2025 20:25:44 +0800 Subject: [PATCH 1/3] :art: Improve av https://github.com/siyuan-note/siyuan/issues/15496 --- kernel/model/attribute_view.go | 26 ++------------------------ 1 file changed, 2 insertions(+), 24 deletions(-) diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go index a73c07555..a5b9f3542 100644 --- a/kernel/model/attribute_view.go +++ b/kernel/model/attribute_view.go @@ -2944,31 +2944,9 @@ func addAttributeViewBlock(now int64, avID, blockID, groupID, previousBlockID, a } else { v.ItemIDs = append([]string{addingBlockID}, v.ItemIDs...) } - - for _, g := range v.Groups { - if "" != previousBlockID { - changed := false - for i, id := range g.GroupItemIDs { - if id == previousBlockID { - g.GroupItemIDs = append(g.GroupItemIDs[:i+1], append([]string{addingBlockID}, g.GroupItemIDs[i+1:]...)...) - changed = true - break - } - } - if !changed { - g.GroupItemIDs = append(g.GroupItemIDs, addingBlockID) - } - } else { - g.GroupItemIDs = append([]string{addingBlockID}, g.GroupItemIDs...) - } - } - } - - groupKey := view.GetGroupKey(attrView) - if nil != groupKey { - regenAttrViewViewGroups(attrView, groupKey.ID) } + regenAttrViewViewGroups(attrView, "force") err = av.SaveAttributeView(attrView) return } @@ -4074,7 +4052,7 @@ func replaceAttributeViewBlock0(attrView *av.AttributeView, oldBlockID, newBlock content = util.UnescapeHTML(content) value.Block.Icon, value.Block.Content = icon, content value.UpdatedAt = now - regenAttrViewViewGroups(attrView, value.KeyID) + regenAttrViewViewGroups(attrView, "force") err = av.SaveAttributeView(attrView) } return From 7b1b36d3abc38e12e43eb181ff99d496d4b2e9cd Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Sat, 9 Aug 2025 20:39:12 +0800 Subject: [PATCH 2/3] :art: Improve av https://github.com/siyuan-note/siyuan/issues/15496 --- kernel/model/attribute_view.go | 40 +++++++++++++++------------ kernel/model/attribute_view_render.go | 4 +-- kernel/model/transaction.go | 4 +-- 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go index a5b9f3542..e82da8b1b 100644 --- a/kernel/model/attribute_view.go +++ b/kernel/model/attribute_view.go @@ -429,7 +429,7 @@ func SetAttributeViewGroup(avID, blockID string, group *av.ViewGroup) (err error groupStates := getAttrViewGroupStates(view) view.Group = group - regenAttrViewViewGroups(attrView, "force") + regenAttrViewGroups(attrView, "force") setAttrViewGroupStates(view, groupStates) if view.Group.HideEmpty != oldHideEmpty { @@ -615,7 +615,7 @@ func ChangeAttrViewLayout(blockID, avID string, layout av.LayoutType) (err error } } - regenAttrViewViewGroups(attrView, "force") + regenAttrViewGroups(attrView, "force") if err = av.SaveAttributeView(attrView); nil != err { logging.LogErrorf("save attribute view [%s] failed: %s", avID, err) @@ -1554,7 +1554,7 @@ func GetBlockAttributeViewKeys(blockID string) (ret []*BlockAttributeViewKeys) { return } -func genAttrViewViewGroups(view *av.View, attrView *av.AttributeView) { +func genAttrViewGroups(view *av.View, attrView *av.AttributeView) { if nil == view.Group { return } @@ -2134,18 +2134,19 @@ func updateAttributeViewColRelation(operation *Operation) (err error) { } destVal.Relation.BlockIDs = append(destVal.Relation.BlockIDs, srcVal.BlockID) destVal.Relation.BlockIDs = gulu.Str.RemoveDuplicatedElem(destVal.Relation.BlockIDs) - regenAttrViewViewGroups(srcAv, destVal.KeyID) destKeyValues.Values = append(destKeyValues.Values, destVal) } } } } + regenAttrViewGroups(srcAv, "force") err = av.SaveAttributeView(srcAv) if err != nil { return } if !isSameAv { + regenAttrViewGroups(destAv, "force") err = av.SaveAttributeView(destAv) ReloadAttrView(destAv.ID) } @@ -2946,7 +2947,7 @@ func addAttributeViewBlock(now int64, avID, blockID, groupID, previousBlockID, a } } - regenAttrViewViewGroups(attrView, "force") + regenAttrViewGroups(attrView, "force") err = av.SaveAttributeView(attrView) return } @@ -3079,7 +3080,7 @@ func removeAttributeViewBlock(srcIDs []string, avID string, tx *Transaction) (er } } - regenAttrViewViewGroups(attrView, "force") + regenAttrViewGroups(attrView, "force") relatedAvIDs := av.GetSrcAvIDs(avID) for _, relatedAvID := range relatedAvIDs { @@ -3499,7 +3500,7 @@ func sortAttributeViewRow(operation *Operation) (err error) { targetGroupView.GroupItemIDs = util.InsertElem(targetGroupView.GroupItemIDs, previousIndex, itemID) } - regenAttrViewViewGroups(attrView, "force") + regenAttrViewGroups(attrView, "force") } else { // 同分组内排序 for i, r := range groupView.GroupItemIDs { if r == operation.PreviousID { @@ -3796,7 +3797,7 @@ func updateAttributeViewColTemplate(operation *Operation) (err error) { } } - regenAttrViewViewGroups(attrView, operation.ID) + regenAttrViewGroups(attrView, operation.ID) err = av.SaveAttributeView(attrView) return } @@ -4052,7 +4053,7 @@ func replaceAttributeViewBlock0(attrView *av.AttributeView, oldBlockID, newBlock content = util.UnescapeHTML(content) value.Block.Icon, value.Block.Content = icon, content value.UpdatedAt = now - regenAttrViewViewGroups(attrView, "force") + regenAttrViewGroups(attrView, "force") err = av.SaveAttributeView(attrView) } return @@ -4118,6 +4119,8 @@ func replaceAttributeViewBlock0(attrView *av.AttributeView, oldBlockID, newBlock } } + regenAttrViewGroups(attrView, "force") + changedAvIDs = gulu.Str.RemoveDuplicatedElem(changedAvIDs) for _, id := range changedAvIDs { ReloadAttrView(id) @@ -4389,7 +4392,7 @@ func updateAttributeViewValue(tx *Transaction, attrView *av.AttributeView, keyID updateTwoWayRelationDestAttrView(attrView, key, val, relationChangeMode, oldRelationBlockIDs) } - regenAttrViewViewGroups(attrView, keyID) + regenAttrViewGroups(attrView, keyID) return } @@ -4430,7 +4433,6 @@ func updateTwoWayRelationDestAttrView(attrView *av.AttributeView, relKey *av.Key destVal.Relation.BlockIDs = append(destVal.Relation.BlockIDs, val.BlockID) destVal.Relation.BlockIDs = gulu.Str.RemoveDuplicatedElem(destVal.Relation.BlockIDs) - regenAttrViewViewGroups(destAv, relKey.Relation.BackKeyID) break } } @@ -4450,7 +4452,6 @@ func updateTwoWayRelationDestAttrView(attrView *av.AttributeView, relKey *av.Key if value.BlockID == blockID { value.Relation.BlockIDs = gulu.Str.RemoveElem(value.Relation.BlockIDs, val.BlockID) value.SetUpdatedAt(now) - regenAttrViewViewGroups(destAv, relKey.Relation.BackKeyID) break } } @@ -4459,11 +4460,14 @@ func updateTwoWayRelationDestAttrView(attrView *av.AttributeView, relKey *av.Key } if destAv != attrView { + regenAttrViewGroups(destAv, "force") av.SaveAttributeView(destAv) } } -func regenAttrViewViewGroups(attrView *av.AttributeView, keyID string) { +// regenAttrViewGroups 重新生成分组视图。 +// keyID: 如果是 "force" 则强制重新生成所有分组视图,否则只生成 keyID 指定的分组字段的分组视图 +func regenAttrViewGroups(attrView *av.AttributeView, keyID string) { for _, view := range attrView.Views { groupKey := view.GetGroupKey(attrView) if nil == groupKey { @@ -4477,7 +4481,7 @@ func regenAttrViewViewGroups(attrView *av.AttributeView, keyID string) { } } - genAttrViewViewGroups(view, attrView) + genAttrViewGroups(view, attrView) for _, g := range view.Groups { if view.Group.HideEmpty { @@ -4639,7 +4643,7 @@ func updateAttributeViewColumnOptions(operation *Operation) (err error) { } } - regenAttrViewViewGroups(attrView, operation.ID) + regenAttrViewGroups(attrView, operation.ID) err = av.SaveAttributeView(attrView) return } @@ -4692,7 +4696,7 @@ func removeAttributeViewColumnOption(operation *Operation) (err error) { break } - regenAttrViewViewGroups(attrView, operation.ID) + regenAttrViewGroups(attrView, operation.ID) err = av.SaveAttributeView(attrView) return } @@ -4811,7 +4815,7 @@ func updateAttributeViewColumnOption(operation *Operation) (err error) { } } - regenAttrViewViewGroups(attrView, operation.ID) + regenAttrViewGroups(attrView, operation.ID) err = av.SaveAttributeView(attrView) return } @@ -4898,13 +4902,13 @@ func replaceRelationAvValues(avID, previousID, nextID string) (changedSrcAvID [] srcAvChanged := false srcValue.Relation.BlockIDs, srcAvChanged = util.ReplaceStr(srcValue.Relation.BlockIDs, previousID, nextID) if srcAvChanged { - regenAttrViewViewGroups(srcAv, srcValue.KeyID) changed = true } } } if changed { + regenAttrViewGroups(srcAv, "force") av.SaveAttributeView(srcAv) changedSrcAvID = append(changedSrcAvID, srcAvID) } diff --git a/kernel/model/attribute_view_render.go b/kernel/model/attribute_view_render.go index 37e805173..9810933fa 100644 --- a/kernel/model/attribute_view_render.go +++ b/kernel/model/attribute_view_render.go @@ -92,7 +92,7 @@ func renderAttributeViewGroups(viewable av.Viewable, attrView *av.AttributeView, if isGroupByDate(view) { createdDate := time.UnixMilli(view.GroupCreated).Format("2006-01-02") if time.Now().Format("2006-01-02") != createdDate { - regenAttrViewViewGroups(attrView, "force") + regenAttrViewGroups(attrView, "force") av.SaveAttributeView(attrView) } } @@ -108,7 +108,7 @@ func renderAttributeViewGroups(viewable av.Viewable, attrView *av.AttributeView, for _, groupView := range view.Groups { if "" == groupView.GetGroupValue() && !fixDev { // TODO 分组上线后删除,预计 2025 年 9 月后可以删除 - regenAttrViewViewGroups(attrView, "force") + regenAttrViewGroups(attrView, "force") av.SaveAttributeView(attrView) fixDev = true } diff --git a/kernel/model/transaction.go b/kernel/model/transaction.go index 6366c679e..4537f5dba 100644 --- a/kernel/model/transaction.go +++ b/kernel/model/transaction.go @@ -1010,7 +1010,7 @@ func syncDelete2AttributeView(node *ast.Node) (changedAvIDs []string) { } if changedAv { - regenAttrViewViewGroups(attrView, "force") + regenAttrViewGroups(attrView, "force") av.SaveAttributeView(attrView) changedAvIDs = append(changedAvIDs, avID) } @@ -1571,7 +1571,7 @@ func upsertAvBlockRel(node *ast.Node) { for _, avID := range affectedAvIDs { attrView, _ := av.ParseAttributeView(avID) if nil != attrView { - regenAttrViewViewGroups(attrView, "force") + regenAttrViewGroups(attrView, "force") av.SaveAttributeView(attrView) } From 087d2522fe52f6c607084550eb8f35ed2a686e30 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Sat, 9 Aug 2025 20:46:21 +0800 Subject: [PATCH 3/3] :art: Improve av https://github.com/siyuan-note/siyuan/issues/15496 --- kernel/model/attribute_view.go | 16 ---------------- kernel/model/attribute_view_render.go | 6 ++---- 2 files changed, 2 insertions(+), 20 deletions(-) diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go index e82da8b1b..254ad91c9 100644 --- a/kernel/model/attribute_view.go +++ b/kernel/model/attribute_view.go @@ -4482,22 +4482,6 @@ func regenAttrViewGroups(attrView *av.AttributeView, keyID string) { } genAttrViewGroups(view, attrView) - - for _, g := range view.Groups { - if view.Group.HideEmpty { - if 1 > len(g.GroupItemIDs) { - if 0 == g.GroupHidden { - g.GroupHidden = 1 - } - } else { - g.GroupHidden = 0 - } - } else { - if 2 != g.GroupHidden { - g.GroupHidden = 0 - } - } - } } } diff --git a/kernel/model/attribute_view_render.go b/kernel/model/attribute_view_render.go index 9810933fa..5025b1e99 100644 --- a/kernel/model/attribute_view_render.go +++ b/kernel/model/attribute_view_render.go @@ -362,10 +362,8 @@ func getRenderAttributeViewView(attrView *av.AttributeView, viewID, blockID stri } if "" == viewID && "" != blockID { - node, _, getErr := getNodeByBlockID(nil, blockID) - if nil != getErr { - logging.LogWarnf("get node by block ID [%s] failed: %s", blockID, getErr) - } else { + node, _, _ := getNodeByBlockID(nil, blockID) + if nil != node { viewID = node.IALAttr(av.NodeAttrView) } }