1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-30 18:39:51 +02:00

fix core onBeforeInput logic to re-render only when necessary

This commit is contained in:
Ian Storm Taylor
2016-07-22 13:02:13 -07:00
parent 5eda330279
commit b2f7349432

View File

@@ -110,10 +110,9 @@ function Plugin(options = {}) {
/**
* The core `onBeforeInput` handler.
*
* If the current selection is expanded, we have to re-render.
*
* If the next state resolves a new list of decorations for any of its text
* nodes, we have to re-render.
*
*
*
* Otherwise, we can allow the default, native text insertion, avoiding a
* re-render for improved performance.
@@ -128,17 +127,32 @@ function Plugin(options = {}) {
const transform = state.transform().insertText(e.data)
const synthetic = transform.apply()
const resolved = editor.resolveState(synthetic)
let isNative = true
const isSynthenic = (
state.isExpanded ||
!resolved.equals(synthetic)
)
// If the current selection is expanded, we have to re-render.
if (state.isExpanded) {
isNative = false
}
if (isSynthenic) e.preventDefault()
// If the current node was empty, we have to re-render so that any empty
// placeholder logic will be updated.
if (state.startText.text == '') {
isNative = false
}
return isSynthenic
? synthetic
: transform.apply({ isNative: true })
// If the next state resolves a new list of decorations for any of its
// text nodes, we have to re-render.
else if (!resolved.equals(synthetic)) {
isNative = false
}
// Update the state with the proper `isNative`.
state = isNative
? transform.apply({ isNative })
: synthetic
if (!isNative) e.preventDefault()
return state
},
/**