TextDiff.tsx 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { Tooltip, Typography } from "antd";
  2. import { type Change, diffChars } from "diff"
  3. const { Text } = Typography;
  4. interface IWidget {
  5. content?: string | null;
  6. oldContent?: string | null;
  7. showToolTip?: boolean;
  8. }
  9. const TextDiffWidget = ({
  10. content,
  11. oldContent,
  12. showToolTip = true,
  13. }: IWidget) => {
  14. if (content) {
  15. if (oldContent) {
  16. const diff: Change[] = diffChars(oldContent, content);
  17. const diffResult = diff.map((item, id) => {
  18. return (
  19. <Text
  20. key={id}
  21. type={
  22. item.added ? "success" : item.removed ? "danger" : "secondary"
  23. }
  24. delete={item.removed ? true : undefined}
  25. >
  26. {item.value}
  27. </Text>
  28. );
  29. });
  30. return showToolTip ? (
  31. <Tooltip title={content}>
  32. <div>{diffResult}</div>
  33. </Tooltip>
  34. ) : (
  35. <div> {diffResult}</div>
  36. );
  37. } else {
  38. return <Text>{content}</Text>;
  39. }
  40. } else {
  41. return <></>;
  42. }
  43. };
  44. export default TextDiffWidget;