Quellcode durchsuchen

Merge remote-tracking branch 'origin/dev' into dev

Vanessa vor 1 Jahr
Ursprung
Commit
b148f646ca
1 geänderte Dateien mit 69 neuen und 2 gelöschten Zeilen
  1. 69 2
      kernel/model/import.go

+ 69 - 2
kernel/model/import.go

@@ -22,8 +22,6 @@ import (
 	"encoding/json"
 	"errors"
 	"fmt"
-	"github.com/siyuan-note/riff"
-	"github.com/siyuan-note/siyuan/kernel/av"
 	"image"
 	"image/jpeg"
 	"image/png"
@@ -48,6 +46,9 @@ import (
 	"github.com/88250/lute/render"
 	"github.com/siyuan-note/filelock"
 	"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"
@@ -289,6 +290,72 @@ func ImportSY(zipPath, boxID, toPath string) (err error) {
 			avNodes := tree.Root.ChildrenByType(ast.NodeAttributeView)
 			av.BatchUpsertBlockRel(avNodes)
 		}
+
+		// 如果数据库中绑定的块不在导入的文档中
+		cachedTrees, saveTrees := map[string]*parse.Tree{}, map[string]*parse.Tree{}
+		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)
+		}
 	}
 
 	// 将关联的闪卡数据合并到默认卡包 data/storage/riff/20230218211946-2kw8jgx 中