Deal with it

This commit is contained in:
Peter Savchenko 2018-05-25 19:56:10 +03:00
commit 58d640a5d2
No known key found for this signature in database
GPG key ID: 63E739583C761566
5 changed files with 77 additions and 60 deletions

View file

@ -558,7 +558,6 @@ var Dom = function () {
var child = atLast ? 'lastChild' : 'firstChild',
sibling = atLast ? 'previousSibling' : 'nextSibling';
console.log('node is ', node);
if (node && node.nodeType === Node.ELEMENT_NODE && node[child]) {
var nodeChild = node[child];
@ -592,13 +591,6 @@ var Dom = function () {
return this.getDeepestNode(nodeChild, atLast);
}
if (node.nodeType === Node.TEXT_NODE && node.textContent.trim() === '') {
var zeroSpaceSymbol = document.createTextNode('\u200B');
node.parentNode.appendChild(zeroSpaceSymbol);
}
return node;
}
@ -1515,23 +1507,35 @@ var BlockManager = function (_Module) {
key: 'navigateNext',
value: function navigateNext() {
var lastNode = $.getDeepestNode(this.currentBlock.pluginsContent, true),
textNodeLength = void 0;
var lastNode = $.getDeepestNode(this.currentBlock.pluginsContent, true);
if (lastNode.nodeType === Node.ELEMENT_NODE) {
/**
* Founded contentEditable element doesn't have childs
* Or maybe New created block
*/
var currentBlockIsEmpty = this.currentBlock.isEmpty;
textNodeLength = lastNode.textContent.length;
} else {
/**
* Case of
* <div contenteditable>
* adaddad|
* <p><b></b></p> <---- deepest (lastNode) node is <b>, but caret is in 'adaddad'
* </div>
*/
if (!currentBlockIsEmpty && $.isEmpty(lastNode)) {
textNodeLength = lastNode.length;
lastNode = $.getDeepestNode(this.currentBlock.pluginsContent, false);
}
if (_Selection2.default.getAnchorNode() !== lastNode) {
var caretInTheLastNode = _Selection2.default.getAnchorNode() === lastNode,
caretAtTheEndOfLastNode = _Selection2.default.getAnchorOffset() === lastNode.textContent.length;
if (!currentBlockIsEmpty && !caretInTheLastNode) {
return;
}
if (_Selection2.default.getAnchorOffset() === textNodeLength) {
if (caretAtTheEndOfLastNode) {
var nextBlock = this.nextBlock;
@ -1543,7 +1547,7 @@ var BlockManager = function (_Module) {
/**
* Set's caret to the previous Block
* Before moving caret, we should check if caret position is at the end of Plugins node
* Before moving caret, we should check if caret position is start of the Plugins node
* Using {@link Dom#getDeepestNode} to get a last node and match with current selection
*/
@ -1554,7 +1558,15 @@ var BlockManager = function (_Module) {
var firstTextNode = $.getDeepestNode(this.currentBlock.pluginsContent, false),
textNodeLength = firstTextNode.length;
if (_Selection2.default.getAnchorNode() !== firstTextNode) {
var caretInTheFirstNode = _Selection2.default.getAnchorNode() === firstTextNode;
/**
* Founded contentEditable element doesn't have childs
* Or maybe New created block
*/
var currentBlockIsEmpty = this.currentBlock.isEmpty;
if (!currentBlockIsEmpty && !caretInTheFirstNode) {
return;
}
@ -1563,7 +1575,10 @@ var BlockManager = function (_Module) {
var previousBlock = this.previousBlock;
if (!previousBlock) return;
if (!previousBlock) {
return;
}
this.Editor.Caret.setToBlock(previousBlock, textNodeLength, true);
}
@ -2497,19 +2512,12 @@ var Caret = function (_Module) {
offset = nodeToSet.length;
}
/** if found deepest node is native input */
if ($.isNativeInput(nodeToSet)) {
nodeToSet.focus();
return;
}
/**
* @todo try to fix via Promises or use querySelectorAll to not to use timeout
*/
_.delay(function () {
return _this2.set(nodeToSet, offset);
_this2.set(nodeToSet, offset);
}, 20)();
this.Editor.BlockManager.currentNode = block.wrapper;

File diff suppressed because one or more lines are too long

View file

@ -124,7 +124,6 @@ export default class Dom {
let child = atLast ? 'lastChild' : 'firstChild',
sibling = atLast ? 'previousSibling' : 'nextSibling';
console.log('node is ', node);
if (node && node.nodeType === Node.ELEMENT_NODE && node[child]) {
let nodeChild = node[child];
@ -163,14 +162,6 @@ export default class Dom {
}
if (node.nodeType === Node.TEXT_NODE && node.textContent.trim() === '') {
let zeroSpaceSymbol = document.createTextNode('\u200b');
node.parentNode.appendChild(zeroSpaceSymbol);
}
return node;
}

View file

@ -121,26 +121,37 @@ export default class BlockManager extends Module {
*/
navigateNext() {
let lastNode = $.getDeepestNode(this.currentBlock.pluginsContent, true),
textNodeLength;
let lastNode = $.getDeepestNode(this.currentBlock.pluginsContent, true);
if (lastNode.nodeType === Node.ELEMENT_NODE) {
/**
* Founded contentEditable element doesn't have childs
* Or maybe New created block
*/
let currentBlockIsEmpty = this.currentBlock.isEmpty;
textNodeLength = lastNode.textContent.length;
/**
* Case of
* <div contenteditable>
* adaddad|
* <p><b></b></p> <---- deepest (lastNode) node is <b>, but caret is in 'adaddad'
* </div>
*/
if (!currentBlockIsEmpty && $.isEmpty(lastNode)) {
} else {
textNodeLength = lastNode.length;
lastNode = $.getDeepestNode(this.currentBlock.pluginsContent, false);
}
if (Selection.getAnchorNode() !== lastNode) {
let caretInTheLastNode = Selection.getAnchorNode() === lastNode,
caretAtTheEndOfLastNode = Selection.getAnchorOffset() === lastNode.textContent.length;
if (!currentBlockIsEmpty && !caretInTheLastNode) {
return;
}
if (Selection.getAnchorOffset() === textNodeLength) {
if (caretAtTheEndOfLastNode) {
let nextBlock = this.nextBlock;
@ -154,7 +165,7 @@ export default class BlockManager extends Module {
/**
* Set's caret to the previous Block
* Before moving caret, we should check if caret position is at the end of Plugins node
* Before moving caret, we should check if caret position is start of the Plugins node
* Using {@link Dom#getDeepestNode} to get a last node and match with current selection
*/
navigatePrevious() {
@ -162,7 +173,15 @@ export default class BlockManager extends Module {
let firstTextNode = $.getDeepestNode(this.currentBlock.pluginsContent, false),
textNodeLength = firstTextNode.length;
if (Selection.getAnchorNode() !== firstTextNode) {
let caretInTheFirstNode = Selection.getAnchorNode() === firstTextNode;
/**
* Founded contentEditable element doesn't have childs
* Or maybe New created block
*/
let currentBlockIsEmpty = this.currentBlock.isEmpty;
if (!currentBlockIsEmpty && !caretInTheFirstNode) {
return;
@ -172,7 +191,11 @@ export default class BlockManager extends Module {
let previousBlock = this.previousBlock;
if (!previousBlock) return;
if (!previousBlock) {
return;
}
this.Editor.Caret.setToBlock( previousBlock, textNodeLength, true );

View file

@ -54,19 +54,14 @@ export default class Caret extends Module {
offset = nodeToSet.length;
}
/** if found deepest node is native input */
if ($.isNativeInput(nodeToSet)) {
nodeToSet.focus();
return;
}
/**
* @todo try to fix via Promises or use querySelectorAll to not to use timeout
*/
_.delay( () => this.set(nodeToSet, offset), 20)();
_.delay( () => {
this.set(nodeToSet, offset);
}, 20)();
this.Editor.BlockManager.currentNode = block.wrapper;