mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-05-04 06:31:06 +08:00
🎨 Add Rollup column to database table view https://github.com/siyuan-note/siyuan/issues/9958
This commit is contained in:
parent
2d6900eafa
commit
290df47a1e
@ -16,7 +16,35 @@ var (
|
|||||||
attributeViewRelationsLock = sync.Mutex{}
|
attributeViewRelationsLock = sync.Mutex{}
|
||||||
)
|
)
|
||||||
|
|
||||||
func RemoveAvRel(avID, destAvID string) {
|
func GetSrcAvIDs(destAvID string) []string {
|
||||||
|
attributeViewRelationsLock.Lock()
|
||||||
|
defer attributeViewRelationsLock.Unlock()
|
||||||
|
|
||||||
|
relations := filepath.Join(util.DataDir, "storage", "av", "relations.msgpack")
|
||||||
|
if !filelock.IsExist(relations) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
data, err := filelock.ReadFile(relations)
|
||||||
|
if nil != err {
|
||||||
|
logging.LogErrorf("read attribute view relations failed: %s", err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
avRels := map[string][]string{}
|
||||||
|
if err = msgpack.Unmarshal(data, &avRels); nil != err {
|
||||||
|
logging.LogErrorf("unmarshal attribute view relations failed: %s", err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
srcAvIDs := avRels[destAvID]
|
||||||
|
if nil == srcAvIDs {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return srcAvIDs
|
||||||
|
}
|
||||||
|
|
||||||
|
func RemoveAvRel(srcAvID, destAvID string) {
|
||||||
attributeViewRelationsLock.Lock()
|
attributeViewRelationsLock.Lock()
|
||||||
defer attributeViewRelationsLock.Unlock()
|
defer attributeViewRelationsLock.Unlock()
|
||||||
|
|
||||||
@ -37,18 +65,18 @@ func RemoveAvRel(avID, destAvID string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
destAvIDs := avRels[avID]
|
srcAvIDs := avRels[destAvID]
|
||||||
if nil == destAvIDs {
|
if nil == srcAvIDs {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var newAvIDs []string
|
var newAvIDs []string
|
||||||
for _, v := range destAvIDs {
|
for _, v := range srcAvIDs {
|
||||||
if v != destAvID {
|
if v != srcAvID {
|
||||||
newAvIDs = append(newAvIDs, v)
|
newAvIDs = append(newAvIDs, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
avRels[avID] = newAvIDs
|
avRels[destAvID] = newAvIDs
|
||||||
|
|
||||||
data, err = msgpack.Marshal(avRels)
|
data, err = msgpack.Marshal(avRels)
|
||||||
if nil != err {
|
if nil != err {
|
||||||
@ -61,7 +89,7 @@ func RemoveAvRel(avID, destAvID string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpsertAvRel(avID, destAvID string) {
|
func UpsertAvBackRel(srcAvID, destAvID string) {
|
||||||
attributeViewRelationsLock.Lock()
|
attributeViewRelationsLock.Lock()
|
||||||
defer attributeViewRelationsLock.Unlock()
|
defer attributeViewRelationsLock.Unlock()
|
||||||
|
|
||||||
@ -85,10 +113,10 @@ func UpsertAvRel(avID, destAvID string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
destAvIDs := avRelations[avID]
|
srcAvIDs := avRelations[destAvID]
|
||||||
destAvIDs = append(destAvIDs, destAvID)
|
srcAvIDs = append(srcAvIDs, srcAvID)
|
||||||
destAvIDs = gulu.Str.RemoveDuplicatedElem(destAvIDs)
|
srcAvIDs = gulu.Str.RemoveDuplicatedElem(srcAvIDs)
|
||||||
avRelations[avID] = destAvIDs
|
avRelations[destAvID] = srcAvIDs
|
||||||
|
|
||||||
data, err := msgpack.Marshal(avRelations)
|
data, err := msgpack.Marshal(avRelations)
|
||||||
if nil != err {
|
if nil != err {
|
||||||
|
@ -693,6 +693,14 @@ func renderAttributeViewTable(attrView *av.AttributeView, view *av.View) (ret *a
|
|||||||
}
|
}
|
||||||
rows[val.BlockID] = values
|
rows[val.BlockID] = values
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 数据订正,补全关联
|
||||||
|
if av.KeyTypeRelation == keyValues.Key.Type && nil != keyValues.Key.Relation {
|
||||||
|
av.UpsertAvBackRel(attrView.ID, keyValues.Key.Relation.AvID)
|
||||||
|
if keyValues.Key.Relation.IsTwoWay {
|
||||||
|
av.UpsertAvBackRel(keyValues.Key.Relation.AvID, attrView.ID)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 过滤掉不存在的行
|
// 过滤掉不存在的行
|
||||||
@ -1074,7 +1082,7 @@ func updateAttributeViewColRelation(operation *Operation) (err error) {
|
|||||||
util.BroadcastByType("protyle", "refreshAttributeView", 0, "", map[string]interface{}{"id": destAv.ID})
|
util.BroadcastByType("protyle", "refreshAttributeView", 0, "", map[string]interface{}{"id": destAv.ID})
|
||||||
}
|
}
|
||||||
|
|
||||||
av.UpsertAvRel(srcAv.ID, destAv.ID)
|
av.UpsertAvBackRel(srcAv.ID, destAv.ID)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2332,7 +2340,7 @@ func UpdateAttributeViewCell(tx *Transaction, avID, keyID, rowID, cellID string,
|
|||||||
// 关联列得 content 是自动渲染的,所以不需要保存
|
// 关联列得 content 是自动渲染的,所以不需要保存
|
||||||
val.Relation.Contents = nil
|
val.Relation.Contents = nil
|
||||||
|
|
||||||
// 计算关联排序模式
|
// 计算关联变更模式
|
||||||
if len(oldRelationBlockIDs) == len(val.Relation.BlockIDs) {
|
if len(oldRelationBlockIDs) == len(val.Relation.BlockIDs) {
|
||||||
relationChangeMode = 0
|
relationChangeMode = 0
|
||||||
} else {
|
} else {
|
||||||
@ -2377,9 +2385,10 @@ func UpdateAttributeViewCell(tx *Transaction, avID, keyID, rowID, cellID string,
|
|||||||
}
|
}
|
||||||
|
|
||||||
key, _ := attrView.GetKey(val.KeyID)
|
key, _ := attrView.GetKey(val.KeyID)
|
||||||
if nil != key && av.KeyTypeRelation == key.Type && nil != key.Relation && key.Relation.IsTwoWay {
|
if nil != key && av.KeyTypeRelation == key.Type && nil != key.Relation {
|
||||||
destAv, _ := av.ParseAttributeView(key.Relation.AvID)
|
destAv, _ := av.ParseAttributeView(key.Relation.AvID)
|
||||||
if nil != destAv {
|
if nil != destAv {
|
||||||
|
if key.Relation.IsTwoWay {
|
||||||
// relationChangeMode
|
// relationChangeMode
|
||||||
// 0:关联列值不变(仅排序),不影响目标值
|
// 0:关联列值不变(仅排序),不影响目标值
|
||||||
// 1:关联列值增加,增加目标值
|
// 1:关联列值增加,增加目标值
|
||||||
@ -2431,9 +2440,14 @@ func UpdateAttributeViewCell(tx *Transaction, avID, keyID, rowID, cellID string,
|
|||||||
}
|
}
|
||||||
|
|
||||||
av.SaveAttributeView(destAv)
|
av.SaveAttributeView(destAv)
|
||||||
util.BroadcastByType("protyle", "refreshAttributeView", 0, "", map[string]interface{}{"id": destAv.ID})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
relatedAvIDs := av.GetSrcAvIDs(avID)
|
||||||
|
for _, relatedAvID := range relatedAvIDs {
|
||||||
|
util.BroadcastByType("protyle", "refreshAttributeView", 0, "", map[string]interface{}{"id": relatedAvID})
|
||||||
|
}
|
||||||
|
|
||||||
if err = av.SaveAttributeView(attrView); nil != err {
|
if err = av.SaveAttributeView(attrView); nil != err {
|
||||||
return
|
return
|
||||||
|
Loading…
Reference in New Issue
Block a user