Skip to content

Commit

Permalink
Merge pull request #711 from m-a-x-s-e-e-l-i-g/develop
Browse files Browse the repository at this point in the history
Added Plugin: Paste link into editor and it'll be replaced by iframe, using noembed API.
  • Loading branch information
Alex-D authored Apr 9, 2018
2 parents 49b4350 + 1e32974 commit e4cd992
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/demos/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ <h1 class="documentation-title">
<li><a href="./plugins/history.html">History</a></li>
<li><a href="./plugins/insertaudio.html">Insert audio</a></li>
<li><a href="./plugins/noembed.html">Noembed</a></li>
<li><a href="./plugins/pasteembed.html">Paste embed</a></li>
<!-- TODO <li><a href="./plugins/pasteimage.html">Paste image</a></li> -->
<!-- TODO <li><a href="./plugins/preformatted.html">Preformatted</a></li> -->
<li><a href="./plugins/table.html">Table</a></li>
Expand Down
69 changes: 69 additions & 0 deletions docs/demos/plugins/pasteembed.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>pasteEmbed | Trumbowyg</title>
<link href='https://fonts.googleapis.com/css?family=Open+Sans:300,400' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="../../css/main.css">
</head>
<body class="documentation-body">
<div class="main main-demo-inner">
<section class="wrapper section">
<h2 class="section-title">Embed plugin</h2>

<div class="feature">
<h3>Basic usage</h3>
<p>
This plugin allow you to insert iframes into your editor just by pasting an url.<br/>
It uses <a href="https://noembed.com">noembed</a> API to support Twitter, Youtube, Soundcloud and more. Find all supported websites at <a href="https://noembed.com">noembed</a>.<br/>
This plugin also uses <a href="https://www.maxmade.nl/">MAXmade</a> API in order to extend the list of supported websites. Need a website supported? Message them, they're nice.
</p>

<a href="../../documentation/plugins/#plugin-pasteembed" class="button button-demo">Read paste embed plugin documentation</a>

<div id="editor">
<h2>Try to paste some urls!</h2>
<iframe width="100%" height="166" scrolling="no" frameborder="no" allow="autoplay" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/94194736&amp;color=%23ff5500&amp;auto_play=false&amp;hide_related=false&amp;show_comments=true&amp;show_user=true&amp;show_reposts=false&amp;show_teaser=true"></iframe>
<p>Try pasting this one: https://www.youtube.com/watch?v=000al7ru3ms</p>
</div>

<h3>The code</h3>
<pre><code class="js-code-to-eval javascript">
$('#editor')
.trumbowyg({
});
</code></pre>
</div>

<div class="feature">
<h3>Setup</h3>

<h4>In head tag</h4>
<pre><code class="html loading-head">
</code></pre>
<h4>At the end of body</h4>
<pre><code class="html loading-body">
&lt;!-- Import jQuery -->
&lt;script src="//ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">&lt;/script>
&lt;script>window.jQuery || document.write('&lt;script src="js/vendor/jquery-3.2.1.min.js">&lt;\/script>')&lt;/script>
</code></pre>
</div>
</section>
</div>


<!-- Import jQuery -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="../../js/vendor/jquery-3.2.1.min.js"><\/script>')</script>

<!-- DO NOT COPY THIS LINES IN YOUR PROJECT, THEY ARE THERE JUST FOR THE EXAMPLE PAGE PRUPOSE -->
<script src="../js/loader.js"></script>
<script>
loadStyle('dist/ui/trumbowyg.css');
loadScript('dist/trumbowyg.js', 'Import Trumbowyg');
loadScript('dist/plugins/pasteembed/trumbowyg.pasteembed.js', 'Import all plugins you want AFTER importing jQuery and Trumbowyg');
</script>
<script src="../js/runExampleCode.js"></script>
<script src="../js/highlight.js"></script>
</body>
</html>
26 changes: 26 additions & 0 deletions docs/documentation/plugins/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ <h1 class="documentation-title">
<li><a href="#plugin-mathml">MathML</a></li>
<li><a href="#plugin-mention">Mention</a></li>
<li><a href="#plugin-noembed">Noembed</a></li>
<li><a href="#plugin-pasteembed">Paste embed</a></li>
<li><a href="#plugin-pasteimage">Paste image</a></li>
<li><a href="#plugin-preformatted">Preformatted</a></li>
<li><a href="#plugin-resizimg">Resizimg</a></li>
Expand Down Expand Up @@ -524,6 +525,31 @@ <h4>How to use it?</h4>
});
</code></pre>
</div>

<div class="feature">
<h3 id="plugin-pasteembed">Paste embed</h3>
<p class="added-feature">Stable</p>
<p class="deprecated-feature">Need demo</p>

<p>
Paste embed plugin handles paste events. It looks for paste event, checks if it's a url and uses noembed and MAXmade APIs to retrieve an iframe from that url. If it can't retrieve an iframe, it will put an anchor tag around the url. It doesn't do anything on text or HTML paste.
</p>

<p>
<a href="https://github.com/Alex-D/Trumbowyg/tree/develop/plugins/pasteembed/trumbowyg.pasteembed.js" class="button button-demo">
View pasteembed plugin code on GitHub
</a>
</p>

<h4>How to use it?</h4>
<pre><code class="html">
&lt;-- Import Trumbowyg plugins... -->
&lt;script src="node_modules/trumbowyg/dist/plugins/pasteembed/trumbowyg.pasteembed.min.js">&lt;/script>
</code></pre>
<p>
Paste embed now works on every new Trumbowyg instance.
</p>
</div>

<div class="feature">
<h3 id="plugin-pasteimage">Paste image</h3>
Expand Down
81 changes: 81 additions & 0 deletions plugins/pasteembed/trumbowyg.pasteembed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/* ===========================================================
* trumbowyg.pasteembed.js v1.0
* Url paste to iframe with noembed. Plugin for Trumbowyg
* http://alex-d.github.com/Trumbowyg
* ===========================================================
* Author : Max Seelig
* Facebook : https://facebook.com/maxse
* Website : https://www.maxmade.nl/
*/

(function($) {
"use strict";

$.extend(true, $.trumbowyg, {
plugins: {
pasteImage: {
init: function(trumbowyg) {
trumbowyg.pasteHandlers.push(function(pasteEvent) {
try {
var clipboardData = (pasteEvent.originalEvent || pasteEvent).clipboardData;
var pastedData = clipboardData.getData("Text");
var request = null;

if (pastedData.startsWith("http")) {

pasteEvent.stopPropagation();
pasteEvent.preventDefault();

var query = {
url: pastedData.trim()
};
var content = "";
var fails = 0;

if (request && request.transport) request.transport.abort();

request = $.ajax({
crossOrigin: true,
url: "https://noembed.com/embed?nowrap=on",
url2: "https://api.maxmade.nl/url2iframe/embed",
type: "GET",
data: query,
cache: false,
dataType: "jsonp",
success: function(res) {
if (res.html) {
fails = 0;
content = res.html;
} else {
fails++;
}
},
error: function(res) {
fails++;
},
complete: function() {
if (fails === 1) {
this.url = this.url2;
this.data = query;
$.ajax(this);
}
if (fails === 2) {
content = $("<a>", {
href: pastedData,
text: pastedData
}).prop('outerHTML');
}
if (content.length > 0) {
fails = 0;
trumbowyg.execCmd("insertHTML", content);
}
}
});
}
} catch (c) {}
});
}
}
}
});
})(jQuery);

0 comments on commit e4cd992

Please sign in to comment.