|
| 1 | +<!-- |
| 2 | + - SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors |
| 3 | + - SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | +--> |
| 5 | + |
| 6 | +<template> |
| 7 | + <!-- Plyr currently replaces the parent. Wrapping to prevent this |
| 8 | + https://github.com/redxtech/vue-plyr/issues/259 --> |
| 9 | + <div v-if="url"> |
| 10 | + <VuePlyr ref="plyr" |
| 11 | + :options="options" |
| 12 | + :style="{ |
| 13 | + height: height + 'px', |
| 14 | + width: width + 'px' |
| 15 | + }"> |
| 16 | + <video ref="video" |
| 17 | + :autoplay="active ? true : null" |
| 18 | + :playsinline="true" |
| 19 | + :poster="livePhotoPath" |
| 20 | + :src="url" |
| 21 | + preload="metadata" |
| 22 | + @error.capture.prevent.stop.once="onFail" |
| 23 | + @ended="donePlaying" |
| 24 | + @canplay="doneLoading" |
| 25 | + @loadedmetadata="onLoadedMetadata"> |
| 26 | + |
| 27 | + <!-- Omitting `type` on purpose because most of the |
| 28 | + browsers auto detect the appropriate codec. |
| 29 | + Having it set force the browser to comply to |
| 30 | + the provided mime instead of detecting a potential |
| 31 | + compatibility. --> |
| 32 | + |
| 33 | + {{ t('viewer', 'Your browser does not support videos.') }} |
| 34 | + </video> |
| 35 | + </VuePlyr> |
| 36 | + </div> |
| 37 | +</template> |
| 38 | + |
| 39 | +<script lang='ts'> |
| 40 | +import '@skjnldsv/vue-plyr/dist/vue-plyr.css' |
| 41 | +import type { File } from '@nextcloud/files' |
| 42 | +
|
| 43 | +import { imagePath } from '@nextcloud/router' |
| 44 | +
|
| 45 | +import { logger } from '../services/logger.ts' |
| 46 | +import { findLivePhotoPeerFromName } from '../utils/livePhotoUtils' |
| 47 | +import { getPreviewIfAny } from '../utils/previewUtils' |
| 48 | +import { preloadMedia } from '../services/mediaPreloader.js' |
| 49 | +
|
| 50 | +const VuePlyr = () => import(/* webpackChunkName: 'plyr' */'@skjnldsv/vue-plyr') |
| 51 | +
|
| 52 | +const blankVideo = imagePath('viewer', 'blank.mp4') |
| 53 | +
|
| 54 | +export default { |
| 55 | + name: 'Videos', |
| 56 | +
|
| 57 | + components: { |
| 58 | + VuePlyr, |
| 59 | + }, |
| 60 | +
|
| 61 | + props: { |
| 62 | + node: { |
| 63 | + type: Node, |
| 64 | + required: true, |
| 65 | + }, |
| 66 | + nodes: { |
| 67 | + type: Array as () => File[], |
| 68 | + required: true, |
| 69 | + }, |
| 70 | + isFullScreen: { |
| 71 | + type: Boolean, |
| 72 | + default: false, |
| 73 | + }, |
| 74 | + isSidebarShown: { |
| 75 | + type: Boolean, |
| 76 | + default: false, |
| 77 | + }, |
| 78 | + height: { |
| 79 | + type: Number, |
| 80 | + required: true, |
| 81 | + }, |
| 82 | + width: { |
| 83 | + type: Number, |
| 84 | + required: true, |
| 85 | + }, |
| 86 | + }, |
| 87 | + data() { |
| 88 | + return { |
| 89 | + isFullscreenButtonVisible: false, |
| 90 | + fallback: false, |
| 91 | + } |
| 92 | + }, |
| 93 | +
|
| 94 | + computed: { |
| 95 | + livePhotoPath() { |
| 96 | + const peerFile = findLivePhotoPeerFromName(this, this.nodes) |
| 97 | +
|
| 98 | + if (peerFile === undefined) { |
| 99 | + return undefined |
| 100 | + } |
| 101 | +
|
| 102 | + return getPreviewIfAny(peerFile) |
| 103 | + }, |
| 104 | + player() { |
| 105 | + return this.$refs.plyr.player |
| 106 | + }, |
| 107 | + options() { |
| 108 | + return { |
| 109 | + autoplay: true, |
| 110 | + // Used to reset the video streams https://github.com/sampotts/plyr#javascript-1 |
| 111 | + blankVideo, |
| 112 | + controls: ['play-large', 'play', 'progress', 'current-time', 'mute', 'volume', 'captions', 'settings', 'fullscreen'], |
| 113 | + loadSprite: false, |
| 114 | + fullscreen: { |
| 115 | + iosNative: true, |
| 116 | + }, |
| 117 | + } |
| 118 | + }, |
| 119 | + }, |
| 120 | +
|
| 121 | + asyncComputed: { |
| 122 | + async url(): Promise<string> { |
| 123 | + if (this.fallback) { |
| 124 | + return preloadMedia(this.filename) |
| 125 | + } else { |
| 126 | + return this.src |
| 127 | + } |
| 128 | + }, |
| 129 | + }, |
| 130 | +
|
| 131 | + watch: { |
| 132 | + active(val, old) { |
| 133 | + // the item was hidden before and is now the current view |
| 134 | + if (val === true && old === false) { |
| 135 | + this.player.play() |
| 136 | +
|
| 137 | + // the item was playing before and is now hidden |
| 138 | + } else if (val === false && old === true) { |
| 139 | + this.player.pause() |
| 140 | + } |
| 141 | + }, |
| 142 | + }, |
| 143 | +
|
| 144 | + // for some reason the video controls don't get mounted to the dom until after the component (Videos) is mounted, |
| 145 | + // using the mounted() hook will leave us with an empty array |
| 146 | + updated() { |
| 147 | + // Prevent swiping to the next/previous item when scrubbing the timeline or changing volume |
| 148 | + const plyrControls = this.$el.querySelectorAll('.plyr__controls__item') |
| 149 | + if (!plyrControls || !plyrControls.length) { |
| 150 | + return |
| 151 | + } |
| 152 | + [...plyrControls].forEach(control => { |
| 153 | + if (control.getAttribute('data-plyr') === 'fullscreen') { |
| 154 | + control.addEventListener('click', this.hideHeaderAndFooter) |
| 155 | + } |
| 156 | + if (!control?.addEventListener) { |
| 157 | + return |
| 158 | + } |
| 159 | + control.addEventListener('mouseenter', this.disableSwipe) |
| 160 | + control.addEventListener('mouseleave', this.enableSwipe) |
| 161 | + }) |
| 162 | + }, |
| 163 | +
|
| 164 | + beforeDestroy() { |
| 165 | + // Force stop any ongoing request |
| 166 | + logger.debug('Closing video stream', { filename: this.filename }) |
| 167 | + this.$refs.video?.pause?.() |
| 168 | + this.player.stop() |
| 169 | + this.player.destroy() |
| 170 | + }, |
| 171 | +
|
| 172 | + methods: { |
| 173 | + hideHeaderAndFooter() { |
| 174 | + // work around to get the state of the fullscreen button, aria-selected attribute is not reliable |
| 175 | + this.isFullscreenButtonVisible = !this.isFullscreenButtonVisible |
| 176 | + if (this.isFullscreenButtonVisible) { |
| 177 | + document.body.querySelector('main').classList.add('viewer__hidden-fullscreen') |
| 178 | + document.body.querySelector('footer').classList.add('viewer__hidden-fullscreen') |
| 179 | + } else { |
| 180 | + document.body.querySelector('main').classList.remove('viewer__hidden-fullscreen') |
| 181 | + document.body.querySelector('footer').classList.remove('viewer__hidden-fullscreen') |
| 182 | + } |
| 183 | + }, |
| 184 | + // Updates the dimensions of the modal |
| 185 | + updateVideoSize() { |
| 186 | + this.naturalHeight = this.$refs.video?.videoHeight |
| 187 | + this.naturalWidth = this.$refs.video?.videoWidth |
| 188 | + this.updateHeightWidth() |
| 189 | + }, |
| 190 | +
|
| 191 | + donePlaying() { |
| 192 | + // reset and show poster after play |
| 193 | + this.$refs.video.autoplay = false |
| 194 | + this.$refs.video.load() |
| 195 | + }, |
| 196 | +
|
| 197 | + onLoadedMetadata() { |
| 198 | + this.updateVideoSize() |
| 199 | + // Force any further loading once we have the metadata |
| 200 | + if (!this.active) { |
| 201 | + this.player.stop() |
| 202 | + } |
| 203 | + }, |
| 204 | +
|
| 205 | + // Fallback to the original image if not already done |
| 206 | + onFail() { |
| 207 | + if (!this.fallback) { |
| 208 | + console.error(`Loading of file ${this.filename} failed, falling back to fetching it by hand`) |
| 209 | + this.fallback = true |
| 210 | + } |
| 211 | + }, |
| 212 | + }, |
| 213 | +} |
| 214 | +</script> |
| 215 | + |
| 216 | +<style scoped lang="scss"> |
| 217 | +video { |
| 218 | + /* over arrows in tiny screens */ |
| 219 | + z-index: 20050; |
| 220 | + align-self: center; |
| 221 | + max-width: 100%; |
| 222 | + max-height: 100% !important; |
| 223 | + background-color: black; |
| 224 | +
|
| 225 | + justify-self: center; |
| 226 | +} |
| 227 | +
|
| 228 | +:deep() { |
| 229 | + .plyr:-webkit-full-screen video { |
| 230 | + width: 100% !important; |
| 231 | + height: 100% !important; |
| 232 | + } |
| 233 | + .plyr:fullscreen video { |
| 234 | + width: 100% !important; |
| 235 | + height: 100% !important; |
| 236 | + } |
| 237 | + .plyr__progress__container { |
| 238 | + flex: 1 1; |
| 239 | + } |
| 240 | +
|
| 241 | + .plyr { |
| 242 | + @import '../mixins/Plyr'; |
| 243 | +
|
| 244 | + // Override server font style |
| 245 | + button { |
| 246 | + color: white; |
| 247 | +
|
| 248 | + &:hover, |
| 249 | + &:focus { |
| 250 | + color: var(--color-primary-element-text); |
| 251 | + background-color: var(--color-primary-element); |
| 252 | + } |
| 253 | + } |
| 254 | + } |
| 255 | +} |
| 256 | +</style> |
| 257 | + |
| 258 | +<style lang="scss"> |
| 259 | +main.viewer__hidden-fullscreen { |
| 260 | + height: 100vh !important; |
| 261 | + width: 100vw !important; |
| 262 | + margin: 0 !important; |
| 263 | +} |
| 264 | +
|
| 265 | +footer.viewer__hidden-fullscreen { |
| 266 | + display: none !important; |
| 267 | +} |
| 268 | +</style> |
0 commit comments