diff --git a/lib/models/node.js b/lib/models/node.js index b6b6cc7ca..2d27144eb 100644 --- a/lib/models/node.js +++ b/lib/models/node.js @@ -459,8 +459,6 @@ const Node = { getInlinesAtRange(range) { range = range.normalize(this) - if (range.isUnset) return Inline.createList() - return this .getTextsAtRange(range) .map(text => this.getClosestInline(text)) @@ -479,9 +477,6 @@ const Node = { const { startKey, startOffset } = range const marks = Mark.createSet() - // If the range isn't set, return an empty set. - if (range.isUnset) return marks - // If the range is collapsed at the start of the node, check the previous. if (range.isCollapsed && startOffset == 0) { const text = this.getDescendant(startKey) @@ -723,10 +718,6 @@ const Node = { getTextsAtRange(range) { range = range.normalize(this) - - // If the selection is unset, return an empty list. - if (range.isUnset) return Block.createList() - const { startKey, endKey } = range const texts = this.getTextNodes() const startText = this.getDescendant(startKey) diff --git a/lib/models/selection.js b/lib/models/selection.js index 12a6c9fb3..3eeff3dc9 100644 --- a/lib/models/selection.js +++ b/lib/models/selection.js @@ -85,16 +85,6 @@ class Selection extends new Record(DEFAULTS) { return !this.isCollapsed } - /** - * Get whether the range's anchor of focus keys are not set yet. - * - * @return {Boolean} isUnset - */ - - get isUnset() { - return this.anchorKey == null || this.focusKey == null - } - /** * Get whether the selection is forward. * @@ -282,12 +272,25 @@ class Selection extends new Record(DEFAULTS) { const { isCollapsed } = selection let { anchorKey, anchorOffset, focusKey, focusOffset, isBackward } = selection - // If the selection isn't formed yet, abort. - if (this.isUnset) return this + // If the selection isn't formed yet or is malformed, set it to the + // beginning of the node. + if ( + anchorKey == null || + focusKey == null || + !node.hasDescendant(anchorKey) || + !node.hasDescendant(focusKey) + ) { + const first = node.getTextNodes().first() + return selection.merge({ + anchorKey: first.key, + anchorOffset: 0, + focusKey: first.key, + focusOffset: 0, + isBackward: false + }) + } - // Asset that the anchor and focus nodes exist in the node tree. - node.assertHasDescendant(anchorKey) - node.assertHasDescendant(focusKey) + // Get the anchor and focus nodes. let anchorNode = node.getDescendant(anchorKey) let focusNode = node.getDescendant(focusKey) diff --git a/lib/models/state.js b/lib/models/state.js index 1376e1d40..f4a073b59 100644 --- a/lib/models/state.js +++ b/lib/models/state.js @@ -44,7 +44,7 @@ class State extends new Record(DEFAULTS) { static create(properties = {}) { if (properties instanceof State) return properties properties.document = Document.create(properties.document) - properties.selection = Selection.create(properties.selection) + properties.selection = Selection.create(properties.selection).normalize(properties.document) return new State(properties) } diff --git a/lib/models/transforms.js b/lib/models/transforms.js index b3778f4a8..ec1e988fe 100644 --- a/lib/models/transforms.js +++ b/lib/models/transforms.js @@ -779,8 +779,8 @@ const Transforms = { data = Data.create(data) let node = this - // If collapsed or unset, there's nothing to wrap. - if (range.isCollapsed || range.isUnset) return node + // If collapsed, there's nothing to wrap. + if (range.isCollapsed) return node // Split at the start of the range. const start = range.collapseToStart()