Skip to content

Commit

Permalink
fix: normalizeNode (#5311)
Browse files Browse the repository at this point in the history
* fix

* Create large-worms-jog.md

* Update large-worms-jog.md

* Update large-worms-jog.md

* Update large-worms-jog.md

* fix
  • Loading branch information
zbeyens committed Feb 23, 2023
1 parent b94254d commit 0ac72a6
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/large-worms-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'slate': patch
---

Fix #5295 regression. `editor.shouldNormalize` new option: `initialDirtyPathsLength: number`
2 changes: 1 addition & 1 deletion docs/api/nodes/editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions packages/slate/src/create-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
11 changes: 7 additions & 4 deletions packages/slate/src/interfaces/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export interface BaseEditor {
operation,
}: {
iteration: number
initialDirtyPathsLength: number
dirtyPaths: Path[]
operation?: Operation
}) => boolean
Expand Down Expand Up @@ -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,
})
) {
Expand All @@ -1095,6 +1097,7 @@ export const Editor: EditorInterface = {
editor.normalizeNode(entry, { operation })
}
iteration++
dirtyPaths = getDirtyPaths(editor)
}
})
},
Expand Down

0 comments on commit 0ac72a6

Please sign in to comment.