};
});
+// 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
/**
"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;
}
if (!args.isDefaultPrevented()) {
- editor.insertContent(html, {merge: editor.settings.paste_merge_formats !== false, data: {paste: true}});
+ SmartPaste.insertContent(editor, html);
}
}
}
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.
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];
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();
self.pasteHtml = pasteHtml;
self.pasteText = pasteText;
+ self.pasteImageData = pasteImageData;
editor.on('preInit', function() {
registerEventHandlers();