From 183a5d36fff8262a92bb18f560290b9e9ce9b0a2 Mon Sep 17 00:00:00 2001 From: Jinxuan Zhu Date: Thu, 16 Aug 2018 18:04:05 -0400 Subject: [PATCH] Introduce Range.updatePoints (#2080) * With Mutation * Use updatePoints * Remove mapRanges changes * Remove mapRanges changes * Remove mapRanges changes --- packages/slate/src/models/range.js | 61 +++++++++++------------------- packages/slate/src/models/value.js | 33 +++++----------- 2 files changed, 32 insertions(+), 62 deletions(-) diff --git a/packages/slate/src/models/range.js b/packages/slate/src/models/range.js index 963a68855..5796a902e 100644 --- a/packages/slate/src/models/range.js +++ b/packages/slate/src/models/range.js @@ -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`. * diff --git a/packages/slate/src/models/value.js b/packages/slate/src/models/value.js index a09b20137..53cf639a1 100644 --- a/packages/slate/src/models/value.js +++ b/packages/slate/src/models/value.js @@ -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 })