diff --git a/.changeset/large-worms-jog.md b/.changeset/large-worms-jog.md new file mode 100644 index 0000000000..cf22f79c26 --- /dev/null +++ b/.changeset/large-worms-jog.md @@ -0,0 +1,5 @@ +--- +'slate': patch +--- + +Fix #5295 regression. `editor.shouldNormalize` new option: `initialDirtyPathsLength: number` diff --git a/docs/api/nodes/editor.md b/docs/api/nodes/editor.md index fbe450e5f6..7e73046c43 100644 --- a/docs/api/nodes/editor.md +++ b/docs/api/nodes/editor.md @@ -420,7 +420,7 @@ Check if a value is a void `Element` object. Override this method to prevent normalizing the editor. -Options: `{ iteration: number; dirtyPaths: Path[]; operation?: Operation(entry: NodeEntry, { operation }` +Options: `{ dirtyPaths: Path[]; initialDirtyPathsLength: number; iteration: number; operation?: Operation }` ### Callback method diff --git a/packages/slate/src/create-editor.ts b/packages/slate/src/create-editor.ts index bb131b887a..76bdbb4b25 100644 --- a/packages/slate/src/create-editor.ts +++ b/packages/slate/src/create-editor.ts @@ -414,8 +414,8 @@ export const createEditor = (): Editor => { } }, - shouldNormalize: ({ iteration, dirtyPaths }) => { - const maxIterations = dirtyPaths.length * 42 // HACK: better way? + shouldNormalize: ({ iteration, initialDirtyPathsLength }) => { + const maxIterations = initialDirtyPathsLength * 42 // HACK: better way? if (iteration > maxIterations) { throw new Error( diff --git a/packages/slate/src/interfaces/editor.ts b/packages/slate/src/interfaces/editor.ts index c4196f571c..aa85593da3 100644 --- a/packages/slate/src/interfaces/editor.ts +++ b/packages/slate/src/interfaces/editor.ts @@ -85,6 +85,7 @@ export interface BaseEditor { operation, }: { iteration: number + initialDirtyPathsLength: number dirtyPaths: Path[] operation?: Operation }) => boolean @@ -1072,15 +1073,16 @@ export const Editor: EditorInterface = { } } - const dirtyPaths = getDirtyPaths(editor) - + let dirtyPaths = getDirtyPaths(editor) + const initialDirtyPathsLength = dirtyPaths.length let iteration = 0 - while (getDirtyPaths(editor).length !== 0) { + while (dirtyPaths.length !== 0) { if ( !editor.shouldNormalize({ + dirtyPaths, iteration, - dirtyPaths: getDirtyPaths(editor), + initialDirtyPathsLength, operation, }) ) { @@ -1095,6 +1097,7 @@ export const Editor: EditorInterface = { editor.normalizeNode(entry, { operation }) } iteration++ + dirtyPaths = getDirtyPaths(editor) } }) },