generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 261
Expand file tree
/
Copy pathnotes.ts
More file actions
87 lines (76 loc) · 3.31 KB
/
notes.ts
File metadata and controls
87 lines (76 loc) · 3.31 KB
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
import { Moment } from "moment";
import { App } from "obsidian";
import { RepItemScheduleInfo } from "src/algorithms/base/rep-item-schedule-info";
import { RepItemScheduleInfoOsr } from "src/algorithms/osr/rep-item-schedule-info-osr";
import { LEGACY_SCHEDULING_EXTRACTOR, MULTI_SCHEDULING_EXTRACTOR } from "src/constants";
import { IDataStore } from "src/data-stores/base/data-store";
import { RepItemStorageInfo } from "src/data-stores/base/rep-item-storage-info";
import { Question } from "src/question";
import { SRSettings } from "src/settings";
import { DateUtil, formatDateYYYYMMDD, globalDateProvider } from "src/utils/dates";
import { MultiLineTextFinder } from "src/utils/strings";
export class StoreInNotes implements IDataStore {
private settings: SRSettings;
app: App;
constructor(settings: SRSettings) {
this.settings = settings;
}
questionCreateSchedule(
originalQuestionText: string,
_: RepItemStorageInfo,
): RepItemScheduleInfo[] {
let scheduling: RegExpMatchArray[] = [
...originalQuestionText.matchAll(MULTI_SCHEDULING_EXTRACTOR),
];
if (scheduling.length === 0)
scheduling = [...originalQuestionText.matchAll(LEGACY_SCHEDULING_EXTRACTOR)];
const result: RepItemScheduleInfo[] = [];
for (let i = 0; i < scheduling.length; i++) {
const match: RegExpMatchArray = scheduling[i];
const dueDateStr = match[1];
const interval = parseInt(match[2]);
const ease = parseInt(match[3]);
const dueDate: Moment = DateUtil.dateStrToMoment(dueDateStr);
let info: RepItemScheduleInfo;
if (
dueDate == null ||
formatDateYYYYMMDD(dueDate) == RepItemScheduleInfoOsr.dummyDueDateForNewCard
) {
info = null;
} else {
const delayBeforeReviewTicks: number =
dueDate.valueOf() - globalDateProvider.today.valueOf();
info = new RepItemScheduleInfoOsr(dueDate, interval, ease, delayBeforeReviewTicks);
}
result.push(info);
}
return result;
}
questionRemoveScheduleInfo(questionText: string): string {
return questionText.replace(/<!--SR:.+-->/gm, "");
}
async questionWriteSchedule(question: Question): Promise<void> {
await this.questionWrite(question);
}
async questionWrite(question: Question): Promise<void> {
const fileText: string = await question.note.file.read();
const newText: string = question.updateQuestionWithinNoteText(fileText, this.settings);
await question.note.file.write(newText);
question.hasChanged = false;
}
async questionDelete(question: Question): Promise<void> {
const fileText: string = await question.note.file.read();
const originalText: string = question.questionText.original;
const newText = MultiLineTextFinder.findAndReplace(fileText, originalText, "");
if (newText) {
await question.note.file.write(newText);
} else {
console.error(
`questionDelete: Text not found: ${originalText.substring(
0,
100,
)} in note: ${fileText.substring(0, 100)}`,
);
}
}
}