ConflictDiffModal.tsx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import React, { useState, useEffect, FC } from 'react';
  2. import PropTypes from 'prop-types';
  3. import {
  4. Modal, ModalHeader, ModalBody, ModalFooter,
  5. } from 'reactstrap';
  6. import CodeMirror from 'codemirror/lib/codemirror';
  7. import { useTranslation } from 'react-i18next';
  8. require('codemirror/lib/codemirror.css');
  9. require('codemirror/addon/merge/merge');
  10. require('codemirror/addon/merge/merge.css');
  11. const DMP = require('diff_match_patch');
  12. Object.keys(DMP).forEach((key) => { window[key] = DMP[key] });
  13. type ConflictDiffModalProps = {
  14. isOpen: boolean | null;
  15. onCancel: (() => void) | null;
  16. onResolveConflict: (() => void) | null;
  17. };
  18. export const ConflictDiffModal: FC<ConflictDiffModalProps> = (props) => {
  19. const [val, setVal] = useState('value 1');
  20. const [orig, setOrig] = useState('value 2');
  21. const [codeMirrorRef, setCodeMirrorRef] = useState<HTMLDivElement | null>(null);
  22. const { t } = useTranslation('');
  23. useEffect(() => {
  24. if (codeMirrorRef) {
  25. CodeMirror.MergeView(codeMirrorRef, {
  26. value: val,
  27. origLeft: orig,
  28. origRight: 'test!',
  29. connect: 'align',
  30. lineNumbers: true,
  31. collapseIdentical: true,
  32. highlightDifferences: true,
  33. allowEditingOriginals: false,
  34. });
  35. }
  36. }, [codeMirrorRef, orig, val]);
  37. const onCancel = () => {
  38. if (props.onCancel != null) {
  39. props.onCancel();
  40. }
  41. };
  42. const onResolveConflict = () => {
  43. if (props.onResolveConflict != null) {
  44. props.onResolveConflict();
  45. }
  46. };
  47. return (
  48. <Modal isOpen={props.isOpen || false} toggle={onCancel} className="modal-gfm-cheatsheet">
  49. <ModalHeader tag="h4" toggle={onCancel} className="bg-primary text-light">
  50. <i className="icon-fw icon-exclamation" />{t('modal_resolve_conflict.resolve_conflict')}
  51. </ModalHeader>
  52. <ModalBody>
  53. <div ref={(el) => { setCodeMirrorRef(el) }}></div>
  54. <div className="bg-danger">body test</div>
  55. </ModalBody>
  56. <ModalFooter>
  57. <button
  58. type="button"
  59. className="btn btn-outline-secondary"
  60. onClick={onCancel}
  61. >
  62. {t('Cancel')}
  63. </button>
  64. <button
  65. type="button"
  66. className="btn btn-outline-primary ml-3"
  67. onClick={onResolveConflict}
  68. >
  69. {t('modal_resolve_conflict.resolve_and_save')}
  70. </button>
  71. </ModalFooter>
  72. </Modal>
  73. );
  74. };
  75. ConflictDiffModal.propTypes = {
  76. isOpen: PropTypes.bool,
  77. onCancel: PropTypes.func,
  78. onResolveConflict: PropTypes.func,
  79. };
  80. ConflictDiffModal.defaultProps = {
  81. isOpen: false,
  82. };