1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-18 13:11:17 +02:00

remove the concept of "unset" selections, normalizing instead, closes #36

This commit is contained in:
Ian Storm Taylor
2016-07-17 16:50:40 -07:00
parent 7930df4fc2
commit e6335266eb
4 changed files with 21 additions and 27 deletions

View File

@@ -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)

View File

@@ -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)

View File

@@ -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)
}

View File

@@ -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()