-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathPodNotesSettingsTab.ts
More file actions
432 lines (373 loc) · 12.7 KB
/
PodNotesSettingsTab.ts
File metadata and controls
432 lines (373 loc) · 12.7 KB
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
429
430
431
432
import {
type App,
MarkdownRenderer,
Notice,
PluginSettingTab,
Setting,
TFile,
} from "obsidian";
import type PodNotes from "../../main";
import PodcastQueryGrid from "./PodcastQueryGrid.svelte";
import PlaylistManager from "./PlaylistManager.svelte";
import {
DownloadPathTemplateEngine,
TimestampTemplateEngine,
TranscriptTemplateEngine,
} from "../../TemplateEngine";
import { FilePathTemplateEngine } from "../../TemplateEngine";
import { episodeCache, savedFeeds } from "src/store/index";
import type { Episode } from "src/types/Episode";
import { get } from "svelte/store";
import { exportOPML, importOPML } from "src/opml";
import { Component } from "obsidian";
export class PodNotesSettingsTab extends PluginSettingTab {
plugin: PodNotes;
private podcastQueryGrid: PodcastQueryGrid;
private playlistManager: PlaylistManager;
private settingsTab: PodNotesSettingsTab;
constructor(app: App, plugin: PodNotes) {
super(app, plugin);
this.plugin = plugin;
this.settingsTab = this;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
const header = containerEl.createEl("h2", { text: "PodNotes" });
header.style.textAlign = "center";
const settingsContainer = containerEl.createDiv();
settingsContainer.classList.add("settings-container");
new Setting(settingsContainer)
.setName("Search Podcasts")
.setHeading()
.setDesc("Search for podcasts by name or custom feed URL.");
const queryGridContainer = settingsContainer.createDiv();
this.podcastQueryGrid = new PodcastQueryGrid({
target: queryGridContainer,
});
new Setting(settingsContainer)
.setName("Playlists")
.setHeading()
.setDesc("Add playlists to gather podcast episodes.");
const playlistManagerContainer = settingsContainer.createDiv();
this.playlistManager = new PlaylistManager({
target: playlistManagerContainer,
});
this.addDefaultPlaybackRateSetting(settingsContainer);
this.addSkipLengthSettings(settingsContainer);
this.addNoteSettings(settingsContainer);
this.addDownloadSettings(settingsContainer);
this.addImportExportSettings(settingsContainer);
this.addTranscriptSettings(settingsContainer);
}
hide(): void {
this.podcastQueryGrid?.$destroy();
this.playlistManager?.$destroy();
}
private addDefaultPlaybackRateSetting(container: HTMLElement): void {
new Setting(container)
.setName("Default Playback Rate")
.addSlider((slider) =>
slider
.setLimits(0.5, 4, 0.1)
.setValue(this.plugin.settings.defaultPlaybackRate)
.onChange((value) => {
this.plugin.settings.defaultPlaybackRate = value;
this.plugin.saveSettings();
})
.setDynamicTooltip(),
);
}
private addSkipLengthSettings(container: HTMLElement): void {
new Setting(container)
.setName("Skip backward length (s)")
.addText((textComponent) => {
textComponent.inputEl.type = "number";
textComponent
.setValue(`${this.plugin.settings.skipBackwardLength}`)
.onChange((value) => {
this.plugin.settings.skipBackwardLength = Number.parseInt(value);
this.plugin.saveSettings();
})
.setPlaceholder("seconds");
});
new Setting(container)
.setName("Skip forward length (s)")
.addText((textComponent) => {
textComponent.inputEl.type = "number";
textComponent
.setValue(`${this.plugin.settings.skipForwardLength}`)
.onChange((value) => {
this.plugin.settings.skipForwardLength = Number.parseInt(value);
this.plugin.saveSettings();
})
.setPlaceholder("seconds");
});
}
private addNoteSettings(settingsContainer: HTMLDivElement) {
const container = settingsContainer.createDiv();
container.createEl("h4", { text: "Note settings" });
const timestampSetting = new Setting(container)
.setName("Capture timestamp format")
.setHeading()
.addTextArea((textArea) => {
textArea.setValue(this.plugin.settings.timestamp.template);
textArea.setPlaceholder("- {{linktime}} ");
textArea.onChange((value) => {
this.plugin.settings.timestamp.template = value;
this.plugin.saveSettings();
updateTimestampDemo(value);
});
textArea.inputEl.style.width = "100%";
});
timestampSetting.settingEl.style.flexDirection = "column";
timestampSetting.settingEl.style.alignItems = "unset";
timestampSetting.settingEl.style.gap = "10px";
const timestampFormatDemoEl = container.createDiv();
const updateTimestampDemo = (value: string) => {
if (!this.plugin.api.podcast) return;
const demoVal = TimestampTemplateEngine(value);
timestampFormatDemoEl.empty();
MarkdownRenderer.renderMarkdown(
demoVal,
timestampFormatDemoEl,
"",
new Component(),
);
};
updateTimestampDemo(this.plugin.settings.timestamp.template);
const randomEpisode = getRandomEpisode();
const noteCreationFilePathSetting = new Setting(container)
.setName("Note creation file path")
.setHeading()
.addText((textComponent) => {
textComponent.setValue(this.plugin.settings.note.path);
textComponent.setPlaceholder(
"inputs/podcasts/{{podcast}} - {{title}}.md",
);
textComponent.onChange((value) => {
this.plugin.settings.note.path = value;
this.plugin.saveSettings();
const demoVal = FilePathTemplateEngine(value, randomEpisode);
noteCreationFilePathDemoEl.empty();
MarkdownRenderer.renderMarkdown(
demoVal,
noteCreationFilePathDemoEl,
"",
new Component(),
);
});
textComponent.inputEl.style.width = "100%";
});
noteCreationFilePathSetting.settingEl.style.flexDirection = "column";
noteCreationFilePathSetting.settingEl.style.alignItems = "unset";
noteCreationFilePathSetting.settingEl.style.gap = "10px";
const noteCreationFilePathDemoEl = container.createDiv();
const noteCreationSetting = new Setting(container)
.setName("Note creation template")
.setHeading()
.addTextArea((textArea) => {
textArea.setValue(this.plugin.settings.note.template);
textArea.onChange((value) => {
this.plugin.settings.note.template = value;
this.plugin.saveSettings();
});
textArea.inputEl.style.width = "100%";
textArea.inputEl.style.height = "25vh";
textArea.setPlaceholder(
"## {{title}}" +
"\n" +
"\n### Metadata" +
"\nPodcast:: {{podcast}}" +
"\nEpisode:: {{title}}" +
"\nPublishDate:: {{date:YYYY-MM-DD}}" +
"\n### Description" +
"\n> {{description}}",
"\n### Audio File URL" +
"\n> {{stream}}",
);
});
noteCreationSetting.settingEl.style.flexDirection = "column";
noteCreationSetting.settingEl.style.alignItems = "unset";
noteCreationSetting.settingEl.style.gap = "10px";
}
private addDownloadSettings(container: HTMLDivElement) {
container.createEl("h4", { text: "Download settings" });
const randomEpisode = getRandomEpisode();
const downloadPathSetting = new Setting(container)
.setName("Episode download path")
.setDesc(
"The path where the episode will be downloaded to. Avoid setting an extension, as it will be added automatically.",
)
.setHeading()
.addText((textComponent) => {
textComponent.setValue(this.plugin.settings.download.path);
textComponent.setPlaceholder("inputs/podcasts/{{podcast}} - {{title}}");
textComponent.onChange((value) => {
this.plugin.settings.download.path = value;
this.plugin.saveSettings();
const demoVal = DownloadPathTemplateEngine(value, randomEpisode);
downloadFilePathDemoEl.empty();
MarkdownRenderer.renderMarkdown(
`${demoVal}.mp3`,
downloadFilePathDemoEl,
"",
new Component(),
);
});
textComponent.inputEl.style.width = "100%";
});
downloadPathSetting.settingEl.style.flexDirection = "column";
downloadPathSetting.settingEl.style.alignItems = "unset";
downloadPathSetting.settingEl.style.gap = "10px";
const downloadFilePathDemoEl = container.createDiv();
}
private addImportExportSettings(containerEl: HTMLElement): void {
containerEl.createEl("h3", { text: "Import/Export" });
new Setting(containerEl)
.setName("Import OPML")
.setDesc("Import podcasts from an OPML file.")
.addButton((button) =>
button.setButtonText("Import").onClick(() => {
const fileInput = document.createElement("input");
fileInput.type = "file";
fileInput.accept = ".opml";
fileInput.style.display = "none";
document.body.appendChild(fileInput);
fileInput.click();
fileInput.onchange = async (e: Event) => {
const target = e.target as HTMLInputElement;
const file = target.files?.[0];
if (file) {
const reader = new FileReader();
reader.onload = async (event) => {
const contents = event.target?.result as string;
if (contents) {
try {
await importOPML(contents);
} catch (e) {
console.error("Error importing OPML:", e);
new Notice(
`Error importing OPML: ${e instanceof Error ? e.message : "Unknown error"}`,
10000,
);
}
}
};
reader.readAsText(file);
} else {
new Notice("No file selected");
}
};
}),
);
let exportFilePath = "PodNotes_Export.opml";
new Setting(containerEl)
.setName("Export OPML")
.setDesc("Export saved podcast feeds to an OPML file.")
.addText((text) =>
text
.setPlaceholder("Export file name")
.setValue(exportFilePath)
.onChange((value) => {
exportFilePath = value;
}),
)
.addButton((button) =>
button.setButtonText("Export").onClick(() => {
const feeds = Object.values(get(savedFeeds));
if (feeds.length === 0) {
new Notice("No podcasts to export.");
return;
}
exportOPML(
this.app,
feeds,
exportFilePath.endsWith(".opml")
? exportFilePath
: `${exportFilePath}.opml`,
);
}),
);
}
private addTranscriptSettings(container: HTMLDivElement) {
container.createEl("h4", { text: "Transcript settings" });
const randomEpisode = getRandomEpisode();
new Setting(container)
.setName("OpenAI API Key")
.setDesc("Enter your OpenAI API key for transcription functionality.")
.addText((text) => {
text
.setPlaceholder("Enter your OpenAI API key")
.setValue(this.plugin.settings.openAIApiKey)
.onChange(async (value) => {
this.plugin.settings.openAIApiKey = value;
await this.plugin.saveSettings();
});
text.inputEl.type = "password";
});
const transcriptFilePathSetting = new Setting(container)
.setName("Transcript file path")
.setDesc(
"The path where transcripts will be saved. Use {{}} for dynamic values.",
)
.addText((text) => {
text
.setPlaceholder("transcripts/{{podcast}}/{{title}}.md")
.setValue(this.plugin.settings.transcript.path)
.onChange(async (value) => {
this.plugin.settings.transcript.path = value;
await this.plugin.saveSettings();
updateTranscriptPathDemo(value);
});
});
const transcriptPathDemoEl = container.createDiv();
const updateTranscriptPathDemo = (value: string) => {
const demoVal = FilePathTemplateEngine(value, randomEpisode);
transcriptPathDemoEl.empty();
transcriptPathDemoEl.createEl("p", { text: `Example: ${demoVal}` });
};
updateTranscriptPathDemo(this.plugin.settings.transcript.path);
const transcriptTemplateSetting = new Setting(container)
.setName("Transcript template")
.setDesc("The template for the transcript file content.")
.setHeading()
.addTextArea((text) => {
text
.setPlaceholder(
"# {{title}}\n\nPodcast: {{podcast}}\nDate: {{date}}\nURL: {{url}}\n\n## Description\n\n{{description}}\n\n## Transcript\n\n{{transcript}}",
)
.setValue(this.plugin.settings.transcript.template)
.onChange(async (value) => {
this.plugin.settings.transcript.template = value;
await this.plugin.saveSettings();
});
text.inputEl.style.width = "100%";
text.inputEl.style.height = "25vh";
});
transcriptTemplateSetting.settingEl.style.flexDirection = "column";
transcriptTemplateSetting.settingEl.style.alignItems = "unset";
transcriptTemplateSetting.settingEl.style.gap = "10px";
}
}
function getRandomEpisode(): Episode {
const fallbackDemoObj = {
description: "demo",
content: "demo",
podcastName: "demo",
title: "demo",
url: "demo",
artworkUrl: "demo",
streamUrl: "demo",
episodeDate: new Date(),
feedUrl: "demo",
};
const feedEpisodes = Object.values(get(episodeCache));
if (!feedEpisodes.length) return fallbackDemoObj;
const randomFeed =
feedEpisodes[Math.floor(Math.random() * feedEpisodes.length)];
if (!randomFeed.length) return fallbackDemoObj;
const randomEpisode =
randomFeed[Math.floor(Math.random() * randomFeed.length)];
return randomEpisode;
}