diff --git a/src/components/content.js b/src/components/content.js index 89997666f..6631da18c 100644 --- a/src/components/content.js +++ b/src/components/content.js @@ -441,23 +441,26 @@ class Content extends React.Component { const schema = editor.getSchema() const decorators = document.getDescendantDecorators(key, schema) const node = document.getDescendant(key) + const block = document.getClosestBlock(node.key) const ranges = node.getRanges(decorators) - const range = ranges.get(index) + const lastText = block.getLastText() // Get the text information. - const isLast = index == ranges.size - 1 - const { text, marks } = range let { textContent } = anchorNode const lastChar = textContent.charAt(textContent.length - 1) + const isLastText = node == lastText + const isLastRange = index == ranges.size - 1 // If we're dealing with the last leaf, and the DOM text ends in a new line, // we will have added another new line in 's render method to account // for browsers collapsing a single trailing new lines, so remove it. - if (isLast && lastChar == '\n') { + if (isLastText && isLastRange && lastChar == '\n') { textContent = textContent.slice(0, -1) } // If the text is no different, abort. + const range = ranges.get(index) + const { text, marks } = range if (textContent == text) return // Determine what the selection should be after changing the text. diff --git a/src/components/leaf.js b/src/components/leaf.js index d1c24368b..0baa3ab12 100644 --- a/src/components/leaf.js +++ b/src/components/leaf.js @@ -257,7 +257,9 @@ class Leaf extends React.Component { * @return {Element} */ - renderText({ parent, text, index, ranges }) { + renderText(props) { + const { node, state, parent, text, index, ranges } = props + // COMPAT: If the text is empty and it's the only child, we need to render a //
to get the block to have the proper height. if (text == '' && parent.kind == 'block' && parent.text == '') return
@@ -269,9 +271,12 @@ class Leaf extends React.Component { // COMPAT: Browsers will collapse trailing new lines at the end of blocks, // so we need to add an extra trailing new lines to prevent that. + const block = state.document.getClosestBlock(node.key) + const lastText = block.getLastText() const lastChar = text.charAt(text.length - 1) - const isLast = index == ranges.size - 1 - if (isLast && lastChar == '\n') return `${text}\n` + const isLastText = node == lastText + const isLastRange = index == ranges.size - 1 + if (isLastText && isLastRange && lastChar == '\n') return `${text}\n` // Otherwise, just return the text. return text