generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.ts
87 lines (73 loc) · 2.77 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { Notice, Plugin } from 'obsidian';
import { IBookHighlightsPluginSearchModal, OverwriteBookModal } from './src/search';
import { aggregateBookAndHighlightDetails } from './src/methods/aggregateDetails';
import SaveHighlights from './src/methods/saveHighlightsToVault';
import { AppleBooksHighlightsImportPluginSettings, IBookHighlightsSettingTab } from './src/settings';
export default class IBookHighlightsPlugin extends Plugin {
settings: AppleBooksHighlightsImportPluginSettings;
saveHighlights: SaveHighlights;
async onload() {
const settings = await this.loadSettings();
this.saveHighlights = new SaveHighlights(this.app, settings);
if (settings.importOnStart) {
await this.aggregateAndSaveHighlights();
}
this.addRibbonIcon('book-open', this.manifest.name, async () => {
try {
this.settings.backup
? await this.aggregateAndSaveHighlights().then(() => {
new Notice('Apple Books highlights imported successfully');
}).catch((error) => {
new Notice(`[${this.manifest.name}]:\nError importing highlights. Check console for details (⌥ ⌘ I)`, 0);
console.error(`[${this.manifest.name}]: ${error}`);
})
: new OverwriteBookModal(this.app, this).open();
} catch (error) {
console.error(`[${this.manifest.name}]: ${error}`);
}
});
this.addSettingTab(new IBookHighlightsSettingTab(this.app, this));
this.addCommand({
id: 'import-all-highlights',
name: 'Import all',
callback: async () => {
try {
this.settings.backup
? await this.aggregateAndSaveHighlights()
: new OverwriteBookModal(this.app, this).open();
} catch (error) {
new Notice(`[${this.manifest.name}]:\nError importing highlights. Check console for details (⌥ ⌘ I)`, 0);
console.error(`[${this.manifest.name}]: ${error}`);
}
},
});
this.addCommand({
id: 'import-single-highlights',
name: 'From a specific book...',
callback: async () => {
try {
new IBookHighlightsPluginSearchModal(this.app, this).open();
} catch (error) {
new Notice(`[${this.manifest.name}]:\nError importing highlights. Check console for details (⌥ ⌘ I)`, 0);
console.error(`[${this.manifest.name}]: ${error}`);
}
},
});
}
//eslint-disable-next-line
onunload() { }
async loadSettings() {
this.settings = Object.assign(new AppleBooksHighlightsImportPluginSettings(), await this.loadData());
return this.settings;
}
async saveSettings() {
await this.saveData(this.settings);
}
async aggregateAndSaveHighlights(): Promise<void> {
const highlights = await aggregateBookAndHighlightDetails();
if (highlights.length === 0) {
throw ('No highlights found. Make sure you made some highlights in your Apple Books.');
}
await this.saveHighlights.saveAllBooksHighlightsToVault(highlights);
}
}