/
Videos.vue
231 lines (207 loc) · 6.08 KB
/
Videos.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
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
<!--
- @copyright Copyright (c) 2019 John Molakvoæ <skjnldsv@protonmail.com>
-
- @author John Molakvoæ <skjnldsv@protonmail.com>
- @author Richard Steinmetz <richard@steinmetz.cloud>
-
- @license AGPL-3.0-or-later
-
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
-
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
-
-->
<template>
<!-- Plyr currently replaces the parent. Wrapping to prevent this
https://github.com/redxtech/vue-plyr/issues/259 -->
<div v-if="src">
<VuePlyr ref="plyr"
:options="options"
:style="{
height: height + 'px',
width: width + 'px'
}">
<video ref="video"
:autoplay="active ? true : null"
:playsinline="true"
:poster="livePhotoPath"
:src="src"
preload="metadata"
@ended="donePlaying"
@canplay="doneLoading"
@loadedmetadata="onLoadedMetadata">
<!-- Omitting `type` on purpose because most of the
browsers auto detect the appropriate codec.
Having it set force the browser to comply to
the provided mime instead of detecting a potential
compatibility. -->
{{ t('viewer', 'Your browser does not support videos.') }}
</video>
</VuePlyr>
</div>
</template>
<script>
// eslint-disable-next-line n/no-missing-import
import '@skjnldsv/vue-plyr/dist/vue-plyr.css'
import logger from '../services/logger.js'
import { imagePath } from '@nextcloud/router'
const VuePlyr = () => import(/* webpackChunkName: 'plyr' */'@skjnldsv/vue-plyr')
const liveExt = ['jpg', 'jpeg', 'png']
const liveExtRegex = new RegExp(`\\.(${liveExt.join('|')})$`, 'i')
const blankVideo = imagePath('viewer', 'blank.mp4')
export default {
name: 'Videos',
components: {
VuePlyr,
},
data() {
return {
isFullscreenButtonVisible: false,
}
},
computed: {
livePhoto() {
return this.fileList.find(file => {
// if same filename and extension is allowed
return file.filename !== this.filename
&& file.basename.startsWith(this.name)
&& liveExtRegex.test(file.basename)
})
},
livePhotoPath() {
return this.livePhoto && this.getPreviewIfAny(this.livePhoto)
},
player() {
return this.$refs.plyr.player
},
options() {
return {
autoplay: this.active === true,
// Used to reset the video streams https://github.com/sampotts/plyr#javascript-1
blankVideo,
controls: ['play-large', 'play', 'progress', 'current-time', 'mute', 'volume', 'captions', 'settings', 'fullscreen'],
loadSprite: false,
}
},
},
watch: {
active(val, old) {
// the item was hidden before and is now the current view
if (val === true && old === false) {
this.player.play()
// the item was playing before and is now hidden
} else if (val === false && old === true) {
this.player.pause()
}
},
},
// for some reason the video controls don't get mounted to the dom until after the component (Videos) is mounted,
// using the mounted() hook will leave us with an empty array
updated() {
// Prevent swiping to the next/previous item when scrubbing the timeline or changing volume
[...this.$el.querySelectorAll('.plyr__controls__item')].forEach(control => {
if (control.getAttribute('data-plyr') === 'fullscreen') {
control.addEventListener('click', this.hideHeaderAndFooter)
}
if (!control?.addEventListener) {
return
}
control.addEventListener('mouseenter', this.disableSwipe)
control.addEventListener('mouseleave', this.enableSwipe)
})
},
beforeDestroy() {
// Force stop any ongoing request
logger.debug('Closing video stream', { filename: this.filename })
this.$refs.video.pause()
this.player.stop()
this.player.destroy()
},
methods: {
hideHeaderAndFooter(e) {
// work arround to get the state of the fullscreen button, aria-selected attribute is not reliable
this.isFullscreenButtonVisible = !this.isFullscreenButtonVisible
if (this.isFullscreenButtonVisible) {
document.body.querySelector('main').classList.add('viewer__hidden-fullscreen')
document.body.querySelector('footer').classList.add('viewer__hidden-fullscreen')
} else {
document.body.querySelector('main').classList.remove('viewer__hidden-fullscreen')
document.body.querySelector('footer').classList.remove('viewer__hidden-fullscreen')
}
},
// Updates the dimensions of the modal
updateVideoSize() {
this.naturalHeight = this.$refs.video?.videoHeight
this.naturalWidth = this.$refs.video?.videoWidth
this.updateHeightWidth()
},
donePlaying() {
// reset and show poster after play
this.$refs.video.autoplay = false
this.$refs.video.load()
},
onLoadedMetadata() {
this.updateVideoSize()
// Force any further loading once we have the metadata
if (!this.active) {
this.player.stop()
}
},
},
}
</script>
<style scoped lang="scss">
video {
/* over arrows in tiny screens */
z-index: 20050;
align-self: center;
max-width: 100%;
max-height: 100% !important;
background-color: black;
justify-self: center;
}
:deep() {
.plyr:-webkit-full-screen video {
width: 100% !important;
height: 100% !important;
}
.plyr:fullscreen video {
width: 100% !important;
height: 100% !important;
}
.plyr__progress__container {
flex: 1 1;
}
.plyr {
@import '../mixins/Plyr';
// Override server font style
button {
color: white;
&:hover,
&:focus {
color: var(--color-primary-element-text);
background-color: var(--color-primary-element);
}
}
}
}
</style>
<style lang="scss">
main.viewer__hidden-fullscreen {
height: 100vh !important;
width: 100vw !important;
margin: 0 !important;
}
footer.viewer__hidden-fullscreen {
display: none !important;
}
</style>