generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.ts
144 lines (125 loc) · 4.68 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { Notice, Plugin } from "obsidian";
import { log } from "src/logger/logger";
import { DiffModal, type FileDiff } from "src/modals/conflict";
import { Syncinator as SyncinatorPlugin } from "src/plugin";
import { Disk } from "src/storage/storage";
import { EventBus, type ObsidianEventMap, type SnapshotEventMap } from "src/utils/eventBus";
import { SnapshotView, VIEW_TYPE_SNAPSHOT } from "src/views/snapshots";
import { ApiClient } from "./src/api/api";
import { HttpClient } from "./src/api/http";
import { WsClient } from "./src/api/ws";
import { DEFAULT_SETTINGS, type PluginSettings, SettingTab } from "./src/settings";
export default class Syncinator extends Plugin {
settings: PluginSettings = DEFAULT_SETTINGS;
private wsClient: WsClient;
private storage: Disk;
private apiClient: ApiClient;
async registerPlugin(snapshotEventBus: EventBus<SnapshotEventMap>) {
const obsidianEventBus = new EventBus<ObsidianEventMap>();
const plugin = new SyncinatorPlugin(
this.storage,
this.apiClient,
this.wsClient,
{
diffModal: this.wrappedDiffModal.bind(this),
snapshotEventBus,
obsidianEventBus,
},
{
conflictResolution: this.settings.conflictResolution,
},
);
await plugin.init();
this.registerEvent(
this.app.vault.on("create", (file) => {
obsidianEventBus.emit("create", { file });
}),
);
this.registerEvent(
this.app.vault.on("modify", (file) => {
obsidianEventBus.emit("modify", { file });
}),
);
this.registerEvent(
this.app.vault.on("delete", (file) => {
obsidianEventBus.emit("delete", { file });
}),
);
this.registerEvent(
this.app.vault.on("rename", (file, oldPath) => {
obsidianEventBus.emit("rename", { file, oldPath });
}),
);
}
private async refreshToken() {
try {
const res = await this.apiClient.login(
this.settings.workspaceName,
this.settings.workspacePass,
);
this.apiClient.setAuthorizationHeader(res.token);
this.wsClient.setAuthorization(res.token);
} catch (error) {
log.error(error);
}
}
async onload() {
// Settings
await this.loadSettings();
this.addSettingTab(new SettingTab(this.app, this));
log.setGlobalLevel(this.settings.logLevel);
// Init
this.storage = new Disk(this.app.vault);
const httpClient = new HttpClient(
this.settings.useTLS ? "https" : "http",
this.settings.domain,
{},
);
this.apiClient = new ApiClient(httpClient);
this.wsClient = new WsClient(this.settings.useTLS ? "wss" : "ws", this.settings.domain);
await this.refreshToken();
this.registerInterval(
window.setInterval(async () => await this.refreshToken(), 5 * 60 * 1000),
);
const snapshotEventBus = new EventBus<SnapshotEventMap>();
this.registerView(VIEW_TYPE_SNAPSHOT, (leaf) => new SnapshotView(leaf, snapshotEventBus));
this.app.workspace.onLayoutReady(() => this.activateSnapshotView());
// Deferred startup
setTimeout(async () => {
await this.registerPlugin(snapshotEventBus);
new Notice("Obsidian Live Syncinator inizialized");
}, 2000);
}
onunload() {
this.app.workspace.detachLeavesOfType(VIEW_TYPE_SNAPSHOT);
this.wsClient.close(true);
}
async loadSettings() {
this.settings = Object.assign({}, this.settings, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
async activateSnapshotView() {
const { workspace } = this.app;
// Check if view already exists
const existingView = workspace.getLeavesOfType(VIEW_TYPE_SNAPSHOT)[0];
if (existingView) {
// Focus the existing view
// workspace.revealLeaf(existingView);
return;
}
// Create a new leaf in the right sidebar
const leaf = workspace.getRightLeaf(false);
if (leaf) {
await leaf.setViewState({
type: VIEW_TYPE_SNAPSHOT,
active: false,
});
}
}
async wrappedDiffModal(filename: string, local: FileDiff, remote: FileDiff): Promise<string> {
const modal = new DiffModal(this.app, filename, local, remote);
return await modal.open();
}
}