1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-01-18 05:59:13 +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:
zhugexinxin 2022-03-24 15:25:36 +08:00 committed by GitHub
parent 3453432f7a
commit 43ca2b56c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 3 deletions

View File

@ -0,0 +1,5 @@
---
'slate': patch
---
feat: add merge to setNodes and test

View File

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

View File

@ -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,9 +662,13 @@ 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 (merge) {
if (props[k] != null) newProperties[k] = merge(node[k], props[k])
} else {
if (props[k] != null) newProperties[k] = props[k]
}
}
}
if (hasChanges) {
editor.apply({

View 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>
)