diff --git a/.changeset/cyan-plums-grow.md b/.changeset/cyan-plums-grow.md new file mode 100644 index 0000000000..fa1329915d --- /dev/null +++ b/.changeset/cyan-plums-grow.md @@ -0,0 +1,5 @@ +--- +'@udecode/slate-utils': minor +--- + +Add replaceNode diff --git a/packages/slate-utils/src/transforms/index.ts b/packages/slate-utils/src/transforms/index.ts index 7f48139601..484869d7d9 100644 --- a/packages/slate-utils/src/transforms/index.ts +++ b/packages/slate-utils/src/transforms/index.ts @@ -6,8 +6,8 @@ export * from './insertElements'; export * from './insertEmptyElement'; export * from './moveChildren'; export * from './removeMark'; -export * from './removeNodeChildren'; export * from './removeSelectionMark'; +export * from './replaceNode'; export * from './replaceNodeChildren'; export * from './selectEndOfBlockAboveSelection'; export * from './setMarks'; diff --git a/packages/slate-utils/src/transforms/replaceNode.ts b/packages/slate-utils/src/transforms/replaceNode.ts new file mode 100644 index 0000000000..b4f1a16363 --- /dev/null +++ b/packages/slate-utils/src/transforms/replaceNode.ts @@ -0,0 +1,27 @@ +import { + EElementOrText, + insertNodes, + removeNodes, + TEditor, + Value, + withoutNormalizing, +} from '@udecode/slate'; + +import { ReplaceNodeChildrenOptions } from './replaceNodeChildren'; + +export const replaceNode = < + N extends EElementOrText, + V extends Value = Value, +>( + editor: TEditor, + { at, nodes, insertOptions, removeOptions }: ReplaceNodeChildrenOptions +) => { + withoutNormalizing(editor, () => { + removeNodes(editor, { ...insertOptions, at }); + + insertNodes(editor, nodes, { + ...insertOptions, + at, + }); + }); +};