-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmain.ts
185 lines (161 loc) · 5.29 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { Notice, Plugin } from "obsidian";
import { RaindropSettingTab } from "./settings";
import RaindropSync from "./sync";
import type { RaindropCollection, RaindropPluginSettings, SyncCollection, SyncCollectionSettings } from "./types";
import { RaindropAPI } from "./api";
import { VERSION, DEFAULT_SETTINGS } from "./constants";
import BreakingChangeModal from "./modal/breakingChange";
import CollectionsModal from "./modal/collections";
import semver from "semver";
export default class RaindropPlugin extends Plugin {
private raindropSync: RaindropSync;
public settings: RaindropPluginSettings;
public api: RaindropAPI;
private timeoutIDAutoSync?: number;
async onload() {
await this.loadSettings();
this.api = new RaindropAPI(this.app);
this.raindropSync = new RaindropSync(this.app, this, this.api);
if (this.settings.ribbonIcon) {
this.addRibbonIcon("cloud", "Sync your Raindrop bookmarks", () => {
if (!this.settings.isConnected) {
new Notice("Please configure Raindrop API token in the plugin setting");
} else {
this.raindropSync.sync({ fullSync: false });
}
});
}
this.addCommand({
id: "raindrop-sync-new",
name: "Sync newly created bookmarks (sync from last sync time)",
callback: async () => {
await this.raindropSync.sync({ fullSync: false });
},
});
this.addCommand({
id: "raindrop-sync-all",
name: "Sync all bookmarks (full sync)",
callback: async () => {
await this.raindropSync.sync({ fullSync: true });
},
});
this.addCommand({
id: "raindrop-sync-this",
name: "Sync this bookmark",
callback: async () => {
const file = app.workspace.getActiveFile();
await this.raindropSync.syncSingle({ file: file });
},
});
this.addCommand({
id: "raindrop-show-last-sync-time",
name: "Show last sync time",
callback: async () => {
const message = Object.values(this.settings.syncCollections)
.filter((collection: SyncCollection) => collection.sync)
.map((collection: SyncCollection) => {
return `${collection.title}: ${collection.lastSyncDate?.toLocaleString()}`;
})
.join("\n");
new Notice(message);
},
});
this.addCommand({
id: "raindrop-open-link",
name: "Open link in Raindrop",
callback: async () => {
const file = app.workspace.getActiveFile();
if (file) {
const fmc = app.metadataCache.getFileCache(file)?.frontmatter;
if (fmc?.raindrop_id) {
const bookmark = await this.api.getRaindrop(fmc.raindrop_id);
window.open(`https://app.raindrop.io/my/${bookmark.collectionId}/item/${bookmark.id}/edit`);
} else {
new Notice("This is not a Raindrop bookmark file");
}
} else {
new Notice("No active file");
}
},
});
this.addCommand({
id: "raindrop-manage-collection",
name: "Manage collections to be synced",
callback: async () => {
const notice = new Notice("Loading collections...");
// update for new collections
const collectionGroup = this.settings.collectionGroups;
const allCollections = await this.api.getCollections(collectionGroup);
this.updateCollectionSettings(allCollections);
notice.hide();
new CollectionsModal(this.app, this);
},
});
this.addSettingTab(new RaindropSettingTab(this.app, this, this.api));
if (this.settings.autoSyncInterval) {
await this.startAutoSync();
}
}
async onunload() {
await this.clearAutoSync();
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
for (const id in this.settings.syncCollections) {
const collection = this.settings.syncCollections[id];
if (collection.lastSyncDate) {
collection.lastSyncDate = new Date(collection.lastSyncDate);
}
}
// version migration notice
new BreakingChangeModal(this.app, this.settings.version);
// setting migration
if (semver.lt(this.settings.version, "0.0.18")) {
if ("dateTimeFormat" in this.settings) {
// @ts-expect-error
delete this.settings["dateTimeFormat"];
}
}
this.settings.version = VERSION;
await this.saveSettings();
}
async saveSettings() {
await this.saveData(this.settings);
}
async updateCollectionSettings(collections: RaindropCollection[]) {
const syncCollections: SyncCollectionSettings = {};
collections.forEach(async (collection) => {
const { id, title } = collection;
if (!(id in this.settings.syncCollections)) {
syncCollections[id] = {
id: id,
title: title,
sync: false,
lastSyncDate: undefined,
};
} else {
syncCollections[id] = this.settings.syncCollections[id];
syncCollections[id].title = title;
}
});
this.settings.syncCollections = syncCollections;
await this.saveSettings();
}
async clearAutoSync(): Promise<void> {
if (this.timeoutIDAutoSync) {
window.clearTimeout(this.timeoutIDAutoSync);
this.timeoutIDAutoSync = undefined;
}
console.info("Clearing auto sync...");
}
async startAutoSync(minutes?: number): Promise<void> {
const minutesToSync = minutes ?? this.settings.autoSyncInterval;
if (minutesToSync > 0) {
this.timeoutIDAutoSync = window.setTimeout(() => {
this.raindropSync.sync({ fullSync: false });
this.startAutoSync();
}, minutesToSync * 60000);
}
console.info(`StartAutoSync: this.timeoutIDAutoSync ${this.timeoutIDAutoSync} with ${minutesToSync} minutes`);
}
}