Skip to content

Commit

Permalink
Fix erroneous text after native insert (#4529)
Browse files Browse the repository at this point in the history
  • Loading branch information
nemanja-tosic authored Sep 19, 2021
1 parent 35b722c commit bd80a0b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/light-gorillas-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'slate-react': patch
---

Fix erroneous text after native insert
12 changes: 7 additions & 5 deletions packages/slate-react/src/components/editable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
Expand All @@ -436,6 +434,10 @@ export const Editable = (props: EditableProps) => {
break
}
}

if (!native) {
event.preventDefault()
}
}
},
[readOnly, propsOnDOMBeforeInput]
Expand Down
23 changes: 19 additions & 4 deletions packages/slate-react/src/utils/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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?.()
}
}

/**
Expand All @@ -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, () => {
Expand Down

0 comments on commit bd80a0b

Please sign in to comment.