Browse Source

:art: Export related flashcard data when exporting .sy.zip https://github.com/siyuan-note/siyuan/issues/9372

Daniel 1 year ago
parent
commit
a8806968d0
3 changed files with 72 additions and 2 deletions
  1. 22 0
      kernel/model/export.go
  2. 35 2
      kernel/model/flashcard.go
  3. 15 0
      kernel/treenode/blocktree.go

+ 22 - 0
kernel/model/export.go

@@ -46,6 +46,7 @@ import (
 	"github.com/siyuan-note/filelock"
 	"github.com/siyuan-note/httpclient"
 	"github.com/siyuan-note/logging"
+	"github.com/siyuan-note/riff"
 	"github.com/siyuan-note/siyuan/kernel/av"
 	"github.com/siyuan-note/siyuan/kernel/filesys"
 	"github.com/siyuan-note/siyuan/kernel/sql"
@@ -1382,6 +1383,27 @@ func exportSYZip(boxID, rootDirPath, baseFolderName string, docPaths []string) (
 		})
 	}
 
+	// 导出闪卡 Export related flashcard data when exporting .sy.zip https://github.com/siyuan-note/siyuan/issues/9372
+	exportStorageRiffDir := filepath.Join(exportFolder, "storage", "riff")
+	deckID := ast.NewNodeID()
+	deck, loadErr := riff.LoadDeck(exportStorageRiffDir, deckID, Conf.Flashcard.RequestRetention, Conf.Flashcard.MaximumInterval, Conf.Flashcard.Weights)
+	for _, tree := range trees {
+		cards := getTreeFlashcards(tree.ID)
+		if nil != loadErr {
+			logging.LogErrorf("load deck [%s] failed: %s", name, loadErr)
+			continue
+		}
+
+		for _, card := range cards {
+			deck.AddCard(card.ID(), card.BlockID())
+		}
+	}
+	if 0 < deck.CountCards() {
+		if saveErr := deck.Save(); nil != saveErr {
+			logging.LogErrorf("save deck [%s] failed: %s", name, saveErr)
+		}
+	}
+
 	// 导出自定义排序
 	sortPath := filepath.Join(util.DataDir, box.ID, ".siyuan", "sort.json")
 	fullSortIDs := map[string]int{}

+ 35 - 2
kernel/model/flashcard.go

@@ -148,6 +148,12 @@ func GetNotebookFlashcards(boxID string, page int) (blocks []*Block, total, page
 
 func GetTreeFlashcards(rootID string, page int) (blocks []*Block, total, pageCount int) {
 	blocks = []*Block{}
+	cards := getTreeSubTreeFlashcards(rootID)
+	blocks, total, pageCount = getCardsBlocks(cards, page)
+	return
+}
+
+func getTreeSubTreeFlashcards(rootID string) (ret []riff.Card) {
 	deck := Decks[builtinDeckID]
 	if nil == deck {
 		return
@@ -162,9 +168,26 @@ func GetTreeFlashcards(rootID string, page int) (blocks []*Block, total, pageCou
 		}
 	}
 	allBlockIDs = gulu.Str.RemoveDuplicatedElem(allBlockIDs)
-	cards := deck.GetCardsByBlockIDs(allBlockIDs)
+	ret = deck.GetCardsByBlockIDs(allBlockIDs)
+	return
+}
 
-	blocks, total, pageCount = getCardsBlocks(cards, page)
+func getTreeFlashcards(rootID string) (ret []riff.Card) {
+	deck := Decks[builtinDeckID]
+	if nil == deck {
+		return
+	}
+
+	var allBlockIDs []string
+	deckBlockIDs := deck.GetBlockIDs()
+	treeBlockIDsMap, _ := getTreeBlocks(rootID)
+	for _, blockID := range deckBlockIDs {
+		if treeBlockIDsMap[blockID] {
+			allBlockIDs = append(allBlockIDs, blockID)
+		}
+	}
+	allBlockIDs = gulu.Str.RemoveDuplicatedElem(allBlockIDs)
+	ret = deck.GetCardsByBlockIDs(allBlockIDs)
 	return
 }
 
@@ -420,6 +443,16 @@ func getTreeSubTreeChildBlocks(rootID string) (treeBlockIDsMap map[string]bool,
 	return
 }
 
+func getTreeBlocks(rootID string) (treeBlockIDsMap map[string]bool, treeBlockIDs []string) {
+	treeBlockIDsMap = map[string]bool{}
+	bts := treenode.GetBlockTreesByRootID(rootID)
+	for _, bt := range bts {
+		treeBlockIDsMap[bt.ID] = true
+		treeBlockIDs = append(treeBlockIDs, bt.ID)
+	}
+	return
+}
+
 func getBoxBlocks(boxID string) (blockIDsMap map[string]bool, blockIDs []string) {
 	blockIDsMap = map[string]bool{}
 	bts := treenode.GetBlockTreesByBoxID(boxID)

+ 15 - 0
kernel/treenode/blocktree.go

@@ -226,6 +226,21 @@ func GetBlockTreesByPathPrefix(pathPrefix string) (ret []*BlockTree) {
 	return
 }
 
+func GetBlockTreesByRootID(rootID string) (ret []*BlockTree) {
+	blockTrees.Range(func(key, value interface{}) bool {
+		slice := value.(*btSlice)
+		slice.m.Lock()
+		for _, b := range slice.data {
+			if b.RootID == rootID {
+				ret = append(ret, b)
+			}
+		}
+		slice.m.Unlock()
+		return true
+	})
+	return
+}
+
 func RemoveBlockTreesByPathPrefix(pathPrefix string) {
 	var ids []string
 	blockTrees.Range(func(key, value interface{}) bool {