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

Introduce Range.updatePoints (#2080)

* With Mutation

* Use updatePoints

* Remove mapRanges changes

* Remove mapRanges changes

* Remove mapRanges changes
This commit is contained in:
Jinxuan Zhu
2018-08-16 18:04:05 -04:00
committed by Ian Storm Taylor
parent e717452f55
commit 183a5d36ff
2 changed files with 32 additions and 62 deletions

View File

@@ -212,8 +212,9 @@ class Range extends Record(DEFAULTS) {
get isCollapsed() {
return (
this.anchor.key === this.focus.key &&
this.anchor.offset === this.focus.offset
this.anchor === this.focus ||
(this.anchor.key === this.focus.key &&
this.anchor.offset === this.focus.offset)
)
}
@@ -321,12 +322,7 @@ class Range extends Record(DEFAULTS) {
*/
moveForward(n) {
const range = this.setPoints([
this.anchor.moveForward(n),
this.focus.moveForward(n),
])
return range
return this.updatePoints(point => point.moveForward(n))
}
/**
@@ -337,12 +333,7 @@ class Range extends Record(DEFAULTS) {
*/
moveBackward(n) {
const range = this.setPoints([
this.anchor.moveBackward(n),
this.focus.moveBackward(n),
])
return range
return this.updatePoints(point => point.moveBackward(n))
}
/**
@@ -609,12 +600,7 @@ class Range extends Record(DEFAULTS) {
*/
moveTo(path, offset) {
const range = this.setPoints([
this.anchor.moveTo(path, offset),
this.focus.moveTo(path, offset),
])
return range
return this.updatePoints(point => point.moveTo(path, offset))
}
/**
@@ -647,12 +633,7 @@ class Range extends Record(DEFAULTS) {
*/
moveToEndOfNode(node) {
const range = this.setPoints([
this.anchor.moveToEndOfNode(node),
this.focus.moveToEndOfNode(node),
])
return range
return this.updatePoints(point => point.moveToEndOfNode(node))
}
/**
@@ -702,12 +683,7 @@ class Range extends Record(DEFAULTS) {
*/
moveToStartOfNode(node) {
const range = this.setPoints([
this.anchor.moveToStartOfNode(node),
this.focus.moveToStartOfNode(node),
])
return range
return this.updatePoints(point => point.moveToStartOfNode(node))
}
/**
@@ -719,12 +695,7 @@ class Range extends Record(DEFAULTS) {
*/
normalize(node) {
const range = this.setPoints([
this.anchor.normalize(node),
this.focus.normalize(node),
])
return range
return this.updatePoints(point => point.normalize(node))
}
/**
@@ -812,6 +783,20 @@ class Range extends Record(DEFAULTS) {
return range
}
/**
* Set the anchor and focus points with `updator` callback
*
* @param {Function} updator
* @return {Range}
*/
updatePoints(updator) {
let { anchor, focus } = this
anchor = updator(anchor)
focus = updator(focus)
return this.merge({ anchor, focus })
}
/**
* Set the start point to a new `point`.
*

View File

@@ -588,12 +588,9 @@ class Value extends Record(DEFAULTS) {
document = document.insertNode(path, node)
value = value.set('document', document)
value = value.mapRanges(range => {
return range.setPoints([
range.anchor.setPath(null),
range.focus.setPath(null),
])
})
value = value.mapRanges(range =>
range.updatePoints(point => point.setPath(null))
)
return value
}
@@ -673,10 +670,7 @@ class Value extends Record(DEFAULTS) {
}
}
range = range.setPoints([
range.anchor.setPath(null),
range.focus.setPath(null),
])
range = range.updatePoints(point => point.setPath(null))
return range
})
@@ -702,12 +696,9 @@ class Value extends Record(DEFAULTS) {
document = document.moveNode(path, newPath, newIndex)
value = value.set('document', document)
value = value.mapRanges(range => {
return range.setPoints([
range.anchor.setPath(null),
range.focus.setPath(null),
])
})
value = value.mapRanges(range =>
range.updatePoints(point => point.setPath(null))
)
return value
}
@@ -764,10 +755,7 @@ class Value extends Record(DEFAULTS) {
: next ? range.moveEndTo(next.key, 0) : Range.create()
}
range = range.setPoints([
range.anchor.setPath(null),
range.focus.setPath(null),
])
range = range.updatePoints(point => point.setPath(null))
return range
})
@@ -905,10 +893,7 @@ class Value extends Record(DEFAULTS) {
range = range.moveEndTo(next.key, end.offset - position)
}
range = range.setPoints([
range.anchor.setPath(null),
range.focus.setPath(null),
])
range = range.updatePoints(point => point.setPath(null))
return range
})