change name & component source

This commit is contained in:
Sveltecasts
2019-07-13 17:32:53 +02:00
parent a20b6f2a70
commit 10a6e271c8
2 changed files with 56 additions and 29 deletions

View File

@@ -1,5 +1,5 @@
{
"name": "SvelteComponent",
"name": "@sveltecasts/svelte-youtube",
"svelte": "src/index.svelte",
"module": "index.mjs",
"main": "index.js",
@@ -20,5 +20,18 @@
"src",
"index.mjs",
"index.js"
]
],
"description": "*Psst — looking for an app template? Go here --> [sveltejs/template](https://github.com/sveltejs/template)*",
"version": "0.0.1",
"dependencies": {},
"repository": {
"type": "git",
"url": "git+https://github.com/sveltecasts/svelte-youtube.git"
},
"author": "Sveltecasts.dev",
"license": "ISC",
"bugs": {
"url": "https://github.com/sveltecasts/svelte-youtube/issues"
},
"homepage": "https://github.com/sveltecasts/svelte-youtube#readme"
}

View File

@@ -1,28 +1,32 @@
<script context="module">
<script>
let player;
import { createEventDispatcher , onMount} from "svelte";
const dispatch = createEventDispatcher();
let divId = "player_"+parseInt(Math.random() * 100000).toString()
export let videoId;
export let height = '390';
export let width = '640';
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 = "https://www.youtube.com/iframe_api";
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"))
}
</script>
<script>
let player;
import { createEventDispatcher } from "svelte";
const dispatch = createEventDispatcher();
let divId = "player_"+parseInt(Math.random() * 100000).toString()
export let videoId;
window.addEventListener("YouTubeIframeAPIReady", function(){
console.log('load player..')
player = new YT.Player(divId, {
height: '390',
width: '640',
height,
width,
videoId: videoId,
events: {
//'onReady': onPlayerReady,
@@ -30,6 +34,17 @@
}
});
})
})
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)
}
@@ -37,5 +52,4 @@
player.playVideo()
}
</script>
YT-Component VID: {videoId}<br>
<div id={divId}></div><br>
<div id={divId}></div>