-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathwith-react.ts
42 lines (36 loc) · 1.23 KB
/
with-react.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import ReactDOM from 'react-dom'
import { BaseEditor } from 'slate'
import { withDOM } from 'slate-dom'
import { ReactEditor } from './react-editor'
import { REACT_MAJOR_VERSION } from '../utils/environment'
/**
* `withReact` adds React and DOM specific behaviors to the editor.
*
* If you are using TypeScript, you must extend Slate's CustomTypes to use
* this plugin.
*
* See https://docs.slatejs.org/concepts/11-typescript to learn how.
*/
export const withReact = <T extends BaseEditor>(
editor: T,
clipboardFormatKey = 'x-slate-fragment'
): T & ReactEditor => {
let e = editor as T & ReactEditor
e = withDOM(e, clipboardFormatKey)
const { onChange } = e
e.onChange = options => {
// COMPAT: React < 18 doesn't batch `setState` hook calls, which means
// that the children and selection can get out of sync for one render
// pass. So we have to use this unstable API to ensure it batches them.
// (2019/12/03)
// https://github.com/facebook/react/issues/14259#issuecomment-439702367
const maybeBatchUpdates =
REACT_MAJOR_VERSION < 18
? ReactDOM.unstable_batchedUpdates
: (callback: () => void) => callback()
maybeBatchUpdates(() => {
onChange(options)
})
}
return e
}