1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-09-01 11:12:42 +02:00

fix range offset defaults to be null (#2026)

This commit is contained in:
Ian Storm Taylor
2018-08-03 14:19:00 -07:00
committed by GitHub
parent de3420b0f1
commit 5a539df0f9

View File

@@ -14,10 +14,10 @@ import Mark from './mark'
const DEFAULTS = { const DEFAULTS = {
anchorKey: null, anchorKey: null,
anchorOffset: 0, anchorOffset: null,
anchorPath: null, anchorPath: null,
focusKey: null, focusKey: null,
focusOffset: 0, focusOffset: null,
focusPath: null, focusPath: null,
isAtomic: false, isAtomic: false,
isBackward: null, isBackward: null,
@@ -134,10 +134,10 @@ class Range extends Record(DEFAULTS) {
static fromJSON(object) { static fromJSON(object) {
const { const {
anchorKey = null, anchorKey = null,
anchorOffset = 0, anchorOffset = null,
anchorPath = null, anchorPath = null,
focusKey = null, focusKey = null,
focusOffset = 0, focusOffset = null,
focusPath = null, focusPath = null,
isAtomic = false, isAtomic = false,
isBackward = null, isBackward = null,
@@ -398,7 +398,7 @@ class Range extends Record(DEFAULTS) {
*/ */
hasFocusAtStartOf(node) { hasFocusAtStartOf(node) {
if (this.focusOffset != 0) return false if (this.focusOffset !== 0) return false
const first = getFirstText(node) const first = getFirstText(node)
return this.focusKey == first.key return this.focusKey == first.key
} }
@@ -489,10 +489,10 @@ class Range extends Record(DEFAULTS) {
deselect() { deselect() {
return this.merge({ return this.merge({
anchorKey: null, anchorKey: null,
anchorOffset: 0, anchorOffset: null,
anchorPath: null, anchorPath: null,
focusKey: null, focusKey: null,
focusOffset: 0, focusOffset: null,
focusPath: null, focusPath: null,
isFocused: false, isFocused: false,
isBackward: false, isBackward: false,
@@ -786,29 +786,14 @@ class Range extends Record(DEFAULTS) {
isBackward, isBackward,
} = range } = range
const anchorOffsetType = typeof anchorOffset
const focusOffsetType = typeof focusOffset
if (anchorOffsetType != 'number' || focusOffsetType != 'number') {
logger.warn(
`The range offsets should be numbers, but they were of type "${anchorOffsetType}" and "${focusOffsetType}".`
)
}
// If either point in the range is unset, make sure it is fully unset. // If either point in the range is unset, make sure it is fully unset.
if ( if (
(anchorKey == null && anchorPath == null) || (anchorKey == null && anchorPath == null) ||
(focusKey == null && focusPath == null) (focusKey == null && focusPath == null) ||
anchorOffset == null ||
focusOffset == null
) { ) {
return range.merge({ return Range.create()
anchorKey: null,
anchorOffset: 0,
anchorPath: null,
focusKey: null,
focusOffset: 0,
focusPath: null,
isBackward: false,
})
} }
// Get the anchor and focus nodes. // Get the anchor and focus nodes.
@@ -826,10 +811,10 @@ class Range extends Record(DEFAULTS) {
const path = first && node.getPath(first.key) const path = first && node.getPath(first.key)
return range.merge({ return range.merge({
anchorKey: first ? first.key : null, anchorKey: first ? first.key : null,
anchorOffset: 0, anchorOffset: first ? 0 : null,
anchorPath: first ? path : null, anchorPath: first ? path : null,
focusKey: first ? first.key : null, focusKey: first ? first.key : null,
focusOffset: 0, focusOffset: first ? 0 : null,
focusPath: first ? path : null, focusPath: first ? path : null,
isBackward: false, isBackward: false,
}) })