-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmain.ts
More file actions
160 lines (132 loc) · 4.38 KB
/
main.ts
File metadata and controls
160 lines (132 loc) · 4.38 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { Plugin } from "obsidian";
import { PluginSettings, DEFAULT_SETTINGS, SyncProgressSnapshot } from "./src/types";
import { RemarkableClient } from "./src/RemarkableClient";
import { SyncEngine } from "./src/SyncEngine";
import { RemarkableSyncSettingTab } from "./src/SettingsTab";
import { SyncProgressModal } from "./src/SyncProgressModal";
import { EpubView, EPUB_VIEW_TYPE } from "./src/EpubView";
export default class RemarkableSyncPlugin extends Plugin {
settings!: PluginSettings;
private client: RemarkableClient | null = null;
private syncEngine: SyncEngine | null = null;
private statusBarItemEl!: HTMLElement;
private syncProgressModal: SyncProgressModal | null = null;
async onload(): Promise<void> {
await this.loadPluginSettings();
// Initialize client if we have a device token
if (this.settings.deviceToken) {
this.initClient();
}
this.registerView(EPUB_VIEW_TYPE, (leaf) => new EpubView(leaf));
this.registerExtensions(["epub"], EPUB_VIEW_TYPE);
this.addSettingTab(new RemarkableSyncSettingTab(this.app, this));
// Status bar
const statusBarItem = this.addStatusBarItem();
this.statusBarItemEl = statusBarItem;
statusBarItem.addClass("mod-clickable");
statusBarItem.addEventListener("click", () => {
if (this.syncEngine?.syncing) {
this.showSyncProgress();
return;
}
void this.runSync({ showProgress: true });
});
this.updateStatusBar();
this.registerInterval(window.setInterval(() => this.updateStatusBar(), 1000));
// Commands
this.addCommand({
id: "sync-remarkable",
name: "Sync notes",
callback: () => { void this.runSync(); },
});
this.addCommand({
id: "force-sync-remarkable",
name: "Force re-sync all notes",
callback: async () => {
this.settings.syncState = {};
await this.savePluginSettings();
await this.runSync();
},
});
// Auto-sync on startup
if (this.settings.syncOnStartup && this.settings.deviceToken) {
this.app.workspace.onLayoutReady(() => {
// Small delay to let Obsidian fully initialize
setTimeout(() => { void this.runSync(); }, 3000);
});
}
}
onunload(): void {
// Cleanup if needed
}
// ── Public API ──────────────────────────────────────────────────────────
initClient(): void {
this.client = new RemarkableClient(
this.settings.deviceToken,
this.settings.deviceId,
);
this.syncEngine = new SyncEngine(
this.client,
this.app.vault,
this.settings,
() => this.savePluginSettings(),
);
}
async runSync(options: { showProgress?: boolean } = {}): Promise<void> {
const { showProgress = false } = options;
if (!this.syncEngine) {
return;
}
if (this.syncEngine.syncing) {
if (showProgress) {
this.showSyncProgress();
}
return;
}
if (showProgress) {
this.showSyncProgress();
}
await this.syncEngine.sync();
}
getSyncProgress(): SyncProgressSnapshot | null {
return this.syncEngine?.getProgressSnapshot() ?? null;
}
private showSyncProgress(): void {
if (!this.syncProgressModal) {
this.syncProgressModal = new SyncProgressModal(this.app, this, () => {
this.syncProgressModal = null;
});
}
this.syncProgressModal.open();
}
private updateStatusBar(): void {
if (!this.statusBarItemEl) {
return;
}
const progress = this.getSyncProgress();
if (!this.syncEngine?.syncing || !progress) {
this.statusBarItemEl.setText("Slate");
return;
}
if (progress.phase === "Listing cloud items") {
this.statusBarItemEl.setText(`Slate ${progress.inspectedItemCount}/${progress.cloudItemCount}`);
return;
}
if (progress.documentCount > 0) {
this.statusBarItemEl.setText(`Slate ${progress.processedDocumentCount}/${progress.documentCount}`);
return;
}
this.statusBarItemEl.setText(`Slate ${progress.phase}`);
}
// ── Settings Persistence ────────────────────────────────────────────────
async loadPluginSettings(): Promise<void> {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async savePluginSettings(): Promise<void> {
await this.saveData(this.settings);
// Update client token if it changed
if (this.client && this.settings.deviceToken) {
this.client.updateDeviceToken(this.settings.deviceToken);
}
}
}