mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-01-17 21:49:20 +01:00
feat: add merge to setNodes and test (#4912)
* feat: add merge to setNodes and test * Add changset Co-authored-by: Dylan Schiemann <dylan@dojotoolkit.org>
This commit is contained in:
parent
3453432f7a
commit
43ca2b56c8
5
.changeset/friendly-dogs-judge.md
Normal file
5
.changeset/friendly-dogs-judge.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
'slate': patch
|
||||
---
|
||||
|
||||
feat: add merge to setNodes and test
|
@ -1736,3 +1736,4 @@ export type NodeMatch<T extends Node> =
|
||||
| ((node: Node, path: Path) => boolean)
|
||||
|
||||
export type PropsCompare = (prop: Partial<Node>, node: Partial<Node>) => boolean
|
||||
export type PropsMerge = (prop: Partial<Node>, node: Partial<Node>) => object
|
||||
|
@ -11,7 +11,7 @@ import {
|
||||
NodeEntry,
|
||||
Ancestor,
|
||||
} from '..'
|
||||
import { NodeMatch, PropsCompare } from '../interfaces/editor'
|
||||
import { NodeMatch, PropsCompare, PropsMerge } from '../interfaces/editor'
|
||||
|
||||
export interface NodeTransforms {
|
||||
insertNodes: <T extends Node>(
|
||||
@ -76,6 +76,7 @@ export interface NodeTransforms {
|
||||
split?: boolean
|
||||
voids?: boolean
|
||||
compare?: PropsCompare
|
||||
merge?: PropsMerge
|
||||
}
|
||||
) => void
|
||||
splitNodes: <T extends Node>(
|
||||
@ -570,10 +571,11 @@ export const NodeTransforms: NodeTransforms = {
|
||||
split?: boolean
|
||||
voids?: boolean
|
||||
compare?: PropsCompare
|
||||
merge?: PropsMerge
|
||||
} = {}
|
||||
): void {
|
||||
Editor.withoutNormalizing(editor, () => {
|
||||
let { match, at = editor.selection, compare } = options
|
||||
let { match, at = editor.selection, compare, merge } = options
|
||||
const {
|
||||
hanging = false,
|
||||
mode = 'lowest',
|
||||
@ -660,7 +662,11 @@ export const NodeTransforms: NodeTransforms = {
|
||||
// Omit new properties from the old properties list
|
||||
if (node.hasOwnProperty(k)) properties[k] = node[k]
|
||||
// Omit properties that have been removed from the new properties list
|
||||
if (props[k] != null) newProperties[k] = props[k]
|
||||
if (merge) {
|
||||
if (props[k] != null) newProperties[k] = merge(node[k], props[k])
|
||||
} else {
|
||||
if (props[k] != null) newProperties[k] = props[k]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
26
packages/slate/test/transforms/setNodes/merge/text.tsx
Normal file
26
packages/slate/test/transforms/setNodes/merge/text.tsx
Normal file
@ -0,0 +1,26 @@
|
||||
/** @jsx jsx */
|
||||
import { Transforms, Text } from 'slate'
|
||||
import { jsx } from '../../..'
|
||||
import _ from 'lodash'
|
||||
|
||||
export const run = editor => {
|
||||
Transforms.setNodes(
|
||||
editor,
|
||||
{ a: { b: 2, c: 3 } },
|
||||
{ at: [0, 0], match: Text.isText, merge: (n, p) => _.defaultsDeep(p, n) }
|
||||
)
|
||||
}
|
||||
export const input = (
|
||||
<editor>
|
||||
<block>
|
||||
<text a={{ b: 1 }}>word</text>
|
||||
</block>
|
||||
</editor>
|
||||
)
|
||||
export const output = (
|
||||
<editor>
|
||||
<block>
|
||||
<text a={{ b: 2, c: 3 }}>word</text>
|
||||
</block>
|
||||
</editor>
|
||||
)
|
Loading…
x
Reference in New Issue
Block a user