generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy patharrangement.ts
204 lines (194 loc) · 6.17 KB
/
arrangement.ts
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import { NewCardSearch } from "cardSearch"
import { CardsWatcher, NewCardsWatch } from "cardWatcher"
import { ParseRule } from "deck"
import i18next from "i18next"
import { RuleProperties } from "json-rules-engine"
import { Pattern } from "Pattern"
class ArrangementItem {
Name: TAGNAME
Count: number
Display: string
constructor(name: TAGNAME, count: number, display: string) {
this.Name = name
this.Count = count
this.Display = display
}
}
enum TAGNAME {
NEWTAG = "new",
REVIEWTAG = "review",
LEARNTAG = "learn",
ALLTAG = "all",
}
export class Stats {
NewCount: number
ReviewCount: number
LearnCount: number
}
export class PatternIter {
pattern: Pattern
index: number
total: number
constructor(pattern: Pattern, index: number, total: number) {
this.pattern = pattern
this.index = index
this.total = total
}
}
abstract class ArrangementBase {
abstract PatternSequence(Name: string): AsyncGenerator<PatternIter, boolean, unknown>
abstract ArrangementList(): ArrangementItem[]
abstract stats(): Stats
isOnlyAllTag(): boolean {
for (const item of this.ArrangementList()) {
if (item.Name !== TAGNAME.ALLTAG) {
return false;
}
}
return true;
}
}
function isToday(date: moment.Moment): boolean {
const todayStart = window.moment().startOf('day')
const todayEnd = window.moment().endOf('day')
return date.isBetween(todayStart, todayEnd, null, '[]');
}
export class Arrangement extends ArrangementBase {
private allPattern: Pattern[]
private newPattern: Pattern[]
private needReviewPattern: Pattern[]
private needLearn: Pattern[]
// private wait:Pattern[]
private watcher: CardsWatcher
constructor() {
super()
this.allPattern = []
this.newPattern = []
this.needReviewPattern = []
}
async init(rule:RuleProperties|null) {
let search = NewCardSearch()
let allcards = await search.search()
this.allPattern = []
this.newPattern = []
this.needReviewPattern = []
this.watcher = NewCardsWatch(allcards.AllCard)
for (let card of allcards.AllCard) {
for (let p of card.patterns) {
this.allPattern.push(p)
}
}
if (rule != null) {
this.allPattern = await ParseRule(rule, this.allPattern)
}
this.sort()
}
stats(): Stats {
let newCount = 0
let reviewCount = 0
let learnCount = 0
for (let p of this.allPattern) {
if (p.schedule.Last && p.schedule.Last != "") {
if (isToday(p.schedule.LastTime)) {
if (p.schedule.Opts.length == 1) {
newCount++
} else {
reviewCount++
}
}
}
if (p.schedule.Learned && p.schedule.Learned != "") {
if (isToday(p.schedule.LearnedTime)) {
learnCount++
}
}
}
let stats = new Stats
stats.LearnCount = learnCount
stats.NewCount = newCount
stats.ReviewCount = reviewCount
return stats
}
ArrangementList(): ArrangementItem[] {
let retlist: ArrangementItem[] = []
if (this.newPattern.length > 0) {
retlist.push(new ArrangementItem(TAGNAME.NEWTAG, this.newPattern.length, i18next.t('StartTextNew')))
}
if (this.needReviewPattern.length > 0) {
retlist.push(new ArrangementItem(TAGNAME.REVIEWTAG, this.needReviewPattern.length, i18next.t('StartTextReview')))
}
if (this.needLearn.length > 0) {
retlist.push(new ArrangementItem(TAGNAME.LEARNTAG, this.needLearn.length, i18next.t('StartTextLearn')))
}
if (this.allPattern.length > 0) {
retlist.push(new ArrangementItem(TAGNAME.ALLTAG, this.allPattern.length, i18next.t('StartTextALL')))
}
return retlist
}
private sort() {
let now = window.moment()
this.newPattern = []
this.needReviewPattern = []
this.needLearn = []
// this.wait = []
for (let p of this.allPattern) {
let learnInfo = p.schedule.LearnInfo
if (learnInfo.IsNew) {
this.newPattern.push(p)
} else if (p.schedule.NextTime.isBefore(now)) {
this.needReviewPattern.push(p)
} else if (learnInfo.IsLearn) {
this.needLearn.push(p)
}
}
this.newPattern.sort(() => {
return .5 - Math.random()
})
this.needReviewPattern.sort(() => {
return .5 - Math.random()
})
this.needLearn.sort((a, b) => {
if (a.schedule.LearnedTime.isAfter(b.schedule.LearnedTime)) {
return 1
}
if (a.schedule.LearnedTime.isSame(b.schedule.LearnedTime)) {
return 0
}
return -1
})
}
async findLivePattern(p: Pattern): Promise<Pattern | undefined> {
let liveCard = await this.watcher.getLiveCard(p.card)
if (!liveCard) {
return
}
for (let cardp of liveCard.patterns) {
if (p.TagID == cardp.TagID) {
return cardp
}
}
return
}
async *PatternSequence(name: string) {
let patterns = null;
if (name == TAGNAME.REVIEWTAG) {
patterns = this.needReviewPattern;
} else if (name == TAGNAME.NEWTAG) {
patterns = this.newPattern;
} else if (name == TAGNAME.LEARNTAG) {
patterns = this.needLearn;
} else if (name == TAGNAME.ALLTAG) {
patterns = this.allPattern;
}
if (patterns) {
for (let i = 0; i < patterns.length; i++) {
let p = patterns[i]
let cardp = await this.findLivePattern(p)
if (cardp) {
yield new PatternIter(cardp, i, patterns.length)
}
}
}
return true;
}
}