Browse Source

:art: Spaced repetition interface supports review by document selection https://github.com/siyuan-note/siyuan/issues/7954

Liang Ding 2 năm trước cách đây
mục cha
commit
0ebdd49f8a
2 tập tin đã thay đổi với 59 bổ sung8 xóa
  1. 10 0
      kernel/api/riff.go
  2. 49 8
      kernel/model/flashcard.go

+ 10 - 0
kernel/api/riff.go

@@ -27,6 +27,16 @@ import (
 	"github.com/siyuan-note/siyuan/kernel/util"
 	"github.com/siyuan-note/siyuan/kernel/util"
 )
 )
 
 
+func getRiffCardNotebooks(c *gin.Context) {
+	ret := gulu.Ret.NewResult()
+	defer c.JSON(http.StatusOK, ret)
+
+	notebooks := model.GetFlashcardNotebooks()
+	ret.Data = map[string]interface{}{
+		"notebooks": notebooks,
+	}
+}
+
 func getNotebookRiffCards(c *gin.Context) {
 func getNotebookRiffCards(c *gin.Context) {
 	ret := gulu.Ret.NewResult()
 	ret := gulu.Ret.NewResult()
 	defer c.JSON(http.StatusOK, ret)
 	defer c.JSON(http.StatusOK, ret)

+ 49 - 8
kernel/model/flashcard.go

@@ -38,8 +38,53 @@ import (
 	"github.com/siyuan-note/siyuan/kernel/util"
 	"github.com/siyuan-note/siyuan/kernel/util"
 )
 )
 
 
-var Decks = map[string]*riff.Deck{}
-var deckLock = sync.Mutex{}
+func GetFlashcardNotebooks() (ret []*Box) {
+	deck := Decks[builtinDeckID]
+	if nil == deck {
+		return
+	}
+	deckBlockIDs := deck.GetBlockIDs()
+
+	boxes := Conf.GetOpenedBoxes()
+	for _, box := range boxes {
+		if isNotebookContainFlashcard(box.ID, deckBlockIDs) {
+			ret = append(ret, box)
+		}
+	}
+	return
+}
+
+func isNotebookContainFlashcard(boxID string, deckBlockIDs []string) (ret bool) {
+	entries, err := os.ReadDir(filepath.Join(util.DataDir, boxID))
+	if nil != err {
+		logging.LogErrorf("read dir failed: %s", err)
+		return
+	}
+
+	for _, entry := range entries {
+		if entry.IsDir() {
+			continue
+		}
+
+		if !strings.HasSuffix(entry.Name(), ".sy") {
+			continue
+		}
+
+		rootID := strings.TrimSuffix(entry.Name(), ".sy")
+		blockIDs := getTreeSubTreeChildBlocks(rootID)
+		for _, blockID := range deckBlockIDs {
+			if gulu.Str.Contains(blockID, blockIDs) {
+				return true
+			}
+		}
+	}
+	return
+}
+
+var (
+	Decks    = map[string]*riff.Deck{}
+	deckLock = sync.Mutex{}
+)
 
 
 func GetNotebookFlashcards(boxID string, page int) (blocks []*Block, total, pageCount int) {
 func GetNotebookFlashcards(boxID string, page int) (blocks []*Block, total, pageCount int) {
 	blocks = []*Block{}
 	blocks = []*Block{}
@@ -66,9 +111,7 @@ func GetNotebookFlashcards(boxID string, page int) (blocks []*Block, total, page
 	var treeBlockIDs []string
 	var treeBlockIDs []string
 	for _, rootID := range rootIDs {
 	for _, rootID := range rootIDs {
 		blockIDs := getTreeSubTreeChildBlocks(rootID)
 		blockIDs := getTreeSubTreeChildBlocks(rootID)
-		for _, blockID := range blockIDs {
-			treeBlockIDs = append(treeBlockIDs, blockID)
-		}
+		treeBlockIDs = append(treeBlockIDs, blockIDs...)
 	}
 	}
 	treeBlockIDs = gulu.Str.RemoveDuplicatedElem(treeBlockIDs)
 	treeBlockIDs = gulu.Str.RemoveDuplicatedElem(treeBlockIDs)
 
 
@@ -307,9 +350,7 @@ func GetNotebookDueFlashcards(boxID string, reviewedCardIDs []string) (ret []*Fl
 	var treeBlockIDs []string
 	var treeBlockIDs []string
 	for _, rootID := range rootIDs {
 	for _, rootID := range rootIDs {
 		blockIDs := getTreeSubTreeChildBlocks(rootID)
 		blockIDs := getTreeSubTreeChildBlocks(rootID)
-		for _, blockID := range blockIDs {
-			treeBlockIDs = append(treeBlockIDs, blockID)
-		}
+		treeBlockIDs = append(treeBlockIDs, blockIDs...)
 	}
 	}
 	treeBlockIDs = gulu.Str.RemoveDuplicatedElem(treeBlockIDs)
 	treeBlockIDs = gulu.Str.RemoveDuplicatedElem(treeBlockIDs)