From 5d27344fb715a70570b2a94aa817201a4178505e Mon Sep 17 00:00:00 2001 From: Ian Storm Taylor Date: Thu, 6 Oct 2016 18:39:54 -0700 Subject: [PATCH] handle text cases for setNodeByKey --- src/transforms/apply-operation.js | 1 - src/transforms/by-key.js | 52 ++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/src/transforms/apply-operation.js b/src/transforms/apply-operation.js index 5410913a5..5cc8dd70d 100644 --- a/src/transforms/apply-operation.js +++ b/src/transforms/apply-operation.js @@ -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 } diff --git a/src/transforms/by-key.js b/src/transforms/by-key.js index b041b04ca..1be0b1c7b 100644 --- a/src/transforms/by-key.js +++ b/src/transforms/by-key.js @@ -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 } /**