CodeMirrorEditor.tsx 986 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import {
  2. forwardRef, useMemo, useRef,
  3. } from 'react';
  4. import type { ReactCodeMirrorProps } from '@uiw/react-codemirror';
  5. import { GlobalCodeMirrorEditorKey } from '../consts';
  6. import { useCodeMirrorEditorIsolated } from '../stores';
  7. import style from './CodeMirrorEditor.module.scss';
  8. const CodeMirrorEditorContainer = forwardRef<HTMLDivElement>((props, ref) => {
  9. return (
  10. <div {...props} className={`${style['codemirror-editor-container']}`} ref={ref} />
  11. );
  12. });
  13. type Props = {
  14. editorKey: string | GlobalCodeMirrorEditorKey,
  15. onChange?: (value: string) => void,
  16. }
  17. export const CodeMirrorEditor = (props: Props): JSX.Element => {
  18. const {
  19. editorKey,
  20. onChange,
  21. } = props;
  22. const containerRef = useRef(null);
  23. const cmProps = useMemo<ReactCodeMirrorProps>(() => {
  24. return {
  25. onChange,
  26. };
  27. }, [onChange]);
  28. useCodeMirrorEditorIsolated(editorKey, containerRef.current, cmProps);
  29. return <CodeMirrorEditorContainer ref={containerRef} />;
  30. };