diff --git a/packages/slate/src/commands/by-path.js b/packages/slate/src/commands/by-path.js index 7e0c039b5..836f496cd 100644 --- a/packages/slate/src/commands/by-path.js +++ b/packages/slate/src/commands/by-path.js @@ -643,6 +643,34 @@ Commands.unwrapNodeByPath = (change, path) => { }) } +/** + * Unwrap all of the children of a node, by removing the node and replacing it + * with the children in the tree. + * + * @param {Change} change + * @param {Array} path + */ + +Commands.unwrapChildrenByPath = (change, path) => { + path = PathUtils.create(path) + const { value } = change + const { document } = value + const node = document.assertNode(path) + const parentPath = PathUtils.lift(path) + const index = path.last() + const { nodes } = node + + change.withoutNormalizing(() => { + nodes.reverse().forEach((child, i) => { + const childIndex = nodes.size - i - 1 + const childPath = path.push(childIndex) + change.moveNodeByPath(childPath, parentPath, index + 1) + }) + + change.removeNodeByPath(path) + }) +} + /** * Wrap a node in a block with `properties`. * @@ -713,18 +741,19 @@ const COMMANDS = [ 'insertNode', 'insertText', 'mergeNode', - 'removeMark', 'removeAllMarks', + 'removeMark', 'removeNode', - 'setText', - 'replaceText', 'removeText', 'replaceNode', + 'replaceText', 'setMark', 'setNode', + 'setText', 'splitNode', - 'unwrapInline', 'unwrapBlock', + 'unwrapChildren', + 'unwrapInline', 'unwrapNode', 'wrapBlock', 'wrapInline', diff --git a/packages/slate/test/commands/by-key/unwrap-children-by-key/block-multiple.js b/packages/slate/test/commands/by-key/unwrap-children-by-key/block-multiple.js new file mode 100644 index 000000000..756a21c10 --- /dev/null +++ b/packages/slate/test/commands/by-key/unwrap-children-by-key/block-multiple.js @@ -0,0 +1,27 @@ +/** @jsx h */ + +import h from '../../../helpers/h' + +export default function(change) { + change.unwrapChildrenByKey('a') +} + +export const input = ( + + + + one + two + + + +) + +export const output = ( + + + one + two + + +) diff --git a/packages/slate/test/commands/by-key/unwrap-children-by-key/block.js b/packages/slate/test/commands/by-key/unwrap-children-by-key/block.js new file mode 100644 index 000000000..b89dbc481 --- /dev/null +++ b/packages/slate/test/commands/by-key/unwrap-children-by-key/block.js @@ -0,0 +1,25 @@ +/** @jsx h */ + +import h from '../../../helpers/h' + +export default function(change) { + change.unwrapChildrenByKey('a') +} + +export const input = ( + + + + word + + + +) + +export const output = ( + + + word + + +)