forked from trashhalo/obsidian-webhooks
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SettingsTab.ts
400 lines (365 loc) · 13.1 KB
/
SettingsTab.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
/* eslint-disable no-console */
import {
App,
PluginSettingTab,
Setting,
TAbstractFile,
TFolder,
} from 'obsidian'
import { MultiSuggest } from './MultiSuggest'
import { withConfirm } from './button'
import ObsidianAudioPenPlugin from '../main'
import {
Auth,
getAuth,
signInWithCustomToken,
signOut,
Unsubscribe,
} from 'firebase/auth'
import { NewLineType } from '@shared/types'
export interface MyPluginSettings {
token: string
frequency: string
triggerOnLoad: boolean
error?: string
newLineType?: NewLineType
tagsAsLinks?: boolean
linkProperty?: string
markdownTemplate?: string
folderPath: string
updateMode?: 'overwrite' | 'append' | 'prepend' | 'new'
useCustomTemplate: boolean
}
export const DEFAULT_SETTINGS: MyPluginSettings = {
token: '',
frequency: '0', // manual by default
triggerOnLoad: true,
newLineType: undefined,
tagsAsLinks: true, // Default to rendering tags as links
linkProperty: 'x', // Default link property
markdownTemplate: '',
folderPath: '',
updateMode: 'new',
useCustomTemplate: false,
}
export class AudioPenSettingTab extends PluginSettingTab {
plugin: ObsidianAudioPenPlugin
auth: Auth
authObserver: Unsubscribe
constructor(oApp: App, plugin: ObsidianAudioPenPlugin) {
super(oApp, plugin)
this.plugin = plugin
this.auth = getAuth(this.plugin.firebase)
this.authObserver = this.auth.onAuthStateChanged(this.display.bind(this))
}
hide(): void {
this.authObserver()
}
async display() {
if (!this) {
return
}
this.plugin.loadSettings()
let { containerEl } = this
containerEl.empty()
// General Settings - Header
containerEl.createEl('h2', { text: 'AudioPen-Obsidian' })
if (this.plugin.settings.error) {
containerEl.createEl('p', {
text: `error: ${this.plugin.settings.error}`,
})
}
// Settings for Logged-In Users
if (this.auth.currentUser) {
containerEl
.createEl('p', { text: 'See your buffer and read the docs at ' })
.createEl('a', {
text: 'AudioPen-Obsidian',
href: 'https://audiopen-obsidian.web.app',
})
new Setting(containerEl)
.setName(`logged in as ${this.auth.currentUser.email}`)
.addButton((button) => {
button
.setButtonText('Logout')
.setCta()
.onClick(async (evt) => {
try {
await signOut(this.auth)
this.plugin.settings.error = undefined
} catch (err) {
this.plugin.settings.error = err.message
} finally {
await this.plugin.saveSettings()
this.display()
}
})
})
new Setting(containerEl)
.setName('Destination folder')
.setDesc('Select the folder where new notes will be created')
.addText((text) => {
const inputEl = text.inputEl
const getContent = () => {
const rootFolder = this.app.vault.getAbstractFileByPath('/')
const folderOptions = this.getFolderOptions(rootFolder)
return new Set(folderOptions)
}
const onSelectCb = async (value: string) => {
this.plugin.settings.folderPath = value
await this.plugin.saveSettings()
this.display()
}
const multiSuggest = new MultiSuggest(
inputEl,
getContent,
this.app,
onSelectCb
)
inputEl.addEventListener('input', () => multiSuggest.open())
inputEl.addEventListener('focus', () => multiSuggest.open())
inputEl.value = this.plugin.settings.folderPath || ''
})
new Setting(containerEl)
.setName('Update mode')
.setDesc(
'How to handle existing files when receiving updates to an existing AudioPen note (identified by AudioPen ID). Append and prepend will only insert the new/edited summary.'
)
.addDropdown((dropdown) => {
dropdown
.addOption('overwrite', 'Overwrite existing file')
.addOption('append', 'Append (note only) to existing file')
.addOption('prepend', 'Prepend (note only) to existing file')
.addOption('new', 'Always create a new file')
.setValue(this.plugin.settings.updateMode || 'new')
.onChange(async (value) => {
// @ts-ignore
this.plugin.settings.updateMode = value
await this.plugin.saveSettings()
this.display()
})
})
new Setting(containerEl)
.setName('New line')
.setDesc(
'When appending/prepending, should we add new lines between the existing content and the new content?'
)
.addDropdown((dropdown) => {
dropdown.addOption('none', 'No new lines')
dropdown.addOption('windows', 'Windows style newlines')
dropdown.addOption('unixMac', 'Linux, Unix or Mac style new lines')
const { newLineType } = this.plugin.settings
if (newLineType === undefined) {
dropdown.setValue('none')
} else if (newLineType == NewLineType.Windows) {
dropdown.setValue('windows')
} else if (newLineType == NewLineType.UnixMac) {
dropdown.setValue('unixMac')
}
dropdown.onChange(async (value) => {
if (value == 'none') {
this.plugin.settings.newLineType = undefined
} else if (value == 'windows') {
this.plugin.settings.newLineType = NewLineType.Windows
} else if (value == 'unixMac') {
this.plugin.settings.newLineType = NewLineType.UnixMac
}
await this.plugin.saveSettings()
this.display()
})
})
if (!this.plugin.settings.useCustomTemplate) {
new Setting(containerEl)
.setName('Render tags as')
.setDesc('How should we render AudioPen tags?')
.addDropdown((dropdown) => {
dropdown
.addOption('links', '[[Links]] to notes')
.addOption('tags', 'Simple #tags')
.setValue(this.plugin.settings.tagsAsLinks ? 'links' : 'tags')
.onChange(async (value) => {
this.plugin.settings.tagsAsLinks = value === 'links'
await this.plugin.saveSettings()
this.display()
})
})
if (this.plugin.settings.tagsAsLinks) {
new Setting(containerEl)
.setName('Link property')
.setDesc(
"Frontmatter property for tags as links (e.g., 'x', 'links')"
)
.addText((text) => {
text
.setPlaceholder('x')
.setValue(this.plugin.settings.linkProperty || 'x')
.onChange(async (value) => {
this.plugin.settings.linkProperty = value
await this.plugin.saveSettings()
this.display()
})
})
}
}
containerEl.createEl('h2', { text: 'Advanced settings' })
containerEl.createEl('p', {
text: 'You can use custom templates to render your notes and make them yours. If you break the template, you may lose data from the buffer.',
})
new Setting(containerEl)
.setName('Use custom template')
.setDesc(
'Toggle between using the default template or a custom template'
)
.addToggle((toggle) =>
toggle
.setValue(this.plugin.settings.useCustomTemplate)
.onChange(async (value) => {
this.plugin.settings.useCustomTemplate = value
await this.plugin.saveSettings()
this.display()
})
)
if (this.plugin.settings.useCustomTemplate) {
containerEl.createEl('p', {
text: 'You can use the following variables in your template: {title}, {body}, {orig_transcript}, {id}, {date_created},{date_formatted}, {tagsAsLinks}, {tagsAsTags}',
})
containerEl.createEl('a', {
text: 'Check the README for more information',
href: 'https://github.com/jonashaefele/audiopen-obsidian#custom-templates',
})
new Setting(containerEl)
.setName('Custom template')
.setDesc(
'Select a Markdown file from your vault to use as a custom template'
)
.addText((text) => {
const inputEl = text.inputEl
const getContent = () => {
const markdownFiles = this.app.vault.getMarkdownFiles()
return new Set(markdownFiles.map((file) => file.path))
}
const onSelectCb = async (value: string) => {
this.plugin.settings.markdownTemplate = value
await this.plugin.saveSettings()
this.display()
}
const multiSuggest = new MultiSuggest(
inputEl,
getContent,
this.app,
onSelectCb
)
inputEl.addEventListener('input', () => multiSuggest.open())
inputEl.addEventListener('focus', () => multiSuggest.open())
inputEl.value = this.plugin.settings.markdownTemplate || ''
})
}
containerEl.createEl('h2', { text: 'Danger zone' })
new Setting(containerEl)
.setName(`Reset all settings to defaults.`)
.addButton(
withConfirm((button) => {
button
.setButtonText('Reset Settings')
.setCta()
.onClick(async (evt) => {
try {
await signOut(this.auth)
this.plugin.settings = DEFAULT_SETTINGS
} catch (err) {
this.plugin.settings.error = err.message
} finally {
await this.plugin.saveSettings()
this.display()
}
})
})
)
} // end this.auth.currentuser - logged
else {
// Settings for Logged-Out Users (need to log in)
containerEl
.createEl('p', { text: 'Generate a login token at ' })
.createEl('a', {
text: 'AudioPen-Obsidian',
href: 'https://audiopen-obsidian.web.app',
})
new Setting(containerEl).setName('Webhook login token').addText((text) =>
text
.setPlaceholder('Paste your token')
.setValue(this.plugin.settings.token)
.onChange(async (value) => {
this.plugin.settings.token = value
await this.plugin.saveSettings()
})
)
new Setting(containerEl)
.setName('Login')
.setDesc('Exchanges webhook token for authentication')
.addButton((button) => {
button
.setButtonText('Login')
.setCta()
.onClick(async (evt) => {
try {
await signInWithCustomToken(
this.auth,
this.plugin.settings.token
)
this.plugin.settings.token = ''
this.plugin.settings.error = undefined
} catch (err) {
this.plugin.settings.error = err.message
} finally {
await this.plugin.saveSettings()
this.display()
}
})
})
} // end logged-out
containerEl.createEl('h2', { text: 'Support' })
containerEl.createEl('p', {
text: 'If you like this plugin and get value from it, consider donating to support continued development and to help cover server costs.',
})
const donateKoFi = new Setting(this.containerEl)
.setName('Buy me a Coffee')
.setDesc('One-off or recurring donations are welcome. Thank you!')
const kofi = document.createElement('a')
kofi.setAttribute('href', 'https://ko-fi.com/jonashaefele')
const kofiImg = document.createElement('img')
kofiImg.src = 'https://storage.ko-fi.com/cdn/kofi3.png?v=3'
kofiImg.height = 36
kofiImg.setAttribute(
'style',
'border:0px; height:36px; border-radius: 18px;'
)
kofiImg.alt = 'Buy Me a Coffee at ko-fi.com'
kofi.appendChild(kofiImg)
donateKoFi.controlEl.appendChild(kofi)
const donateGH = new Setting(this.containerEl)
.setName('Become a Github Sponsor')
.setDesc('For the fellow developers out there. Thank you!')
const ghSponsor = document.createElement('iframe')
ghSponsor.setAttribute(
'src',
'https://github.com/sponsors/jonashaefele/button'
)
ghSponsor.setAttribute('title', 'Sponsor jonashaefele')
ghSponsor.setAttribute('height', '32')
ghSponsor.setAttribute('width', '114')
ghSponsor.setAttribute('style', 'border: 0; border-radius: 6px;')
donateGH.controlEl.appendChild(ghSponsor)
}
getFolderOptions(folder: TAbstractFile): string[] {
const options: string[] = []
if (folder instanceof TFolder) {
options.push(folder.path)
folder.children.forEach((child) => {
if (child instanceof TFolder) {
options.push(...this.getFolderOptions(child))
}
})
}
return options
}
}