From 9fd9ccd0385ba40693e2ca47294ee34eb3d802c4 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Sun, 19 Feb 2023 09:47:03 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E6=94=AF=E6=8C=81=E5=9F=BA=E4=BA=8E?= =?UTF-8?q?=E6=96=87=E6=A1=A3=E5=A4=8D=E4=B9=A0=E9=97=AA=E5=8D=A1=20https:?= =?UTF-8?q?//github.com/siyuan-note/siyuan/issues/7057?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/model/flashcard.go | 91 ++++++++++++++++++--------------------- 1 file changed, 41 insertions(+), 50 deletions(-) diff --git a/kernel/model/flashcard.go b/kernel/model/flashcard.go index 481e4f2f8..61d6ba695 100644 --- a/kernel/model/flashcard.go +++ b/kernel/model/flashcard.go @@ -45,40 +45,7 @@ var deckLock = sync.Mutex{} func GetTreeFlashcards(rootID string, page int) (blocks []*Block, total, pageCount int) { blocks = []*Block{} - tree, err := loadTreeByBlockID(rootID) - if nil != err { - return - } - - trees := []*parse.Tree{tree} - box := Conf.Box(tree.Box) - luteEngine := util.NewLute() - files := box.ListFiles(tree.Path) - for _, subFile := range files { - if !strings.HasSuffix(subFile.path, ".sy") { - continue - } - - subTree, loadErr := filesys.LoadTree(box.ID, subFile.path, luteEngine) - if nil != loadErr { - continue - } - - trees = append(trees, subTree) - } - - treeBlockIDs := map[string]bool{} - for _, t := range trees { - ast.Walk(t.Root, func(n *ast.Node, entering bool) ast.WalkStatus { - if !entering || !n.IsBlock() || ast.NodeDocument == n.Type { - return ast.WalkContinue - } - - treeBlockIDs[n.ID] = true - return ast.WalkContinue - }) - } - + treeBlockIDs := getTreeSubTreeChildBlocks(rootID) var allBlockIDs []string const pageSize = 20 deck := Decks[builtinDeckID] @@ -229,26 +196,12 @@ func GetTreeDueFlashcards(rootID string) (ret []*Flashcard, err error) { return } - tree, err := loadTreeByBlockID(rootID) - if nil != err { - return - } - - blockIDs := map[string]bool{} - ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus { - if !entering || !n.IsBlock() { - return ast.WalkContinue - } - - blockIDs[n.ID] = true - return ast.WalkContinue - }) - + treeBlockIDs := getTreeSubTreeChildBlocks(rootID) cards := deck.Dues() now := time.Now() for _, card := range cards { blockID := card.BlockID() - if !blockIDs[blockID] { + if !treeBlockIDs[blockID] { continue } @@ -269,6 +222,44 @@ func GetTreeDueFlashcards(rootID string) (ret []*Flashcard, err error) { return } +func getTreeSubTreeChildBlocks(rootID string) (treeBlockIDs map[string]bool) { + treeBlockIDs = map[string]bool{} + + tree, err := loadTreeByBlockID(rootID) + if nil != err { + return + } + + trees := []*parse.Tree{tree} + box := Conf.Box(tree.Box) + luteEngine := util.NewLute() + files := box.ListFiles(tree.Path) + for _, subFile := range files { + if !strings.HasSuffix(subFile.path, ".sy") { + continue + } + + subTree, loadErr := filesys.LoadTree(box.ID, subFile.path, luteEngine) + if nil != loadErr { + continue + } + + trees = append(trees, subTree) + } + + for _, t := range trees { + ast.Walk(t.Root, func(n *ast.Node, entering bool) ast.WalkStatus { + if !entering || !n.IsBlock() || ast.NodeDocument == n.Type { + return ast.WalkContinue + } + + treeBlockIDs[n.ID] = true + return ast.WalkContinue + }) + } + return +} + func GetDueFlashcards(deckID string) (ret []*Flashcard, err error) { deckLock.Lock() defer deckLock.Unlock()