improve isAtStart

This commit is contained in:
Peter Savchenko 2018-05-25 21:09:22 +03:00
commit 12e1d37089
No known key found for this signature in database
GPG key ID: 63E739583C761566
5 changed files with 118 additions and 3 deletions

View file

@ -709,6 +709,14 @@ var Dom = function () {
treeWalker.push(node);
}
/**
* If one of childs is not empty, checked Node is not empty too
*/
if (node && !this.isNodeEmpty(node)) {
return false;
}
node = treeWalker.shift();
if (!node) continue;
@ -2599,6 +2607,33 @@ var Caret = function (_Module) {
}
}
/**
* Get all first-level (first child of [contenteditabel]) siblings from passed node
*/
}, {
key: 'getHigherLevelSiblings',
value: function getHigherLevelSiblings(from, direction) {
var current = from;
while (current.parentNode.contentEditable !== 'true') {
current = current.parentNode;
}
var siblings = [],
sibling = direction === 'left' ? 'previousSibling' : 'nextSibling';
while (current[sibling]) {
current = current[sibling];
siblings.push(current);
}
return siblings;
}
/**
* Get's deepest first node and checks if offset is zero
* @return {boolean}
@ -2612,6 +2647,26 @@ var Caret = function (_Module) {
anchorNode = selection.anchorNode,
firstNode = $.getDeepestNode(this.Editor.BlockManager.currentBlock.pluginsContent);
/**
* In case of
* <div contenteditable>
* <p><b></b></p>
* |adaddad
* </div>
*/
if ($.isEmpty(firstNode)) {
var leftSiblings = this.getHigherLevelSiblings(anchorNode, 'left'),
nothingAtLeft = leftSiblings.every(function (node) {
return node.textContent.length === 0;
});
if (nothingAtLeft && selection.anchorOffset === 0) {
return true;
}
}
return firstNode === null || anchorNode === firstNode && selection.anchorOffset === 0;
}
@ -2904,6 +2959,8 @@ var Keyboard = function (_Module) {
this.Editor.BlockManager.navigatePrevious();
}
var caretAtTheEnd = targetBlock.isEmpty ? false : true;
this.Editor.BlockManager.mergeBlocks(targetBlock, blockToMerge).then(function () {
// decrease current block index so that to know current actual
@ -2912,7 +2969,7 @@ var Keyboard = function (_Module) {
window.setTimeout(function () {
// set caret to the block without offset at the end
_this2.Editor.Caret.setToBlock(_this2.Editor.BlockManager.currentBlock, 0, true);
_this2.Editor.Caret.setToBlock(_this2.Editor.BlockManager.currentBlock, 0, caretAtTheEnd);
_this2.Editor.Toolbar.close();
}, 10);
});

File diff suppressed because one or more lines are too long

View file

@ -278,6 +278,15 @@ export default class Dom {
}
/**
* If one of childs is not empty, checked Node is not empty too
*/
if (node && !this.isNodeEmpty(node)) {
return false;
}
node = treeWalker.shift();
if (!node) continue;

View file

@ -139,6 +139,33 @@ export default class Caret extends Module {
}
/**
* Get all first-level (first child of [contenteditabel]) siblings from passed node
*/
getHigherLevelSiblings(from, direction ) {
let current = from;
while (current.parentNode.contentEditable !== 'true') {
current = current.parentNode;
}
let siblings = [],
sibling = direction === 'left' ? 'previousSibling' : 'nextSibling';
while (current[sibling]) {
current = current[sibling];
siblings.push(current);
}
return siblings;
}
/**
* Get's deepest first node and checks if offset is zero
* @return {boolean}
@ -149,6 +176,26 @@ export default class Caret extends Module {
anchorNode = selection.anchorNode,
firstNode = $.getDeepestNode(this.Editor.BlockManager.currentBlock.pluginsContent);
/**
* In case of
* <div contenteditable>
* <p><b></b></p>
* |adaddad
* </div>
*/
if ($.isEmpty(firstNode)) {
let leftSiblings = this.getHigherLevelSiblings(anchorNode, 'left'),
nothingAtLeft = leftSiblings.every( node => node.textContent.length === 0 );
if (nothingAtLeft && selection.anchorOffset === 0) {
return true;
}
}
return firstNode === null || anchorNode === firstNode && selection.anchorOffset === 0;
}

View file

@ -131,6 +131,8 @@ export default class Keyboard extends Module {
}
let caretAtTheEnd = targetBlock.isEmpty ? false : true;
this.Editor.BlockManager.mergeBlocks(targetBlock, blockToMerge)
.then( () => {
@ -140,7 +142,7 @@ export default class Keyboard extends Module {
window.setTimeout( () => {
// set caret to the block without offset at the end
this.Editor.Caret.setToBlock(this.Editor.BlockManager.currentBlock, 0, true);
this.Editor.Caret.setToBlock(this.Editor.BlockManager.currentBlock, 0, caretAtTheEnd);
this.Editor.Toolbar.close();
}, 10);