🎨 改进闪卡存储数据结构,支持一个块对应多张闪卡 https://github.com/siyuan-note/siyuan/issues/7417

This commit is contained in:
Liang Ding 2023-02-24 23:24:40 +08:00
parent 7bceb8e641
commit 127327060f
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
2 changed files with 31 additions and 47 deletions

View file

@ -60,6 +60,8 @@ type Block struct {
Sort int `json:"sort"` Sort int `json:"sort"`
Created string `json:"created"` Created string `json:"created"`
Updated string `json:"updated"` Updated string `json:"updated"`
RiffCardID string `json:"riffCardID"`
} }
func (block *Block) IsContainerBlock() bool { func (block *Block) IsContainerBlock() bool {

View file

@ -43,8 +43,6 @@ var deckLock = sync.Mutex{}
func GetTreeFlashcards(rootID string, page int) (blocks []*Block, total, pageCount int) { func GetTreeFlashcards(rootID string, page int) (blocks []*Block, total, pageCount int) {
blocks = []*Block{} blocks = []*Block{}
const pageSize = 20
deck := Decks[builtinDeckID] deck := Decks[builtinDeckID]
if nil == deck { if nil == deck {
return return
@ -59,49 +57,19 @@ func GetTreeFlashcards(rootID string, page int) (blocks []*Block, total, pageCou
} }
} }
allBlockIDs = gulu.Str.RemoveDuplicatedElem(allBlockIDs) allBlockIDs = gulu.Str.RemoveDuplicatedElem(allBlockIDs)
sort.Strings(allBlockIDs) cards := deck.GetCardsByBlockIDs(allBlockIDs)
start := (page - 1) * pageSize blocks, total, pageCount = getCardsBlocks(cards, page)
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),
}
}
}
return return
} }
func GetFlashcards(deckID string, page int) (blocks []*Block, total, pageCount int) { func GetFlashcards(deckID string, page int) (blocks []*Block, total, pageCount int) {
blocks = []*Block{} blocks = []*Block{}
var allBlockIDs []string var cards []riff.Card
const pageSize = 20
if "" == deckID { if "" == deckID {
for _, deck := range Decks { for _, deck := range Decks {
allBlockIDs = append(allBlockIDs, deck.GetBlockIDs()...) blockIDs := deck.GetBlockIDs()
cards = append(cards, deck.GetCardsByBlockIDs(blockIDs)...)
} }
} else { } else {
deck := Decks[deckID] deck := Decks[deckID]
@ -109,28 +77,38 @@ func GetFlashcards(deckID string, page int) (blocks []*Block, total, pageCount i
return return
} }
allBlockIDs = append(allBlockIDs, deck.GetBlockIDs()...) blockIDs := deck.GetBlockIDs()
cards = append(cards, deck.GetCardsByBlockIDs(blockIDs)...)
} }
allBlockIDs = gulu.Str.RemoveDuplicatedElem(allBlockIDs) blocks, total, pageCount = getCardsBlocks(cards, page)
sort.Strings(allBlockIDs) return
}
func getCardsBlocks(cards []riff.Card, page int) (blocks []*Block, total, pageCount int) {
const pageSize = 20
start := (page - 1) * pageSize start := (page - 1) * pageSize
end := page * pageSize end := page * pageSize
if start > len(allBlockIDs) { if start > len(cards) {
start = len(allBlockIDs) start = len(cards)
} }
if end > len(allBlockIDs) { if end > len(cards) {
end = len(allBlockIDs) end = len(cards)
} }
blockIDs := allBlockIDs[start:end] cardIDs := cards[start:end]
total = len(allBlockIDs) total = len(cards)
pageCount = int(math.Ceil(float64(total) / float64(pageSize))) pageCount = int(math.Ceil(float64(total) / float64(pageSize)))
if 1 > len(blockIDs) { if 1 > len(cardIDs) {
blocks = []*Block{} blocks = []*Block{}
return return
} }
var blockIDs []string
for _, card := range cards {
blockIDs = append(blockIDs, card.BlockID())
}
sort.Strings(blockIDs)
sqlBlocks := sql.GetBlocks(blockIDs) sqlBlocks := sql.GetBlocks(blockIDs)
blocks = fromSQLBlocks(&sqlBlocks, "", 36) blocks = fromSQLBlocks(&sqlBlocks, "", 36)
if 1 > len(blocks) { if 1 > len(blocks) {
@ -144,7 +122,11 @@ func GetFlashcards(deckID string, page int) (blocks []*Block, total, pageCount i
ID: blockIDs[i], ID: blockIDs[i],
Content: Conf.Language(180), Content: Conf.Language(180),
} }
continue
} }
b.RiffCardID = cards[i].ID()
} }
return return
} }