2016-12-25 15:41:57 +01:00
|
|
|
/**
|
|
|
|
* Paste plugin.
|
|
|
|
*
|
2017-01-12 15:42:10 +01:00
|
|
|
* Listen to clipboard paste event and analize pasted text whit patterns in pattern.js
|
2016-12-25 15:41:57 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @protected
|
|
|
|
*
|
|
|
|
* Main tool settings.
|
|
|
|
*/
|
2017-02-02 14:58:05 +01:00
|
|
|
|
|
|
|
let editor = codex.editor;
|
|
|
|
|
2017-01-26 00:55:40 +01:00
|
|
|
var paste = function(paste){
|
2016-12-25 15:41:57 +01:00
|
|
|
/**
|
|
|
|
* Saves data
|
|
|
|
* @param event
|
|
|
|
*/
|
2017-01-26 00:55:40 +01:00
|
|
|
paste.pasted = function(event) {
|
2016-12-25 15:41:57 +01:00
|
|
|
|
|
|
|
var clipBoardData = event.clipboardData || window.clipboardData,
|
|
|
|
content = clipBoardData.getData('Text');
|
|
|
|
|
2017-01-26 01:47:19 +01:00
|
|
|
var result = analize(content);
|
2017-01-18 16:12:50 +01:00
|
|
|
|
|
|
|
if (result) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopImmediatePropagation();
|
|
|
|
}
|
2017-01-26 00:55:40 +01:00
|
|
|
};
|
2016-12-25 15:41:57 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Analizes pated string and calls necessary method
|
|
|
|
*/
|
|
|
|
|
2017-01-26 00:55:40 +01:00
|
|
|
var analize = function(string) {
|
2016-12-25 15:41:57 +01:00
|
|
|
|
2017-02-02 14:58:05 +01:00
|
|
|
var result = false,
|
|
|
|
content = editor.content.currentNode,
|
|
|
|
plugin = content.dataset.tool;
|
2016-12-25 15:41:57 +01:00
|
|
|
|
2017-01-26 00:55:40 +01:00
|
|
|
paste.patterns.map(function(pattern, i){
|
2017-02-02 14:58:05 +01:00
|
|
|
|
2017-01-12 14:56:06 +01:00
|
|
|
if (pattern.regex.test(string)) {
|
2017-02-02 14:58:05 +01:00
|
|
|
|
|
|
|
/** current block is not empty */
|
|
|
|
if ( content.textContent.trim() && plugin == editor.settings.initialBlockPlugin ) {
|
|
|
|
|
|
|
|
pasteToNewBlock_();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-01-12 14:56:06 +01:00
|
|
|
pattern.callback.call(null, string, pattern);
|
2017-01-26 01:47:19 +01:00
|
|
|
result = true;
|
2017-01-12 14:56:06 +01:00
|
|
|
}
|
2017-01-26 01:47:19 +01:00
|
|
|
});
|
2016-12-25 15:41:57 +01:00
|
|
|
|
2017-01-26 01:47:19 +01:00
|
|
|
return result;
|
2016-12-25 15:41:57 +01:00
|
|
|
|
2017-01-26 01:47:19 +01:00
|
|
|
};
|
2017-01-11 16:48:57 +01:00
|
|
|
|
2017-02-02 14:58:05 +01:00
|
|
|
var pasteToNewBlock_ = function() {
|
|
|
|
|
|
|
|
/** Create new initial block */
|
|
|
|
editor.content.insertBlock({
|
|
|
|
|
|
|
|
type : editor.settings.initialBlockPlugin,
|
|
|
|
block : editor.tools[editor.settings.initialBlockPlugin].render({
|
|
|
|
text : ''
|
|
|
|
})
|
|
|
|
|
2017-02-02 15:13:05 +01:00
|
|
|
}, false);
|
2017-02-02 14:58:05 +01:00
|
|
|
|
|
|
|
};
|
2017-01-26 00:55:40 +01:00
|
|
|
|
|
|
|
return paste;
|
|
|
|
|
|
|
|
}(paste || {});
|
2017-01-12 14:56:06 +01:00
|
|
|
|
|
|
|
|