]> scripts.mit.edu Git - autoinstalls/wordpress.git/blobdiff - wp-includes/js/tinymce/plugins/paste/plugin.js
Wordpress 4.6-scripts
[autoinstalls/wordpress.git] / wp-includes / js / tinymce / plugins / paste / plugin.js
index 7ecf23a44a7e6878ecfc5756706a230f3975e51e..8839e9ad4486934e993cb2ee66356692bbf299f7 100644 (file)
@@ -240,6 +240,99 @@ define("tinymce/pasteplugin/Utils", [
        };
 });
 
+// Included from: js/tinymce/plugins/paste/classes/SmartPaste.js
+
+/**
+ * SmartPaste.js
+ *
+ * Released under LGPL License.
+ * Copyright (c) 1999-2016 Ephox Corp. All rights reserved
+ *
+ * License: http://www.tinymce.com/license
+ * Contributing: http://www.tinymce.com/contributing
+ */
+
+/**
+ * Tries to be smart depending on what the user pastes if it looks like an url
+ * it will make a link out of the current selection. If it's an image url that looks
+ * like an image it will check if it's an image and insert it as an image.
+ *
+ * @class tinymce.pasteplugin.SmartPaste
+ * @private
+ */
+define("tinymce/pasteplugin/SmartPaste", [
+       "tinymce/util/Tools"
+], function (Tools) {
+       var isAbsoluteUrl = function (url) {
+               return /^https?:\/\/[\w\?\-\/+=.&%]+$/i.test(url);
+       };
+
+       var isImageUrl = function (url) {
+               return isAbsoluteUrl(url) && /.(gif|jpe?g|png)$/.test(url);
+       };
+
+       var createImage = function (editor, url, pasteHtml) {
+               editor.undoManager.extra(function () {
+                       pasteHtml(editor, url);
+               }, function () {
+                       editor.insertContent('<img src="' + url + '">');
+               });
+
+               return true;
+       };
+
+       var createLink = function (editor, url, pasteHtml) {
+               editor.undoManager.extra(function () {
+                       pasteHtml(editor, url);
+               }, function () {
+                       editor.execCommand('mceInsertLink', false, url);
+               });
+
+               return true;
+       };
+
+       var linkSelection = function (editor, html, pasteHtml) {
+               return editor.selection.isCollapsed() === false && isAbsoluteUrl(html) ? createLink(editor, html, pasteHtml) : false;
+       };
+
+       var insertImage = function (editor, html, pasteHtml) {
+               return isImageUrl(html) ? createImage(editor, html, pasteHtml) : false;
+       };
+
+       var pasteHtml = function (editor, html) {
+               editor.insertContent(html, {
+                       merge: editor.settings.paste_merge_formats !== false,
+                       paste: true
+               });
+
+               return true;
+       };
+
+       var smartInsertContent = function (editor, html) {
+               Tools.each([
+                       linkSelection,
+                       insertImage,
+                       pasteHtml
+               ], function (action) {
+                       return action(editor, html, pasteHtml) !== true;
+               });
+       };
+
+       var insertContent = function (editor, html) {
+               if (editor.settings.smart_paste === false) {
+                       pasteHtml(editor, html);
+               } else {
+                       smartInsertContent(editor, html);
+               }
+       };
+
+       return {
+               isImageUrl: isImageUrl,
+               isAbsoluteUrl: isAbsoluteUrl,
+               insertContent: insertContent
+       };
+});
+
 // Included from: js/tinymce/plugins/paste/classes/Clipboard.js
 
 /**
@@ -276,8 +369,9 @@ define("tinymce/pasteplugin/Clipboard", [
        "tinymce/dom/RangeUtils",
        "tinymce/util/VK",
        "tinymce/pasteplugin/Utils",
+       "tinymce/pasteplugin/SmartPaste",
        "tinymce/util/Delay"
-], function(Env, RangeUtils, VK, Utils, Delay) {
+], function(Env, RangeUtils, VK, Utils, SmartPaste, Delay) {
        return function(editor) {
                var self = this, pasteBinElm, lastRng, keyboardPasteTimeStamp = 0, draggingInternally = false;
                var pasteBinDefaultContent = '%MCEPASTEBIN%', keyboardPastePlainTextState;
@@ -311,7 +405,7 @@ define("tinymce/pasteplugin/Clipboard", [
                                }
 
                                if (!args.isDefaultPrevented()) {
-                                       editor.insertContent(html, {merge: editor.settings.paste_merge_formats !== false, data: {paste: true}});
+                                       SmartPaste.insertContent(editor, html);
                                }
                        }
                }
@@ -571,6 +665,55 @@ define("tinymce/pasteplugin/Clipboard", [
                        return hasContentType(content, 'text/html') || hasContentType(content, 'text/plain');
                }
 
+               function getBase64FromUri(uri) {
+                       var idx;
+
+                       idx = uri.indexOf(',');
+                       if (idx !== -1) {
+                               return uri.substr(idx + 1);
+                       }
+
+                       return null;
+               }
+
+               function isValidDataUriImage(settings, imgElm) {
+                       return settings.images_dataimg_filter ? settings.images_dataimg_filter(imgElm) : true;
+               }
+
+               function pasteImage(rng, reader, blob) {
+                       if (rng) {
+                               editor.selection.setRng(rng);
+                               rng = null;
+                       }
+
+                       var dataUri = reader.result;
+                       var base64 = getBase64FromUri(dataUri);
+
+                       var img = new Image();
+                       img.src = dataUri;
+
+                       // TODO: Move the bulk of the cache logic to EditorUpload
+                       if (isValidDataUriImage(editor.settings, img)) {
+                               var blobCache = editor.editorUpload.blobCache;
+                               var blobInfo, existingBlobInfo;
+
+                               existingBlobInfo = blobCache.findFirst(function(cachedBlobInfo) {
+                                       return cachedBlobInfo.base64() === base64;
+                               });
+
+                               if (!existingBlobInfo) {
+                                       blobInfo = blobCache.create(uniqueId(), blob, base64);
+                                       blobCache.add(blobInfo);
+                               } else {
+                                       blobInfo = existingBlobInfo;
+                               }
+
+                               pasteHtml('<img src="' + blobInfo.blobUri() + '">');
+                       } else {
+                               pasteHtml('<img src="' + dataUri + '">');
+                       }
+               }
+
                /**
                 * Checks if the clipboard contains image data if it does it will take that data
                 * and convert it into a data url image and paste that image at the caret location.
@@ -582,33 +725,9 @@ define("tinymce/pasteplugin/Clipboard", [
                function pasteImageData(e, rng) {
                        var dataTransfer = e.clipboardData || e.dataTransfer;
 
-                       function getBase64FromUri(uri) {
-                               var idx;
-
-                               idx = uri.indexOf(',');
-                               if (idx !== -1) {
-                                       return uri.substr(idx + 1);
-                               }
-
-                               return null;
-                       }
-
                        function processItems(items) {
                                var i, item, reader, hadImage = false;
 
-                               function pasteImage(reader, blob) {
-                                       if (rng) {
-                                               editor.selection.setRng(rng);
-                                               rng = null;
-                                       }
-
-                                       var blobCache = editor.editorUpload.blobCache;
-                                       var blobInfo = blobCache.create(uniqueId(), blob, getBase64FromUri(reader.result));
-                                       blobCache.add(blobInfo);
-
-                                       pasteHtml('<img src="' + blobInfo.blobUri() + '">');
-                               }
-
                                if (items) {
                                        for (i = 0; i < items.length; i++) {
                                                item = items[i];
@@ -617,7 +736,7 @@ define("tinymce/pasteplugin/Clipboard", [
                                                        var blob = item.getAsFile ? item.getAsFile() : item;
 
                                                        reader = new FileReader();
-                                                       reader.onload = pasteImage.bind(null, reader, blob);
+                                                       reader.onload = pasteImage.bind(null, rng, reader, blob);
                                                        reader.readAsDataURL(blob);
 
                                                        e.preventDefault();
@@ -874,6 +993,7 @@ define("tinymce/pasteplugin/Clipboard", [
 
                self.pasteHtml = pasteHtml;
                self.pasteText = pasteText;
+               self.pasteImageData = pasteImageData;
 
                editor.on('preInit', function() {
                        registerEventHandlers();