generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
77 lines (62 loc) · 1.8 KB
/
main.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
import { App, Plugin, PluginSettingTab, Setting } from 'obsidian';
interface AudioPausePluginSettings {
resetToBeginning: boolean;
}
const DEFAULT_SETTINGS: AudioPausePluginSettings = {
resetToBeginning: false
}
export default class AudioPausePlugin extends Plugin {
settings: AudioPausePluginSettings;
private audioElements: HTMLAudioElement[] = [];
async onload() {
await this.loadSettings();
this.registerDomEvent(document, 'play', (evt: Event) => {
const target = evt.target;
if (target instanceof HTMLAudioElement) {
this.pauseOtherAudio(target);
if (!this.audioElements.includes(target)) {
this.audioElements.push(target);
}
}
}, true);
this.addSettingTab(new AudioPauseSettingTab(this.app, this));
}
onunload() {
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
private pauseOtherAudio(currentAudio: HTMLAudioElement) {
this.audioElements.forEach(audio => {
if (audio !== currentAudio && !audio.paused) {
audio.pause();
if (this.settings.resetToBeginning) {
audio.currentTime = 0;
}
}
});
}
}
class AudioPauseSettingTab extends PluginSettingTab {
plugin: AudioPausePlugin;
constructor(app: App, plugin: AudioPausePlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const {containerEl} = this;
containerEl.empty();
new Setting(containerEl)
.setName('Reset to beginning')
.setDesc('When enabled, other audio clips will be reset to the beginning instead of just pausing.')
.addToggle(toggle => toggle
.setValue(this.plugin.settings.resetToBeginning)
.onChange(async (value) => {
this.plugin.settings.resetToBeginning = value;
await this.plugin.saveSettings();
}));
}
}