mirror of
https://github.com/codex-team/editor.js
synced 2024-06-20 22:55:15 +02:00
Rewrite plugin with function closure
This commit is contained in:
parent
d3e9b57324
commit
7439c5aa38
|
@ -177,11 +177,11 @@
|
||||||
},
|
},
|
||||||
video: {
|
video: {
|
||||||
type: 'video',
|
type: 'video',
|
||||||
make: videoTool.make,
|
make: video.make,
|
||||||
appendCallback: null,
|
appendCallback: null,
|
||||||
settings: null,
|
settings: null,
|
||||||
render: videoTool.render,
|
render: video.render,
|
||||||
save: videoTool.save,
|
save: video.save,
|
||||||
displayInToolbox: false,
|
displayInToolbox: false,
|
||||||
enableLineBreaks: false,
|
enableLineBreaks: false,
|
||||||
allowedToPaste: false
|
allowedToPaste: false
|
||||||
|
|
|
@ -39,37 +39,37 @@ pasteTool.patterns = [
|
||||||
{
|
{
|
||||||
type: 'youtube',
|
type: 'youtube',
|
||||||
regex: /^.*(?:(?:youtu\.be\/)|(?:youtube\.com)\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*)(?:[\?\&]t\=(\d*)|)/,
|
regex: /^.*(?:(?:youtu\.be\/)|(?:youtube\.com)\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*)(?:[\?\&]t\=(\d*)|)/,
|
||||||
callback: videoTool.urlPastedCallbacks.generalCallback
|
callback: video.urlPastedCallback
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: 'vimeo',
|
type: 'vimeo',
|
||||||
regex: /(?:http[s]?:\/\/)?(?:www.)?vimeo\.co(?:.+\/([^\/]\d+)(?:#t=[\d]+)?s?$)/,
|
regex: /(?:http[s]?:\/\/)?(?:www.)?vimeo\.co(?:.+\/([^\/]\d+)(?:#t=[\d]+)?s?$)/,
|
||||||
callback: videoTool.urlPastedCallbacks.generalCallback
|
callback: video.urlPastedCallback
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: 'coub',
|
type: 'coub',
|
||||||
regex: /https?:\/\/coub\.com\/view\/([^\/\?\&]+)/,
|
regex: /https?:\/\/coub\.com\/view\/([^\/\?\&]+)/,
|
||||||
callback: videoTool.urlPastedCallbacks.generalCallback
|
callback: video.urlPastedCallback
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: 'vine',
|
type: 'vine',
|
||||||
regex: /https?:\/\/vine\.co\/v\/([^\/\?\&]+)/,
|
regex: /https?:\/\/vine\.co\/v\/([^\/\?\&]+)/,
|
||||||
callback: videoTool.urlPastedCallbacks.generalCallback
|
callback: video.urlPastedCallback
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: 'vk',
|
type: 'vk',
|
||||||
regex: /https?:\/\/vk\.com\/.*(?:video)[-0-9]+_([0-9]+)/,
|
regex: /https?:\/\/vk\.com\/.*(?:video)[-0-9]+_([0-9]+)/,
|
||||||
callback: videoTool.urlPastedCallbacks.generalCallback
|
callback: video.urlPastedCallback
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: 'imgur',
|
type: 'imgur',
|
||||||
regex: /https?:\/\/(?:i\.)?imgur\.com.*\/([a-zA-Z0-9]+)(?:\.gifv)?/,
|
regex: /https?:\/\/(?:i\.)?imgur\.com.*\/([a-zA-Z0-9]+)(?:\.gifv)?/,
|
||||||
callback: videoTool.urlPastedCallbacks.generalCallback
|
callback: video.urlPastedCallback
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: 'gfycat',
|
type: 'gfycat',
|
||||||
regex: /https?:\/\/gfycat\.com(?:\/detail)?\/([a-zA-Z]+)/,
|
regex: /https?:\/\/gfycat\.com(?:\/detail)?\/([a-zA-Z]+)/,
|
||||||
callback: videoTool.urlPastedCallbacks.generalCallback
|
callback: video.urlPastedCallback
|
||||||
}
|
}
|
||||||
|
|
||||||
];
|
];
|
|
@ -2,88 +2,34 @@
|
||||||
* Video plugin by gohabereg
|
* Video plugin by gohabereg
|
||||||
* @version 1.0.0
|
* @version 1.0.0
|
||||||
*/
|
*/
|
||||||
var videoTool = {
|
var video = function(video){
|
||||||
|
|
||||||
make : function(data, isInternal) {
|
var methods = {
|
||||||
|
|
||||||
if (!data.video_id)
|
addInternal: function (content) {
|
||||||
return;
|
codex.content.switchBlock(codex.content.currentNode, content, 'video');
|
||||||
|
|
||||||
var html = videoTool.content.getHtmlWithVideoId(data.service, data.video_id),
|
var blockContent = codex.content.currentNode.childNodes[0];
|
||||||
block = videoTool.content.makeElementFromHtml(html);
|
blockContent.classList.add('video__loader');
|
||||||
|
|
||||||
block.dataset.id = data.video_id;
|
setTimeout(function(){
|
||||||
block.dataset.videoSeirvice = data.service;
|
blockContent.classList.remove('video__loader');
|
||||||
|
}, 1000);
|
||||||
|
|
||||||
if (isInternal) {
|
},
|
||||||
|
|
||||||
setTimeout(function() {
|
getHtmlWithVideoId: function (type, id) {
|
||||||
|
return services[type].html.replace(/<\%\= remote\_id \%\>/g, id);
|
||||||
|
},
|
||||||
|
|
||||||
/** Render block */
|
makeElementFromHtml: function(html) {
|
||||||
videoTool.content.render(block);
|
var wrapper = document.createElement('DIV');
|
||||||
|
wrapper.innerHTML = html;
|
||||||
}, 200);
|
return wrapper.firstElementChild;
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return block;
|
var services = {
|
||||||
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Saving JSON output.
|
|
||||||
* Upload data via ajax
|
|
||||||
*/
|
|
||||||
save : function(blockContent) {
|
|
||||||
|
|
||||||
var data;
|
|
||||||
|
|
||||||
if (!blockContent)
|
|
||||||
return;
|
|
||||||
|
|
||||||
data = {
|
|
||||||
video_id: blockContent.dataset.id,
|
|
||||||
service: blockContent.dataset.videoService
|
|
||||||
};
|
|
||||||
|
|
||||||
return data;
|
|
||||||
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Render data
|
|
||||||
*/
|
|
||||||
render : function(data) {
|
|
||||||
return videoTool.make(data);
|
|
||||||
},
|
|
||||||
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
videoTool.content = {
|
|
||||||
|
|
||||||
render: function (content) {
|
|
||||||
codex.content.switchBlock(codex.content.currentNode, content, 'video');
|
|
||||||
|
|
||||||
var blockContent = codex.content.currentNode.childNodes[0];
|
|
||||||
blockContent.classList.add('video__loader');
|
|
||||||
|
|
||||||
setTimeout(function(){
|
|
||||||
blockContent.classList.remove('video__loader');
|
|
||||||
}, 1000);
|
|
||||||
|
|
||||||
},
|
|
||||||
|
|
||||||
getHtmlWithVideoId: function (type, id) {
|
|
||||||
return videoTool.content.services[type].html.replace(/<\%\= remote\_id \%\>/g, id);
|
|
||||||
},
|
|
||||||
|
|
||||||
makeElementFromHtml: function(html) {
|
|
||||||
var wrapper = document.createElement('DIV');
|
|
||||||
wrapper.innerHTML = html;
|
|
||||||
return wrapper.firstElementChild;
|
|
||||||
},
|
|
||||||
|
|
||||||
services: {
|
|
||||||
vimeo: {
|
vimeo: {
|
||||||
regex: /(?:http[s]?:\/\/)?(?:www.)?vimeo\.co(?:.+\/([^\/]\d+)(?:#t=[\d]+)?s?$)/,
|
regex: /(?:http[s]?:\/\/)?(?:www.)?vimeo\.co(?:.+\/([^\/]\d+)(?:#t=[\d]+)?s?$)/,
|
||||||
html: "<iframe src=\"https://player.vimeo.com/video/<%= remote_id %>?title=0&byline=0\" width=\"580\" height=\"320\" frameborder=\"0\"></iframe>"
|
html: "<iframe src=\"https://player.vimeo.com/video/<%= remote_id %>?title=0&byline=0\" width=\"580\" height=\"320\" frameborder=\"0\"></iframe>"
|
||||||
|
@ -113,11 +59,56 @@ videoTool.content = {
|
||||||
regex: /https?:\/\/gfycat\.com(?:\/detail)?\/([a-zA-Z]+)/,
|
regex: /https?:\/\/gfycat\.com(?:\/detail)?\/([a-zA-Z]+)/,
|
||||||
html: "<iframe src='https://gfycat.com/ifr/<%= remote_id %>' frameborder='0' scrolling='no' width='580' height='436' allowfullscreen ></iframe>",
|
html: "<iframe src='https://gfycat.com/ifr/<%= remote_id %>' frameborder='0' scrolling='no' width='580' height='436' allowfullscreen ></iframe>",
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
};
|
|
||||||
|
|
||||||
videoTool.urlPastedCallbacks = {
|
|
||||||
generalCallback: function(url, pattern) {
|
video.make = function(data, isInternal) {
|
||||||
|
|
||||||
|
if (!data.video_id)
|
||||||
|
return;
|
||||||
|
|
||||||
|
var html = methods.getHtmlWithVideoId(data.service, data.video_id),
|
||||||
|
block = methods.makeElementFromHtml(html);
|
||||||
|
|
||||||
|
block.dataset.id = data.video_id;
|
||||||
|
block.dataset.videoSeirvice = data.service;
|
||||||
|
|
||||||
|
if (isInternal) {
|
||||||
|
methods.addInternal(block);
|
||||||
|
}
|
||||||
|
|
||||||
|
return block;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Saving JSON output.
|
||||||
|
* Upload data via ajax
|
||||||
|
*/
|
||||||
|
video.save = function(blockContent) {
|
||||||
|
|
||||||
|
var data;
|
||||||
|
|
||||||
|
if (!blockContent)
|
||||||
|
return;
|
||||||
|
|
||||||
|
data = {
|
||||||
|
video_id: blockContent.dataset.id,
|
||||||
|
service: blockContent.dataset.videoService
|
||||||
|
};
|
||||||
|
|
||||||
|
return data;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Render data
|
||||||
|
*/
|
||||||
|
video.render = function(data) {
|
||||||
|
return video.make(data);
|
||||||
|
};
|
||||||
|
|
||||||
|
video.urlPastedCallback = function(url, pattern) {
|
||||||
|
|
||||||
var id = pattern.regex.exec(url)[1];
|
var id = pattern.regex.exec(url)[1];
|
||||||
|
|
||||||
|
@ -126,6 +117,9 @@ videoTool.urlPastedCallbacks = {
|
||||||
service: pattern.type
|
service: pattern.type
|
||||||
};
|
};
|
||||||
|
|
||||||
videoTool.make(data, true);
|
video.make(data, true);
|
||||||
}
|
};
|
||||||
};
|
|
||||||
|
return video;
|
||||||
|
|
||||||
|
}({});
|
Loading…
Reference in a new issue