generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
/
msgraphPluginSettings.ts
428 lines (379 loc) · 12.7 KB
/
msgraphPluginSettings.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
import { App, ButtonComponent, Setting } from 'obsidian'
import { PluginSettingTab } from 'obsidian'
import MSGraphPlugin from './MSGraphPlugin'
import { MSGraphAccount, MSGraphMailFolderAccess } from 'types';
import { SelectMailFolderModal } from 'selectMailFolderModal';
import { MailFolder } from '@microsoft/microsoft-graph-types';
import { defaultEventTemplate } from "./defaultEventTemplate"
import { defaultMailTemplate } from "./defaultMailTemplate"
import { defaultFlaggedMailTemplate } from "./defaultFlaggedMailTemplate"
export interface MSGraphPluginSettings {
accounts: Array<MSGraphAccount>,
mailFolders: Array<MSGraphMailFolderAccess>,
eventTemplate: string,
mailTemplate: string,
flaggedMailTemplate: string,
}
export const DEFAULT_SETTINGS: MSGraphPluginSettings = {
accounts: [new MSGraphAccount()],
mailFolders: [new MSGraphMailFolderAccess()],
eventTemplate: defaultEventTemplate,
mailTemplate: defaultMailTemplate,
flaggedMailTemplate: defaultFlaggedMailTemplate,
}
export class MSGraphPluginSettingsTab extends PluginSettingTab {
plugin: MSGraphPlugin;
constructor(app: App, plugin: MSGraphPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const {containerEl} = this;
containerEl.empty();
containerEl.createEl('h2', {text: 'Settings for MSGraphPlugin.'});
this.add_account_setting()
this.add_mail_settings()
this.containerEl.createEl("h2", { text: "Templates" });
const descHeading = document.createDocumentFragment();
descHeading.append(
"Templates for converting MSGraph objects to Markdown."
);
new Setting(this.containerEl).setDesc(descHeading);
this.add_event_template()
this.add_mail_template()
}
add_account_setting(): void {
this.containerEl.createEl("h2", { text: "MSGraph Access" });
const descHeading = document.createDocumentFragment();
descHeading.append(
"Each account you want to query MSGraph with needs to be authorized."
);
new Setting(this.containerEl).setDesc(descHeading);
new Setting(this.containerEl)
.setName("Add Account")
.setDesc("Add MSGraph Account")
.addButton((button: ButtonComponent) => {
button
.setTooltip("Add additional MSGraph account")
.setButtonText("+")
.setCta()
.onClick(() => {
this.plugin.settings.accounts.push(new MSGraphAccount());
this.plugin.saveSettings();
this.display();
});
});
this.plugin.settings.accounts.forEach(
(account, index) => {
const div = this.containerEl.createEl("div");
div.addClass("msgraph_div");
const title = this.containerEl.createEl("h4", {
text: "Account #" + index,
});
title.addClass("msgraph_title");
const s = new Setting(this.containerEl)
.addExtraButton((extra) => {
extra
.setIcon("cross")
.setTooltip("Delete")
.onClick(() => {
this.plugin.settings.accounts.splice(
index,
1
);
this.plugin.saveSettings();
// Force refresh
this.display();
});
})
.addText((text) => {
const t = text
.setPlaceholder("Display Name")
.setValue(account.displayName)
.onChange((new_value) => {
this.plugin.settings.accounts[index].displayName = new_value;
this.plugin.saveSettings();
});
t.inputEl.addClass("msgraph_display_name");
return t;
})
.addText((text) => {
const t = text
.setPlaceholder("Client Id")
.setValue(account.clientId)
.onChange((new_value) => {
this.plugin.settings.accounts[index].clientId = new_value;
this.plugin.saveSettings();
});
t.inputEl.addClass("msgraph_client_id");
return t;
})
.addText((text) => {
const t = text
.setPlaceholder("Client Secret (optional)")
.setValue(account.clientSecret)
.onChange((new_value) => {
this.plugin.settings.accounts[index].clientSecret = new_value;
this.plugin.saveSettings();
});
t.inputEl.addClass("msgraph_client_secret");
return t;
})
.addText((text) => {
const t = text
.setPlaceholder("authority")
.setValue(account.authority)
.onChange((new_value) => {
this.plugin.settings.accounts[index].authority = new_value;
this.plugin.saveSettings();
});
t.inputEl.addClass("msgraph_authority");
return t;
})
.addToggle((toggle) => {
toggle.setValue(this.plugin.checkProvider(this.plugin.settings.accounts[index].displayName))
.setTooltip("Authorize account")
.onChange((new_value) => {
const account = this.plugin.settings.accounts[index]
if (new_value) {
this.plugin.authenticateAccount(account)
account.enabled = true
} else {
// todo: de-authorize token
this.plugin.msalProviders[account.displayName].removeAccessToken()
delete this.plugin.msalProviders[account.displayName]
account.enabled = false
}
})
})
.addDropdown((dropdown) => {
dropdown.addOptions(
{MSGraph: "MSGraph", EWS: "EWS"}
)
.setValue(this.plugin.settings.accounts[index].type)
.onChange((new_value) => {
this.plugin.settings.accounts[index].type = new_value
this.plugin.saveSettings()
})
dropdown.selectEl.addClass("msgraph_provider_type")
})
.addText((text) => {
const t = text
.setPlaceholder("EWS URL")
.setValue(account.baseUri)
.onChange((new_value) => {
this.plugin.settings.accounts[index].baseUri = new_value;
this.plugin.saveSettings();
});
t.inputEl.addClass("msgraph_baseuri");
return t;
})
s.infoEl.remove();
div.appendChild(title);
div.appendChild(this.containerEl.lastChild);
}
);
}
add_mail_settings = (): void => {
this.containerEl.createEl("h2", { text: "Mail related settings" });
const descHeading = document.createDocumentFragment();
descHeading.append(
"Mail folders you want to access"
);
new Setting(this.containerEl).setDesc(descHeading);
new Setting(this.containerEl)
.setName("Add Mail Folder")
.addButton((button: ButtonComponent) => {
button
.setTooltip("Add additional mail folder")
.setButtonText("+")
.setCta()
.onClick(() => {
this.plugin.settings.mailFolders.push(new MSGraphMailFolderAccess());
this.plugin.saveSettings();
this.display();
});
});
this.plugin.settings.mailFolders.forEach(
(folder, index) => {
const div = this.containerEl.createEl("div");
div.addClass("msgraph_div");
const s = new Setting(this.containerEl)
.addExtraButton((extra) => {
extra
.setIcon("cross")
.setTooltip("Delete")
.onClick(() => {
this.plugin.settings.mailFolders.splice(
index,
1
);
this.plugin.saveSettings();
// Force refresh
this.display();
});
})
.addText((text) => {
const t = text
.setPlaceholder("Display Name")
.setValue(folder.displayName)
.onChange((new_value) => {
folder.displayName = new_value;
this.plugin.saveSettings();
});
t.inputEl.addClass("msgraph_display_name");
return t;
})
.addDropdown((dropdown) => {
const options: Record<string, string> = {invalid: "Choose Provider"}
for (const account of this.plugin.settings.accounts) {
options[account.displayName] = account.displayName
}
const d = dropdown
.addOptions(options)
.setValue(folder.provider)
.onChange((new_value) => {
folder.provider = new_value
this.plugin.saveSettings()
// Force refresh
this.display();
});
d.selectEl.addClass("msgraph_input")
})
.addText((text) => {
text
.setPlaceholder("<Id or path>")
.setValue(folder.id)
.onChange((new_value) => {
folder.id = new_value
this.plugin.saveSettings()
// Force refresh
this.display();
});
})
.addButton((button) => {
button
.setButtonText("Browse")
.setTooltip("Select search folder")
.onClick(() => {
if (this.plugin.checkProvider(folder.provider)) {
const modal = new SelectMailFolderModal(this.app, this.plugin)
modal.onChooseItem = (f:MailFolder, evt:Event) => {
folder.id = f.id
this.plugin.saveSettings()
this.display()
}
this.plugin.mailHandler
.getMailFolders(this.plugin.msalProviders[folder.provider])
.then((folders) => modal.setFolders(folders))
.then(() => modal.open())
}
})
})
.addText((text) => {
const t = text
.setPlaceholder("Maximum number of mails to return")
.setValue(folder.limit.toString())
.onChange((new_value) => {
const new_limit = parseInt(new_value, 10)
if (!isNaN(new_limit)) {
folder.limit = new_limit
this.plugin.saveSettings()
} else {
this.display()
}
})
t.inputEl.addClass("msgraph-number")
})
.addDropdown((dropdown) => {
dropdown
.addOptions({all: "All emails", flagged: "Only flagged emails"})
.setValue(folder.onlyFlagged ? 'flagged' : 'all')
.onChange((new_value) => {
folder.onlyFlagged = new_value === 'flagged'
this.plugin.saveSettings()
})
dropdown.selectEl.addClass("msgraph_input")
}
)
s.infoEl.remove();
div.appendChild(this.containerEl.lastChild);
});
}
add_event_template = (): void => {
new Setting(this.containerEl)
.setName("Event Template")
.setDesc("Template for rendering Events")
.addButton((button) => {
button
.setButtonText("Default")
.setTooltip("Restore default template")
.onClick(() => {
this.plugin.settings.eventTemplate = defaultEventTemplate
this.plugin.saveSettings();
// Force refresh
this.display();
});
})
.addTextArea((text) => {
const t = text
.setPlaceholder("Template Text")
.setValue(this.plugin.settings.eventTemplate)
.onChange((new_value) => {
this.plugin.settings.eventTemplate = new_value
this.plugin.saveSettings()
})
t.inputEl.addClass("msgraph_template")
})
}
add_mail_template = (): void => {
new Setting(this.containerEl)
.setName("Mail Template")
.setDesc("Template for rendering Mail items")
.addButton((button) => {
button
.setButtonText("Default")
.setTooltip("Restore default template")
.onClick(() => {
this.plugin.settings.mailTemplate = defaultMailTemplate
this.plugin.saveSettings();
// Force refresh
this.display();
});
})
.addTextArea((text) => {
const t = text
.setPlaceholder("Template Text")
.setValue(this.plugin.settings.mailTemplate)
.onChange((new_value) => {
this.plugin.settings.mailTemplate = new_value
this.plugin.saveSettings()
})
t.inputEl.addClass("msgraph_template")
})
new Setting(this.containerEl)
.setName("Flagged mail template")
.setDesc("Template for rendering flagged mail items as tasks")
.addButton((button) => {
button
.setButtonText("Default")
.setTooltip("Restore default template")
.onClick(() => {
this.plugin.settings.flaggedMailTemplate = defaultFlaggedMailTemplate
this.plugin.saveSettings();
// Force refresh
this.display();
});
})
.addTextArea((text) => {
const t = text
.setPlaceholder("Template Text")
.setValue(this.plugin.settings.flaggedMailTemplate)
.onChange((new_value) => {
this.plugin.settings.flaggedMailTemplate = new_value
this.plugin.saveSettings()
})
t.inputEl.addClass("msgraph_template")
})
}
}