Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/editors/slate/slate-react/hooks/use-slate.tsx
1698 views
1
import { createContext, useContext } from "react";
2
3
import { ReactEditor } from "../plugin/react-editor";
4
5
/**
6
* A React context for sharing the editor object, in a way that re-renders the
7
* context whenever changes occur.
8
*/
9
10
export const SlateContext = createContext<[ReactEditor] | null>(null);
11
12
/**
13
* Get the current editor object from the React context.
14
*/
15
16
export const useSlate = () => {
17
const context = useContext(SlateContext);
18
19
if (!context) {
20
throw new Error(
21
`The \`useSlate\` hook must be used inside the <SlateProvider> component's context.`
22
);
23
}
24
25
const [editor] = context;
26
return editor;
27
};
28
29