Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

defer native events within Editable to avoid bugs with Editor #4605

Merged
merged 4 commits into from
Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/many-baboons-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'slate-react': patch
---

defer native events within Editable to avoid bugs with Editor
18 changes: 14 additions & 4 deletions packages/slate-react/src/components/editable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ import {
PLACEHOLDER_SYMBOL,
EDITOR_TO_WINDOW,
} from '../utils/weak-maps'
import { asNative, flushNativeEvents } from '../utils/native'

type DeferredOperation = { type: 'insertText'; text: string }
jaked marked this conversation as resolved.
Show resolved Hide resolved

const Children = (props: Parameters<typeof useChildren>[0]) => (
<React.Fragment>{useChildren(props)}</React.Fragment>
Expand Down Expand Up @@ -124,6 +125,7 @@ export const Editable = (props: EditableProps) => {
// Rerender editor when composition status changed
const [isComposing, setIsComposing] = useState(false)
const ref = useRef<HTMLDivElement>(null)
const deferredOperations = useRef<DeferredOperation[]>([])

// Update internal state on each render.
IS_READ_ONLY.set(editor, readOnly)
Expand Down Expand Up @@ -433,8 +435,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), {
onFlushed: () => event.preventDefault(),
deferredOperations.current.push({
type: 'insertText',
text: data,
jaked marked this conversation as resolved.
Show resolved Hide resolved
})
} else {
Editor.insertText(editor, data)
Expand Down Expand Up @@ -622,7 +625,14 @@ export const Editable = (props: EditableProps) => {
// and we can correctly compare DOM text values in components
// to stop rendering, so that browser functions like autocorrect
// and spellcheck work as expected.
flushNativeEvents(editor)
for (const op of deferredOperations.current) {
switch (op.type) {
case 'insertText':
Editor.insertText(editor, op.text)
break
}
}
jaked marked this conversation as resolved.
Show resolved Hide resolved
deferredOperations.current = []
}, [])}
onBlur={useCallback(
(event: React.FocusEvent<HTMLDivElement>) => {
Expand Down
30 changes: 0 additions & 30 deletions packages/slate-react/src/plugin/with-react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ import {
EDITOR_TO_ON_CHANGE,
NODE_TO_KEY,
} from '../utils/weak-maps'
import {
AS_NATIVE,
NATIVE_OPERATIONS,
flushNativeEvents,
} from '../utils/native'
import {
isDOMText,
getPlainText,
Expand Down Expand Up @@ -66,31 +61,6 @@ export const withReact = <T extends Editor>(editor: T) => {
}

e.apply = (op: Operation) => {
// if we're NOT an insert_text and there's a queue
// of native events, bail out and flush the queue.
// otherwise transforms as part of this cycle will
// be incorrect.
//
// This is needed as overriden operations (e.g. `insertText`)
// can call additional transforms, which will need accurate
// content, and will be called _before_ `onInput` is fired.
if (op.type !== 'insert_text') {
AS_NATIVE.set(editor, false)
flushNativeEvents(editor)
}

// If we're in native mode, queue the operation
// and it will be applied later.
if (AS_NATIVE.get(editor)) {
const nativeOps = NATIVE_OPERATIONS.get(editor)
if (nativeOps) {
nativeOps.push(op)
} else {
NATIVE_OPERATIONS.set(editor, [op])
}
return
}

const matches: [Path, Key][] = []

switch (op.type) {
Expand Down
52 changes: 0 additions & 52 deletions packages/slate-react/src/utils/native.ts

This file was deleted.