Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement a timer in the presenter view #430

Merged
merged 6 commits into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/templates/bespoke/bespoke.scss
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,10 @@ $progress-height: 5px;
color: #999;
order: 3;
text-align: right;

&:hover {
cursor: pointer;
}
}
}
}
Expand Down
23 changes: 20 additions & 3 deletions src/templates/bespoke/presenter/presenter-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const presenterView = (deck) => {
</button>
</div>
<time class={classes.infoTime} title="Current time"></time>
<div class={classes.infoTimer}>{/* TODO: Implement timer */}</div>
<time class={classes.infoTimer} title="Timer"></time>
</div>
</>
)
Expand All @@ -76,6 +76,10 @@ const presenterView = (deck) => {
const subscribe = (deck) => {
// Next slide view
$(classes.nextContainer).addEventListener('click', () => deck.next())
$(classes.infoTimer).addEventListener(
'click',
() => (startTime = new Date())
)

const nextIframe = $(classes.next) as HTMLIFrameElement
const nextNav = createNavigateFunc(nextIframe)
Expand Down Expand Up @@ -133,8 +137,21 @@ const presenterView = (deck) => {
})

// Current time
const update = () =>
($(classes.infoTime).textContent = new Date().toLocaleTimeString())
let startTime = new Date()
const update = () => {
const time = new Date()

const formatTime = (time: number) => ('0' + Math.floor(time)).slice(-2)
chrisns marked this conversation as resolved.
Show resolved Hide resolved

const diff = time.getTime() - startTime.getTime()

const seconds = formatTime((diff / 1000) % 60)
const minutes = formatTime((diff / 1000 / 60) % 60)
const hours = formatTime((diff / (1000 * 60 * 60)) % 24)

$(classes.infoTime).textContent = time.toLocaleTimeString()
$(classes.infoTimer).textContent = `${hours}:${minutes}:${seconds}`
}

update()
setInterval(update, 250)
Expand Down
19 changes: 18 additions & 1 deletion test/templates/bespoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,24 @@ describe("Bespoke template's browser context", () => {
})
})
})

describe('Presenter timer', () => {
it('timer should start at 00:00:00', () =>
testPresenterView(() => {
expect($p(classes.infoTimer).textContent).toBe('00:00:00')
}))
it('timer should be at 00:00:01 after one second', () =>
testPresenterView(() => {
jest.advanceTimersByTime(12000)
expect($p(classes.infoTimer).textContent).toBe('00:00:12')
}))
it('timer should be at 00:00:00 after clicking', () =>
testPresenterView(() => {
jest.advanceTimersByTime(12000)
$p(classes.infoTimer).click()
jest.advanceTimersByTime(1200)
expect($p(classes.infoTimer).textContent).toBe('00:00:01')
}))
})
describe('Presenter note', () => {
it('marks element with bespoke-marp-note class and current slide index as active', () =>
testPresenterView(({ deck }) => {
Expand Down