From 127327060fd45dbf0f009969cafaabe64ae97444 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Fri, 24 Feb 2023 23:24:40 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E6=94=B9=E8=BF=9B=E9=97=AA=E5=8D=A1?= =?UTF-8?q?=E5=AD=98=E5=82=A8=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84=EF=BC=8C?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E4=B8=80=E4=B8=AA=E5=9D=97=E5=AF=B9=E5=BA=94?= =?UTF-8?q?=E5=A4=9A=E5=BC=A0=E9=97=AA=E5=8D=A1=20https://github.com/siyua?= =?UTF-8?q?n-note/siyuan/issues/7417?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/model/block.go | 2 ++ kernel/model/flashcard.go | 76 +++++++++++++++------------------------ 2 files changed, 31 insertions(+), 47 deletions(-) diff --git a/kernel/model/block.go b/kernel/model/block.go index 066c3a8f7..5fe311a1f 100644 --- a/kernel/model/block.go +++ b/kernel/model/block.go @@ -60,6 +60,8 @@ type Block struct { Sort int `json:"sort"` Created string `json:"created"` Updated string `json:"updated"` + + RiffCardID string `json:"riffCardID"` } func (block *Block) IsContainerBlock() bool { diff --git a/kernel/model/flashcard.go b/kernel/model/flashcard.go index 11be65d0b..8afc132a4 100644 --- a/kernel/model/flashcard.go +++ b/kernel/model/flashcard.go @@ -43,8 +43,6 @@ var deckLock = sync.Mutex{} func GetTreeFlashcards(rootID string, page int) (blocks []*Block, total, pageCount int) { blocks = []*Block{} - - const pageSize = 20 deck := Decks[builtinDeckID] if nil == deck { return @@ -59,49 +57,19 @@ func GetTreeFlashcards(rootID string, page int) (blocks []*Block, total, pageCou } } allBlockIDs = gulu.Str.RemoveDuplicatedElem(allBlockIDs) - sort.Strings(allBlockIDs) + cards := deck.GetCardsByBlockIDs(allBlockIDs) - start := (page - 1) * pageSize - end := page * pageSize - if start > len(allBlockIDs) { - start = len(allBlockIDs) - } - if end > len(allBlockIDs) { - end = len(allBlockIDs) - } - blockIDs := allBlockIDs[start:end] - total = len(allBlockIDs) - pageCount = int(math.Ceil(float64(total) / float64(pageSize))) - if 1 > len(blockIDs) { - blocks = []*Block{} - return - } - - sqlBlocks := sql.GetBlocks(blockIDs) - blocks = fromSQLBlocks(&sqlBlocks, "", 36) - if 1 > len(blocks) { - blocks = []*Block{} - return - } - - for i, b := range blocks { - if nil == b { - blocks[i] = &Block{ - ID: blockIDs[i], - Content: Conf.Language(180), - } - } - } + blocks, total, pageCount = getCardsBlocks(cards, page) return } func GetFlashcards(deckID string, page int) (blocks []*Block, total, pageCount int) { blocks = []*Block{} - var allBlockIDs []string - const pageSize = 20 + var cards []riff.Card if "" == deckID { for _, deck := range Decks { - allBlockIDs = append(allBlockIDs, deck.GetBlockIDs()...) + blockIDs := deck.GetBlockIDs() + cards = append(cards, deck.GetCardsByBlockIDs(blockIDs)...) } } else { deck := Decks[deckID] @@ -109,28 +77,38 @@ func GetFlashcards(deckID string, page int) (blocks []*Block, total, pageCount i return } - allBlockIDs = append(allBlockIDs, deck.GetBlockIDs()...) + blockIDs := deck.GetBlockIDs() + cards = append(cards, deck.GetCardsByBlockIDs(blockIDs)...) } - allBlockIDs = gulu.Str.RemoveDuplicatedElem(allBlockIDs) - sort.Strings(allBlockIDs) + blocks, total, pageCount = getCardsBlocks(cards, page) + return +} +func getCardsBlocks(cards []riff.Card, page int) (blocks []*Block, total, pageCount int) { + const pageSize = 20 start := (page - 1) * pageSize end := page * pageSize - if start > len(allBlockIDs) { - start = len(allBlockIDs) + if start > len(cards) { + start = len(cards) } - if end > len(allBlockIDs) { - end = len(allBlockIDs) + if end > len(cards) { + end = len(cards) } - blockIDs := allBlockIDs[start:end] - total = len(allBlockIDs) + cardIDs := cards[start:end] + total = len(cards) pageCount = int(math.Ceil(float64(total) / float64(pageSize))) - if 1 > len(blockIDs) { + if 1 > len(cardIDs) { blocks = []*Block{} return } + var blockIDs []string + for _, card := range cards { + blockIDs = append(blockIDs, card.BlockID()) + } + sort.Strings(blockIDs) + sqlBlocks := sql.GetBlocks(blockIDs) blocks = fromSQLBlocks(&sqlBlocks, "", 36) if 1 > len(blocks) { @@ -144,7 +122,11 @@ func GetFlashcards(deckID string, page int) (blocks []*Block, total, pageCount i ID: blockIDs[i], Content: Conf.Language(180), } + + continue } + + b.RiffCardID = cards[i].ID() } return }