1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-27 17:09:53 +02:00

Fix error 💣 in shouldComponentUpdate

This commit is contained in:
Samy Pessé
2016-11-01 13:24:50 +01:00
parent af6b0e0388
commit 247e546b3b

View File

@@ -84,12 +84,12 @@ class Node extends React.Component {
/** /**
* Should the node update? * Should the node update?
* *
* @param {Object} props * @param {Object} nextProps
* @param {Object} state * @param {Object} state
* @return {Boolean} * @return {Boolean}
*/ */
shouldComponentUpdate = (props) => { shouldComponentUpdate = (nextProps) => {
const { Component } = this.state const { Component } = this.state
// If the node is rendered with a `Component` that has enabled suppression // If the node is rendered with a `Component` that has enabled suppression
@@ -100,29 +100,33 @@ class Node extends React.Component {
} }
// If the node has changed, update. // If the node has changed, update.
if (props.node != this.props.node) { if (nextProps.node != this.props.node) {
if (!isDev() || !Immutable.is(props.node, this.props.node)) { if (!isDev() || !Immutable.is(nextProps.node, this.props.node)) {
return true return true
} else { } else {
warning('Encountered different references for identical node values in "shouldComponentUpdate". Check that you are preserving references for the following node:\n', props.node) warning('Encountered different references for identical node values in "shouldComponentUpdate". Check that you are preserving references for the following node:\n', nextProps.node)
} }
} }
const nextHasEdgeIn = nextProps.state.selection.hasEdgeIn(nextProps.node)
// If the selection is focused and is inside the node, we need to update so // If the selection is focused and is inside the node, we need to update so
// that the selection will be set by one of the <Leaf> components. // that the selection will be set by one of the <Leaf> components.
if ( if (
props.state.isFocused && nextProps.state.isFocused &&
props.state.selection.hasEdgeIn(props.node) nextHasEdgeIn
) { ) {
return true return true
} }
// If the selection is blurred but was previously focused inside the node, const hasEdgeIn = this.props.state.selection.hasEdgeIn(nextProps.node)
// If the selection is blurred but was previously focused (or vice versa) inside the node,
// we need to update to ensure the selection gets updated by re-rendering. // we need to update to ensure the selection gets updated by re-rendering.
if ( if (
props.state.isBlurred && this.props.state.isFocused != nextProps.state.isFocused &&
this.props.state.isFocused && (
this.props.state.selection.hasEdgeIn(props.node) hasEdgeIn || nextHasEdgeIn
)
) { ) {
return true return true
} }
@@ -132,18 +136,15 @@ class Node extends React.Component {
// the node, to allow for intuitive selection-based rendering. // the node, to allow for intuitive selection-based rendering.
if ( if (
this.props.node.kind != 'text' && this.props.node.kind != 'text' &&
( hasEdgeIn != nextHasEdgeIn
props.state.isFocused != this.props.state.isFocused ||
this.props.state.selection.hasEdgeIn(this.props.node) != props.state.selection.hasEdgeIn(props.node)
)
) { ) {
return true return true
} }
// For text nodes, which can have custom decorations, we need to check to // For text nodes, which can have custom decorations, we need to check to
// see if the block has changed, which has caused the decorations to change. // see if the block has changed, which has caused the decorations to change.
if (props.node.kind == 'text') { if (nextProps.node.kind == 'text') {
const { node, schema, state } = props const { node, schema, state } = nextProps
const { document } = state const { document } = state
const decorators = document.getDescendantDecorators(node.key, schema) const decorators = document.getDescendantDecorators(node.key, schema)
const ranges = node.getRanges(decorators) const ranges = node.getRanges(decorators)