1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-24 07:52:50 +02:00

add unwrapChildrenByPath/Key command

This commit is contained in:
Ian Storm Taylor
2018-10-22 15:21:45 -07:00
parent 805c329e5d
commit 93df22a128
3 changed files with 85 additions and 4 deletions

View File

@@ -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',

View File

@@ -0,0 +1,27 @@
/** @jsx h */
import h from '../../../helpers/h'
export default function(change) {
change.unwrapChildrenByKey('a')
}
export const input = (
<value>
<document>
<quote key="a">
<paragraph>one</paragraph>
<paragraph>two</paragraph>
</quote>
</document>
</value>
)
export const output = (
<value>
<document>
<paragraph>one</paragraph>
<paragraph>two</paragraph>
</document>
</value>
)

View File

@@ -0,0 +1,25 @@
/** @jsx h */
import h from '../../../helpers/h'
export default function(change) {
change.unwrapChildrenByKey('a')
}
export const input = (
<value>
<document>
<quote key="a">
<paragraph>word</paragraph>
</quote>
</document>
</value>
)
export const output = (
<value>
<document>
<paragraph>word</paragraph>
</document>
</value>
)