|
|
import ReactDOM from 'react-dom' |
|
|
import { BaseEditor, Node } from 'slate' |
|
|
import { withDOM, IS_ANDROID, EDITOR_TO_PENDING_SELECTION } from 'slate-dom' |
|
|
import { ReactEditor } from './react-editor' |
|
|
import { REACT_MAJOR_VERSION } from '../utils/environment' |
|
|
import { getChunkTreeForNode } from '../chunking' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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, apply, insertText } = e |
|
|
|
|
|
e.getChunkSize = () => null |
|
|
|
|
|
if (IS_ANDROID) { |
|
|
e.insertText = (text, options) => { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
EDITOR_TO_PENDING_SELECTION.delete(e) |
|
|
|
|
|
return insertText(text, options) |
|
|
} |
|
|
} |
|
|
|
|
|
e.onChange = options => { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const maybeBatchUpdates = |
|
|
REACT_MAJOR_VERSION < 18 |
|
|
? ReactDOM.unstable_batchedUpdates |
|
|
: (callback: () => void) => callback() |
|
|
|
|
|
maybeBatchUpdates(() => { |
|
|
onChange(options) |
|
|
}) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
e.apply = operation => { |
|
|
if (operation.type === 'move_node') { |
|
|
const parent = Node.parent(e, operation.path) |
|
|
const chunking = !!e.getChunkSize(parent) |
|
|
|
|
|
if (chunking) { |
|
|
const node = Node.get(e, operation.path) |
|
|
const chunkTree = getChunkTreeForNode(e, parent) |
|
|
const key = ReactEditor.findKey(e, node) |
|
|
chunkTree.movedNodeKeys.add(key) |
|
|
} |
|
|
} |
|
|
|
|
|
apply(operation) |
|
|
} |
|
|
|
|
|
return e |
|
|
} |
|
|
|