SmartPaste.js
2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
* 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
};
});