Fix append new line after node..

This commit is contained in:
Chi Vinh Le
2018-03-25 22:18:57 +02:00
parent 0226991521
commit ee9786de02
@@ -286,22 +286,6 @@ export function isSelectionInside(...nodes) {
return false;
}
/**
* Append a newline to the node.
*/
export function appendNewLine(node, changeSelection) {
const el = document.createElement('br');
node.appendChild(el);
if (changeSelection) {
const offset = indexOfChildNode(node, el);
const range = document.createRange();
range.setStart(node, offset);
range.setEnd(node, offset);
replaceSelection(range);
}
}
/**
* Insert new line. This is what happens
* when adding new lines through pressing Enter.
@@ -344,17 +328,19 @@ export function insertNewLine(changeSelection) {
* Inserts a new line after given node.
*/
export function insertNewLineAfterNode(node, changeSelection) {
if (node.parentNode.lastChild === node) {
appendNewLine(node.parentNode, changeSelection);
const el = document.createElement('br');
if (node.nextSibling) {
node.parentNode.insertBefore(el, node.nextSibling);
} else {
if (changeSelection) {
const offset = indexOfChildNode(node.parentNode, node) + 1;
const range = document.createRange();
range.setStart(node.parentNode, offset);
range.setEnd(node.parentNode, offset);
replaceSelection(range);
}
insertNewLine();
node.parentNode.appendChild(el);
}
if (changeSelection) {
const offset = indexOfChildNode(node.parentNode, el);
const range = document.createRange();
range.setStart(node.parentNode, offset);
range.setEnd(node.parentNode, offset);
replaceSelection(range);
}
}