1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-03-06 13:59:47 +01:00

158 lines
3.7 KiB
Markdown
Raw Normal View History

# Utils
```js
import {
cloneFragment,
2017-10-16 18:58:31 -07:00
findDOMNode,
findDOMRange,
findNode,
findRange,
getEventRange,
getEventTransfer,
setEventTransfer,
2017-10-16 18:58:31 -07:00
} from 'slate-react'
```
React-specific utility functions for Slate that may be useful in certain use cases.
## Functions
### `cloneFragment`
`cloneFragment(event: DOMEvent|ReactEvent, value: Value, fragment: Document)`
During a cut or copy event, sets `fragment` as the Slate document fragment to be copied.
```js
function onCopy(event, change, editor) {
const { value } = change
const fragment = // ... create a fragment from a set of nodes ...
if (fragment) {
cloneFragment(event, value, fragment)
return true
}
}
```
Note that calling `cloneFragment` should be the last thing you do in your event handler. If you change the window selection after calling `cloneFragment`, the browser may copy the wrong content. If you need to perform an action after calling `cloneFragment`, wrap it in `requestAnimationFrame`:
```js
function onCut(event, change, editor) {
const { value } = change
const fragment = // ... create a fragment from a set of nodes ...
if (fragment) {
cloneFragment(event, value, fragment)
window.requestAnimationFrame(() => {
editor.change(change => change.delete())
})
return true
}
}
```
### `findDOMNode`
`findDOMNode(node: Node) => DOMElement`
2017-10-16 18:58:31 -07:00
Find the DOM node from a Slate [`Node`](../slate/node.md). Modelled after React's built-in `findDOMNode` helper.
2017-10-17 18:49:02 -07:00
```js
function componentDidUpdate() {
const { node } = this.props
const element = findDOMNode(node)
// Do something with the DOM `element`...
}
```
2017-10-16 18:58:31 -07:00
### `findDOMRange`
2017-10-16 18:58:31 -07:00
`findDOMRange(range: Range) => DOMRange`
Find the DOM range from a Slate [`Range`](../slate/range.md).
```js
2017-10-17 18:49:02 -07:00
function onChange(change) {
const { value } = change
const range = findDOMRange(value.selection)
2017-10-17 18:49:02 -07:00
// Do something with the DOM `range`...
}
```
2017-10-16 18:58:31 -07:00
### `findNode`
`findNode(element: DOMElement, value: Value) => Node`
2017-10-16 18:58:31 -07:00
Find the Slate node from a DOM `element` and Slate `value`.
2017-10-16 18:58:31 -07:00
2017-10-17 18:49:02 -07:00
```js
function onSomeNativeEvent(event) {
2017-10-17 20:07:14 -07:00
const node = findNode(event.target)
2017-10-17 18:49:02 -07:00
// Do something with `node`...
}
```
2017-10-16 18:58:31 -07:00
### `findRange`
`findRange(selection: DOMSelection, value: Value) => Range`
`findRange(range: DOMRange, value: Value) => Range`
2017-10-16 18:58:31 -07:00
Find the Slate range from a DOM `range` or `selection` and a Slate `value`.
2017-10-16 18:58:31 -07:00
2017-10-17 18:49:02 -07:00
```js
function onSomeNativeEvent() {
// You can find a range from a native DOM selection...
const nativeSelection = window.getSelection()
const range = findRange(nativeSelection, value)
2017-10-17 18:49:02 -07:00
// ...or from a native DOM range...
const nativeRange = nativeSelection.getRangeAt(0)
const range = findRange(nativeRange, value)
2017-10-17 18:49:02 -07:00
}
```
2017-10-16 18:58:31 -07:00
### `getEventRange`
`getEventRange(event: DOMEvent|ReactEvent, value: Value) => Range`
2017-10-16 18:58:31 -07:00
Get the affected Slate range from a DOM `event` and Slate `value`.
2017-10-16 18:58:31 -07:00
2017-10-17 18:49:02 -07:00
```js
function onDrop(event, change, editor) {
const targetRange = getEventRange(event)
// Do something at the drop `targetRange`...
}
```
2017-10-16 18:58:31 -07:00
### `getEventTransfer`
2017-10-17 18:49:02 -07:00
`getEventTransfer(event: DOMEvent|ReactEvent) => Object`
2017-10-16 18:58:31 -07:00
Get the Slate-related data from a DOM `event` and Slate `value`.
```js
function onDrop(event, change, editor) {
const transfer = getEventTransfer(event)
const { type, node } = transfer
if (type == 'node') {
// Do something with `node`...
}
}
```
### `setEventTransfer`
2017-10-17 18:49:02 -07:00
`setEventTransfer(event: DOMEvent|ReactEvent, type: String, data: Any)`
Sets the Slate-related `data` with `type` on an `event`. The `type` must be one of the types Slate recognizes: `'fragment'`, `'html'`, `'node'`, `'rich'`, or `'text'`.
```js
function onDragStart(event, change, editor) {
const { value } = change
const { startNode } = value
setEventTransfer(event, 'node', startNode)
}
```