1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-13 18:53:59 +02:00

Fix erroneous text after native insert (#4529)

This commit is contained in:
Nemanja Tosic
2021-09-19 22:43:29 +02:00
committed by GitHub
parent 35b722cadd
commit bd80a0b8dc
3 changed files with 31 additions and 9 deletions

View File

@@ -0,0 +1,5 @@
---
'slate-react': patch
---
Fix erroneous text after native insert

View File

@@ -310,10 +310,6 @@ export const Editable = (props: EditableProps) => {
}
}
if (!native) {
event.preventDefault()
}
// COMPAT: For the deleting forward/backward input types we don't want
// to change the selection because it is the range that will be deleted,
// and those commands determine that for themselves.
@@ -427,7 +423,9 @@ export const Editable = (props: EditableProps) => {
// Only insertText operations use the native functionality, for now.
// Potentially expand to single character deletes, as well.
if (native) {
asNative(editor, () => Editor.insertText(editor, data))
asNative(editor, () => Editor.insertText(editor, data), {
onFlushed: () => (native = false),
})
} else {
Editor.insertText(editor, data)
}
@@ -436,6 +434,10 @@ export const Editable = (props: EditableProps) => {
break
}
}
if (!native) {
event.preventDefault()
}
}
},
[readOnly, propsOnDOMBeforeInput]

View File

@@ -10,10 +10,25 @@ export const NATIVE_OPERATIONS: WeakMap<Editor, Operation[]> = new WeakMap()
* @param {Editor} editor - Editor on which the operations are being applied
* @param {callback} fn - Function containing .exec calls which will be queued as native
*/
export const asNative = (editor: Editor, fn: () => void) => {
export const asNative = (
editor: Editor,
fn: () => void,
{ onFlushed }: { onFlushed?: () => void } = {}
) => {
const isNative = AS_NATIVE.get(editor)
AS_NATIVE.set(editor, true)
fn()
AS_NATIVE.set(editor, false)
try {
fn()
} finally {
if (isNative !== undefined) {
AS_NATIVE.set(editor, isNative)
}
}
if (!NATIVE_OPERATIONS.get(editor)) {
onFlushed?.()
}
}
/**
@@ -25,7 +40,7 @@ export const flushNativeEvents = (editor: Editor) => {
// Clear list _before_ applying, as we might flush
// events in each op, as well.
NATIVE_OPERATIONS.set(editor, [])
NATIVE_OPERATIONS.delete(editor)
if (nativeOps) {
Editor.withoutNormalizing(editor, () => {