1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-09 16:56:36 +02:00

change offset key logic to never throw when not found

This commit is contained in:
Ian Storm Taylor
2016-12-12 12:04:22 -08:00
parent 4e177e4092
commit 98dade9ac7
2 changed files with 10 additions and 5 deletions

View File

@@ -134,6 +134,8 @@ class Content extends React.Component {
const { document } = state const { document } = state
const schema = editor.getSchema() const schema = editor.getSchema()
const offsetKey = OffsetKey.findKey(element, offset) const offsetKey = OffsetKey.findKey(element, offset)
if (!offsetKey) return null
const { key } = offsetKey const { key } = offsetKey
const node = document.getDescendant(key) const node = document.getDescendant(key)
const decorators = document.getDescendantDecorators(key, schema) const decorators = document.getDescendantDecorators(key, schema)
@@ -399,6 +401,8 @@ class Content extends React.Component {
const startNode = range.startContainer const startNode = range.startContainer
const startOffset = range.startOffset const startOffset = range.startOffset
const point = this.getPoint(startNode, startOffset) const point = this.getPoint(startNode, startOffset)
if (!point) return
const target = Selection.create({ const target = Selection.create({
anchorKey: point.key, anchorKey: point.key,
anchorOffset: point.offset, anchorOffset: point.offset,
@@ -441,9 +445,10 @@ class Content extends React.Component {
const native = window.getSelection() const native = window.getSelection()
const { anchorNode, anchorOffset } = native const { anchorNode, anchorOffset } = native
const point = this.getPoint(anchorNode, anchorOffset) const point = this.getPoint(anchorNode, anchorOffset)
const { key, index, start, end } = point if (!point) return
// Get the range in question. // Get the range in question.
const { key, index, start, end } = point
const { state, editor } = this.props const { state, editor } = this.props
const { document, selection } = state const { document, selection } = state
const schema = editor.getSchema() const schema = editor.getSchema()
@@ -622,6 +627,7 @@ class Content extends React.Component {
const { anchorNode, anchorOffset, focusNode, focusOffset } = native const { anchorNode, anchorOffset, focusNode, focusOffset } = native
const anchor = this.getPoint(anchorNode, anchorOffset) const anchor = this.getPoint(anchorNode, anchorOffset)
const focus = this.getPoint(focusNode, focusOffset) const focus = this.getPoint(focusNode, focusOffset)
if (!anchor || !focus) return
// There are valid situations where a select event will fire when we're // There are valid situations where a select event will fire when we're
// already at that position (for example when entering a character), since // already at that position (for example when entering a character), since

View File

@@ -75,6 +75,7 @@ function findKey(rawNode, rawOffset) {
// ancestor, so find it by going down from the nearest void parent. // ancestor, so find it by going down from the nearest void parent.
if (!closest) { if (!closest) {
const closestVoid = parentNode.closest(VOID_SELECTOR) const closestVoid = parentNode.closest(VOID_SELECTOR)
if (!closestVoid) return null
closest = closestVoid.querySelector(SELECTOR) closest = closestVoid.querySelector(SELECTOR)
offset = closest.textContent.length offset = closest.textContent.length
} }
@@ -82,10 +83,8 @@ function findKey(rawNode, rawOffset) {
// Get the string value of the offset key attribute. // Get the string value of the offset key attribute.
offsetKey = closest.getAttribute(ATTRIBUTE) offsetKey = closest.getAttribute(ATTRIBUTE)
// If we still didn't find an offset key, this is a bug. // If we still didn't find an offset key, abort.
if (!offsetKey) { if (!offsetKey) return null
throw new Error(`Unable to find offset key for ${node} with offset "${offset}".`)
}
// Return the parsed the offset key. // Return the parsed the offset key.
const parsed = parse(offsetKey) const parsed = parse(offsetKey)