2016-12-25 15:41:57 +01:00
|
|
|
/**
|
2017-02-12 16:25:20 +01:00
|
|
|
* Codex Editor Paste module
|
2016-12-25 15:41:57 +01:00
|
|
|
*
|
2017-02-12 16:25:20 +01:00
|
|
|
* @author Codex Team
|
|
|
|
* @version 1.0
|
2016-12-25 15:41:57 +01:00
|
|
|
*/
|
|
|
|
|
2017-02-12 16:25:20 +01:00
|
|
|
module.exports = function (paste) {
|
|
|
|
|
|
|
|
let editor = codex.editor;
|
|
|
|
|
|
|
|
var patterns = [];
|
|
|
|
|
|
|
|
paste.prepare = function () {
|
|
|
|
|
|
|
|
var tools = editor.tools;
|
|
|
|
|
|
|
|
for (var tool in tools) {
|
2017-02-02 14:58:05 +01:00
|
|
|
|
2017-02-12 16:25:20 +01:00
|
|
|
if (!tools[tool].renderOnPastePatterns || !Array.isArray(tools[tool].renderOnPastePatterns)) {
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
tools[tool].renderOnPastePatterns.map(function (pattern) {
|
|
|
|
|
|
|
|
patterns.push(pattern);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.resolve();
|
|
|
|
|
|
|
|
};
|
2017-02-02 14:58:05 +01:00
|
|
|
|
2016-12-25 15:41:57 +01:00
|
|
|
/**
|
|
|
|
* Saves data
|
|
|
|
* @param event
|
|
|
|
*/
|
2017-02-12 16:25:20 +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) {
|
2017-02-12 16:25:20 +01:00
|
|
|
|
2017-01-18 16:12:50 +01:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopImmediatePropagation();
|
2017-02-12 16:25:20 +01:00
|
|
|
|
2017-01-18 16:12:50 +01:00
|
|
|
}
|
2017-02-12 16:25:20 +01:00
|
|
|
|
|
|
|
return result;
|
|
|
|
|
2017-01-26 00:55:40 +01:00
|
|
|
};
|
2016-12-25 15:41:57 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Analizes pated string and calls necessary method
|
|
|
|
*/
|
|
|
|
|
2017-02-12 16:25:20 +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-02-12 16:25:20 +01:00
|
|
|
patterns.map( function (pattern) {
|
2017-02-02 14:58:05 +01:00
|
|
|
|
2017-02-23 11:32:41 +01:00
|
|
|
var execArray = pattern.regex.exec(string),
|
|
|
|
match = execArray && execArray[0];
|
|
|
|
|
|
|
|
if ( match && match === string.trim()) {
|
2017-02-02 14:58:05 +01:00
|
|
|
|
|
|
|
/** current block is not empty */
|
|
|
|
if ( content.textContent.trim() && plugin == editor.settings.initialBlockPlugin ) {
|
|
|
|
|
|
|
|
pasteToNewBlock_();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-02-12 16:25:20 +01:00
|
|
|
pattern.callback(string, pattern);
|
2017-01-26 01:47:19 +01:00
|
|
|
result = true;
|
2017-02-12 16:25:20 +01:00
|
|
|
|
2017-01-12 14:56:06 +01:00
|
|
|
}
|
2017-02-12 16:25:20 +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-12 16:25:20 +01:00
|
|
|
var pasteToNewBlock_ = function () {
|
2017-02-02 14:58:05 +01:00
|
|
|
|
|
|
|
/** 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
|
|
|
|
2017-02-12 16:25:20 +01:00
|
|
|
return paste;
|
2017-01-12 14:56:06 +01:00
|
|
|
|
2017-02-12 16:25:20 +01:00
|
|
|
}({});
|