Skip to content

Commit

Permalink
Restore selection after text wrap
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Isakov committed Jul 3, 2017
1 parent 82a4123 commit 277b50a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
10 changes: 9 additions & 1 deletion src/js/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,15 @@

// do some DOM clean-up for known browser issues after the action
if (action === 'insertunorderedlist' || action === 'insertorderedlist') {
MediumEditor.util.cleanListDOM(this.options.ownerDocument, this.getSelectedParentElement());
var element = this.getSelectedParentElement();

if (element.nodeName.toLowerCase() === 'li') {
MediumEditor.util.cleanListDOM(this.options.ownerDocument, element);
} else {
this.saveSelection();
MediumEditor.util.cleanAndWrapSelectedTextNodeToParagraph(this.options.ownerDocument);
setTimeout(this.restoreSelection.bind(this), 0);
}
}

this.checkSelection();
Expand Down
52 changes: 24 additions & 28 deletions src/js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -667,34 +667,6 @@
},

cleanListDOM: function (ownerDocument, element) {
if (element.nodeName.toLowerCase() !== 'li') {
var selection = ownerDocument.getSelection(),
node = selection.anchorNode;

if (node && node.nodeType === 3) {
var nextElement = node.nextElementSibling,
p = ownerDocument.createElement('p'),
context = this;

if (
nextElement &&
nextElement.tagName &&
nextElement.tagName.toLowerCase() === 'br' &&
nextElement.parentNode
) {
nextElement.parentNode.removeChild(nextElement);
}

setTimeout(function () {
context.moveTextRangeIntoElement(node, node, p);
MediumEditor.selection.moveCursor(ownerDocument, node, node.textContent.length);
}, 0);

}

return;
}

var list = element.parentElement;

if (list.parentElement.nodeName.toLowerCase() === 'p') { // yes we need to clean up
Expand All @@ -706,6 +678,30 @@
}
},

cleanAndWrapSelectedTextNodeToParagraph: function (ownerDocument) {
var selection = ownerDocument.getSelection(),
node = selection.anchorNode;

if (node && node.nodeType === 3) {
var nextElement = node.nextElementSibling,
p = ownerDocument.createElement('p'),
context = this;

if (
nextElement &&
nextElement.tagName &&
nextElement.tagName.toLowerCase() === 'br' &&
nextElement.parentNode
) {
nextElement.parentNode.removeChild(nextElement);
}

setTimeout(function () {
context.moveTextRangeIntoElement(node, node, p);
}, 0);
}
},

/* splitDOMTree
*
* Given a root element some descendant element, split the root element
Expand Down

0 comments on commit 277b50a

Please sign in to comment.