mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-19 21:51:51 +02:00
Fix replaceNodeByKey failing to normalize node's parent (#1791)
This commit is contained in:
committed by
Ian Storm Taylor
parent
9e50bc6b1f
commit
d6245fbadf
@@ -489,7 +489,7 @@ Changes.replaceNodeByKey = (change, key, newNode, options = {}) => {
|
|||||||
const parent = document.getParent(key)
|
const parent = document.getParent(key)
|
||||||
const index = parent.nodes.indexOf(node)
|
const index = parent.nodes.indexOf(node)
|
||||||
change.removeNodeByKey(key, { normalize: false })
|
change.removeNodeByKey(key, { normalize: false })
|
||||||
change.insertNodeByKey(parent.key, index, newNode, options)
|
change.insertNodeByKey(parent.key, index, newNode, { normalize: false })
|
||||||
if (normalize) {
|
if (normalize) {
|
||||||
change.normalizeNodeByKey(parent.key)
|
change.normalizeNodeByKey(parent.key)
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,37 @@
|
|||||||
|
/** @jsx h */
|
||||||
|
|
||||||
|
import h from '../../../helpers/h'
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This test makes sure there are no intermediate normalization
|
||||||
|
* when calling replaceNodeByKey which calls removeNodeByKey and insertNodeByKey successively.
|
||||||
|
*/
|
||||||
|
|
||||||
|
export default function(change) {
|
||||||
|
// Replacing <hashtag> by an empty text means the <link> is now empty
|
||||||
|
// and empty inlines are removed, according to Slate's core schema.
|
||||||
|
// If an intermediate normalization were to happen, the final normalization
|
||||||
|
// would fail to find the node's parent and thus throw because that parent
|
||||||
|
// has already been removed before (due to the intermediate normalization).
|
||||||
|
change.replaceNodeByKey('a', { object: 'text', text: '' })
|
||||||
|
}
|
||||||
|
|
||||||
|
export const input = (
|
||||||
|
<value>
|
||||||
|
<document>
|
||||||
|
<paragraph>
|
||||||
|
<link>
|
||||||
|
<hashtag key="a">lorem</hashtag>
|
||||||
|
</link>
|
||||||
|
</paragraph>
|
||||||
|
</document>
|
||||||
|
</value>
|
||||||
|
)
|
||||||
|
|
||||||
|
export const output = (
|
||||||
|
<value>
|
||||||
|
<document>
|
||||||
|
<paragraph />
|
||||||
|
</document>
|
||||||
|
</value>
|
||||||
|
)
|
Reference in New Issue
Block a user