-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
SlidevVideo.vue
84 lines (68 loc) · 2.32 KB
/
SlidevVideo.vue
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
<script setup lang="ts">
import { computed, inject, onMounted, onUnmounted, ref, watch } from 'vue'
import { injectionClicksContext, injectionRenderContext, injectionRoute, injectionSlidevContext } from '../constants'
const props = defineProps<{
autoPlay?: boolean | 'once' | 'resume' | 'resumeOnce'
autoPause?: 'slide' | 'click'
autoReset?: 'slide' | 'click'
}>()
const $slidev = inject(injectionSlidevContext)
const route = inject(injectionRoute)
const currentContext = inject(injectionRenderContext)
const clicks = inject(injectionClicksContext)?.value
const video = ref<HTMLMediaElement>()
const played = ref(false)
const ended = ref(false)
const matchRoute = computed(() => {
if (!video.value || currentContext?.value !== 'slide')
return false
return route === $slidev?.nav.currentRoute
})
const matchClick = computed(() => {
if (!video.value || currentContext?.value !== 'slide' || clicks?.disabled || clicks?.current === undefined)
return false
return clicks.map.get(video.value)?.isShown?.value ?? true
})
const matchRouteAndClick = computed(() => matchRoute.value && matchClick.value)
watch(matchRouteAndClick, () => {
if (!video.value || currentContext?.value !== 'slide')
return
if (matchRouteAndClick.value) {
if (props.autoReset === 'click')
video.value.currentTime = 0
if (props.autoPlay && (!played.value || props.autoPlay === 'resume' || (props.autoPlay === 'resumeOnce' && !ended.value)))
video.value.play()
}
if ((props.autoPause === 'click' && !matchRouteAndClick.value) || (props.autoPause === 'slide' && !matchRoute.value))
video.value.pause()
})
watch(matchRoute, () => {
if (!video.value || currentContext?.value !== 'slide')
return
if (matchRoute.value && props.autoReset === 'slide')
video.value.currentTime = 0
})
function onPlay() {
played.value = true
}
function onEnded() {
ended.value = true
}
onMounted(() => {
if (!video.value || currentContext?.value !== 'slide')
return
video.value?.addEventListener('play', onPlay)
video.value?.addEventListener('ended', onEnded)
})
onUnmounted(() => {
if (!video.value || currentContext?.value !== 'slide')
return
video.value?.removeEventListener('play', onPlay)
video.value?.removeEventListener('ended', onEnded)
})
</script>
<template>
<video ref="video">
<slot />
</video>
</template>