-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountdownView.ts
More file actions
104 lines (83 loc) · 2.85 KB
/
Copy pathcountdownView.ts
File metadata and controls
104 lines (83 loc) · 2.85 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
import { ItemView, WorkspaceLeaf } from 'obsidian';
import SimpleCountdownPlugin from './main';
export const VIEW_TYPE_COUNTDOWN = 'countdown-view';
export class CountdownView extends ItemView {
plugin: SimpleCountdownPlugin;
private intervalId: number | null = null;
private countdownEl: HTMLElement | null = null;
constructor(leaf: WorkspaceLeaf, plugin: SimpleCountdownPlugin) {
super(leaf);
this.plugin = plugin;
}
getViewType(): string {
return VIEW_TYPE_COUNTDOWN;
}
getDisplayText(): string {
return 'Simple Countdown';
}
getIcon(): string {
return 'calendar-clock';
}
async onOpen() {
const container = this.containerEl.children[1];
container.empty();
this.countdownEl = container.createDiv({ cls: 'countdown-widget' });
this.updateCountdown();
this.intervalId = window.setInterval(
() => this.updateCountdown(),
60 * 1000
);
}
updateCountdown() {
if (!this.countdownEl) return;
const now = new Date();
const targetDate = new Date(this.plugin.settings.targetDate + 'T00:00:00');
// Clear existing content
this.countdownEl.empty();
if (isNaN(targetDate.getTime())) {
// Create error elements using DOM API
const errorEl = this.countdownEl.createDiv({ cls: 'countdown-error' });
errorEl.textContent = 'Invalid date format';
const eventEl = this.countdownEl.createDiv({ cls: 'countdown-event' });
eventEl.textContent = this.plugin.settings.eventName;
return;
}
const diffTime = targetDate.getTime() - now.getTime();
const diffDays = Math.ceil(diffTime / (1000 * 3600 * 24));
let displayText: string;
let labelText: string;
let showDateFooter: boolean;
if (diffDays === 0) {
displayText = 'Today';
labelText = '';
showDateFooter = false;
} else if (diffDays > 0) {
displayText = diffDays.toString();
labelText = diffDays === 1 ? 'day left' : 'days left';
showDateFooter = true;
} else {
displayText = Math.abs(diffDays).toString();
labelText = Math.abs(diffDays) === 1 ? 'day ago' : 'days ago';
showDateFooter = true;
}
// Create elements using DOM API
const daysEl = this.countdownEl.createDiv({ cls: 'countdown-days' });
daysEl.textContent = displayText;
const labelEl = this.countdownEl.createDiv({ cls: 'countdown-label' });
labelEl.textContent = labelText;
const eventEl = this.countdownEl.createDiv({ cls: 'countdown-event' });
eventEl.textContent = this.plugin.settings.eventName;
if (showDateFooter) {
const dateFooterEl = this.countdownEl.createDiv({
cls: 'countdown-date-footer',
});
dateFooterEl.textContent = this.plugin.settings.targetDate;
}
}
async onClose() {
if (this.intervalId) {
window.clearInterval(this.intervalId);
this.intervalId = null;
}
}
}