comparison src/plugins/autoyoutube.js @ 4:b7725dab7482

move /development/* to /
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 04 Aug 2022 17:59:02 -0600
parents src/development/plugins/autoyoutube.js@4c4fc447baea
children ea32a44b5a6e
comparison
equal deleted inserted replaced
3:ec68006a495e 4:b7725dab7482
1 /**
2 * SCEditor Auto Youtube Plugin
3 * http://www.sceditor.com/
4 *
5 * Copyright (C) 2016, Sam Clarke (samclarke.com)
6 *
7 * SCEditor is licensed under the MIT license:
8 * http://www.opensource.org/licenses/mit-license.php
9 *
10 * @author Sam Clarke
11 */
12 (function (document, sceditor) {
13 'use strict';
14
15 var dom = sceditor.dom;
16
17 /*
18 (^|\s) Start of line or space
19 (?:https?:\/\/)? Optional scheme like http://
20 (?:www\.)? Optional www. prefix
21 (?:
22 youtu\.be\/ Ends with .be/ so whatever comes next is the ID
23 |
24 youtube\.com\/watch\?v= Matches the .com version
25 )
26 ([^"&?\/ ]{11}) The actual YT ID
27 (?:\&[\&_\?0-9a-z\#]+)? Any extra URL params
28 (\s|$) End of line or space
29 */
30 var ytUrlRegex = /(^|\s)(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/watch\?v=)([^"&?\/ ]{11})(?:\&[\&_\?0-9a-z\#]+)?(\s|$)/i;
31
32 function youtubeEmbedCode(id) {
33 return '<iframe width="560" height="315" frameborder="0" ' +
34 'src="https://www.youtube-nocookie.com/embed/' + id + '" ' +
35 'data-youtube-id="' + id + '" allowfullscreen></iframe>';
36 }
37
38 function convertYoutubeLinks(parent, isRoot) {
39 var node = parent.firstChild;
40 var wholeContent = (parent.textContent || '');
41
42 // Don't care about whitespace if is the root node
43 if (isRoot) {
44 wholeContent = wholeContent.trim();
45 }
46
47 var match = wholeContent.match(ytUrlRegex);
48 // Whole content match so only return URL embed
49 if (wholeContent === wholeContent.trim() && match &&
50 match[0].length === wholeContent.length) {
51 dom.removeAttr(parent, 'style');
52 dom.removeAttr(parent, 'class');
53 parent.innerHTML = youtubeEmbedCode(match[2]);
54 return;
55 }
56
57 while (node) {
58 // 3 is TextNodes
59 if (node.nodeType === 3) {
60 var text = node.nodeValue;
61 var nodeParent = node.parentNode;
62
63 if ((match = text.match(ytUrlRegex))) {
64 nodeParent.insertBefore(document.createTextNode(
65 text.substr(0, match.index) + match[1]
66 ), node);
67
68 nodeParent.insertBefore(
69 dom.parseHTML(youtubeEmbedCode(match[2])), node
70 );
71
72 node.nodeValue = match[3] +
73 text.substr(match.index + match[0].length);
74 }
75 } else {
76 // TODO: Make this tag configurable.
77 if (!dom.is(node, 'code')) {
78 convertYoutubeLinks(node);
79 }
80 }
81
82 node = node.nextSibling;
83 }
84 };
85
86 sceditor.plugins.autoyoutube = function () {
87 this.signalPasteRaw = function (data) {
88 // TODO: Make this tag configurable.
89 // Skip code tags
90 if (dom.closest(this.currentNode(), 'code')) {
91 return;
92 }
93
94 if (data.html || data.text) {
95 var node = document.createElement('div');
96
97 node.innerHTML = data.html ||
98 sceditor.escapeEntities(data.text);
99
100 convertYoutubeLinks(node, true);
101
102 data.html = node.innerHTML;
103 }
104 };
105 };
106 })(document, sceditor);