/** * Embed plugin by gohabereg * @version 1.0.0 */ var embed = function(embed){ var methods = { addInternal: function (content) { codex.content.switchBlock(codex.content.currentNode, content, 'embed'); var blockContent = codex.content.currentNode.childNodes[0]; blockContent.classList.add('embed__loader'); setTimeout(function(){ blockContent.classList.remove('embed__loader'); }, 1000); }, getHtmlWithEmbedId: function (type, id) { return services[type].html.replace(/<\%\= remote\_id \%\>/g, id); }, makeElementFromHtml: function(html) { var wrapper = document.createElement('DIV'); wrapper.innerHTML = html; return wrapper.firstElementChild; } }; var services = { vimeo: { regex: /(?:http[s]?:\/\/)?(?:www.)?vimeo\.co(?:.+\/([^\/]\d+)(?:#t=[\d]+)?s?$)/, html: "" }, youtube: { regex: /^.*(?:(?:youtu\.be\/)|(?:youtube\.com)\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*)(?:[\?\&]t\=(\d*)|)/, html: "", timestamp: '?t=' }, coub: { regex: /https?:\/\/coub\.com\/view\/([^\/\?\&]+)/, html: "" }, vine: { regex: /https?:\/\/vine\.co\/v\/([^\/\?\&]+)/, html: "" }, vk: { regex: /https?:\/\/vk\.com\/.*(?:video)([-_0-9]+)/, html: "" }, imgur: { regex: /https?:\/\/(?:i\.)?imgur\.com.*\/([a-zA-Z0-9]+)(?:\.gifv)?/, html: "" }, gfycat: { regex: /https?:\/\/gfycat\.com(?:\/detail)?\/([a-zA-Z]+)/, html: "", }, 'twitch-channel': { regex: /https?:\/\/twitch.tv\/([^\/\?\&]*)/, html: "" }, 'twitch-video': { regex: /https?:\/\/www.twitch.tv\/[^\/\?\&]*\/v\/([0-9]*)/, html: "" } }; embed.make = function(data, isInternal) { if (!data.embed_id) return; var html = methods.getHtmlWithEmbedId(data.service, data.embed_id), block = methods.makeElementFromHtml(html); block.dataset.id = data.embed_id; block.dataset.embedSeirvice = data.service; if (isInternal) { methods.addInternal(block); } return block; }; /** * Saving JSON output. * Upload data via ajax */ embed.save = function(blockContent) { var data; if (!blockContent) return; data = { embed_id: blockContent.dataset.id, service: blockContent.dataset.embedService }; return data; }; /** * Render data */ embed.render = function(data) { return embed.make(data); }; embed.urlPastedCallback = function(url, pattern) { var id = pattern.regex.exec(url)[1]; var data = { embed_id: id, service: pattern.type }; embed.make(data, true); }; return embed; }({});