editor.js/plugins/paste/paste.js

82 lines
1.6 KiB
JavaScript
Raw Normal View History

/**
* Paste plugin.
*
2017-01-12 15:42:10 +01:00
* Listen to clipboard paste event and analize pasted text whit patterns in pattern.js
*/
/**
* @protected
*
* Main tool settings.
*/
let editor = codex.editor;
2017-01-26 00:55:40 +01:00
var paste = function(paste){
/**
* Saves data
* @param event
*/
2017-01-26 00:55:40 +01:00
paste.pasted = function(event) {
var clipBoardData = event.clipboardData || window.clipboardData,
content = clipBoardData.getData('Text');
var result = analize(content);
if (result) {
event.preventDefault();
event.stopImmediatePropagation();
}
2017-01-26 00:55:40 +01:00
};
/**
* Analizes pated string and calls necessary method
*/
2017-01-26 00:55:40 +01:00
var analize = function(string) {
var result = false,
content = editor.content.currentNode,
plugin = content.dataset.tool;
2017-01-26 00:55:40 +01:00
paste.patterns.map(function(pattern, i){
if (pattern.regex.test(string)) {
/** current block is not empty */
if ( content.textContent.trim() && plugin == editor.settings.initialBlockPlugin ) {
pasteToNewBlock_();
}
pattern.callback.call(null, string, pattern);
result = true;
}
});
return result;
};
2017-01-11 16:48:57 +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-01-26 00:55:40 +01:00
return paste;
}(paste || {});