DiffViewer.tsx 1.4 KB

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