diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go index a73c07555..254ad91c9 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) } @@ -2944,31 +2945,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) } + regenAttrViewGroups(attrView, "force") err = av.SaveAttributeView(attrView) return } @@ -3101,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 { @@ -3521,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 { @@ -3818,7 +3797,7 @@ func updateAttributeViewColTemplate(operation *Operation) (err error) { } } - regenAttrViewViewGroups(attrView, operation.ID) + regenAttrViewGroups(attrView, operation.ID) err = av.SaveAttributeView(attrView) return } @@ -4074,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, value.KeyID) + regenAttrViewGroups(attrView, "force") err = av.SaveAttributeView(attrView) } return @@ -4140,6 +4119,8 @@ func replaceAttributeViewBlock0(attrView *av.AttributeView, oldBlockID, newBlock } } + regenAttrViewGroups(attrView, "force") + changedAvIDs = gulu.Str.RemoveDuplicatedElem(changedAvIDs) for _, id := range changedAvIDs { ReloadAttrView(id) @@ -4411,7 +4392,7 @@ func updateAttributeViewValue(tx *Transaction, attrView *av.AttributeView, keyID updateTwoWayRelationDestAttrView(attrView, key, val, relationChangeMode, oldRelationBlockIDs) } - regenAttrViewViewGroups(attrView, keyID) + regenAttrViewGroups(attrView, keyID) return } @@ -4452,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 } } @@ -4472,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 } } @@ -4481,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 { @@ -4499,23 +4481,7 @@ func regenAttrViewViewGroups(attrView *av.AttributeView, keyID string) { } } - genAttrViewViewGroups(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 - } - } - } + genAttrViewGroups(view, attrView) } } @@ -4661,7 +4627,7 @@ func updateAttributeViewColumnOptions(operation *Operation) (err error) { } } - regenAttrViewViewGroups(attrView, operation.ID) + regenAttrViewGroups(attrView, operation.ID) err = av.SaveAttributeView(attrView) return } @@ -4714,7 +4680,7 @@ func removeAttributeViewColumnOption(operation *Operation) (err error) { break } - regenAttrViewViewGroups(attrView, operation.ID) + regenAttrViewGroups(attrView, operation.ID) err = av.SaveAttributeView(attrView) return } @@ -4833,7 +4799,7 @@ func updateAttributeViewColumnOption(operation *Operation) (err error) { } } - regenAttrViewViewGroups(attrView, operation.ID) + regenAttrViewGroups(attrView, operation.ID) err = av.SaveAttributeView(attrView) return } @@ -4920,13 +4886,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..5025b1e99 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 } @@ -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) } } 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) }