Skip to content

Commit

Permalink
Add "Play on YouTube" functionality (#467)
Browse files Browse the repository at this point in the history
* feat: added open in YouTube functionality

* chore: replace fixed px size

* feat: pause video when openInYouTube is clicked

* refactor: rename svgIconContainer to videoPlayerIcon for clarity

* feat: added external YouTube play button for talks with YouTube videos

* feat: pause video when external YouTube play button is clicked

* refactor: add eventlistener through JS and move shared code in a function

* refactor: simplify video container selection by using playerTarget

* feat: replace eventlisterns with StimulusJS method

* chore: revert changes and use <a> with href only
  • Loading branch information
aamfahim authored Dec 2, 2024
1 parent f62fc88 commit b315761
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions app/assets/stylesheets/components/video.css
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@
.v-bigPlay {
opacity: 0.5 !important;
}

.v-openInYouTube.v-controlButton svg {
width: 65%;
fill: var(--vlite-controlsColor);
}
}
20 changes: 20 additions & 0 deletions app/javascript/controllers/video_player_controller.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Controller } from '@hotwired/stimulus'
import Vlitejs from 'vlitejs'
import VlitejsYoutube from 'vlitejs/providers/youtube.js'
import youtubeSvg from '../../assets/images/icons/fontawesome/youtube-brands-solid.svg?raw'

Vlitejs.registerProvider('youtube', VlitejsYoutube)

Expand Down Expand Up @@ -31,7 +32,9 @@ export default class extends Controller {
if (controlBar) {
const volumeButton = player.elements.container.querySelector('.v-volumeButton')
const playbackRateSelect = this.createPlaybackRateSelect(this.playbackRateOptions, player)
const openInYouTube = this.createOpenInYoutube()
volumeButton.parentNode.insertBefore(playbackRateSelect, volumeButton.nextSibling)
volumeButton.parentNode.insertBefore(openInYouTube, volumeButton.previousSibling)
}
// for seekTo to work we need to store again the player instance
this.player = player
Expand Down Expand Up @@ -61,4 +64,21 @@ export default class extends Controller {
this.player.seekTo(time)
}
}

pause () {
this.player.pause()
}

createOpenInYoutube () {
const videoId = this.playerTarget.dataset.youtubeId

const anchorTag = document.createElement('a')
anchorTag.className = 'v-openInYouTube v-controlButton'
anchorTag.innerHTML = youtubeSvg
anchorTag.href = `https://www.youtube.com/watch?v=${videoId}`
anchorTag.target = '_blank'
anchorTag.dataset.action = 'click->video-player#pause'

return anchorTag
}
}
9 changes: 9 additions & 0 deletions app/views/talks/_talk.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@
</div>

<div class="flex gap-1">
<% if talk.video_provider == "youtube" %>
<a class="tooltip tooltip-top v-playOnYoutubeButton" data-tip="Watch on YouTube" href="<%= "https://www.youtube.com/watch?v=#{talk.video_id}" %>" target="_blank" data-action="click->video-player#pause">
<div class="flex gap-2 items-center rounded-full border bg-gray-100 hover:bg-gray-200/50 border-gray-200 font-regular px-3 py-1 text-sm cursor-pointer">
<span class="text-nowrap">Play on</span>
<%= fa("youtube-brands", size: :sm, style: :solid) %>
</div>
</a>
<% end %>
<% if signed_in? && !talk.scheduled? %>
<%= turbo_frame_tag dom_id(talk, :watched_talk) do %>
<% if talk.watched? %>
Expand Down

0 comments on commit b315761

Please sign in to comment.