1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-22 23:12:52 +02:00

Add transform wrapBlockByKey, to wrap any node

This can be used to simply wrap a Text node. This is not possible with
the current implementation of `wrapBlockAtRange`, which wraps the
closest parent block of the text node.
This commit is contained in:
Soreine
2016-10-27 14:35:52 +02:00
parent 081ade234e
commit 6e0799010c

View File

@@ -363,3 +363,28 @@ export function unwrapBlockByKey(transform, key, properties, options) {
const range = selection.moveToRangeOf(texts.first(), texts.last()) const range = selection.moveToRangeOf(texts.first(), texts.last())
return transform.unwrapBlockAtRange(range, properties, options) return transform.unwrapBlockAtRange(range, properties, options)
} }
/**
* Wrap a node in a block with `properties`.
*
* @param {Transform} transform
* @param {String} key The node to wrap
* @param {Block || Object || String} block The wrapping block (its children are discarded)
* @param {Object} options
* @param {Boolean} normalize
* @return {Transform}
*/
export function wrapBlockByKey(transform, key, block, options) {
block = Normalize.block(block)
block = block.merge({ nodes: block.nodes.clear() })
const { document } = transform.state
const node = document.assertDescendant(key)
const parent = document.getParent(node)
const index = parent.nodes.indexOf(node)
return transform
.insertNodeByKey(parent.key, index, block, { normalize: false })
.moveNodeByKey(node.key, block.key, 0, options)
}