-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcard_source.go
109 lines (81 loc) · 2.2 KB
/
card_source.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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
}