-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.js
More file actions
434 lines (387 loc) · 11.7 KB
/
plugin.js
File metadata and controls
434 lines (387 loc) · 11.7 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
(function videoPlugin(tinymce) {
"use strict";
const PROVIDERS = ["youtube", "vimeo"];
PROVIDERS["youtube"] = {
// https://youtu.be/$id
// https://www.youtube.com/watch?v=$id
// https://www.youtube.com/embed/$id
pattern: /youtu(?:\.be|be\.com)\/(?:watch\?v=|embed\/)?([a-z0-9\-_]+)(?:\?|&)?(.+)?/i,
api: "//www.youtube.com/embed/",
options: [
{ name: "rel", type: "checkbox", text: "Show suggested videos when the video finishes" },
{ name: "controls", type: "checkbox", text: "Show player controls" },
{ name: "showinfo", type: "checkbox", text: "Show video title and player actions" }
]
};
PROVIDERS["vimeo"] = {
// https://vimeo.com/$id
// https://vimeo.com/*/*/video/$id
// https://vimeo.com/album/*/video/$id
// https://vimeo.com/channels/*/*
// https://vimeo.com/groups/*/videos/$id
// https://vimeo.com/ondemand/*/*
// https://player.vimeo.com/video/$id
pattern: /(?:player.)?vimeo.com\/(?:video\/)?([0-9]+)(?:\?|&)?(.+)?/,
api: "//player.vimeo.com/video/",
options: [
{ name: "portrait", type: "checkbox", "text": "Show portrait in overlay" },
{ name: "title", type: "checkbox", "text": "Show title in overlay" },
{ name: "byline", type: "checkbox", "text": "Show byline in overlay" }
]
};
/**
* @typedef {Object} Video
* @property {string|number} id Video ID
* @property {string} provider The video provider, i.e., youtube, vimeo, etc...
* @property {string} url The embed URL
* @property {...any} option The rest of options.
*/
/**
* Parses video URL into data object
*
* @param {string} url The video URL
* @returns {Video} The video data
*/
function parseUrl(url) {
const data = {};
PROVIDERS.forEach(function buildData(name) {
const provider = PROVIDERS[name];
const matches = url.match(provider.pattern);
if (matches) {
data.name = name;
const id = matches[1];
const providerUrl = provider.api + id;
data.url = providerUrl;
return data;
}
});
//if data is an empty object, then it is not a known provider
if (Object.keys(data).length === 0) {
return false;
} else {
return data;
}
}
function Plugin(editor, url) {
this.showDialog = this.showDialog.bind(this);
this.render = this.render.bind(this);
const plugin = this;
plugin.editor = editor;
plugin.currentProvider = null;
this.configs = {};
const baseConfig = {
title: "Insert a video",
initialData: {},
body: {
type: "panel",
items: []
},
buttons: [
{
type: "cancel",
name: "cancel",
text: "Cancel"
},
{
type: "submit",
name: "save",
text: "Save",
primary: true
}
],
onChange: function onChange(api) {
plugin.providerChange(api);
plugin.render();
},
onSubmit: function onSubmit(api) {
plugin.onsubmit();
api.close();
}
};
const sharedFields = [
{
type: "input",
label: "Source",
name: "url",
},
{
type: "grid",
columns: 3,
items: [
{
type: "input",
label: "Width",
name: "width",
},
{
type: "input",
label: "Height",
name: "height",
},
{
type: "checkbox",
label: "Fullscreen",
name: "fullscreen",
}
]
},
];
const unkownFields = [ ...sharedFields, ...[
{
type: "htmlpanel",
html: "<div id='preview'></div>",
}
]
];
const youtubeFields = [ ...sharedFields, ...[
{
type: "checkbox",
label: "Show suggested videos when the video finishes",
name: "rel",
},
{
type: "checkbox",
label: "Show player controls",
name: "controls",
},
{
type: "checkbox",
label: "Show video title and player actions",
name: "showinfo",
},
{
type: "htmlpanel",
html: "<div id='preview'></div>",
}
]
];
const vimeoFields = [ ...sharedFields, ...[
{
type: "checkbox",
label: "Show portrait in overlay",
name: "portrait",
},
{
type: "checkbox",
label: "Show title in overlay",
name: "title",
},
{
type: "checkbox",
label: "Show byline in overlay",
name: "byline",
},
{
type: "htmlpanel",
html: "<div id='preview'></div>",
}
]
];
this.configs["unknown"] = { ...baseConfig, ...{
body: {
type: "panel",
items: unkownFields
}
}
};
this.configs["youtube"] = { ...baseConfig, ...{
body: {
type: "panel",
items: youtubeFields
}
}
};
this.configs["vimeo"] = { ...baseConfig, ...{
body: {
type: "panel",
items: vimeoFields
}
}
};
const mapSVG = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" x="0px" y="0px" width="24px" height="24px" viewBox="0 0 48 48" xml:space="preserve"><g><g><g>
<path d="M44,4.982H4c-2.2,0-4,1.8-4,4v30.036c0,2.2,1.8,4,4,4h40c2.2,0,4-1.8,4-4V8.982C48,6.782,46.2,4.982,44,4.982z M20.204,12.789l9.593,6.55l-9.593,6.55V12.789z M40.001,35.212H18.875c-0.57,1.239-1.816,2.104-3.27,2.104 s-2.699-0.866-3.27-2.104H8c-0.829,0-1.5-0.673-1.5-1.5c0-0.828,0.671-1.5,1.5-1.5h4.336c0.57-1.24,1.815-2.106,3.27-2.106 s2.7,0.866,3.27,2.106h21.126c0.828,0,1.5,0.672,1.5,1.5C41.502,34.54,40.829,35.212,40.001,35.212z" fill="#333333"/>
<circle cx="15.605" cy="33.712" r="1.397" fill="#333333"/>
</g></g></g></svg>`;
editor.ui.registry.addIcon("video", mapSVG);
// Add a button that opens a window
editor.ui.registry.addButton("video", {
icon: "video",
tooltip: "Insert/edit video",
onAction: (_) => plugin.showDialog()
});
editor.ui.registry.addMenuItem("video", {
icon: "video",
text: "Video",
onAction: (_) => plugin.showDialog(),
context: "insert"
});
}
/**
* Parses selected video and display dialog for it
*
* If there is a selected video, it will parse information out of it,
* such as the source URL, the dimensions, etc...
*
* It then uses the information to display the dialog.
*/
Plugin.prototype.showDialog = function showDialog() {
const editor = this.editor;
const dom = editor.dom;
const params = {};
const plugin = this;
// Parse the current map source for values to insert into
// the dialog inputs.
let videoElement = editor.selection.getNode();
if (videoElement) {
let url = dom.getAttrib(videoElement, "src");
if (!url) {
// This is the element used by TinyMCE to wrap the iframe.
videoElement = videoElement.children[0];
url = dom.getAttrib(videoElement, "src");
}
params.url = url;
const allowFullscreen = dom.getAttrib(videoElement, "allowfullscreen");
if (allowFullscreen) {
params.fullscreen = true;
}
params.width = dom.getAttrib(videoElement, "width");
params.height = dom.getAttrib(videoElement, "height");
}
if (!params.width) {
params.width = "400";
}
if (!params.height) {
params.height = "300";
}
// Reset this so `.render` runs correctly.
this.window = null;
const provider = parseUrl(params.url);
if (provider.name === "youtube" || provider.name === "vimeo") {
const queryString = params.url.split("?")[1];
const queryParams = new URLSearchParams(queryString);
const options = PROVIDERS[provider.name].options || [];
params.url = provider.url;
options.forEach(function buildParams(option) {
const name = option.name;
const value = queryParams.get(name);
if (value === "0" || value === false) {
params[name] = false;
} else {
params[name] = true;
}
});
let config = this.configs[provider.name];
config.initialData = params;
this.currentProvider = provider.name;
this.window = this.editor.windowManager.open(config);
} else {
let config = this.configs["unknown"];
config.initialData = params;
this.currentProvider = null;
this.window = this.editor.windowManager.open(config);
}
plugin.render();
}
/**
* Handles change in provider. Loads the correct dialog when video provider is changed.
*/
Plugin.prototype.providerChange = function providerChange(api) {
const data = api.getData();
const provider = parseUrl(data.url);
if (provider.name === this.currentProvider || (this.currentProvider === null && provider === false)) {
return;
}
if (provider.name === "youtube") {
data.rel = true;
data.controls = true;
data.showinfo = true;
let config = this.configs[provider.name];
config.initialData = data;
api.redial(config);
this.currentProvider = provider.name;
} else if (provider.name === "vimeo") {
data.portrait = true;
data.title = true;
data.byline = true;
let config = this.configs[provider.name];
config.initialData = data;
api.redial(config);
this.currentProvider = provider.name;
} else {
let config = this.configs["unknown"];
config.initialData = data;
api.redial(config);
this.currentProvider = null;
}
};
/**
* Renders video option inputs and preview panel.
*
* Different video provider has different option set, so
* we need to load different inputs depending on the URL.
*
* It then also renders the preview panel for the video.
*/
Plugin.prototype.render = function render() {
const data = this.window.getData();
let html = this.html(true);
const url = data.url;
if (!url) {
return "";
}
html = this.html(true);
const preview = $AM.find("#preview")[0];
preview.innerHTML = html;
return html;
}
/**
* Generates the HTML for the iframe.
*/
Plugin.prototype.html = function html(useInlineStyle) {
const data = this.window.getData();
let embedUrl = data.url;
const provider = parseUrl(data.url);
const queries = [];
if (provider) {
const options = PROVIDERS[provider.name].options || [];
options.forEach(function buildQueries(option) {
const name = option.name;
const value = data[name];
if (value === "0" || value === false) {
queries.push(name + "=" + 0);
}
});
embedUrl = provider.url;
}
if (queries.length) {
embedUrl += "?" + queries.join("&");
}
const width = data.width;
const height = data.height;
let html = "<iframe" +
" src='" + embedUrl + "'" +
" width='" + width + "' height='" + height + "'";
if (data.fullscreen) {
html += " allowfullscreen";
}
if (useInlineStyle) {
html += " style='width: " + width + "px; height: " + height + "px;'";
}
html += " ></iframe>";
return html;
};
Plugin.prototype.onsubmit = function onSubmit() {
// Insert content when the window form is submitted
this.editor.insertContent(this.html());
};
// Register plugin
tinymce.PluginManager.add("video", (editor, url) => {
let plugin = new Plugin(editor, url);
return {
getMetadata: () => ({
name: "Video - Add embeded video easily.",
url: "http://www.brandextract.com",
})
};
});
})(window.tinymce);