diff --git a/src/transforms/by-key.js b/src/transforms/by-key.js index 366563cc9..20895bdba 100644 --- a/src/transforms/by-key.js +++ b/src/transforms/by-key.js @@ -38,41 +38,7 @@ export function insertNodeByKey(transform, key, index, node) { const path = document.getPath(key) const newPath = path.slice().push(index) - transform.insertNodeOperation(path, index, node) - - // If the node is an inline void, the parent is a block, and the node will be - // inserted at the block's edge, we need to add surrounding text nodes. - if (node.kind == 'inline' && node.isVoid) { - const parent = document.assertDescendant(key) - - if (index == 0) { - const text = Text.create() - transform.insertNodeByKey(key, index, text) - } - - if (index == parent.nodes.size) { - const text = Text.create() - transform.insertNodeByKey(key, index + 1, text) - } - } - - // If the node is a text node, and it is insert next to a text node, it should - // be joined with it. - if (node.kind == 'text') { - const parent = document.assertDescendant(key) - const previous = index == 0 ? null : parent.nodes.get(index - 1) - const next = parent.nodes.get(index) - - if (next && next.kind == 'text') { - transform.joinNodeByKey(next.key, node.key) - } - - if (previous && previous.kind == 'text') { - transform.joinNodeByKey(node.key, previous.key) - } - } - - return transform + return transform.insertNodeOperation(path, index, node) } /** @@ -132,29 +98,6 @@ export function moveNodeByKey(transform, key, newKey, newIndex) { const next = parent.nodes.get(newIndex) transform.moveNodeOperation(path, newPath, newIndex) - // If the node to move is a text node, and it will be moved adjacent to - // another text node, join them together. - if (node.kind == 'text') { - if (next && next.kind == 'text') { - transform.joinNodeByKey(next.key, node.key) - } - - if (previous && previous.kind == 'text') { - transform.joinNodeByKey(node.key, previous.key) - } - } - - // If the node to be moved is the last child of its parent, then create a new - // empty text node in its place. - if (prevParent.nodes.size == 1) { - if (prevParent.kind == 'block') { - const text = Text.create() - transform.insertNodeByKey(prevParent.key, 0, text) - } else { - transform.removeNodeByKey(prevParent.key) - } - } - return transform } @@ -193,24 +136,8 @@ export function removeNodeByKey(transform, key) { const parent = document.getParent(key) const previous = document.getPreviousSibling(key) const next = document.getNextSibling(key) - transform.removeNodeOperation(path) - // If there are no more remaining nodes in the parent, re-add an empty text - // node so that we guarantee to always have text nodes as the tree's leaves. - if (parent.nodes.size == 1) { - const text = Text.create() - transform.insertNodeByKey(parent.key, 0, text) - } - - // If the previous and next siblings are both text nodes, join them. - if ( - (previous && previous.kind == 'text') && - (next && next.kind == 'text') - ) { - transform.joinNodeByKey(next.key, previous.key) - } - - return transform + return transform.removeNodeOperation(path) } /** @@ -251,14 +178,6 @@ export function removeTextByKey(transform, key, offset, length) { transform.removeNodeByKey(parent.key) } - // Otherwise, if the text node is not needed in the tree any more, remove it. - else if ( - (previous && previous.isVoid == false) || - (next && next.isVoid == false) - ) { - transform.removeNodeByKey(key) - } - return transform } @@ -303,50 +222,6 @@ export function setNodeByKey(transform, key, properties) { 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 around 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 } @@ -363,89 +238,8 @@ export function splitNodeByKey(transform, key, offset) { let { state } = transform let { document } = state const path = document.getPath(key) - transform.splitNodeOperation(path, offset) - // Traverse the nodes on both sides of the split, ensuring that there are no - // empty inline nodes, or empty text nodes that should be removed. - state = transform.state - document = state.document - const parent = document.getParent(key) - - // Define an iterator that will apply normalization transforms. - parent.filterDescendants((d) => { - - // We don't need to do any normalization for block nodes. - if (d.kind == 'block') { - return - } - - // If an inline void node has no text, add a space character. - if ( - d.kind == 'inline' && - d.text == '' && - d.isVoid == true - ) { - transform.insertTextByKey(d.key, 0, ' ') - } - - // If an non-void inline node has no text now, remove it. - if ( - d.kind == 'inline' && - d.text == '' && - d.isVoid == false - ) { - transform.removeNodeByKey(d.key) - } - - // Check to ensure that extra empty text nodes are preserved around inline - // void nodes. - if ( - d.kind == 'inline' && - d.isVoid == true - ) { - const previous = document.getPreviousSibling(d) - const next = document.getNextSibling(d) - - if ( - (!previous) || - (previous.kind == 'block' || previous.kind == 'inline' && previous.isVoid) - ) { - const p = document.getParent(d) - const index = p.nodes.indexOf(d) - const text = Text.create() - transform.insertNodeByKey(p, index, text) - } - - if ( - (!next) || - (next.kind == 'block' || next.kind == 'inline' && next.isVoid) - ) { - const p = document.getParent(d) - const index = p.nodes.indexOf(d) - const text = Text.create() - transform.insertNodeByKey(p, index + 1, text) - } - } - - // If an empty text node is adjacent to an non-void inline node, remove it. - if ( - d.kind == 'text' && - d.text == '' - ) { - const previous = document.getPreviousSibling(d) - const next = document.getNextSibling(d) - - if ( - (previous && previous.kind == 'inline' && previous.isVoid == false) || - (next && next.kind == 'inline' && next.isVoid == false) - ) { - transform.removeNodeByKey(d.key) - } - } - }) - - // Return the transform. - return transform + return transform.splitNodeOperation(path, offset) } /** @@ -462,5 +256,6 @@ export function unwrapInlineByKey(transform, key, properties) { const { document, selection } = state const node = document.assertDescendant(key) const range = selection.moveToRangeOf(node) + return transform.unwrapInline(range, properties) }