This commit is contained in:
George Berezhnoy 2017-04-24 19:21:47 +03:00
parent 0968152570
commit eb362a6e23
6 changed files with 42 additions and 31 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -71,23 +71,8 @@ module.exports = (function (callbacks) {
*/
event.preventDefault();
var nativeInputs = editor.content.currentNode.querySelectorAll('textarea, input'),
nativeInputsAreEmpty = true,
textContentIsEmpty = !editor.content.currentNode.textContent.trim();
Array.prototype.map.call(nativeInputs, function (input) {
if (input.type == 'textarea' || input.type == 'text') {
nativeInputsAreEmpty = nativeInputsAreEmpty && !input.value.trim();
}
});
var blockIsEmpty = textContentIsEmpty && nativeInputsAreEmpty;
if (!blockIsEmpty) {
if (!editor.core.isBlockEmpty(editor.content.currentNode)) {
return;

View file

@ -271,14 +271,14 @@ module.exports = (function (caret) {
/**
* Inserts node at the caret location
* @param node
* @param {HTMLElement|DocumentFragment} node
*/
caret.insertNode = function (node) {
var selection, range,
lastNode = node;
if (node instanceof window.DocumentFragment) {
if (node.nodeType == editor.core.nodeTypes.DOCUMENT_FRAGMENT) {
lastNode = node.lastChild;

View file

@ -112,7 +112,8 @@ module.exports = (function (core) {
core.nodeTypes = {
TAG : 1,
TEXT : 3,
COMMENT : 8
COMMENT : 8,
DOCUMENT_FRAGMENT: 11
};
/**
@ -351,6 +352,35 @@ module.exports = (function (core) {
};
/**
* Check if block is empty
* We should check block textContent, child native inputs and some exceptions like IMG and IFRAME
*
* @param block
* @returns {boolean}
*/
core.isBlockEmpty = function (block) {
const EXCEPTION_TAGS = ['IMG', 'IFRAME'];
var nativeInputs = block.querySelectorAll('textarea, input'),
nativeInputsAreEmpty = true,
textContentIsEmpty = !block.textContent.trim();
Array.prototype.forEach.call(nativeInputs, function (input) {
if (input.type == 'textarea' || input.type == 'text') {
nativeInputsAreEmpty = nativeInputsAreEmpty && !input.value.trim();
}
});
return textContentIsEmpty && nativeInputsAreEmpty && !(EXCEPTION_TAGS.indexOf(block.tagName) + 1);
};
return core;

View file

@ -197,13 +197,12 @@ module.exports = function (paste) {
*/
var insertPastedParagraphs = function (paragraphs) {
var NEW_BLOCK_TYPE = editor.settings.initialBlockPlugin,
currentBlockContent = editor.content.currentNode.firstChild.firstChild;
var NEW_BLOCK_TYPE = editor.settings.initialBlockPlugin;
paragraphs.forEach(function (paragraph, index) {
/** Don't allow empty paragraphs */
if (paragraph.innerHTML.trim() === '') {
if (editor.core.isBlockEmpty(paragraph)) {
return;
@ -212,13 +211,10 @@ module.exports = function (paste) {
/**
* If there was no data in working node, replace it with first paragraph of pasted text
*/
if (index == 0 && currentBlockContent.innerHTML.trim() === '') {
if (index == 0 && editor.core.isBlockEmpty(editor.content.currentNode)) {
editor.content.switchBlock(editor.content.currentNode, editor.tools[NEW_BLOCK_TYPE].render({
text : paragraph.innerHTML
}), NEW_BLOCK_TYPE);
return;
editor.content.currentNode.remove();
editor.caret.setToPreviousBlock(editor.caret.inputIndex);
}