Skip to content

Commit 3ba948c

Browse files
ken7253kermanx
andauthoredNov 18, 2024··
feat: pause/resume timer (#1928)
Co-authored-by: _Kerman <kermanx@qq.com>
1 parent a4456b7 commit 3ba948c

File tree

3 files changed

+38
-42
lines changed

3 files changed

+38
-42
lines changed
 
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { useInterval } from '@vueuse/core'
2+
import { computed } from 'vue'
3+
4+
export function useTimer() {
5+
const { counter, isActive, reset, pause, resume } = useInterval(1000, { controls: true })
6+
7+
const timer = computed(() => {
8+
const passed = counter.value
9+
const sec = Math.floor(passed % 60).toString().padStart(2, '0')
10+
const min = Math.floor(passed / 60).toString().padStart(2, '0')
11+
return `${min}:${sec}`
12+
})
13+
14+
return {
15+
timer,
16+
isTimerAvctive: isActive,
17+
resetTimer: reset,
18+
toggleTimer: () => (isActive.value ? pause() : resume()),
19+
}
20+
}

‎packages/client/logic/utils.ts

-23
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,4 @@
11
import { parseRangeString } from '@slidev/parser/core'
2-
import { useTimestamp } from '@vueuse/core'
3-
import { computed, ref } from 'vue'
4-
5-
export function useTimer() {
6-
const tsStart = ref(Date.now())
7-
const now = useTimestamp({
8-
interval: 1000,
9-
})
10-
const timer = computed(() => {
11-
const passed = (now.value - tsStart.value) / 1000
12-
const sec = Math.floor(passed % 60).toString().padStart(2, '0')
13-
const min = Math.floor(passed / 60).toString().padStart(2, '0')
14-
return `${min}:${sec}`
15-
})
16-
function resetTimer() {
17-
tsStart.value = now.value
18-
}
19-
20-
return {
21-
timer,
22-
resetTimer,
23-
}
24-
}
252

263
export function makeId(length = 5) {
274
const result = []

‎packages/client/pages/presenter.vue

+18-19
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { createFixedClicks } from '../composables/useClicks'
66
import { useDrawings } from '../composables/useDrawings'
77
import { useNav } from '../composables/useNav'
88
import { useSwipeControls } from '../composables/useSwipeControls'
9+
import { useTimer } from '../composables/useTimer'
910
import { useWakeLock } from '../composables/useWakeLock'
1011
import { slidesTitle } from '../env'
1112
import ClicksSlider from '../internals/ClicksSlider.vue'
@@ -22,7 +23,6 @@ import SlidesShow from '../internals/SlidesShow.vue'
2223
import SlideWrapper from '../internals/SlideWrapper.vue'
2324
import { onContextMenu } from '../logic/contextMenu'
2425
import { registerShortcuts } from '../logic/shortcuts'
25-
import { useTimer } from '../logic/utils'
2626
import { decreasePresenterFontSize, increasePresenterFontSize, presenterLayout, presenterNotesFontSize, showEditor, showPresenterCursor } from '../state'
2727
import { sharedState } from '../state/shared'
2828
@@ -49,7 +49,7 @@ useHead({ title: `Presenter - ${slidesTitle}` })
4949
5050
const notesEditing = ref(false)
5151
52-
const { timer, resetTimer } = useTimer()
52+
const { timer, isTimerAvctive, resetTimer, toggleTimer } = useTimer()
5353
5454
const clicksCtxMap = computed(() => slides.value.map(route => createFixedClicks(route)))
5555
const nextFrame = computed(() => {
@@ -184,16 +184,22 @@ onMounted(() => {
184184
<div class="grid-section bottom flex">
185185
<NavControls :persist="true" />
186186
<div flex-auto />
187-
<div
188-
class="timer-btn my-auto relative w-22px h-22px cursor-pointer text-lg"
189-
opacity="50 hover:100"
190-
@click="resetTimer"
191-
>
192-
<carbon:time class="absolute" />
193-
<carbon:renew class="absolute opacity-0" />
194-
</div>
195-
<div class="text-2xl pl-2 pr-6 my-auto tabular-nums">
196-
{{ timer }}
187+
<div class="group flex items-center justify-center pl-4 select-none">
188+
<div class="w-22px cursor-pointer">
189+
<carbon:time class="group-hover:hidden text-xl" />
190+
<div class="group-not-hover:hidden flex flex-col items-center">
191+
<div class="relative op-80 hover:op-100" @click="toggleTimer">
192+
<carbon:pause v-if="isTimerAvctive" class="text-lg" />
193+
<carbon:play v-else />
194+
</div>
195+
<div class="op-80 hover:op-100" @click="resetTimer">
196+
<carbon:renew />
197+
</div>
198+
</div>
199+
</div>
200+
<div class="text-2xl px-3 my-auto tabular-nums">
201+
{{ timer }}
202+
</div>
197203
</div>
198204
</div>
199205
<DrawingControls v-if="__SLIDEV_FEATURE_DRAWINGS__" />
@@ -215,13 +221,6 @@ onMounted(() => {
215221
--slidev-controls-foreground: current;
216222
}
217223
218-
.timer-btn:hover > :first-child {
219-
opacity: 0;
220-
}
221-
.timer-btn:hover > :last-child {
222-
opacity: 1;
223-
}
224-
225224
.grid-container {
226225
--uno: bg-gray/20;
227226
height: 100%;

0 commit comments

Comments
 (0)
Please sign in to comment.