mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-26 08:34:28 +02:00
Introduce Range.updatePoints (#2080)
* With Mutation * Use updatePoints * Remove mapRanges changes * Remove mapRanges changes * Remove mapRanges changes
This commit is contained in:
committed by
Ian Storm Taylor
parent
e717452f55
commit
183a5d36ff
@@ -212,8 +212,9 @@ class Range extends Record(DEFAULTS) {
|
|||||||
|
|
||||||
get isCollapsed() {
|
get isCollapsed() {
|
||||||
return (
|
return (
|
||||||
this.anchor.key === this.focus.key &&
|
this.anchor === this.focus ||
|
||||||
this.anchor.offset === this.focus.offset
|
(this.anchor.key === this.focus.key &&
|
||||||
|
this.anchor.offset === this.focus.offset)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -321,12 +322,7 @@ class Range extends Record(DEFAULTS) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
moveForward(n) {
|
moveForward(n) {
|
||||||
const range = this.setPoints([
|
return this.updatePoints(point => point.moveForward(n))
|
||||||
this.anchor.moveForward(n),
|
|
||||||
this.focus.moveForward(n),
|
|
||||||
])
|
|
||||||
|
|
||||||
return range
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -337,12 +333,7 @@ class Range extends Record(DEFAULTS) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
moveBackward(n) {
|
moveBackward(n) {
|
||||||
const range = this.setPoints([
|
return this.updatePoints(point => point.moveBackward(n))
|
||||||
this.anchor.moveBackward(n),
|
|
||||||
this.focus.moveBackward(n),
|
|
||||||
])
|
|
||||||
|
|
||||||
return range
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -609,12 +600,7 @@ class Range extends Record(DEFAULTS) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
moveTo(path, offset) {
|
moveTo(path, offset) {
|
||||||
const range = this.setPoints([
|
return this.updatePoints(point => point.moveTo(path, offset))
|
||||||
this.anchor.moveTo(path, offset),
|
|
||||||
this.focus.moveTo(path, offset),
|
|
||||||
])
|
|
||||||
|
|
||||||
return range
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -647,12 +633,7 @@ class Range extends Record(DEFAULTS) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
moveToEndOfNode(node) {
|
moveToEndOfNode(node) {
|
||||||
const range = this.setPoints([
|
return this.updatePoints(point => point.moveToEndOfNode(node))
|
||||||
this.anchor.moveToEndOfNode(node),
|
|
||||||
this.focus.moveToEndOfNode(node),
|
|
||||||
])
|
|
||||||
|
|
||||||
return range
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -702,12 +683,7 @@ class Range extends Record(DEFAULTS) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
moveToStartOfNode(node) {
|
moveToStartOfNode(node) {
|
||||||
const range = this.setPoints([
|
return this.updatePoints(point => point.moveToStartOfNode(node))
|
||||||
this.anchor.moveToStartOfNode(node),
|
|
||||||
this.focus.moveToStartOfNode(node),
|
|
||||||
])
|
|
||||||
|
|
||||||
return range
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -719,12 +695,7 @@ class Range extends Record(DEFAULTS) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
normalize(node) {
|
normalize(node) {
|
||||||
const range = this.setPoints([
|
return this.updatePoints(point => point.normalize(node))
|
||||||
this.anchor.normalize(node),
|
|
||||||
this.focus.normalize(node),
|
|
||||||
])
|
|
||||||
|
|
||||||
return range
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -812,6 +783,20 @@ class Range extends Record(DEFAULTS) {
|
|||||||
return range
|
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`.
|
* Set the start point to a new `point`.
|
||||||
*
|
*
|
||||||
|
@@ -588,12 +588,9 @@ class Value extends Record(DEFAULTS) {
|
|||||||
document = document.insertNode(path, node)
|
document = document.insertNode(path, node)
|
||||||
value = value.set('document', document)
|
value = value.set('document', document)
|
||||||
|
|
||||||
value = value.mapRanges(range => {
|
value = value.mapRanges(range =>
|
||||||
return range.setPoints([
|
range.updatePoints(point => point.setPath(null))
|
||||||
range.anchor.setPath(null),
|
)
|
||||||
range.focus.setPath(null),
|
|
||||||
])
|
|
||||||
})
|
|
||||||
|
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
@@ -673,10 +670,7 @@ class Value extends Record(DEFAULTS) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
range = range.setPoints([
|
range = range.updatePoints(point => point.setPath(null))
|
||||||
range.anchor.setPath(null),
|
|
||||||
range.focus.setPath(null),
|
|
||||||
])
|
|
||||||
|
|
||||||
return range
|
return range
|
||||||
})
|
})
|
||||||
@@ -702,12 +696,9 @@ class Value extends Record(DEFAULTS) {
|
|||||||
document = document.moveNode(path, newPath, newIndex)
|
document = document.moveNode(path, newPath, newIndex)
|
||||||
value = value.set('document', document)
|
value = value.set('document', document)
|
||||||
|
|
||||||
value = value.mapRanges(range => {
|
value = value.mapRanges(range =>
|
||||||
return range.setPoints([
|
range.updatePoints(point => point.setPath(null))
|
||||||
range.anchor.setPath(null),
|
)
|
||||||
range.focus.setPath(null),
|
|
||||||
])
|
|
||||||
})
|
|
||||||
|
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
@@ -764,10 +755,7 @@ class Value extends Record(DEFAULTS) {
|
|||||||
: next ? range.moveEndTo(next.key, 0) : Range.create()
|
: next ? range.moveEndTo(next.key, 0) : Range.create()
|
||||||
}
|
}
|
||||||
|
|
||||||
range = range.setPoints([
|
range = range.updatePoints(point => point.setPath(null))
|
||||||
range.anchor.setPath(null),
|
|
||||||
range.focus.setPath(null),
|
|
||||||
])
|
|
||||||
|
|
||||||
return range
|
return range
|
||||||
})
|
})
|
||||||
@@ -905,10 +893,7 @@ class Value extends Record(DEFAULTS) {
|
|||||||
range = range.moveEndTo(next.key, end.offset - position)
|
range = range.moveEndTo(next.key, end.offset - position)
|
||||||
}
|
}
|
||||||
|
|
||||||
range = range.setPoints([
|
range = range.updatePoints(point => point.setPath(null))
|
||||||
range.anchor.setPath(null),
|
|
||||||
range.focus.setPath(null),
|
|
||||||
])
|
|
||||||
|
|
||||||
return range
|
return range
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user