forked from ianstormtaylor/slate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
use-slate.tsx
47 lines (37 loc) · 991 Bytes
/
use-slate.tsx
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
43
44
45
46
47
import { createContext, useContext } from 'react'
import { Editor } from 'slate'
import { ReactEditor } from '../plugin/react-editor'
/**
* A React context for sharing the editor object, in a way that re-renders the
* context whenever changes occur.
*/
export interface SlateContextValue {
v: number
editor: ReactEditor
}
export const SlateContext = createContext<{
v: number
editor: ReactEditor
} | null>(null)
/**
* Get the current editor object from the React context.
*/
export const useSlate = (): Editor => {
const context = useContext(SlateContext)
if (!context) {
throw new Error(
`The \`useSlate\` hook must be used inside the <Slate> component's context.`
)
}
const { editor } = context
return editor
}
export const useSlateWithV = () => {
const context = useContext(SlateContext)
if (!context) {
throw new Error(
`The \`useSlate\` hook must be used inside the <Slate> component's context.`
)
}
return context
}