From ee9786de027b228486c3440075bca3ab9ea7e2a1 Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Sun, 25 Mar 2018 22:18:57 +0200 Subject: [PATCH] Fix append new line after node.. --- .../client/components/rte/lib/dom.js | 38 ++++++------------- 1 file changed, 12 insertions(+), 26 deletions(-) diff --git a/plugins/talk-plugin-rich-text/client/components/rte/lib/dom.js b/plugins/talk-plugin-rich-text/client/components/rte/lib/dom.js index c1200c931..c2bfb4372 100644 --- a/plugins/talk-plugin-rich-text/client/components/rte/lib/dom.js +++ b/plugins/talk-plugin-rich-text/client/components/rte/lib/dom.js @@ -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); } }