mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-17 23:20:13 +01:00
This commit is contained in:
parent
4e142fd079
commit
1c5eccd510
5 changed files with 107 additions and 0 deletions
99
kernel/model/flashcard.go
Normal file
99
kernel/model/flashcard.go
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
// SiYuan - Build Your Eternal Digital Garden
|
||||
// Copyright (c) 2020-present, b3log.org
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package model
|
||||
|
||||
import (
|
||||
"github.com/88250/lute/ast"
|
||||
"github.com/siyuan-note/logging"
|
||||
"github.com/siyuan-note/riff"
|
||||
"github.com/siyuan-note/siyuan/kernel/util"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var Decks = map[string]*riff.Deck{}
|
||||
var deckLock = sync.Mutex{}
|
||||
|
||||
func AddFlashcard(blockID string, deckName string) (err error) {
|
||||
deckLock.Lock()
|
||||
deck := Decks[deckName]
|
||||
deckLock.Unlock()
|
||||
|
||||
cardID := ast.NewNodeID()
|
||||
deck.AddCard(cardID, blockID)
|
||||
err = deck.Save()
|
||||
if nil != err {
|
||||
logging.LogErrorf("save deck [%s] failed: %s", deckName, err)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func InitFlashcards() {
|
||||
riffSavePath := filepath.Join(util.DataDir, "storage", "riff")
|
||||
entries, err := os.ReadDir(riffSavePath)
|
||||
if nil != err {
|
||||
logging.LogErrorf("read riff dir failed: %s", err)
|
||||
return
|
||||
}
|
||||
for _, entry := range entries {
|
||||
name := entry.Name()
|
||||
if strings.HasSuffix(name, "-deck.msgpack") {
|
||||
name = name[:len(name)-len("-deck.msgpack")]
|
||||
deckName := strings.Split(name, "-")[0]
|
||||
algo := strings.Split(name, "-")[1]
|
||||
deck, loadErr := riff.LoadDeck(riffSavePath, deckName, riff.Algo(algo))
|
||||
if nil != loadErr {
|
||||
logging.LogErrorf("load deck [%s] failed: %s", name, loadErr)
|
||||
continue
|
||||
}
|
||||
|
||||
deckLock.Lock()
|
||||
Decks[deckName] = deck
|
||||
deckLock.Unlock()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func CreateDeck(name string) (err error) {
|
||||
riffSavePath := filepath.Join(util.DataDir, "storage", "riff")
|
||||
deck, err := riff.LoadDeck(riffSavePath, name, riff.AlgoFSRS)
|
||||
if nil != err {
|
||||
logging.LogErrorf("load deck [%s] failed: %s", name, err)
|
||||
return
|
||||
}
|
||||
|
||||
deckLock.Lock()
|
||||
Decks[name] = deck
|
||||
deckLock.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
func SaveDeck(name string) (err error) {
|
||||
deckLock.Lock()
|
||||
deck := Decks[name]
|
||||
deckLock.Unlock()
|
||||
|
||||
err = deck.Save()
|
||||
if nil != err {
|
||||
logging.LogErrorf("save deck [%s] failed: %s", name, err)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue