mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-09-03 04:02:33 +02:00
add setNodeByKey transform (#157)
* modifyNode transform * create DOCUMENT_NODE_TRANSFORMS and use assertDescendant to get the node directly * rename to setNodeByKey
This commit is contained in:
committed by
Ian Storm Taylor
parent
58691691a9
commit
8407023e5f
@@ -60,6 +60,7 @@ Transform methods can either operate on the [`Document`](./document.md), the [`S
|
||||
- [`unwrapInlineAtRange`](#unwrapinlineatrange)
|
||||
- [`wrapBlockAtRange`](#wrapblockatrange)
|
||||
- [`wrapInlineAtRange`](#wrapinlineatrange)
|
||||
- [`setNodeByKey`](#setNodeByKey)
|
||||
- [History Transforms](#history-transforms)
|
||||
- [`redo`](#redo)
|
||||
- [`undo`](#undo)
|
||||
@@ -319,6 +320,12 @@ Wrap the [`Block`](./block.md) nodes in a `range` with a new [`Block`](./block.m
|
||||
|
||||
Wrap the [`Inline`](./inline.md) nodes in a `range` with a new [`Inline`](./inline.md) node of `type`, with optional `data`.
|
||||
|
||||
### `setNodeByKey`
|
||||
`setNodeByKey(key: String, properties: Object) => Transform`
|
||||
`setNodeByKey(key: String, type: String) => Transform`
|
||||
|
||||
Set the properties of the [`Node`](./node.md) having the specified `key`.
|
||||
|
||||
|
||||
## History Transforms
|
||||
|
||||
|
@@ -23,10 +23,10 @@ const Step = new Record({
|
||||
})
|
||||
|
||||
/**
|
||||
* Document transforms.
|
||||
* Document range transforms.
|
||||
*/
|
||||
|
||||
const DOCUMENT_TRANSFORMS = [
|
||||
const DOCUMENT_RANGE_TRANSFORMS = [
|
||||
'deleteAtRange',
|
||||
'deleteBackwardAtRange',
|
||||
'deleteForwardAtRange',
|
||||
@@ -42,7 +42,15 @@ const DOCUMENT_TRANSFORMS = [
|
||||
'unwrapBlockAtRange',
|
||||
'unwrapInlineAtRange',
|
||||
'wrapBlockAtRange',
|
||||
'wrapInlineAtRange'
|
||||
'wrapInlineAtRange',
|
||||
]
|
||||
|
||||
/**
|
||||
* Document node transforms.
|
||||
*/
|
||||
|
||||
const DOCUMENT_NODE_TRANSFORMS = [
|
||||
'setNodeByKey',
|
||||
]
|
||||
|
||||
/**
|
||||
@@ -120,7 +128,8 @@ const STATE_TRANSFORMS = []
|
||||
*/
|
||||
|
||||
const TRANSFORMS = []
|
||||
.concat(DOCUMENT_TRANSFORMS)
|
||||
.concat(DOCUMENT_RANGE_TRANSFORMS)
|
||||
.concat(DOCUMENT_NODE_TRANSFORMS)
|
||||
.concat(SELECTION_TRANSFORMS)
|
||||
.concat(STATE_TRANSFORMS)
|
||||
|
||||
@@ -209,7 +218,7 @@ class Transform extends new Record(DEFAULT_PROPERTIES) {
|
||||
applyStep(state, step) {
|
||||
const { type, args } = step
|
||||
|
||||
if (includes(DOCUMENT_TRANSFORMS, type)) {
|
||||
if (includes(DOCUMENT_RANGE_TRANSFORMS, type)) {
|
||||
let { document, selection } = state
|
||||
let [ range, ...rest ] = args
|
||||
range = range.normalize(document)
|
||||
@@ -219,6 +228,14 @@ class Transform extends new Record(DEFAULT_PROPERTIES) {
|
||||
return state
|
||||
}
|
||||
|
||||
else if (includes(DOCUMENT_NODE_TRANSFORMS, type)) {
|
||||
let { document, selection } = state
|
||||
document = document[type](...args)
|
||||
selection = selection.normalize(document)
|
||||
state = state.merge({ document, selection })
|
||||
return state
|
||||
}
|
||||
|
||||
else if (includes(SELECTION_TRANSFORMS, type)) {
|
||||
let { document, selection } = state
|
||||
selection = selection[type](...args)
|
||||
|
@@ -899,6 +899,30 @@ const Transforms = {
|
||||
})
|
||||
|
||||
return node
|
||||
},
|
||||
|
||||
/**
|
||||
* Modify a block.
|
||||
*
|
||||
* @param {String} key
|
||||
* @param {Object or String} properties
|
||||
* @return {Node} node
|
||||
*/
|
||||
|
||||
setNodeByKey(key, properties) {
|
||||
const node = this
|
||||
|
||||
let newNode = node.assertDescendant(key)
|
||||
|
||||
// Allow for properties to be a string `type` for convenience.
|
||||
if (typeof properties == 'string') {
|
||||
properties = { type: properties }
|
||||
}
|
||||
|
||||
if (properties.data) properties.data = Data.create(properties.data)
|
||||
|
||||
newNode = newNode.merge(properties)
|
||||
return node.mapDescendants(d => d.key == key ? newNode : d)
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -0,0 +1,10 @@
|
||||
|
||||
export default function (state) {
|
||||
const { document, selection } = state
|
||||
const first = document.nodes.get(0)
|
||||
|
||||
return state
|
||||
.transform()
|
||||
.setNodeByKey(first.key, 'code')
|
||||
.apply()
|
||||
}
|
@@ -0,0 +1,8 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: word
|
@@ -0,0 +1,8 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: code
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: word
|
10
test/transforms/fixtures/modify-node/single-block/index.js
Normal file
10
test/transforms/fixtures/modify-node/single-block/index.js
Normal file
@@ -0,0 +1,10 @@
|
||||
|
||||
export default function (state) {
|
||||
const { document, selection } = state
|
||||
const first = document.nodes.get(0)
|
||||
|
||||
return state
|
||||
.transform()
|
||||
.setNodeByKey(first.key, { data: {key: 'bar'} })
|
||||
.apply()
|
||||
}
|
10
test/transforms/fixtures/modify-node/single-block/input.yaml
Normal file
10
test/transforms/fixtures/modify-node/single-block/input.yaml
Normal file
@@ -0,0 +1,10 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
data:
|
||||
key: foo
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: word
|
@@ -0,0 +1,10 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
data:
|
||||
key: bar
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: word
|
Reference in New Issue
Block a user