-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmain.ts
More file actions
82 lines (70 loc) · 2.58 KB
/
Copy pathmain.ts
File metadata and controls
82 lines (70 loc) · 2.58 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
import { Plugin } from 'obsidian';
import type { IBookHighlightsPluginSettings } from './src/types';
import { IBookHighlightsPluginSearchModal } from './src/modals/searchSuggestions';
import { VaultManagement } from './src/modules/vaultManagement';
import { defaultPluginSettings, IBookHighlightsSettingTab } from './src/settings';
import { backupAndImport } from './src/utils/backupAndImportFlow';
import { saveKeepMeSectionData } from './src/utils/manageKeepMeSection';
import { showFailedImportNotice, showErrorInConsole } from './src/utils/notificationCenter';
export default class IBookHighlightsPlugin extends Plugin {
vault: VaultManagement;
settings: IBookHighlightsPluginSettings;
async onload() {
await this.loadSettings();
this.vault = new VaultManagement(this.app, this.settings);
this.addSettingTab(new IBookHighlightsSettingTab(this.app, this));
addRibbonAction(this);
addImportAllBooksCommand(this);
addImportOneBookCommand(this);
if (this.settings.importOnStart) {
this.app.workspace.onLayoutReady(async () => {
await backupAndImport(this, this.settings, 'modify');
});
}
this.registerEvent(
this.app.workspace.on('quick-preview', async (file, data) => {
await saveKeepMeSectionData(file, data, this, this.settings);
}),
);
}
onunload() {}
async loadSettings() {
this.settings = Object.assign({}, defaultPluginSettings, (await this.loadData()) as Partial<IBookHighlightsPluginSettings>);
return this.settings;
}
async saveSettings() {
await this.saveData(this.settings);
}
async onExternalSettingsChange(): Promise<void> {
await this.loadSettings();
this.vault = new VaultManagement(this.app, this.settings);
}
}
function addRibbonAction(plugin: IBookHighlightsPlugin) {
plugin.addRibbonIcon('book-open', `${plugin.manifest.name}: Import all`, async () => {
await backupAndImport(plugin, plugin.settings);
});
}
function addImportAllBooksCommand(plugin: IBookHighlightsPlugin) {
plugin.addCommand({
id: 'import-all-highlights',
name: 'Import all',
callback: async () => {
await backupAndImport(plugin, plugin.settings);
},
});
}
function addImportOneBookCommand(plugin: IBookHighlightsPlugin) {
plugin.addCommand({
id: 'import-single-highlights',
name: 'From a specific book...',
callback: () => {
try {
new IBookHighlightsPluginSearchModal(plugin.app, plugin).open();
} catch (error) {
showFailedImportNotice(plugin.manifest.name);
showErrorInConsole(plugin.manifest.name, error);
}
},
});
}