-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
44 lines (39 loc) · 1014 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
var Nanocomponent = require('nanocomponent')
var html = require('nanohtml')
var raw = require('nanohtml/raw')
var assert = require('nanoassert')
var embedVideo = require('embed-video')
var URL = require('url').URL
class YoutubeComponent extends Nanocomponent {
constructor (opts) {
super()
this.opts = Object.assign({
placeholder: true,
class: ''
}, opts)
this.url = null
}
createElement (url) {
assert(typeof url === 'string', 'YoutubeComponent: youtubeURL must be a string')
this.url = url
var embed
try {
embed = embedVideo(url, this.opts)
} catch (e) {
console.error(e)
}
if (embed) {
return html`<div class='${this.opts.class}'>${raw(embed)}</div>`
} else {
const urlObj = new URL(url)
return (
this.opts.placeholder
? html`<div>Can't embed: ${urlObj.href}</div>`
: html`<div></div>`)
}
}
update (url) {
return this.url !== url
}
}
module.exports = YoutubeComponent