mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-11 17:53:59 +02:00
fix offset key finding logic to account for nested voids
This commit is contained in:
@@ -96,7 +96,7 @@ class Void extends React.Component {
|
|||||||
this.debug('render', { props })
|
this.debug('render', { props })
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Tag style={style} onClick={this.onClick}>
|
<Tag data-slate-void style={style} onClick={this.onClick}>
|
||||||
{this.renderSpacer()}
|
{this.renderSpacer()}
|
||||||
<Tag contentEditable={false}>
|
<Tag contentEditable={false}>
|
||||||
{children}
|
{children}
|
||||||
|
@@ -25,6 +25,14 @@ const ATTRIBUTE = 'data-offset-key'
|
|||||||
|
|
||||||
const SELECTOR = `[${ATTRIBUTE}]`
|
const SELECTOR = `[${ATTRIBUTE}]`
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Void node selection.
|
||||||
|
*
|
||||||
|
* @type {String}
|
||||||
|
*/
|
||||||
|
|
||||||
|
const VOID_SELECTOR = '[data-slate-void]'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find the start and end bounds from an `offsetKey` and `ranges`.
|
* Find the start and end bounds from an `offsetKey` and `ranges`.
|
||||||
*
|
*
|
||||||
@@ -57,29 +65,24 @@ function findBounds(index, ranges) {
|
|||||||
|
|
||||||
function findKey(rawNode, rawOffset) {
|
function findKey(rawNode, rawOffset) {
|
||||||
let { node, offset } = normalizeNodeAndOffset(rawNode, rawOffset)
|
let { node, offset } = normalizeNodeAndOffset(rawNode, rawOffset)
|
||||||
|
const { parentNode } = node
|
||||||
|
|
||||||
// Find the closest parent with an offset key attribute.
|
// Find the closest parent with an offset key attribute.
|
||||||
const closest = node.parentNode.closest(SELECTOR)
|
let closest = parentNode.closest(SELECTOR)
|
||||||
let offsetKey
|
let offsetKey
|
||||||
|
|
||||||
// Get the key from the closest matching node if one exists.
|
// For void nodes, the element with the offset key will be a cousin, not an
|
||||||
if (closest) {
|
// ancestor, so find it by going down from the nearest void parent.
|
||||||
|
if (!closest) {
|
||||||
|
const closestVoid = parentNode.closest(VOID_SELECTOR)
|
||||||
|
closest = closestVoid.querySelector(SELECTOR)
|
||||||
|
offset = closest.textContent.length
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the string value of the offset key attribute.
|
||||||
offsetKey = closest.getAttribute(ATTRIBUTE)
|
offsetKey = closest.getAttribute(ATTRIBUTE)
|
||||||
}
|
|
||||||
|
|
||||||
// Otherwise, for void node scenarios, a cousin node will be selected, and
|
// If we still didn't find an offset key, this is a bug.
|
||||||
// we need to select the first text node cousin we can find.
|
|
||||||
else {
|
|
||||||
while (node = node.parentNode) {
|
|
||||||
const cousin = node.querySelector(SELECTOR)
|
|
||||||
if (!cousin) continue
|
|
||||||
offsetKey = cousin.getAttribute(ATTRIBUTE)
|
|
||||||
offset = cousin.textContent.length
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we still didn't find an offset key, error. This is a bug.
|
|
||||||
if (!offsetKey) {
|
if (!offsetKey) {
|
||||||
throw new Error(`Unable to find offset key for ${node} with offset "${offset}".`)
|
throw new Error(`Unable to find offset key for ${node} with offset "${offset}".`)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user