codemirror-context.ts 877 B

1234567891011121314151617181920212223242526272829303132333435
  1. import { createContext, useContext } from 'react'
  2. import { EditorView } from '@codemirror/view'
  3. import { EditorState } from '@codemirror/state'
  4. export const CodeMirrorStateContext = createContext<EditorState | undefined>(
  5. undefined
  6. )
  7. export const CodeMirrorViewContext = createContext<EditorView | undefined>(
  8. undefined
  9. )
  10. export const useCodeMirrorStateContext = (): EditorState => {
  11. const context = useContext(CodeMirrorStateContext)
  12. if (!context) {
  13. throw new Error(
  14. 'useCodeMirrorStateContext is only available inside CodeMirrorStateContext.Provider'
  15. )
  16. }
  17. return context
  18. }
  19. export const useCodeMirrorViewContext = (): EditorView => {
  20. const context = useContext(CodeMirrorViewContext)
  21. if (!context) {
  22. throw new Error(
  23. 'useCodeMirrorViewContext is only available inside CodeMirrorViewContext.Provider'
  24. )
  25. }
  26. return context
  27. }