-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
81 lines (70 loc) · 2.76 KB
/
Copy pathmain.ts
File metadata and controls
81 lines (70 loc) · 2.76 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
import { Plugin, TFile } from "obsidian";
import { GoodMemSyncSettingTab, DEFAULT_SETTINGS } from "./src/settings";
import { SyncManager, type GoodMemSyncSettings } from "./src/syncManager";
export default class GoodMemSyncPlugin extends Plugin {
settings: GoodMemSyncSettings = { ...DEFAULT_SETTINGS };
private syncManager!: SyncManager;
private statusEl?: HTMLElement;
override async onload(): Promise<void> {
await this.loadSettings();
this.statusEl = this.addStatusBarItem();
this.statusEl.setText("GoodMem: idle");
this.syncManager = new SyncManager(this.app, () => this.settings, (text) => this.statusEl?.setText(text));
this.addSettingTab(new GoodMemSyncSettingTab(this.app, this));
this.addCommand({
id: "goodmem-sync-initial-sync",
name: "GoodMem Sync: Initial sync all notes",
callback: () => {
void this.syncManager.initialSyncAllMarkdownFiles({
onProgress: ({ done, total, failed }) => {
if (!this.statusEl) return;
this.statusEl.setText(`GoodMem: syncing ${done}/${total}${failed > 0 ? ` (${failed} failed)` : ""}`);
if (done >= total) this.statusEl.setText("GoodMem: idle");
}
});
}
});
this.addCommand({
id: "goodmem-sync-current-note",
name: "GoodMem Sync: Sync current note now",
checkCallback: (checking) => {
const file = this.app.workspace.getActiveFile();
const enabled = !!file && file.extension === "md";
if (checking) return enabled;
if (!file) return;
void this.syncManager.syncNow(file.path);
}
});
// Sync on every Markdown file save (Obsidian fires "modify" after writes).
this.registerEvent(
this.app.vault.on("modify", (file) => {
if (!(file instanceof TFile)) return;
if (file.extension !== "md") return;
this.syncManager.queueSync(file.path);
})
);
if (this.settings.initialSyncOnStartup) {
this.app.workspace.onLayoutReady(() => {
void this.syncManager.initialSyncAllMarkdownFiles({
onProgress: ({ done, total, failed }) => {
if (!this.statusEl) return;
this.statusEl.setText(`GoodMem: syncing ${done}/${total}${failed > 0 ? ` (${failed} failed)` : ""}`);
if (done >= total) this.statusEl.setText("GoodMem: idle");
}
});
});
}
}
override onunload(): void {
this.syncManager?.dispose();
this.statusEl?.remove();
this.statusEl = undefined;
}
async loadSettings(): Promise<void> {
const loaded = (await this.loadData()) as Partial<GoodMemSyncSettings> | null;
this.settings = Object.assign({}, DEFAULT_SETTINGS, loaded ?? {});
}
async saveSettings(): Promise<void> {
await this.saveData(this.settings);
}
}