caret position after first-level block modification fixed

This commit is contained in:
khaydarov 2016-05-25 18:27:23 +03:00
parent b160523323
commit ab00d85b54

View file

@ -350,8 +350,13 @@ cEditor.content = {
currentNode : null, currentNode : null,
caretPosition : null, /**
FocusedchildNode: null, * @param caretOffset - caret offset
* @param focusedChildNode - focused child node
*/
caretOffset : null,
focusedChildNode: null,
/** /**
* Synchronizes redactor with original textarea * Synchronizes redactor with original textarea
@ -372,44 +377,49 @@ cEditor.content = {
}, },
getCaretPosition (selection) { saveCaretPosition () {
this.caretPosition = selection.anchorOffset; var selection = window.getSelection();
this.FocusedchildNode = selection.anchorNode;
this.caretOffset = selection.anchorOffset;
this.focusedChildNode = selection.anchorNode;
}, },
/** /**
* Creates Documnt Range and sets to 0 position * Creates Documnt Range and sets caret
* @param NodeElement - Changed Node. * @param NodeElement - Changed Node.
* @param selectedNode - ChildNode before block modification * @param selectedNode - ChildNode before block modification
* @param caretPosition - caret offset before modification * @param caretOffset - caret offset before modification
*/ */
setCaret : function(NodeElement, selectedNode = null, caretPosition = 0) { setCaret : function(NodeElement, selectedNode = null, caretOffset = 0) {
if (selectedNode == null) if (selectedNode == null) {
selectedNode = this.FocusedchildNode; selectedNode = this.focusedChildNode;
}
if (caretPosition == 0)
caretPosition = this.caretPosition;
if (caretOffset == 0) {
caretOffset = this.caretOffset;
}
var childs = NodeElement.childNodes, var childs = NodeElement.childNodes,
nodeChild, nodeChild,
index ; index ;
for( index = 0; index < childs.length; index++) for( index = 0; index < childs.length; index++) {
if (selectedNode.textContent === childs[index].textContent) {
if (selectedNode.textContent === childs[index].textContent) {
nodeChild = childs[index]; nodeChild = childs[index];
break; break;
} }
}
var range = document.createRange(), var range = document.createRange(),
selection = window.getSelection(); selection = window.getSelection();
range.setStart(nodeChild, caretPosition); range.setStart(nodeChild, caretOffset);
range.setEnd(nodeChild, caretPosition); range.setEnd(nodeChild, caretOffset);
selection.removeAllRanges(); selection.removeAllRanges();
selection.addRange(range); selection.addRange(range);
@ -478,7 +488,7 @@ cEditor.content = {
/** /**
* Get caret position before we change block * Get caret position before we change block
*/ */
cEditor.content.getCaretPosition(window.getSelection()); cEditor.content.saveCaretPosition();
/** /**
@ -515,8 +525,6 @@ cEditor.content = {
default : newNodeWrapperTagname = 'P'; break; default : newNodeWrapperTagname = 'P'; break;
} }
console.log('5 %o', cEditor.content.currentNode);
newNodeWrapper = cEditor.draw.block(newNodeWrapperTagname); newNodeWrapper = cEditor.draw.block(newNodeWrapperTagname);
newNodeWrapper.appendChild(nodeCreated); newNodeWrapper.appendChild(nodeCreated);