🎨 Improve copying database block https://github.com/siyuan-note/siyuan/issues/11460
This commit is contained in:
parent
6bf910d776
commit
da629f39f5
2 changed files with 77 additions and 64 deletions
|
@ -35,6 +35,7 @@ import (
|
|||
"github.com/siyuan-note/logging"
|
||||
"github.com/siyuan-note/siyuan/kernel/av"
|
||||
"github.com/siyuan-note/siyuan/kernel/cache"
|
||||
"github.com/siyuan-note/siyuan/kernel/filesys"
|
||||
"github.com/siyuan-note/siyuan/kernel/sql"
|
||||
"github.com/siyuan-note/siyuan/kernel/treenode"
|
||||
"github.com/siyuan-note/siyuan/kernel/util"
|
||||
|
@ -78,6 +79,8 @@ func DuplicateDatabaseBlock(avID string) (newAvID, newBlockID string, err error)
|
|||
logging.LogErrorf("write attribute view [%s] failed: %s", newAvID, err)
|
||||
return
|
||||
}
|
||||
|
||||
updateBoundBlockAvsAttribute([]string{newAvID})
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -3314,3 +3317,73 @@ func replaceRelationAvValues(avID, previousID, nextID string) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func updateBoundBlockAvsAttribute(avIDs []string) {
|
||||
// 更新指定 avIDs 中绑定块的 avs 属性
|
||||
|
||||
cachedTrees, saveTrees := map[string]*parse.Tree{}, map[string]*parse.Tree{}
|
||||
luteEngine := util.NewLute()
|
||||
for _, avID := range avIDs {
|
||||
attrView, _ := av.ParseAttributeView(avID)
|
||||
if nil == attrView {
|
||||
continue
|
||||
}
|
||||
|
||||
blockKeyValues := attrView.GetBlockKeyValues()
|
||||
for _, blockValue := range blockKeyValues.Values {
|
||||
if blockValue.IsDetached {
|
||||
continue
|
||||
}
|
||||
bt := treenode.GetBlockTree(blockValue.BlockID)
|
||||
if nil == bt {
|
||||
continue
|
||||
}
|
||||
|
||||
tree := cachedTrees[bt.RootID]
|
||||
if nil == tree {
|
||||
tree, _ = filesys.LoadTree(bt.BoxID, bt.Path, luteEngine)
|
||||
if nil == tree {
|
||||
continue
|
||||
}
|
||||
cachedTrees[bt.RootID] = tree
|
||||
}
|
||||
|
||||
node := treenode.GetNodeInTree(tree, blockValue.BlockID)
|
||||
if nil == node {
|
||||
continue
|
||||
}
|
||||
|
||||
attrs := parse.IAL2Map(node.KramdownIAL)
|
||||
if "" == attrs[av.NodeAttrNameAvs] {
|
||||
attrs[av.NodeAttrNameAvs] = avID
|
||||
} else {
|
||||
nodeAvIDs := strings.Split(attrs[av.NodeAttrNameAvs], ",")
|
||||
nodeAvIDs = append(nodeAvIDs, avID)
|
||||
nodeAvIDs = gulu.Str.RemoveDuplicatedElem(nodeAvIDs)
|
||||
attrs[av.NodeAttrNameAvs] = strings.Join(nodeAvIDs, ",")
|
||||
saveTrees[bt.RootID] = tree
|
||||
}
|
||||
|
||||
avNames := getAvNames(attrs[av.NodeAttrNameAvs])
|
||||
if "" != avNames {
|
||||
attrs[av.NodeAttrViewNames] = avNames
|
||||
}
|
||||
|
||||
oldAttrs, setErr := setNodeAttrs0(node, attrs)
|
||||
if nil != setErr {
|
||||
continue
|
||||
}
|
||||
cache.PutBlockIAL(node.ID, parse.IAL2Map(node.KramdownIAL))
|
||||
pushBroadcastAttrTransactions(oldAttrs, node)
|
||||
}
|
||||
}
|
||||
|
||||
for _, saveTree := range saveTrees {
|
||||
if treeErr := indexWriteTreeUpsertQueue(saveTree); nil != treeErr {
|
||||
logging.LogErrorf("index write tree upsert queue failed: %s", treeErr)
|
||||
}
|
||||
|
||||
avNodes := saveTree.Root.ChildrenByType(ast.NodeAttributeView)
|
||||
av.BatchUpsertBlockRel(avNodes)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -48,7 +48,6 @@ import (
|
|||
"github.com/siyuan-note/logging"
|
||||
"github.com/siyuan-note/riff"
|
||||
"github.com/siyuan-note/siyuan/kernel/av"
|
||||
"github.com/siyuan-note/siyuan/kernel/cache"
|
||||
"github.com/siyuan-note/siyuan/kernel/filesys"
|
||||
"github.com/siyuan-note/siyuan/kernel/sql"
|
||||
"github.com/siyuan-note/siyuan/kernel/treenode"
|
||||
|
@ -300,71 +299,12 @@ func ImportSY(zipPath, boxID, toPath string) (err error) {
|
|||
av.BatchUpsertBlockRel(avNodes)
|
||||
}
|
||||
|
||||
// 如果数据库中绑定的块不在导入的文档中
|
||||
cachedTrees, saveTrees := map[string]*parse.Tree{}, map[string]*parse.Tree{}
|
||||
// 如果数据库中绑定的块不在导入的文档中,则需要单独更新这些绑定块的属性
|
||||
var attrViewIDs []string
|
||||
for _, avID := range avIDs {
|
||||
attrView, _ := av.ParseAttributeView(avID)
|
||||
if nil == attrView {
|
||||
continue
|
||||
}
|
||||
|
||||
blockKeyValues := attrView.GetBlockKeyValues()
|
||||
for _, blockValue := range blockKeyValues.Values {
|
||||
if blockValue.IsDetached {
|
||||
continue
|
||||
}
|
||||
bt := treenode.GetBlockTree(blockValue.BlockID)
|
||||
if nil == bt {
|
||||
continue
|
||||
}
|
||||
|
||||
tree := cachedTrees[bt.RootID]
|
||||
if nil == tree {
|
||||
tree, _ = filesys.LoadTree(bt.BoxID, bt.Path, luteEngine)
|
||||
if nil == tree {
|
||||
continue
|
||||
}
|
||||
cachedTrees[bt.RootID] = tree
|
||||
}
|
||||
|
||||
node := treenode.GetNodeInTree(tree, blockValue.BlockID)
|
||||
if nil == node {
|
||||
continue
|
||||
}
|
||||
|
||||
attrs := parse.IAL2Map(node.KramdownIAL)
|
||||
if "" == attrs[av.NodeAttrNameAvs] {
|
||||
attrs[av.NodeAttrNameAvs] = avID
|
||||
} else {
|
||||
nodeAvIDs := strings.Split(attrs[av.NodeAttrNameAvs], ",")
|
||||
nodeAvIDs = append(nodeAvIDs, avID)
|
||||
nodeAvIDs = gulu.Str.RemoveDuplicatedElem(nodeAvIDs)
|
||||
attrs[av.NodeAttrNameAvs] = strings.Join(nodeAvIDs, ",")
|
||||
saveTrees[bt.RootID] = tree
|
||||
}
|
||||
|
||||
avNames := getAvNames(attrs[av.NodeAttrNameAvs])
|
||||
if "" != avNames {
|
||||
attrs[av.NodeAttrViewNames] = avNames
|
||||
}
|
||||
|
||||
oldAttrs, setErr := setNodeAttrs0(node, attrs)
|
||||
if nil != setErr {
|
||||
continue
|
||||
}
|
||||
cache.PutBlockIAL(node.ID, parse.IAL2Map(node.KramdownIAL))
|
||||
pushBroadcastAttrTransactions(oldAttrs, node)
|
||||
}
|
||||
}
|
||||
|
||||
for _, saveTree := range saveTrees {
|
||||
if treeErr := indexWriteTreeUpsertQueue(saveTree); nil != treeErr {
|
||||
logging.LogErrorf("index write tree upsert queue failed: %s", treeErr)
|
||||
}
|
||||
|
||||
avNodes := saveTree.Root.ChildrenByType(ast.NodeAttributeView)
|
||||
av.BatchUpsertBlockRel(avNodes)
|
||||
attrViewIDs = append(attrViewIDs, avID)
|
||||
}
|
||||
updateBoundBlockAvsAttribute(attrViewIDs)
|
||||
}
|
||||
|
||||
// 将关联的闪卡数据合并到默认卡包 data/storage/riff/20230218211946-2kw8jgx 中
|
||||
|
|
Loading…
Add table
Reference in a new issue