1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-02-25 09:43:21 +01:00

handle text cases for setNodeByKey

This commit is contained in:
Ian Storm Taylor 2016-10-06 18:39:54 -07:00
parent db3f00db6d
commit 5d27344fb7
2 changed files with 51 additions and 2 deletions

View File

@ -247,7 +247,6 @@ function setNode(state, operation) {
let node = document.assertPath(path)
node = node.merge(properties)
document = document.updateDescendant(node)
document = document.normalize()
state = state.merge({ document })
return state
}

View File

@ -250,8 +250,58 @@ export function setNodeByKey(transform, key, properties) {
const { state } = transform
const { document } = state
const node = document.assertDescendant(key)
const parent = document.getParent(key)
const index = parent.nodes.indexOf(node)
const path = document.getPath(key)
return transform.setNodeOperation(path, properties)
const previous = document.getPreviousSibling(key)
const next = document.getNextSibling(key)
transform.setNodeOperation(path, properties)
// If the `isVoid` property is being changed to true, remove all of the node's
// children, and add additional text nodes around it if necessary.
if (properties.isVoid == true && node.isVoid == false) {
node.nodes.forEach((child) => {
transform.removeNodeByKey(child.key)
})
if (node.kind == 'inline') {
if (!next) {
const text = Text.create()
transform.insertNodeByKey(parent.key, index + 1, text)
}
if (!previous) {
const text = Text.create()
transform.insertNodeByKey(parent.key, index, text)
}
}
}
// If the `isVoid` property is being changed to `false` and the node is an
// inline node, remove any additional unnecessary text it.
if (
properties.isVoid == false &&
node.isVoid == true &&
node.kind == 'inline'
) {
if (
previous &&
previous.kind == 'text' &&
previous.text == ''
) {
transform.removeNodeByKey(previous.key)
}
if (
next &&
next.kind == 'text' &&
next.text == ''
) {
transform.removeNodeByKey(next.key)
}
}
return transform
}
/**