Skip to content

Instantly share code, notes, and snippets.

@lpirir
Forked from bzerangue/videoEmbed.js
Created October 12, 2020 05:55
Show Gist options
  • Save lpirir/d0e2603c80bcc17fd6fe4af8bb688001 to your computer and use it in GitHub Desktop.
Save lpirir/d0e2603c80bcc17fd6fe4af8bb688001 to your computer and use it in GitHub Desktop.

Revisions

  1. @bzerangue bzerangue revised this gist Dec 27, 2019. No changes.
  2. @bzerangue bzerangue created this gist Dec 27, 2019.
    73 changes: 73 additions & 0 deletions videoEmbed.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,73 @@
    import React from 'react'

    const VideoEmbedPreview = ({ value }) => {

    const url = value.url
    const responsiveVideoContainer = {
    padding: "56.25% 0 0 0",
    position: "relative"
    }

    const responsiveVideoPlayer = {
    position: "absolute",
    top: 0,
    left: 0,
    width: "100%",
    height: "100%"
    }

    if (url) {

    // install https://www.npmjs.com/package/get-video-id, to get Vimeo or YouTube IDs
    const getVideoId = require('get-video-id')

    const id = getVideoId(url).id
    const service = getVideoId(url).service

    const vimeoEmbedUrl = 'https://player.vimeo.com/video/' + id
    const youtubeEmbedUrl = 'https://www.youtube.com/embed/' + id


    if (!id) {
    return <div>Missing YouTube or Vimeo URL</div>
    }

    if (service === 'vimeo') {
    return (
    <div style={responsiveVideoContainer}>
    <iframe src={vimeoEmbedUrl} style={responsiveVideoPlayer} frameborder="0" allow="autoplay; fullscreen" allowfullscreen></iframe>
    </div>
    )
    }

    if (service === 'youtube') {
    return (
    <div style={responsiveVideoContainer}>
    <iframe src={youtubeEmbedUrl} style={responsiveVideoPlayer} frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
    </div>
    )
    }
    }

    return <div></div>

    }

    export default {
    name: 'videoEmbed',
    type: 'object',
    title: 'Video Embed',
    fields: [
    {
    name: 'url',
    type: 'url',
    title: 'URL'
    }
    ],
    preview: {
    select: {
    url: 'url'
    },
    component: VideoEmbedPreview
    }
    }