Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing the problem with returning a paragraph after deleting the list #1344

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
28 changes: 24 additions & 4 deletions src/js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -667,10 +667,6 @@
},

cleanListDOM: function (ownerDocument, element) {
if (element.nodeName.toLowerCase() !== 'li') {
return;
}

var list = element.parentElement;

if (list.parentElement.nodeName.toLowerCase() === 'p') { // yes we need to clean up
Expand All @@ -682,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