-
Notifications
You must be signed in to change notification settings - Fork 33
/
youtubeComponent.js
159 lines (132 loc) · 4.42 KB
/
youtubeComponent.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
'use strict';
var Utils = require('../utils');
var IFrameComponent = require('./iframeComponent');
var Loader = require('../loader');
/**
* YouTubeComponent main.
* @param {Object=} opt_params Optional params to initialize the object.
* Default:
* {
* src: '',
* caption: null,
* width: '100%',
* height: '360px',
* name: Utils.getUID()
* }
* @extends {./iframeComponent}
* @constructor
*/
var YouTubeComponent = function(opt_params) {
// Override default params with passed ones if any.
var params = Utils.extend({
src: '',
caption: null,
width: '100%',
// TODO(mkhatib): Implement and auto-height mode where it can calculate
// the best ratio for the player.
height: '360px',
}, opt_params);
IFrameComponent.call(this, params);
};
YouTubeComponent.prototype = Object.create(IFrameComponent.prototype);
module.exports = YouTubeComponent;
/**
* String name for the component class.
* @type {string}
*/
YouTubeComponent.CLASS_NAME = 'YouTubeComponent';
Loader.register(YouTubeComponent.CLASS_NAME, YouTubeComponent);
/**
* Regex strings list that for matching YouTube URLs.
* @type {Array<string>}
*/
YouTubeComponent.YOUTUBE_URL_REGEXS = [
'(?:https?://(?:www\.)?youtube\.com\/(?:[^\/]+/.+/|' +
'(?:v|e(?:mbed)?)/|.*[?&]v=)|' +
'youtu\.be/)([^"&?/ ]{11})',
];
/**
* Returns the class name of the component.
* @return {string} Class name of the component.
*/
YouTubeComponent.prototype.getComponentClassName = function() {
return YouTubeComponent.CLASS_NAME;
};
/**
* Create and initiate a youtube object from JSON.
* @param {Object} json JSON representation of the youtube.
* @return {YouTubeComponent} YouTubeComponent object representing JSON data.
*/
YouTubeComponent.fromJSON = function(json) {
return new YouTubeComponent(json);
};
/**
* Handles onInstall when the YouTubeComponent module installed in an editor.
* @param {../editor} editor Instance of the editor that installed the module.
*/
YouTubeComponent.onInstall = function(editor) {
YouTubeComponent.registerRegexes_(editor);
// TODO(mkhatib): Initialize a toolbar for all YouTube components instances.
};
/**
* Registers regular experessions to create YouTube component from if matched.
* @param {../editor} editor The editor to register regexes with.
* @private
*/
YouTubeComponent.registerRegexes_ = function(editor) {
for (var i = 0; i < YouTubeComponent.YOUTUBE_URL_REGEXS.length; i++) {
editor.registerRegex(
YouTubeComponent.YOUTUBE_URL_REGEXS[i],
YouTubeComponent.handleMatchedRegex);
}
};
/**
* Creates a YouTube video component from a link.
* @param {string} link YouTube video URL.
* @return {YouTubeComponent} YouTubeComponent component created from the link.
*/
YouTubeComponent.createYouTubeComponentFromLink = function(link, attrs) {
var src = link;
for (var i = 0; i < YouTubeComponent.YOUTUBE_URL_REGEXS.length; i++) {
var regex = new RegExp(YouTubeComponent.YOUTUBE_URL_REGEXS);
var matches = regex.exec(src);
if (matches) {
src = YouTubeComponent.createEmbedSrcFromId(matches[1]);
break;
}
}
return new YouTubeComponent(Utils.extend({src: src}, attrs));
};
/**
* Creates a YouTube video component from a link.
* @param {../paragraph} matchedComponent Component that matched registered regex.
* @param {function(Array<../defs.OperationDef>)} opsCallback Callback to send list of operations to exectue.
*/
YouTubeComponent.handleMatchedRegex = function(matchedComponent, opsCallback) {
var atIndex = matchedComponent.getIndexInSection();
var ops = [];
var ytComponent = YouTubeComponent.createYouTubeComponentFromLink(
matchedComponent.text, {});
ytComponent.section = matchedComponent.section;
// Delete current matched component with its text.
Utils.arrays.extend(ops, matchedComponent.getDeleteOps(atIndex));
// Add the new component created from the text.
Utils.arrays.extend(ops, ytComponent.getInsertOps(atIndex));
opsCallback(ops);
};
/**
* Returns the embed src URL for the id.
* @param {string} id YouTube video ID.
* @return {string} Embed src URL.
*/
YouTubeComponent.createEmbedSrcFromId = function(id) {
return 'https://www.youtube.com/embed/' + id +
'?rel=0&showinfo=0&iv_load_policy=3';
};
/**
* Returns the length of the youtube component content.
* @return {number} Length of the youtube component content.
*/
YouTubeComponent.prototype.getLength = function() {
return 1;
};