1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-04-21 22:02:05 +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 schema = editor.getSchema()
const offsetKey = OffsetKey.findKey(element, offset)
if (!offsetKey) return null
const { key } = offsetKey
const node = document.getDescendant(key)
const decorators = document.getDescendantDecorators(key, schema)
@ -399,6 +401,8 @@ class Content extends React.Component {
const startNode = range.startContainer
const startOffset = range.startOffset
const point = this.getPoint(startNode, startOffset)
if (!point) return
const target = Selection.create({
anchorKey: point.key,
anchorOffset: point.offset,
@ -441,9 +445,10 @@ class Content extends React.Component {
const native = window.getSelection()
const { anchorNode, anchorOffset } = native
const point = this.getPoint(anchorNode, anchorOffset)
const { key, index, start, end } = point
if (!point) return
// Get the range in question.
const { key, index, start, end } = point
const { state, editor } = this.props
const { document, selection } = state
const schema = editor.getSchema()
@ -622,6 +627,7 @@ class Content extends React.Component {
const { anchorNode, anchorOffset, focusNode, focusOffset } = native
const anchor = this.getPoint(anchorNode, anchorOffset)
const focus = this.getPoint(focusNode, focusOffset)
if (!anchor || !focus) return
// There are valid situations where a select event will fire when we're
// 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.
if (!closest) {
const closestVoid = parentNode.closest(VOID_SELECTOR)
if (!closestVoid) return null
closest = closestVoid.querySelector(SELECTOR)
offset = closest.textContent.length
}
@ -82,10 +83,8 @@ function findKey(rawNode, rawOffset) {
// Get the string value of the offset key attribute.
offsetKey = closest.getAttribute(ATTRIBUTE)
// If we still didn't find an offset key, this is a bug.
if (!offsetKey) {
throw new Error(`Unable to find offset key for ${node} with offset "${offset}".`)
}
// If we still didn't find an offset key, abort.
if (!offsetKey) return null
// Return the parsed the offset key.
const parsed = parse(offsetKey)