codemirror-view.tsx 701 B

123456789101112131415161718192021222324252627282930
  1. import { memo, useCallback, useEffect } from 'react'
  2. import { useCodeMirrorViewContext } from './codemirror-editor'
  3. import useCodeMirrorScope from '../hooks/use-codemirror-scope'
  4. function CodeMirrorView() {
  5. const view = useCodeMirrorViewContext()
  6. // append the editor view dom to the container node when mounted
  7. const containerRef = useCallback(
  8. node => {
  9. if (node) {
  10. node.appendChild(view.dom)
  11. }
  12. },
  13. [view]
  14. )
  15. // destroy the editor when unmounted
  16. useEffect(() => {
  17. return () => {
  18. view.destroy()
  19. }
  20. }, [view])
  21. useCodeMirrorScope(view)
  22. return <div ref={containerRef} style={{ height: '100%' }} />
  23. }
  24. export default memo(CodeMirrorView)