-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtomato.js
271 lines (231 loc) · 8.47 KB
/
tomato.js
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
/*
* Pomodoro timer for Trilium Notes.
* A gift designed for me, who suffers from lumbar disc herniation, reminding me to take a break every half an hour.
*
* 1. You can tweak configs in the `config` subnote.
* 2. i18n support, check the `translations` subnote for the translated texts.
* 3. There will be a notification tells you to rest :)
* 4. Buttons comes with sound effects so does the notification alarm.
*/
// Access translations based on the selected language
const i18n = key => translations.trans[config.lang][key];
const TPL = `
<audio id="tomatoButtonSound" src="custom/sound/tomatoButton.mp3"></audio>
<audio id="tomatoAlarmSound" src="custom/sound/tomatoAlarm.mp3"></audio>
<div id="tomato-bar" class="tomato">
<center><div id="status">` + i18n('working') + `</div></center>
<center><div id="timer">--:--</div></center>
<button id="workButton">` + i18n('doWork') + `</button>
<button id="restButton">` + i18n('takeBreak') + `</button>
<button id="startButton">` + i18n('start') + `</button>
<button id="pauseButton">` + i18n('pause') + `</button>
<button id="resetButton">` + i18n('reset') + `</button>
</div>
`;
const styles = `
#tomato-bar {
display: flex;
align-items: center;
gap: 4px;
padding: 4px;
border-top: 1px solid #555555;
border-bottom: 1px solid #555555;
contain: none;
white-space: nowrap;
}
#status {
font-size: 18px;
width: auto;
padding-left: 10px;
padding-right: 10px;
font-weight: bold;
}
#timer {
font-size: 24px;
width: 70px;
border: 2px solid #FF6347;
background-color: #transparent;
border-radius: 10px;
}
.tomato button {
background-color: #a52a2a;
border-radius: 5px;
border: none;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
transition: background-color 0.3s;
color: #ffffff;
}
.tomato button:hover {
background-color: #800000;
}
#workButton {
background-color: #2196F3;
}
#workButton:hover {
background-color: #1e87db;
}
#restButton {
background-color: #4CAF50;
}
#restButton:hover {
background-color: #45a049;
}
`;
var isInit = true;
var timerInterval;
class TomatoWidget extends api.NoteContextAwareWidget {
get position() { return 200; }
get parentWidget() { return "center-pane"; }
constructor() {
super();
this.title = "";
}
isEnabled() {
if (!super.isEnabled()) return false;
const widgetDisable = api.startNote.hasLabel("disable");
return !widgetDisable;
}
doRender() {
this.$widget = $(TPL);
this.timer = this.$widget.find("#timer");
this.status = this.$widget.find("#status");
const tomatoStatus = localStorage.getItem('tomatoStatus');
if (tomatoStatus) {
this.status.text(i18n(tomatoStatus));
}
this.cssBlock(styles);
return this.$widget;
}
async refreshWithNote() {
// prevent duplicate timer initialization
if (!isInit) {
return;
}
isInit = false;
const WORK_TIME = config.WORK_TIME;
const BREAK_TIME = config.BREAK_TIME;
let isWorking = localStorage.getItem('tomatoStatus') ? localStorage.getItem('tomatoStatus') == 'working' : true;
let time = localStorage.getItem('tomatoTime') ? parseInt(localStorage.getItem('tomatoTime')): WORK_TIME;
let running = false;
const timerDisplay = document.getElementById('timer');
const startButton = document.getElementById('startButton');
const pauseButton = document.getElementById('pauseButton');
const resetButton = document.getElementById('resetButton');
const workButton = document.getElementById('workButton');
const restButton = document.getElementById('restButton');
const tomatoButtonSound = document.getElementById("tomatoButtonSound");
const tomatoAlarmSound = document.getElementById("tomatoAlarmSound");
const playButtonSound = () => {
if (config.enableSound){
// Reset the play time to enable correct and smooth playback when the button is repeatedly and rapidly pressed
tomatoButtonSound.currentTime = 0;
tomatoButtonSound.play();
}
}
const playAlarmSound = () => {
if (config.enableSound){
tomatoAlarmSound.currentTime = 0;
tomatoAlarmSound.play();
}
}
const startTimer = () => {
if (!running) {
running = true;
timerInterval = setInterval(updateTimer, 1000);
}
};
const pauseTimer = () => {
playButtonSound();
running = false;
clearInterval(timerInterval);
};
const resetTimer = () => {
playButtonSound();
running = false;
clearInterval(timerInterval);
time = isWorking ? WORK_TIME : BREAK_TIME; // 25 minutes for work and 5 minutes for break
localStorage.setItem('tomatoTime', time);
updateTimer();
};
const restTimer = () => {
playButtonSound();
running = false;
clearInterval(timerInterval);
time = BREAK_TIME; // 5 minutes break
isWorking = false; // set rest status
localStorage.setItem('tomatoTime', time);
localStorage.setItem('tomatoStatus', 'resting');
this.status.text(i18n(localStorage.getItem('tomatoStatus')));
updateTimer();
startTimer();
};
const workTimer = () => {
playButtonSound();
running = false;
clearInterval(timerInterval);
time = WORK_TIME; // 25 minutes work
isWorking = true; // set work status
localStorage.setItem('tomatoTime', time);
localStorage.setItem('tomatoStatus', 'working');
this.status.text(i18n(localStorage.getItem('tomatoStatus')));
updateTimer();
startTimer();
};
const updateTimer = () => {
const minutes = Math.floor(time / 60);
let seconds = time % 60;
seconds = seconds < 10 ? '0' + seconds : seconds; // add leading zero if necessary
timerDisplay.textContent = `${minutes}:${seconds}`;
if (time > 0) {
time--;
localStorage.setItem('tomatoTime', time);
} else {
clearInterval(timerInterval);
if (isWorking) {
isWorking = false;
time = BREAK_TIME; // 5 minutes break
localStorage.setItem('tomatoStatus', 'resting');
this.status.text(i18n(localStorage.getItem('tomatoStatus')));
// System level notification, incase trilium is minimized
new window.Notification(i18n('restNotificationTitle'), {
body: i18n('restNotificationBody')
});
// Popup message inside trilium window
api.showMessage(i18n('takeBreakMessage'));
restTimer();
playAlarmSound();
} else {
isWorking = true;
time = WORK_TIME; // 25 minutes work
localStorage.setItem('tomatoTime', time);
localStorage.setItem('tomatoStatus', 'working');
this.status.text(i18n(localStorage.getItem('tomatoStatus')));
new window.Notification(i18n('workNotificationTitle'), {
body: i18n('workNotificationBody')
});
api.showMessage(i18n('timeToDoSomething'));
workTimer();
playAlarmSound();
}
startTimer();
}
};
startButton.addEventListener('click', () => {
playButtonSound();
startTimer();
});
pauseButton.addEventListener('click', pauseTimer);
resetButton.addEventListener('click', resetTimer);
workButton.addEventListener('click', workTimer);
restButton.addEventListener('click', restTimer);
// auto start timer when startup
if (config.autoStartCountDown){
startTimer();
}
}
async entitiesReloadedEvent({ loadResults }) {
}
}
module.exports = new TomatoWidget();