mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-31 10:51:44 +02:00
refactor normalization logic to result in shorter callstacks
This commit is contained in:
@@ -543,10 +543,16 @@ function normalizeDirtyPaths(editor) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
while (editor.tmp.dirty.length) {
|
if (!editor.tmp.dirty.length) {
|
||||||
const path = editor.tmp.dirty.pop()
|
return
|
||||||
normalizeNodeByPath(editor, path)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
editor.withoutNormalizing(() => {
|
||||||
|
while (editor.tmp.dirty.length) {
|
||||||
|
const path = editor.tmp.dirty.pop()
|
||||||
|
normalizeNodeByPath(editor, path)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -557,22 +563,27 @@ function normalizeDirtyPaths(editor) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
function normalizeNodeByPath(editor, path) {
|
function normalizeNodeByPath(editor, path) {
|
||||||
const { controller, value } = editor
|
const { controller } = editor
|
||||||
|
let { value } = editor
|
||||||
let { document } = value
|
let { document } = value
|
||||||
let node = document.assertNode(path)
|
let node = document.assertNode(path)
|
||||||
let iterations = 0
|
let iterations = 0
|
||||||
const max = 100 + (node.object === 'text' ? 1 : node.nodes.size)
|
const max = 100 + (node.object === 'text' ? 1 : node.nodes.size)
|
||||||
|
|
||||||
const iterate = () => {
|
while (node) {
|
||||||
const fn = node.normalize(controller)
|
const fn = node.normalize(controller)
|
||||||
if (!fn) return
|
|
||||||
|
if (!fn) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
// Run the normalize `fn` to fix the node.
|
// Run the normalize `fn` to fix the node.
|
||||||
fn(controller)
|
fn(controller)
|
||||||
|
|
||||||
// Attempt to re-find the node by path, or by key if it has changed
|
// Attempt to re-find the node by path, or by key if it has changed
|
||||||
// locations in the tree continue iterating.
|
// locations in the tree continue iterating.
|
||||||
document = editor.value.document
|
value = editor.value
|
||||||
|
document = value.document
|
||||||
const { key } = node
|
const { key } = node
|
||||||
let found = document.getDescendant(path)
|
let found = document.getDescendant(path)
|
||||||
|
|
||||||
@@ -585,8 +596,8 @@ function normalizeNodeByPath(editor, path) {
|
|||||||
node = found
|
node = found
|
||||||
path = document.getPath(key)
|
path = document.getPath(key)
|
||||||
} else {
|
} else {
|
||||||
// If it no longer exists by key, it was removed, so abort.
|
// If it no longer exists by key, it was removed, so we're done.
|
||||||
return
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -601,14 +612,7 @@ function normalizeNodeByPath(editor, path) {
|
|||||||
'A schema rule could not be normalized after sufficient iterations. This is usually due to a `rule.normalize` or `plugin.normalizeNode` function of a schema being incorrectly written, causing an infinite loop.'
|
'A schema rule could not be normalized after sufficient iterations. This is usually due to a `rule.normalize` or `plugin.normalizeNode` function of a schema being incorrectly written, causing an infinite loop.'
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise, iterate again.
|
|
||||||
iterate()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
editor.withoutNormalizing(() => {
|
|
||||||
iterate()
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user