DiffViewer.tsx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { diff as DiffEditor } from 'react-ace';
  2. import 'ace-builds/src-noconflict/ace';
  3. import 'ace-builds/src-noconflict/mode-json5';
  4. import 'ace-builds/src-noconflict/mode-protobuf';
  5. import 'ace-builds/src-noconflict/theme-textmate';
  6. import React from 'react';
  7. import { IDiffEditorProps } from 'react-ace/lib/diff';
  8. import { SchemaType } from 'generated-sources';
  9. interface DiffViewerProps extends IDiffEditorProps {
  10. isFixedHeight?: boolean;
  11. schemaType: string;
  12. }
  13. const DiffViewer = React.forwardRef<DiffEditor | null, DiffViewerProps>(
  14. (props, ref) => {
  15. const { isFixedHeight, schemaType, ...rest } = props;
  16. const autoHeight =
  17. !isFixedHeight && props.value && props.value.length === 2
  18. ? Math.max(
  19. props.value[0].split(/\r\n|\r|\n/).length + 1,
  20. props.value[1].split(/\r\n|\r|\n/).length + 1
  21. ) * 16
  22. : 500;
  23. return (
  24. <div>
  25. <DiffEditor
  26. name="diff-editor"
  27. ref={ref}
  28. mode={
  29. schemaType === SchemaType.JSON || schemaType === SchemaType.AVRO
  30. ? 'json5'
  31. : 'protobuf'
  32. }
  33. theme="textmate"
  34. tabSize={2}
  35. width="100%"
  36. height={`${autoHeight}px`}
  37. showPrintMargin={false}
  38. maxLines={Infinity}
  39. readOnly
  40. wrapEnabled
  41. {...rest}
  42. />
  43. </div>
  44. );
  45. }
  46. );
  47. DiffViewer.displayName = 'DiffViewer';
  48. export default DiffViewer;