generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 88
/
main.ts
71 lines (60 loc) · 2.25 KB
/
main.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
import { addIcon, Notice, Plugin, TFile } from 'obsidian';
import { ISettings } from 'src/conf/settings';
import { SettingsTab } from 'src/gui/settings-tab';
import { CardsService } from 'src/services/cards';
import { Anki } from 'src/services/anki';
import { noticeTimeout, flashcardsIcon } from 'src/conf/constants';
export default class ObsidianFlashcard extends Plugin {
private settings: ISettings
private cardsService: CardsService
async onload() {
addIcon("flashcards", flashcardsIcon)
// TODO test when file did not insert flashcards, but one of them is in Anki already
const anki = new Anki()
this.settings = await this.loadData() || this.getDefaultSettings()
this.cardsService = new CardsService(this.app, this.settings)
const statusBar = this.addStatusBarItem()
this.addCommand({
id: 'generate-flashcard-current-file',
name: 'Generate for the current file',
checkCallback: (checking: boolean) => {
const activeFile = this.app.workspace.getActiveFile()
if (activeFile) {
if (!checking) {
this.generateCards(activeFile)
}
return true;
}
return false;
}
});
this.addRibbonIcon('flashcards', 'Generate flashcards', () => {
const activeFile = this.app.workspace.getActiveFile()
if (activeFile) {
this.generateCards(activeFile)
} else {
new Notice("Open a file before")
}
});
this.addSettingTab(new SettingsTab(this.app, this));
this.registerInterval(window.setInterval(() =>
anki.ping().then(() => statusBar.setText('Anki ⚡️')).catch(() => statusBar.setText('')), 15 * 1000
));
}
async onunload() {
await this.saveData(this.settings);
}
private getDefaultSettings(): ISettings {
return { contextAwareMode: true, sourceSupport: false, codeHighlightSupport: false, inlineID: false, contextSeparator: " > ", deck: "Default", folderBasedDeck: true, flashcardsTag: "card", inlineSeparator: "::", inlineSeparatorReverse: ":::", defaultAnkiTag: "obsidian", ankiConnectPermission: false }
}
private generateCards(activeFile: TFile) {
this.cardsService.execute(activeFile).then(res => {
for (const r of res) {
new Notice(r, noticeTimeout)
}
console.log(res)
}).catch(err => {
Error(err)
})
}
}