@@ -15,8 +15,11 @@ type PomodoroUpdate = {
1515 mode ?: PomodoroMode
1616 pause ?: number
1717 focus ?: boolean
18- timeFor ?: Partial < Record < PomodoroMode , number > >
1918 sound ?: boolean
19+ volume ?: number
20+ alarm ?: string
21+ listen ?: true
22+ timeFor ?: Partial < Record < PomodoroMode , number > >
2023 history ?: { endedAt : string ; duration ?: number }
2124}
2225
@@ -27,8 +30,6 @@ type PomodoroHistoryEntry = {
2730
2831let currentPomodoroData : Pomodoro
2932
30- const alarmSound = new Audio ( 'src/assets/sounds/clock-alarm-classic.mp3' )
31-
3233const pomodoroContainer = document . getElementById ( 'pomodoro_container' ) as HTMLDivElement
3334const pomodoroStart = document . getElementById ( 'pmdr_start' ) as HTMLButtonElement
3435const pomodoroPause = document . getElementById ( 'pmdr_pause' ) as HTMLButtonElement
@@ -44,6 +45,7 @@ let countdown: number
4445let timeModeTimeout : number
4546let tabTitleTimeout : number
4647const timeBeforeReset = 10000 // time before the timer resets after the end
48+ const alarmAudio : HTMLAudioElement = new Audio ( )
4749
4850const setModeButton = ( value = '' ) => {
4951 return ( document . getElementById ( `pmdr-${ value } ` ) as HTMLInputElement ) . checked = true
@@ -422,6 +424,7 @@ export function togglePomodoroFocus(focus: boolean) {
422424 clone . style . position = 'absolute'
423425 clone . style . top = originalRect . top + 'px'
424426 clone . style . left = originalRect . left + 'px'
427+ clone . style . fontSize = document . documentElement . style . getPropertyValue ( '--font-size' )
425428 clone . style . fontFamily = document . documentElement . style . getPropertyValue ( '--font-family' )
426429 clone . classList . add ( 'clone' )
427430
@@ -472,7 +475,7 @@ function ringTheAlarm() {
472475
473476 if ( willRingAndSave ) {
474477 if ( currentPomodoroData . sound ) {
475- alarmSound . play ( )
478+ playSound ( )
476479 }
477480
478481 // if pomodoro ends, registers new session
@@ -491,6 +494,15 @@ function ringTheAlarm() {
491494 }
492495}
493496
497+ function playSound ( ) {
498+ const filename = currentPomodoroData . alarm || 'marimba'
499+ const volume = currentPomodoroData . volume ?? .7
500+
501+ alarmAudio . src = `src/assets/sounds/${ filename } .mp3`
502+ alarmAudio . volume = volume
503+ alarmAudio . play ( )
504+ }
505+
494506function setPomodoroInfo ( history : PomodoroHistoryEntry [ ] ) {
495507 const now = new Date ( )
496508
@@ -522,42 +534,57 @@ function setPomodoroInfo(history: PomodoroHistoryEntry[]) {
522534 ; ( document . getElementById ( 'poms-month' ) as HTMLSpanElement ) . textContent = pomsMonth . toString ( )
523535}
524536
525- async function updatePomodoro ( { on , sound , end , mode , pause , focus , timeFor , history } : PomodoroUpdate ) {
537+ async function updatePomodoro ( update : PomodoroUpdate ) {
526538 const data = await storage . sync . get ( [ 'pomodoro' ] )
527539
528- if ( on !== undefined ) {
529- data . pomodoro . on = on
540+ if ( update . listen ) {
541+ playSound ( )
542+ return
543+ }
544+
545+ if ( update . on !== undefined ) {
546+ data . pomodoro . on = update . on
547+ }
548+
549+ if ( update . sound !== undefined ) {
550+ data . pomodoro . sound = update . sound
530551 }
531552
532- if ( sound !== undefined ) {
533- data . pomodoro . sound = sound
553+ if ( update . alarm ) {
554+ data . pomodoro . alarm = update . alarm
534555 }
535556
536- if ( end !== undefined ) {
537- data . pomodoro . end = end
557+ if ( update . volume ) {
558+ data . pomodoro . volume = update . volume
538559 }
539560
540- if ( mode ) {
541- data . pomodoro . mode = mode
561+ if ( update . end !== undefined ) {
562+ data . pomodoro . end = update . end
542563 }
543564
544- if ( pause !== undefined ) {
545- data . pomodoro . pause = pause
565+ if ( update . mode ) {
566+ data . pomodoro . mode = update . mode
546567 }
547568
548- if ( focus !== undefined ) {
549- data . pomodoro . focus = focus
569+ if ( update . pause !== undefined ) {
570+ data . pomodoro . pause = update . pause
550571 }
551572
552- if ( history !== undefined ) {
573+ if ( update . focus !== undefined ) {
574+ data . pomodoro . focus = update . focus
575+ }
576+
577+ if ( update . history !== undefined ) {
553578 data . pomodoro . history . push ( {
554- endedAt : history . endedAt ,
579+ endedAt : update . history . endedAt ,
555580 duration : data . pomodoro . timeFor [ 'pomodoro' ] ,
556581 } )
557582 }
558583
559584 // the time defined by the user for each mode (pomodoro, break...)
560- if ( timeFor ) {
585+ if ( update . timeFor ) {
586+ const { timeFor } = update
587+
561588 for ( const mode of Object . keys ( timeFor ) as PomodoroMode [ ] ) {
562589 const value = timeFor [ mode ]
563590
@@ -574,7 +601,7 @@ async function updatePomodoro({ on, sound, end, mode, pause, focus, timeFor, his
574601 // known flaw: sessions are only up to date on the ringing tab
575602 setPomodoroInfo ( data . pomodoro . history )
576603
577- if ( timeFor ) {
604+ if ( update . timeFor ) {
578605 resetTimer ( )
579606 }
580607}
0 commit comments