diff --git a/lib/models/node.js b/lib/models/node.js index b3c79160a..55ce9d8cb 100644 --- a/lib/models/node.js +++ b/lib/models/node.js @@ -415,7 +415,7 @@ const Node = { const text = this.getDescendant(startKey) const previous = this.getPreviousText(startKey) if (!previous) return marks - const char = text.characters.get(previous.length - 1) + const char = previous.characters.get(previous.length - 1) return char.marks } @@ -699,6 +699,14 @@ const Node = { normalize() { let node = this + const texts = node.getTextNodes() + + // If there are no text nodes, add one. + if (!texts.size) { + const text = Text.create() + const nodes = node.nodes.push(text) + return node.merge({ nodes }) + } // See if there are any adjacent text nodes. let firstAdjacent = node.findDescendant((child) => { diff --git a/lib/models/state.js b/lib/models/state.js index 4980211fe..d32194c82 100644 --- a/lib/models/state.js +++ b/lib/models/state.js @@ -400,9 +400,16 @@ class State extends Record(DEFAULTS) { insertText(text) { let state = this let { document, selection } = state + let after = selection + + // Determine what the selection should be after inserting. + if (selection.isExpanded) { + after = selection.moveToStart() + } // Insert the text and update the selection. document = document.insertTextAtRange(selection, text) + selection = after selection = selection.moveForward(text.length) state = state.merge({ document, selection }) return state