-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsettings.ts
352 lines (313 loc) · 11.2 KB
/
settings.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import { App, Notice, PluginSettingTab, Setting } from "obsidian";
import DEFAULT_METADATA_TEMPLATE from "./assets/defaultMetadataTemplate.njk";
import templateInstructions from "./templates/templateInstructions.html";
import metadataTemplateInstructions from "./templates/metadataTemplateInstructions.html";
import filenameTemplateInstructions from "./templates/filenameTemplateInstructions.html";
import collectionGroupsInstructions from "./templates/collectionGroupsInstructions.html";
import appendModeInstructions from "./templates/appendModeInstructions.html";
import autoescapingInstructions from "./templates/autoescapingInstructions.html";
import type { RaindropAPI } from "./api";
import type RaindropPlugin from "./main";
import CollectionsModal from "./modal/collections";
import Renderer from "./renderer";
import ApiTokenModal from "./modal/apiTokenModal";
export class RaindropSettingTab extends PluginSettingTab {
private plugin: RaindropPlugin;
private api: RaindropAPI;
private renderer: Renderer;
constructor(app: App, plugin: RaindropPlugin, api: RaindropAPI) {
super(app, plugin);
this.plugin = plugin;
this.renderer = new Renderer(plugin);
this.api = api;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
if (this.plugin.settings.isConnected) {
this.disconnect();
} else {
this.connect();
}
this.ribbonIcon();
this.onlyBookmarksWithHl();
this.appendMode();
this.collectionsFolders();
this.highlightsFolder();
this.groups();
this.collections();
this.autoSyncInterval();
this.autoSyncSuccessNotice();
this.template();
this.metadataTemplate();
this.filenameTemplate();
this.autoescape();
this.resetSyncHistory();
}
private ribbonIcon(): void {
new Setting(this.containerEl).setName("Enable ribbon icon in the sidebar (need reload)").addToggle((toggle) => {
return toggle.setValue(this.plugin.settings.ribbonIcon).onChange(async (value) => {
this.plugin.settings.ribbonIcon = value;
await this.plugin.saveSettings();
});
});
}
private appendMode(): void {
const descFragment = document.createRange().createContextualFragment(appendModeInstructions);
new Setting(this.containerEl)
.setName("Append Mode")
.setDesc(descFragment)
.addToggle((toggle) => {
return toggle.setValue(this.plugin.settings.appendMode).onChange(async (value) => {
this.plugin.settings.appendMode = value;
await this.plugin.saveSettings();
});
});
}
private onlyBookmarksWithHl(): void {
new Setting(this.containerEl).setName("Only sync bookmarks with highlights").addToggle((toggle) => {
return toggle.setValue(this.plugin.settings.onlyBookmarksWithHl).onChange(async (value) => {
this.plugin.settings.onlyBookmarksWithHl = value;
await this.plugin.saveSettings();
});
});
}
private collectionsFolders(): void {
new Setting(this.containerEl).setName("Store the articles in collections folders").addToggle((toggle) => {
return toggle.setValue(this.plugin.settings.collectionsFolders).onChange(async (value) => {
this.plugin.settings.collectionsFolders = value;
await this.plugin.saveSettings();
});
});
}
private connect(): void {
new Setting(this.containerEl).setName("Connect to Raindrop.io").addButton((button) => {
return button
.setButtonText("Connect")
.setCta()
.onClick(async () => {
const tokenModal = new ApiTokenModal(this.app, this.api);
await tokenModal.waitForClose;
if (this.api.tokenManager.get()) {
new Notice("Token saved");
const user = await this.api.getUser();
this.plugin.settings.isConnected = true;
this.plugin.settings.username = user.fullName;
await this.plugin.saveSettings();
}
this.display(); // rerender
});
});
}
private async disconnect(): Promise<void> {
new Setting(this.containerEl)
.setName(`Connected to Raindrop.io as ${this.plugin.settings.username}`)
.addButton((button) => {
return button
.setButtonText("Test API")
.setCta()
.onClick(async () => {
try {
const user = await this.api.getUser();
new Notice(`Test pass, hello ${user.fullName}`);
} catch (e) {
console.error(e);
new Notice(`Test failed: ${e}`);
this.api.tokenManager.clear();
this.plugin.settings.isConnected = false;
this.plugin.settings.username = undefined;
await this.plugin.saveSettings();
}
});
})
.addButton((button) => {
return button
.setButtonText("Disconnect")
.setCta()
.onClick(async () => {
button.removeCta().setButtonText("Removing API token...").setDisabled(true);
try {
this.api.tokenManager.clear();
this.plugin.settings.isConnected = false;
this.plugin.settings.username = undefined;
await this.plugin.saveSettings();
} catch (e) {
console.error(e);
new Notice(`Token removed failed: ${e}`);
return;
}
new Notice("Token removed successfully");
this.display(); // rerender
});
});
}
private highlightsFolder(): void {
new Setting(this.containerEl)
.setName("Highlights folder location")
.setDesc("Vault folder to use for storing Raindrop.io highlights")
.addDropdown((dropdown) => {
const files = (this.app.vault.adapter as any).files;
Object.keys(files).forEach((key) => {
if (files[key].type == "folder") {
const folder = files[key].realpath;
dropdown.addOption(folder, folder);
}
});
return dropdown.setValue(this.plugin.settings.highlightsFolder).onChange(async (value) => {
this.plugin.settings.highlightsFolder = value;
await this.plugin.saveSettings();
});
});
}
private async groups(): Promise<void> {
const descFragment = document.createRange().createContextualFragment(collectionGroupsInstructions);
new Setting(this.containerEl)
.setName("Collection groups")
.setDesc(descFragment)
.addToggle((toggle) => {
return toggle.setValue(this.plugin.settings.collectionGroups).onChange(async (value) => {
this.plugin.settings.collectionGroups = value;
await this.plugin.saveSettings();
});
});
}
private async collections(): Promise<void> {
new Setting(this.containerEl)
.setName("Collections")
.setDesc("Manage collections to be synced")
.addButton((button) => {
return button
.setDisabled(!this.plugin.settings.isConnected)
.setButtonText("Manage")
.setCta()
.onClick(async () => {
button.setButtonText("Loading collections...");
// update for new collections
const collectionGroup = this.plugin.settings.collectionGroups;
const allCollections = await this.api.getCollections(collectionGroup);
this.plugin.updateCollectionSettings(allCollections);
new CollectionsModal(this.app, this.plugin);
this.display(); // rerender
});
});
}
private async template(): Promise<void> {
const templateDescFragment = document.createRange().createContextualFragment(templateInstructions);
new Setting(this.containerEl)
.setName("Content template")
.setDesc(templateDescFragment)
.setClass("raindrop-content-template")
.addTextArea((text) => {
text.setValue(this.plugin.settings.template).onChange(async (value) => {
const isValid = this.renderer.validate(value);
if (isValid) {
this.plugin.settings.template = value;
await this.plugin.saveSettings();
}
text.inputEl.style.border = isValid ? "" : "1px solid red";
});
return text;
});
}
private async metadataTemplate(): Promise<void> {
const templateDescFragment = document.createRange().createContextualFragment(metadataTemplateInstructions);
new Setting(this.containerEl)
.setName("Metadata template")
.setDesc(templateDescFragment)
.setClass("raindrop-metadata-template")
.addTextArea((text) => {
text.setPlaceholder(DEFAULT_METADATA_TEMPLATE);
text.setValue(this.plugin.settings.metadataTemplate).onChange(async (value) => {
const isValid = this.renderer.validate(value, true);
if (isValid) {
this.plugin.settings.metadataTemplate = value;
await this.plugin.saveSettings();
}
text.inputEl.style.border = isValid ? "" : "1px solid red";
});
return text;
});
}
private async filenameTemplate(): Promise<void> {
const templateDescFragment = document.createRange().createContextualFragment(filenameTemplateInstructions);
new Setting(this.containerEl)
.setName("Filename template")
.setDesc(templateDescFragment)
.setClass("raindrop-filename-template")
.addTextArea((text) => {
text.setValue(this.plugin.settings.filenameTemplate).onChange(async (value) => {
const isValid = this.renderer.validate(value, false);
if (isValid) {
this.plugin.settings.filenameTemplate = value;
await this.plugin.saveSettings();
}
text.inputEl.style.border = isValid ? "" : "1px solid red";
});
return text;
});
}
private resetSyncHistory(): void {
new Setting(this.containerEl)
.setName("Reset the last sync time for each collection")
.setDesc("This is useful if you want to resync all bookmarks.")
.addButton((button) => {
return button
.setButtonText("Reset")
.setDisabled(!this.plugin.settings.isConnected)
.setWarning()
.onClick(async () => {
for (const id in this.plugin.settings.syncCollections) {
const collection = this.plugin.settings.syncCollections[id];
collection.lastSyncDate = undefined;
}
this.plugin.saveSettings();
new Notice("Sync history reset successfully");
});
});
}
private autoSyncInterval(): void {
new Setting(this.containerEl)
.setName("Auto sync in interval (minutes)")
.setDesc("Sync every X minutes. To disable auto sync, specify negative value or zero (default)")
.addText((text) => {
text.setPlaceholder(String(0))
.setValue(this.plugin.settings.autoSyncInterval.toString())
.onChange(async (value) => {
if (!isNaN(Number(value))) {
const minutes = Number(value);
this.plugin.settings.autoSyncInterval = minutes;
await this.plugin.saveSettings();
console.info("Set raindrop.io autosync interval", minutes);
if (minutes > 0) {
this.plugin.clearAutoSync();
this.plugin.startAutoSync(minutes);
console.info(`Raindrop.io auto sync enabled! Every ${minutes} minutes.`);
} else {
this.plugin.clearAutoSync();
console.info("Raindrop.io auto sync disabled!");
}
}
});
});
}
private autoSyncSuccessNotice(): void {
new Setting(this.containerEl).setName("Display a notification when a collection is synced").addToggle((toggle) => {
return toggle.setValue(this.plugin.settings.autoSyncSuccessNotice).onChange(async (value) => {
this.plugin.settings.autoSyncSuccessNotice = value;
await this.plugin.saveSettings();
});
});
}
private autoescape(): void {
const templateDescFragment = document.createRange().createContextualFragment(autoescapingInstructions);
new Setting(this.containerEl)
.setName("Enable autoescaping for nunjucks")
.setDesc(templateDescFragment)
.addToggle((toggle) => {
return toggle.setValue(this.plugin.settings.autoescape).onChange(async (value) => {
this.plugin.settings.autoescape = value;
await this.plugin.saveSettings();
});
});
}
}