Skip to content

Commit

Permalink
fix: make videoID not required anymore.
Browse files Browse the repository at this point in the history
Properly update video when videoID, videoURL or controls change.
  • Loading branch information
dobromir-hristov committed Feb 18, 2021
1 parent f34f367 commit 0c21bad
Show file tree
Hide file tree
Showing 15 changed files with 741 additions and 76 deletions.
71 changes: 52 additions & 19 deletions dist/index.es.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ref, toRefs, onMounted, onBeforeUnmount, watch, openBlock, createBlock } from 'vue';
import { ref, toRefs, onMounted, onBeforeUnmount, watch, toRef, unref, openBlock, createBlock } from 'vue';
import Player from '@vimeo/player';

function emitVueEvent(_ref) {
Expand Down Expand Up @@ -30,11 +30,11 @@ var script = {
},
videoId: {
type: String,
required: true
"default": ''
},
videoUrl: {
type: String,
"default": undefined
"default": ''
},
loop: {
type: Boolean,
Expand All @@ -58,21 +58,30 @@ var script = {
videoId = _toRefs.videoId,
videoUrl = _toRefs.videoUrl;

var options = {
id: props.videoId,
width: props.playerWidth,
height: props.playerHeight,
loop: props.loop,
autoplay: props.autoplay,
controls: props.controls
};

if (videoUrl.value) {
options.url = videoUrl.value;
if (!props.videoId && !props.videoUrl) {
console.warn('[VueVimeoPlayer: You mist provide at least a videoId or a videoUrl prop]');
}

var update = function update(videoId) {
return player.loadVideo(videoId);
var mergeOptions = function mergeOptions(_ref3) {
var id = _ref3.id,
url = _ref3.url;
var opts = {
width: props.playerWidth,
height: props.playerHeight,
loop: props.loop,
autoplay: props.autoplay,
controls: props.controls
};

if (unref(url)) {
opts.url = unref(url);
}

if (unref(id)) {
opts.id = unref(id);
}

return opts;
};

var play = function play() {
Expand Down Expand Up @@ -108,14 +117,38 @@ var script = {
};

onMounted(function () {
player = new Player(elementRef.value, Object.assign(options, props.options));
player = new Player(elementRef.value, mergeOptions({
id: props.videoId,
url: props.videoUrl
}));
setEvents();
});
onBeforeUnmount(function () {
return player.unload();
});
watch(videoId, update);
watch(videoUrl, update);
watch(videoId, function (id) {
return player.loadVideo(mergeOptions({
id: id
}));
});
watch(videoUrl, function (url) {
return player.loadVideo(mergeOptions({
url: url
}));
});
watch(toRef(props, 'controls'), function () {
return player.loadVideo(mergeOptions({
url: videoUrl,
id: videoId
}));
});

var update = function update(id) {
return player.loadVideo(mergeOptions({
id: id
}));
};

return {
update: update,
play: play,
Expand Down
69 changes: 51 additions & 18 deletions dist/index.iife.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ var VueVimeoPlayer = (function (exports, vue, Player) {
},
videoId: {
type: String,
required: true
"default": ''
},
videoUrl: {
type: String,
"default": undefined
"default": ''
},
loop: {
type: Boolean,
Expand All @@ -62,21 +62,30 @@ var VueVimeoPlayer = (function (exports, vue, Player) {
videoId = _toRefs.videoId,
videoUrl = _toRefs.videoUrl;

var options = {
id: props.videoId,
width: props.playerWidth,
height: props.playerHeight,
loop: props.loop,
autoplay: props.autoplay,
controls: props.controls
};

if (videoUrl.value) {
options.url = videoUrl.value;
if (!props.videoId && !props.videoUrl) {
console.warn('[VueVimeoPlayer: You mist provide at least a videoId or a videoUrl prop]');
}

var update = function update(videoId) {
return player.loadVideo(videoId);
var mergeOptions = function mergeOptions(_ref3) {
var id = _ref3.id,
url = _ref3.url;
var opts = {
width: props.playerWidth,
height: props.playerHeight,
loop: props.loop,
autoplay: props.autoplay,
controls: props.controls
};

if (vue.unref(url)) {
opts.url = vue.unref(url);
}

if (vue.unref(id)) {
opts.id = vue.unref(id);
}

return opts;
};

var play = function play() {
Expand Down Expand Up @@ -112,14 +121,38 @@ var VueVimeoPlayer = (function (exports, vue, Player) {
};

vue.onMounted(function () {
player = new Player__default['default'](elementRef.value, Object.assign(options, props.options));
player = new Player__default['default'](elementRef.value, mergeOptions({
id: props.videoId,
url: props.videoUrl
}));
setEvents();
});
vue.onBeforeUnmount(function () {
return player.unload();
});
vue.watch(videoId, update);
vue.watch(videoUrl, update);
vue.watch(videoId, function (id) {
return player.loadVideo(mergeOptions({
id: id
}));
});
vue.watch(videoUrl, function (url) {
return player.loadVideo(mergeOptions({
url: url
}));
});
vue.watch(vue.toRef(props, 'controls'), function () {
return player.loadVideo(mergeOptions({
url: videoUrl,
id: videoId
}));
});

var update = function update(id) {
return player.loadVideo(mergeOptions({
id: id
}));
};

return {
update: update,
play: play,
Expand Down
69 changes: 51 additions & 18 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ var script = {
},
videoId: {
type: String,
required: true
"default": ''
},
videoUrl: {
type: String,
"default": undefined
"default": ''
},
loop: {
type: Boolean,
Expand All @@ -66,21 +66,30 @@ var script = {
videoId = _toRefs.videoId,
videoUrl = _toRefs.videoUrl;

var options = {
id: props.videoId,
width: props.playerWidth,
height: props.playerHeight,
loop: props.loop,
autoplay: props.autoplay,
controls: props.controls
};

if (videoUrl.value) {
options.url = videoUrl.value;
if (!props.videoId && !props.videoUrl) {
console.warn('[VueVimeoPlayer: You mist provide at least a videoId or a videoUrl prop]');
}

var update = function update(videoId) {
return player.loadVideo(videoId);
var mergeOptions = function mergeOptions(_ref3) {
var id = _ref3.id,
url = _ref3.url;
var opts = {
width: props.playerWidth,
height: props.playerHeight,
loop: props.loop,
autoplay: props.autoplay,
controls: props.controls
};

if (vue.unref(url)) {
opts.url = vue.unref(url);
}

if (vue.unref(id)) {
opts.id = vue.unref(id);
}

return opts;
};

var play = function play() {
Expand Down Expand Up @@ -116,14 +125,38 @@ var script = {
};

vue.onMounted(function () {
player = new Player__default['default'](elementRef.value, Object.assign(options, props.options));
player = new Player__default['default'](elementRef.value, mergeOptions({
id: props.videoId,
url: props.videoUrl
}));
setEvents();
});
vue.onBeforeUnmount(function () {
return player.unload();
});
vue.watch(videoId, update);
vue.watch(videoUrl, update);
vue.watch(videoId, function (id) {
return player.loadVideo(mergeOptions({
id: id
}));
});
vue.watch(videoUrl, function (url) {
return player.loadVideo(mergeOptions({
url: url
}));
});
vue.watch(vue.toRef(props, 'controls'), function () {
return player.loadVideo(mergeOptions({
url: videoUrl,
id: videoId
}));
});

var update = function update(id) {
return player.loadVideo(mergeOptions({
id: id
}));
};

return {
update: update,
play: play,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"standard-version": "^9.0.0"
},
"dependencies": {
"@vimeo/player": "^2.14.1",
"@vimeo/player": "^2.15.0",
"@vue/compiler-sfc": "^3.0.3",
"vue": "^3.0.3"
},
Expand Down
42 changes: 26 additions & 16 deletions src/VimeoPlayer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div ref="elementRef" />
</template>
<script>
import { watch, toRefs, onMounted, onBeforeUnmount, ref } from 'vue'
import { watch, toRefs, onMounted, onBeforeUnmount, ref, unref, toRef } from 'vue'
import Player from '@vimeo/player'
function emitVueEvent ({ player, event, emit }) {
Expand Down Expand Up @@ -52,11 +52,11 @@ export default {
},
videoId: {
type: String,
required: true
default: ''
},
videoUrl: {
type: String,
default: undefined
default: ''
},
loop: {
type: Boolean,
Expand All @@ -75,19 +75,26 @@ export default {
let player
const elementRef = ref(null)
const { videoId, videoUrl } = toRefs(props)
const options = {
id: props.videoId,
width: props.playerWidth,
height: props.playerHeight,
loop: props.loop,
autoplay: props.autoplay,
controls: props.controls
if (!props.videoId && !props.videoUrl) {
console.warn('[VueVimeoPlayer: You mist provide at least a videoId or a videoUrl prop]')
}
if (videoUrl.value) {
options.url = videoUrl.value
const mergeOptions = ({ id, url }) => {
const opts = {
width: props.playerWidth,
height: props.playerHeight,
loop: props.loop,
autoplay: props.autoplay,
controls: props.controls
}
if (unref(url)) {
opts.url = unref(url)
}
if (unref(id)) {
opts.id = unref(id)
}
return opts
}
const update = videoId => player.loadVideo(videoId)
const play = () => player.play()
const pause = () => player.pause()
const mute = () => player.setVolume(0)
Expand All @@ -104,15 +111,18 @@ export default {
}
onMounted(() => {
player = new Player(elementRef.value, Object.assign(options, props.options))
player = new Player(elementRef.value, mergeOptions({ id: props.videoId, url: props.videoUrl }))
setEvents()
})
onBeforeUnmount(() => player.unload())
watch(videoId, update)
watch(videoUrl, update)
watch(videoId, (id) => player.loadVideo(mergeOptions({ id })))
watch(videoUrl, (url) => player.loadVideo(mergeOptions({ url })))
watch(toRef(props, 'controls'), () => player.loadVideo(mergeOptions({ url: videoUrl, id: videoId })))
const update = (id) => player.loadVideo(mergeOptions({ id }))
return {
update, play, pause, mute, unmute, elementRef
Expand Down

0 comments on commit 0c21bad

Please sign in to comment.