UncontrolledCodeMirror.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import React, {
  2. useCallback, useRef, MutableRefObject,
  3. } from 'react';
  4. import { commands, Editor } from 'codemirror';
  5. import { ICodeMirror, UnControlled as CodeMirror } from 'react-codemirror2';
  6. // set save handler
  7. // CommandActions in @types/codemirror does not include 'save' but actualy exists
  8. // https://codemirror.net/5/doc/manual.html#commands
  9. (commands as any).save = (instance) => {
  10. if (instance.codeMirrorEditor != null) {
  11. instance.codeMirrorEditor.dispatchSave();
  12. }
  13. };
  14. window.CodeMirror = require('codemirror');
  15. require('codemirror/addon/display/placeholder');
  16. require('~/client/util/codemirror/gfm-growi.mode');
  17. export interface UncontrolledCodeMirrorProps extends ICodeMirror {
  18. value: string;
  19. isGfmMode?: boolean;
  20. lineNumbers?: boolean;
  21. onSave?: () => Promise<void>;
  22. onCtrlEnter?: (event: Event) => void;
  23. onPasteFiles?: (editor: any, event: Event) => void;
  24. onScrollCursorIntoView?: (editor: any, event: Event) => void;
  25. }
  26. export const UncontrolledCodeMirror = React.forwardRef<CodeMirror|null, UncontrolledCodeMirrorProps>((props, forwardedRef): JSX.Element => {
  27. const {
  28. value, lineNumbers, options,
  29. onPasteFiles, onScrollCursorIntoView,
  30. ...rest
  31. } = props;
  32. const editorRef = useRef<Editor>();
  33. const wrapperRef = useRef<CodeMirror|null>();
  34. const editorDidMountHandler = useCallback((editor: Editor): void => {
  35. editorRef.current = editor;
  36. if (onPasteFiles != null) {
  37. editor.on('paste', onPasteFiles);
  38. }
  39. if (onScrollCursorIntoView != null) {
  40. editor.on('scrollCursorIntoView', onScrollCursorIntoView);
  41. }
  42. }, [onPasteFiles, onScrollCursorIntoView]);
  43. const editorWillUnmountHandler = useCallback((): void => {
  44. // workaround to fix editor duplicating by https://github.com/scniro/react-codemirror2/issues/284#issuecomment-1155928554
  45. if (editorRef.current != null) {
  46. (editorRef.current as any).display.wrapper.remove();
  47. }
  48. if (wrapperRef.current != null) {
  49. (wrapperRef.current as any).hydrated = false;
  50. }
  51. }, []);
  52. // default true
  53. const isGfmMode = rest.isGfmMode ?? true;
  54. return (
  55. <CodeMirror
  56. ref={(elem) => {
  57. // register to wrapperRef
  58. wrapperRef.current = elem;
  59. // register to forwardedRef
  60. if (forwardedRef != null) {
  61. if (typeof forwardedRef === 'function') {
  62. forwardedRef(elem);
  63. }
  64. else {
  65. (forwardedRef as MutableRefObject<CodeMirror|null>).current = elem;
  66. }
  67. }
  68. }}
  69. value={value}
  70. options={{
  71. lineNumbers: lineNumbers ?? true,
  72. mode: isGfmMode ? 'gfm-growi' : undefined,
  73. tabSize: 4,
  74. ...options,
  75. }}
  76. editorDidMount={editorDidMountHandler}
  77. editorWillUnmount={editorWillUnmountHandler}
  78. {...rest}
  79. />
  80. );
  81. });
  82. UncontrolledCodeMirror.displayName = 'UncontrolledCodeMirror';