-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
59 lines (51 loc) · 1.48 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
import { App, Plugin, PluginSettingTab, Setting } from 'obsidian';
import moment from 'moment';
interface ObsidianClockSettings {
clockFormatting: string;
}
const DEFAULT_SETTINGS: ObsidianClockSettings = {
clockFormatting: 'HH:mm',
};
export default class ObsidianClock extends Plugin {
settings: ObsidianClockSettings
async onload() {
await this.loadSettings();
this.addSettingTab(new ObsidianClockSettingTab(this.app, this));
const clock = this.addStatusBarItem();
setInterval( () => this.updateTime(clock), 1000 );
}
updateTime(clock: HTMLElement) {
let time = moment();
let formattedTime = time.format(this.settings.clockFormatting);
clock.setText(formattedTime);
}
onunload() {
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class ObsidianClockSettingTab extends PluginSettingTab {
plugin: ObsidianClock;
constructor(app: App, plugin: ObsidianClock) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const {containerEl} = this;
containerEl.empty();
new Setting(containerEl)
.setName('Formatting')
.setDesc('Formatting of the Clock (Example: HH:mm)')
.addText(text => text
.setPlaceholder('Enter formatting')
.setValue(this.plugin.settings.clockFormatting)
.onChange(async (value) => {
this.plugin.settings.clockFormatting = value;
await this.plugin.saveSettings();
}));
}
}