From 18f9778cda9450cd99a71c47ab0767b64813db1a Mon Sep 17 00:00:00 2001 From: zbeyens Date: Sat, 30 Sep 2023 12:05:42 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .changeset/cyan-plums-grow.md | 5 ++++ packages/slate-utils/src/transforms/index.ts | 2 +- .../slate-utils/src/transforms/replaceNode.ts | 27 +++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 .changeset/cyan-plums-grow.md create mode 100644 packages/slate-utils/src/transforms/replaceNode.ts 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, + }); + }); +};