Skip to content
74 changes: 67 additions & 7 deletions card.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,32 @@ type Card interface {
// ID 返回闪卡 ID。
ID() string

// BlockID 返回闪卡关联的内容块 ID。
BlockID() string
// CardSourceID 返回关联的cardsource ID
CardSourceID() string

// GetGroup 返回当前卡片的Group
GetGroup() string

// SetGroup 设置当前卡片的Group
SetGroup(newGroup string)

// GetTag 返回当前卡片的Tag
GetTag() string

// SetTag 设置当前卡片的Tag
SetTag(newTag string)

// GetSuspend 返回当前卡片的暂停状态
GetSuspend() bool

// SwtichSuspend 切换当前卡片的暂停状态
SwtichSuspend()

// GetContext 返回当前卡片的Context
GetContext() map[string]string

// SetContext 使用 value 设置当前卡片的 key
SetContext(key string, value string)

// NextDues 返回每种评分对应的下次到期时间。
NextDues() map[Rating]time.Time
Expand Down Expand Up @@ -59,9 +83,13 @@ type Card interface {

// BaseCard 描述了基础的闪卡实现。
type BaseCard struct {
CID string
BID string
NDues map[Rating]time.Time
CID string
SID string
Group string
Tag string
Suspend bool
Context map[string]string
NDues map[Rating]time.Time
}

func (card *BaseCard) NextDues() map[Rating]time.Time {
Expand All @@ -76,6 +104,38 @@ func (card *BaseCard) ID() string {
return card.CID
}

func (card *BaseCard) BlockID() string {
return card.BID
func (card *BaseCard) CardSourceID() string {
return card.SID
}

func (card *BaseCard) GetGroup() string {
return card.Group
}

func (card *BaseCard) SetGroup(newGroup string) {
card.Group = newGroup
}

func (card *BaseCard) GetTag() string {
return card.Tag
}

func (card *BaseCard) SetTag(newTag string) {
card.Tag = newTag
}

func (card *BaseCard) GetSuspend() bool {
return card.Suspend
}

func (card *BaseCard) SwtichSuspend() {
card.Suspend = !card.Suspend
}

func (card *BaseCard) GetContext() map[string]string {
return card.Context
}

func (card *BaseCard) SetContext(key string, value string) {
card.Context[key] = value
}
109 changes: 109 additions & 0 deletions card_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package riff

type CardSource interface {

// CardSource的ID
SourceID() string

// 返回类型
CardType() CardType

// 返回卡源 Data
GetSourceData() string

// 更新卡源 Data
SetSourceData(NewData string)

// 返回关联的CardID list
GetCardIDs() []string

RemoveCardID(CardID CardID)

GetCardIDMap() map[string]string

// 更新关联的CardID list
SetCardIDMap(key CardKey, CardID CardID)

// 返回卡源的 Context
GetContext() map[string]string

// 设置卡源的 Context
SetContext(key string, value string)
}

type CardType string

type CardKey string

type CardID string

// BaseCardSource 描述了卡源的基础实现
type BaseCardSource struct {
SID string
CType CardType
CIDMap map[CardID]CardKey
Data string
Context map[string]string
}

const (
builtInCardType CardType = "siyuan_busic_card"
builtInCardIDMapKey string = "basic_card"
builtInContext string = "blockIDs"
)

func (source *BaseCardSource) SourceID() string {
return source.SID
}

func (source *BaseCardSource) CardType() CardType {
return source.CType
}

func (source *BaseCardSource) GetSourceData() string {
return source.Data
}

func (source *BaseCardSource) SetSourceData(NewData string) {
source.Data = NewData
}

func (source *BaseCardSource) GetCardIDs() []string {
var CIDs []string
for CID := range source.CIDMap {
CIDs = append(CIDs, string(CID))
}
return CIDs
}

func (source *BaseCardSource) RemoveCardID(cardID CardID) {

delete(source.CIDMap, cardID)

}

func (source *BaseCardSource) GetCardIDMap() map[string]string {
back := make(map[string]string)
for cardID, cardKey := range source.CIDMap {
back[string(cardKey)] = string(cardID)
}
return back
}

func (source *BaseCardSource) SetCardIDMap(key CardKey, cardID CardID) {
// source.CIDMap[key] = CardID
for CID, CKey := range source.CIDMap {
if CKey == key {
delete(source.CIDMap, CID)
}
}
source.CIDMap[cardID] = key
}

func (source *BaseCardSource) GetContext() map[string]string {
return source.Context
}

func (source *BaseCardSource) SetContext(key string, value string) {
source.Context[key] = value
}
26 changes: 26 additions & 0 deletions card_source_store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package riff

type CardSourceStore interface {
// 添加 CardSource 并添加对应卡片
AddCardSource(id string, CType CardType, cardIDMap map[string]string) CardSource

// 更新CardSource CIDMap 对应卡片,使其与 cardIDMap 一致
// 不存在则新建,已存在则不操作,在 cardIDMap 不存在则删除
UpdateCardSource(id string, cardIDMap map[string]string) error

// 通过 CardSourceID 获得 CardSource
GetCardSourceByID(id string) CardSource

// 通过 Card 获取 cardSource
GetCardSourceByCard(card Card) CardSource

// 设置 store 内相同 cardSourceID 的 cardsource 为传入的cardSource
SetCardSource(cardSource CardSource) (err error)

// 通过 id 删除 cardSource
RemoveCardSource(id string)

Load()

Save()
}
53 changes: 53 additions & 0 deletions card_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package riff

import (
"fmt"
"testing"
)

func TestCardSource(t *testing.T) {
sid := newID()
cid := newID()
cid2 := newID()
data := "1111"
context := map[string]string{
"aaa": "bbb",
}
basecardSource := &BaseCardSource{
SID: sid,
CType: builtInCardType,
CIDMap: map[CardID]CardKey{},
Data: data,
}
basecardSource.SetCardIDMap("card", CardID(cid))
var cardSource CardSource = basecardSource
if cardSource.SourceID() != sid {
t.Fatalf("cardSource id [%s] != [%s]", cardSource.SourceID(), sid)
}
if cardSource.CardType() != builtInCardType {
t.Fatalf("cardSource cardType [%s] != [%s]", cardSource.CardType(), builtInCardType)
}
if cardSource.GetSourceData() != data {
t.Fatalf("cardSource SourceData [%s] != [%s]", cardSource.GetSourceData(), data)
}
for key, value := range cardSource.GetContext() {
if context[key] != value {
t.Fatalf("cardSource context key [%s] value [%s] != [%s]", key, value, context[key])
}
}
if cardSource.GetCardIDMap()["card"] != cid {
t.Fatalf("cardSource CardIDMap key card [%s] != [%s]", cardSource.GetCardIDMap()["card"], cid)
}
cardSource.SetCardIDMap("card", CardID(cid2))
if cardSource.GetCardIDMap()["card"] != cid2 {
t.Fatalf("cardSource SetCardIDMap key card [%s] != [%s]", cardSource.GetCardIDMap()["card"], cid)
}
if len(cardSource.GetCardIDs()) != 1 {
t.Logf("current cardIDs is [%s]", fmt.Sprint(cardSource.GetCardIDs()))
t.Fatalf("cardSource GetCardIDs cardIDs len [%s] != 1", fmt.Sprint(len(cardSource.GetCardIDs())))
}
if cardSource.GetCardIDs()[0] != cid2 {
t.Fatalf("cardSource GetCardIDs cardIDs first [%s] != [%s] ", cardSource.GetCardIDs()[0], cid2)
}

}
Loading