-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsettings.ts
184 lines (159 loc) · 6.73 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
import { App, ButtonComponent, Modal, PluginSettingTab, Setting, TFolder, TextComponent, normalizePath } from "obsidian";
import InstapaperPlugin from "./main";
import type { InstapaperAccessToken, InstapaperAccount } from "./api";
export interface InstapaperPluginSettings {
token?: InstapaperAccessToken;
account?: InstapaperAccount;
notesFolder: string;
notesCursor: number;
notesFrequency: number;
notesSyncOnStart: boolean;
}
export const DEFAULT_SETTINGS: InstapaperPluginSettings = {
token: undefined,
account: undefined,
notesFolder: 'Instapaper Notes',
notesCursor: 0,
notesFrequency: 0,
notesSyncOnStart: true,
}
export class InstapaperSettingTab extends PluginSettingTab {
plugin: InstapaperPlugin;
constructor(app: App, plugin: InstapaperPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
containerEl.createEl('h1', { text: 'Instapaper' });
// ACCOUNT
this.addAccountSetting(containerEl);
// NOTES SYNC
containerEl.createEl('h2', { text: 'Notes Sync' });
new Setting(containerEl)
.setName('Notes folder')
.setDesc('Folder in which your notes and highlights will be synced')
.addText((text) => {
text.setValue(this.plugin.settings.notesFolder)
text.onChange(async (value) => {
const previousPath = this.plugin.settings.notesFolder;
const newPath = normalizePath(value);
await this.plugin.saveSettings({ notesFolder: newPath });
const previousFile = this.app.vault.getAbstractFileByPath(previousPath);
if (previousFile instanceof TFolder) {
await previousFile.vault.rename(previousFile, newPath);
}
})
});
new Setting(containerEl)
.setName('Sync frequency')
.setDesc('The frequency at which Obsidian (when running) will automatically sync your notes')
.addDropdown((dropdown) => {
dropdown.addOption("0", "Manual");
dropdown.addOption("60", "Hourly");
dropdown.addOption("720", "Every 12 hours")
dropdown.addOption("1440", "Every 24 hours")
dropdown.setValue(Number(this.plugin.settings.notesFrequency).toString());
dropdown.onChange(async (value) => {
await this.plugin.saveSettings({ notesFrequency: parseInt(value) });
await this.plugin.updateNotesSyncInterval();
});
});
new Setting(containerEl)
.setName('Sync on start')
.setDesc('Automatically sync when Obsidian starts or an account is connected')
.addToggle((toggle) => {
toggle.setValue(this.plugin.settings.notesSyncOnStart);
toggle.onChange(async (value) => {
await this.plugin.saveSettings({ notesSyncOnStart: value });
});
});
}
private addAccountSetting(containerEl: HTMLElement): Setting {
const setting = new Setting(containerEl)
.setName('Instapaper account');
if (this.plugin.settings.account) {
return setting
.setDesc(`Connected as: ${this.plugin.settings.account.username}`)
.addButton((button) => {
button.setButtonText('Disconnect');
button.setTooltip('Disconnect your Instapaper account')
button.onClick(async () => {
this.plugin.disconnectAccount();
this.plugin.notice('Disconnected Instapaper account');
this.display();
})
});
}
return setting
.setDesc('Connect your Instapaper account')
.addButton((button) => {
button.setButtonText('Connect');
button.setTooltip('Connect your Instapaper account')
button.setCta();
button.onClick(async () => {
new ConnectAccountModal(this.app, async (username: string, password: string) => {
try {
const account = await this.plugin.connectAccount(username, password);
this.plugin.notice(`Connected Instapaper account: ${account.username}`);
} catch (e) {
console.log('Failed to connect account:', e);
this.plugin.disconnectAccount();
this.plugin.notice('Failed to connect Instapaper account');
}
this.display();
}).open();
})
});
}
}
class ConnectAccountModal extends Modal {
username: string
password: string
onConnect: (username: string, password: string) => void;
constructor(app: App, onConnect: (username: string, password: string) => void) {
super(app);
this.onConnect = onConnect;
}
onOpen() {
const { contentEl } = this;
contentEl.createEl("h1", { text: "Instapaper account" });
function updateConnectButton() {
const valid = usernameEl.checkValidity() && passwordEl.checkValidity();
connectButton.setDisabled(!valid);
}
const usernameEl = (new Setting(contentEl)
.setName("Email or username")
.addText((text) => {
text.inputEl.required = true;
text.onChange((value) => {
this.username = value;
updateConnectButton();
})
}).components[0] as TextComponent).inputEl;
const passwordEl = (new Setting(contentEl)
.setName("Password (if you have one)")
.addText((text) => {
text.inputEl.type = "password"
text.onChange((value) => {
this.password = value;
updateConnectButton();
})
}).components[0] as TextComponent).inputEl;
const connectButton = new Setting(contentEl)
.addButton((button) => {
button.setCta();
button.setButtonText("Connect");
button.setDisabled(true);
button.onClick(() => {
this.close();
this.onConnect(this.username, this.password);
})
}).components[0] as ButtonComponent;
}
onClose() {
const { contentEl } = this;
contentEl.empty();
}
}