-
-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathindex.ts
More file actions
75 lines (61 loc) · 2.18 KB
/
index.ts
File metadata and controls
75 lines (61 loc) · 2.18 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
import { setLanguage } from "@/i18n";
import "@/styles/main.scss";
import type { PluginManifest } from "obsidian";
import { type App, Plugin } from "obsidian";
import { TodoistApiClient } from "@/api";
import { ObsidianFetcher } from "@/api/fetcher";
import { registerCommands } from "@/commands";
import { QueryInjector } from "@/query/injector";
import { makeServices, type Services } from "@/services";
import { type Settings, useSettingsStore } from "@/settings";
import { SettingsTab } from "@/ui/settings";
// biome-ignore lint/style/noDefaultExport: We must use default export for Obsidian plugins
export default class TodoistPlugin extends Plugin {
public readonly services: Services;
constructor(app: App, pluginManifest: PluginManifest) {
super(app, pluginManifest);
this.services = makeServices(this);
}
async onload() {
const queryInjector = new QueryInjector(this);
this.registerMarkdownCodeBlockProcessor(
"todoist",
queryInjector.onNewBlock.bind(queryInjector),
);
this.addSettingTab(new SettingsTab(this.app, this));
registerCommands(this);
setLanguage(document.documentElement.lang);
await this.loadOptions();
this.app.workspace.onLayoutReady(async () => {
await this.loadApiClient();
});
}
private async loadApiClient(): Promise<void> {
const accessor = this.services.token;
if (await accessor.exists()) {
const token = await accessor.read();
await this.services.todoist.initialize(new TodoistApiClient(token, new ObsidianFetcher()));
return;
}
this.services.modals.onboarding({
onTokenSubmit: async (token) => {
await accessor.write(token);
await this.services.todoist.initialize(new TodoistApiClient(token, new ObsidianFetcher()));
},
});
}
async loadOptions(): Promise<void> {
const options = await this.loadData();
useSettingsStore.setState((old) => {
return {
...old,
...options,
};
}, true);
await this.saveData(useSettingsStore.getState());
}
async writeOptions(update: Partial<Settings>): Promise<void> {
useSettingsStore.setState(update);
await this.saveData(useSettingsStore.getState());
}
}