Compare commits
26 Commits
add-licens
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 08264b28c0 | |||
| 836f989120 | |||
| c01cea656c | |||
| e4da0423f4 | |||
| 2a6de8a9fc | |||
| 17d344d3ba | |||
| 424133d04d | |||
| b8c02fa2d8 | |||
| bba2a15d2b | |||
| a4c8b9a19b | |||
| 4d3ddd2d4b | |||
| 203bd1b908 | |||
|
|
5debdfafcc | ||
|
|
3cf334a59e | ||
|
|
6b70561a9a | ||
|
|
8b8556fe02 | ||
|
|
5840ed9242 | ||
|
|
0f23c4ebb4 | ||
|
|
db7740aeb9 | ||
|
|
a532d5d285 | ||
|
|
8533860cc6 | ||
|
|
159b6dab92 | ||
|
|
e8dcf07a83 | ||
|
|
86e3dce457 | ||
|
|
49a949c793 | ||
|
|
21f08832ba |
26
README.md
26
README.md
@@ -3,29 +3,41 @@
|
||||
This Component allows you to add Youtube-Videos to your Svelte Project
|
||||
|
||||
The orginal Component template comes from [HERE](https://github.com/sveltejs/component-template)
|
||||
|
||||
The Youtube stuff comes from [HERE](https://developers.google.com/youtube/iframe_api_reference)
|
||||
|
||||
---
|
||||
|
||||
# How to use it ?
|
||||
|
||||
Just
|
||||
Just add this to your dependencies in `package.json`, then run `npm install`:
|
||||
|
||||
```bash
|
||||
npm install --save-dev @sveltecasts/svelte-youtube
|
||||
```json
|
||||
"svelte-youtube": "https://github.com/zevaverbach/svelte-youtube.git",
|
||||
````
|
||||
|
||||
And in your Source Code under Script:
|
||||
|
||||
```
|
||||
import Youtube from "@sveltecasts/svelte-youtube";
|
||||
import Youtube from "svelte-youtube"
|
||||
|
||||
<Youtube videoId="..." />
|
||||
```
|
||||
|
||||
## Methods
|
||||
- `play()`
|
||||
- `pause()`
|
||||
- `paused()`
|
||||
- `jumpTo(seconds)`
|
||||
- `position()`
|
||||
|
||||
# Btw
|
||||
# YouTube Video About Its Making
|
||||
|
||||
Here is a youtube video about this component:
|
||||
|
||||
https://www.youtube.com/watch?v=Ank3IdQHOuE
|
||||
https://www.youtube.com/watch?v=Ank3IdQHOuE
|
||||
|
||||
# Changelog
|
||||
0.0.5 Fixed some errors when YT-Component gets destroyed and re-initialized, the problem was that YT replaced my div with an iframe and then on the component-destroy Svelte searches for an inexistent div ¯\_(ツ)_/¯ , an other problem was that the YouTubeIframeAPIReady only get called once when the first YT-Component is used, so when we recreate a Player the 'new YT.Player' function got never called
|
||||
0.0.4 Update svelte version
|
||||
0.0.2 Changed descripton
|
||||
0.0.1 Init
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
"rollup": "^1.11.0",
|
||||
"rollup-plugin-node-resolve": "^4.0.0",
|
||||
"rollup-plugin-svelte": "^5.0.0",
|
||||
"svelte": "^3.0.0"
|
||||
"svelte": "^3.6.8"
|
||||
},
|
||||
"keywords": [
|
||||
"svelte"
|
||||
@@ -21,8 +21,8 @@
|
||||
"index.mjs",
|
||||
"index.js"
|
||||
],
|
||||
"description": "*Psst — looking for an app template? Go here --> [sveltejs/template](https://github.com/sveltejs/template)*",
|
||||
"version": "0.0.1",
|
||||
"description": "Youtube Component for Svelte 3",
|
||||
"version": "0.0.5",
|
||||
"dependencies": {},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@@ -34,4 +34,4 @@
|
||||
"url": "https://github.com/sveltecasts/svelte-youtube/issues"
|
||||
},
|
||||
"homepage": "https://github.com/sveltecasts/svelte-youtube#readme"
|
||||
}
|
||||
}
|
||||
118
src/index.svelte
118
src/index.svelte
@@ -1,55 +1,87 @@
|
||||
<script context="module">
|
||||
let YouTubeIframeAPIReady = false;
|
||||
</script>
|
||||
|
||||
<script>
|
||||
let player;
|
||||
import { createEventDispatcher , onMount} from "svelte";
|
||||
const dispatch = createEventDispatcher();
|
||||
let divId = "player_"+parseInt(Math.random() * 100000).toString()
|
||||
import { onMount, createEventDispatcher } from "svelte";
|
||||
let divId = "player_" + parseInt(Math.random() * 100000).toString();
|
||||
export let videoId;
|
||||
export let height = '390';
|
||||
export let width = '640';
|
||||
export let height = "390";
|
||||
export let width = "640";
|
||||
const dispatch = createEventDispatcher()
|
||||
onMount(() => {
|
||||
let ytTagUrl = "https://www.youtube.com/iframe_api";
|
||||
if(!isMyScriptLoaded(ytTagUrl)){
|
||||
// 2. This code loads the IFrame Player API code asynchronously.
|
||||
var tag = document.createElement("script");
|
||||
tag.src = ytTagUrl;
|
||||
var firstScriptTag = document.getElementsByTagName("script")[0];
|
||||
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
|
||||
}
|
||||
|
||||
window.onYouTubeIframeAPIReady = function() {
|
||||
//console.log('hello')
|
||||
window.dispatchEvent(new Event("YouTubeIframeAPIReady"))
|
||||
let ytTagUrl = "https://www.youtube.com/iframe_api";
|
||||
if (!isMyScriptLoaded(ytTagUrl)) {
|
||||
// 2. This code loads the IFrame Player API code asynchronously.
|
||||
var tag = document.createElement("script");
|
||||
tag.src = ytTagUrl;
|
||||
var firstScriptTag = document.getElementsByTagName("script")[0];
|
||||
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
|
||||
}
|
||||
|
||||
window.onYouTubeIframeAPIReady = function() {
|
||||
//console.log('hello')
|
||||
window.dispatchEvent(new Event("YouTubeIframeAPIReady"));
|
||||
};
|
||||
|
||||
window.addEventListener("YouTubeIframeAPIReady", function() {
|
||||
if (YouTubeIframeAPIReady == false) {
|
||||
// first load of an YT Video on this project
|
||||
YouTubeIframeAPIReady = true; // now the Player can be created
|
||||
createPlayer();
|
||||
}
|
||||
});
|
||||
|
||||
function onPlayerStateChange({ data }) {
|
||||
dispatch("StateChange", data);
|
||||
}
|
||||
function createPlayer() {
|
||||
player = new YT.Player(divId, {
|
||||
height,
|
||||
width,
|
||||
videoId: videoId,
|
||||
events: {
|
||||
//'onReady': onPlayerReady,
|
||||
onStateChange: onPlayerStateChange
|
||||
}
|
||||
});
|
||||
}
|
||||
if (YouTubeIframeAPIReady) {
|
||||
createPlayer(); // if the YT Script is ready, we can create our player
|
||||
}
|
||||
});
|
||||
|
||||
window.addEventListener("YouTubeIframeAPIReady", function(){
|
||||
console.log('load player..')
|
||||
player = new YT.Player(divId, {
|
||||
height,
|
||||
width,
|
||||
videoId: videoId,
|
||||
events: {
|
||||
//'onReady': onPlayerReady,
|
||||
'onStateChange': onPlayerStateChange
|
||||
}
|
||||
});
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
function isMyScriptLoaded(url ="") {
|
||||
var scripts = document.getElementsByTagName('script');
|
||||
for (var i = scripts.length; i--;) {
|
||||
if (scripts[i].src == url) return true;
|
||||
function isMyScriptLoaded(url = "") {
|
||||
var scripts = document.getElementsByTagName("script");
|
||||
for (var i = scripts.length; i--; ) {
|
||||
if (scripts[i].src == url) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function onPlayerStateChange({data}){
|
||||
dispatch("StateChange",data)
|
||||
}
|
||||
export function playVideo(){
|
||||
player.playVideo()
|
||||
export function position() { return player.getCurrentTime() }
|
||||
export function play() { player.playVideo() }
|
||||
export function jumpTo(seconds) { player.seekTo(seconds) }
|
||||
export function pause() { player.pauseVideo() }
|
||||
export function paused() { return [5, 2, -1].includes(player.getPlayerState()) }
|
||||
export function buffering() { return player.getPlayerState() === 3 }
|
||||
export function state() { return player.getPlayerState() }
|
||||
export function toggle() {
|
||||
if (paused()) {
|
||||
player.playVideo()
|
||||
} else {
|
||||
player.pauseVideo()
|
||||
}
|
||||
}
|
||||
|
||||
</script>
|
||||
<div id={divId}></div>
|
||||
|
||||
<span class="yt-component" style="float: left">
|
||||
<div id={divId} />
|
||||
</span>
|
||||
<style>
|
||||
.yt-component {
|
||||
box-shadow: 15px 15px #111;
|
||||
}
|
||||
</style>
|
||||
|
||||
Reference in New Issue
Block a user