mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-18 21:21:21 +02:00
remove the concept of "unset" selections, normalizing instead, closes #36
This commit is contained in:
@@ -459,8 +459,6 @@ const Node = {
|
|||||||
|
|
||||||
getInlinesAtRange(range) {
|
getInlinesAtRange(range) {
|
||||||
range = range.normalize(this)
|
range = range.normalize(this)
|
||||||
if (range.isUnset) return Inline.createList()
|
|
||||||
|
|
||||||
return this
|
return this
|
||||||
.getTextsAtRange(range)
|
.getTextsAtRange(range)
|
||||||
.map(text => this.getClosestInline(text))
|
.map(text => this.getClosestInline(text))
|
||||||
@@ -479,9 +477,6 @@ const Node = {
|
|||||||
const { startKey, startOffset } = range
|
const { startKey, startOffset } = range
|
||||||
const marks = Mark.createSet()
|
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 the range is collapsed at the start of the node, check the previous.
|
||||||
if (range.isCollapsed && startOffset == 0) {
|
if (range.isCollapsed && startOffset == 0) {
|
||||||
const text = this.getDescendant(startKey)
|
const text = this.getDescendant(startKey)
|
||||||
@@ -723,10 +718,6 @@ const Node = {
|
|||||||
|
|
||||||
getTextsAtRange(range) {
|
getTextsAtRange(range) {
|
||||||
range = range.normalize(this)
|
range = range.normalize(this)
|
||||||
|
|
||||||
// If the selection is unset, return an empty list.
|
|
||||||
if (range.isUnset) return Block.createList()
|
|
||||||
|
|
||||||
const { startKey, endKey } = range
|
const { startKey, endKey } = range
|
||||||
const texts = this.getTextNodes()
|
const texts = this.getTextNodes()
|
||||||
const startText = this.getDescendant(startKey)
|
const startText = this.getDescendant(startKey)
|
||||||
|
@@ -85,16 +85,6 @@ class Selection extends new Record(DEFAULTS) {
|
|||||||
return !this.isCollapsed
|
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.
|
* Get whether the selection is forward.
|
||||||
*
|
*
|
||||||
@@ -282,12 +272,25 @@ class Selection extends new Record(DEFAULTS) {
|
|||||||
const { isCollapsed } = selection
|
const { isCollapsed } = selection
|
||||||
let { anchorKey, anchorOffset, focusKey, focusOffset, isBackward } = selection
|
let { anchorKey, anchorOffset, focusKey, focusOffset, isBackward } = selection
|
||||||
|
|
||||||
// If the selection isn't formed yet, abort.
|
// If the selection isn't formed yet or is malformed, set it to the
|
||||||
if (this.isUnset) return this
|
// 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.
|
// Get the anchor and focus nodes.
|
||||||
node.assertHasDescendant(anchorKey)
|
|
||||||
node.assertHasDescendant(focusKey)
|
|
||||||
let anchorNode = node.getDescendant(anchorKey)
|
let anchorNode = node.getDescendant(anchorKey)
|
||||||
let focusNode = node.getDescendant(focusKey)
|
let focusNode = node.getDescendant(focusKey)
|
||||||
|
|
||||||
|
@@ -44,7 +44,7 @@ class State extends new Record(DEFAULTS) {
|
|||||||
static create(properties = {}) {
|
static create(properties = {}) {
|
||||||
if (properties instanceof State) return properties
|
if (properties instanceof State) return properties
|
||||||
properties.document = Document.create(properties.document)
|
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)
|
return new State(properties)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -779,8 +779,8 @@ const Transforms = {
|
|||||||
data = Data.create(data)
|
data = Data.create(data)
|
||||||
let node = this
|
let node = this
|
||||||
|
|
||||||
// If collapsed or unset, there's nothing to wrap.
|
// If collapsed, there's nothing to wrap.
|
||||||
if (range.isCollapsed || range.isUnset) return node
|
if (range.isCollapsed) return node
|
||||||
|
|
||||||
// Split at the start of the range.
|
// Split at the start of the range.
|
||||||
const start = range.collapseToStart()
|
const start = range.collapseToStart()
|
||||||
|
Reference in New Issue
Block a user