|
| 1 | +/* eslint-disable react/no-danger */ |
| 2 | +import React, { useEffect, useState } from 'react'; |
| 3 | +import { clipboard } from 'electron'; |
| 4 | +import { Change } from 'diff'; |
| 5 | +import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; |
| 6 | +import { camelToText } from '../../helpers/stringUtils'; |
| 7 | + |
| 8 | +const DiffLib = require('diff'); |
| 9 | + |
| 10 | +const TextDiff = () => { |
| 11 | + const [left, setLeft] = useState('Left text'); |
| 12 | + const [right, setRight] = useState('Right text'); |
| 13 | + const [diff, setDiff] = useState(''); |
| 14 | + const [diffCount, setDiffCount] = useState(0); |
| 15 | + const [diffType, setDiffType] = useState('diffChars'); |
| 16 | + |
| 17 | + const handleChangeLeft = (evt: { target: { value: string } }) => |
| 18 | + setLeft(evt.target.value); |
| 19 | + |
| 20 | + const handleChangeRight = (evt: { target: { value: string } }) => |
| 21 | + setRight(evt.target.value); |
| 22 | + |
| 23 | + const handleClipboardLeft = () => { |
| 24 | + setLeft(clipboard.readText()); |
| 25 | + }; |
| 26 | + |
| 27 | + const handleClipboardRight = () => { |
| 28 | + setRight(clipboard.readText()); |
| 29 | + }; |
| 30 | + |
| 31 | + const handleSwap = () => { |
| 32 | + setRight(left); |
| 33 | + setLeft(right); |
| 34 | + }; |
| 35 | + |
| 36 | + const diffTypes = ['diffChars', 'diffWords', 'diffLines']; |
| 37 | + |
| 38 | + useEffect(() => { |
| 39 | + const diffFunc = DiffLib[diffType]; |
| 40 | + const changes = diffFunc(left, right); |
| 41 | + let c = 0; |
| 42 | + |
| 43 | + const d = changes |
| 44 | + .map((part: Change) => { |
| 45 | + // eslint-disable-next-line no-nested-ternary |
| 46 | + const cl = part.added |
| 47 | + ? 'text-green-600' |
| 48 | + : part.removed |
| 49 | + ? 'text-red-600' |
| 50 | + : 'text-grey-600'; |
| 51 | + c += part.added || part.removed ? 1 : 0; |
| 52 | + return `<span class="${cl}">${part.value}</span>`; |
| 53 | + }) |
| 54 | + .join(''); |
| 55 | + |
| 56 | + setDiffCount(c); |
| 57 | + setDiff(d); |
| 58 | + }, [left, right, diffType]); |
| 59 | + |
| 60 | + return ( |
| 61 | + <div className="min-h-full flex flex-col"> |
| 62 | + <div className="flex justify-between mb-1"> |
| 63 | + <span className="flex space-x-2"> |
| 64 | + <button type="button" className="btn" onClick={handleClipboardLeft}> |
| 65 | + Clipboard |
| 66 | + </button> |
| 67 | + </span> |
| 68 | + <span className="flex space-x-2"> |
| 69 | + <button type="button" className="btn" onClick={handleSwap}> |
| 70 | + <FontAwesomeIcon icon="exchange-alt" /> |
| 71 | + </button> |
| 72 | + </span> |
| 73 | + <span className="flex space-x-2"> |
| 74 | + <button type="button" className="btn" onClick={handleClipboardRight}> |
| 75 | + Clipboard |
| 76 | + </button> |
| 77 | + </span> |
| 78 | + </div> |
| 79 | + <section className="flex flex-1 flex-col space-y-2 min-h-full"> |
| 80 | + <div className="flex min-h-full flex-1 space-x-2"> |
| 81 | + <textarea |
| 82 | + onChange={handleChangeLeft} |
| 83 | + className="flex-1 min-h-full bg-white p-4 rounded-md" |
| 84 | + value={left} |
| 85 | + /> |
| 86 | + <textarea |
| 87 | + onChange={handleChangeRight} |
| 88 | + className="flex-1 min-h-full bg-white p-4 rounded-md" |
| 89 | + value={right} |
| 90 | + /> |
| 91 | + </div> |
| 92 | + |
| 93 | + <div className="flex flex-0 space-x-4 items-center justify-between"> |
| 94 | + <div className="flex flex-0 space-x-4 items-center"> |
| 95 | + {diffTypes.map((dt) => ( |
| 96 | + <label |
| 97 | + htmlFor={dt} |
| 98 | + className="flex items-center space-x-1" |
| 99 | + key={dt} |
| 100 | + > |
| 101 | + <input |
| 102 | + type="radio" |
| 103 | + className="btn" |
| 104 | + name="diffType" |
| 105 | + id={dt} |
| 106 | + checked={diffType === dt} |
| 107 | + onChange={() => setDiffType(dt)} |
| 108 | + /> |
| 109 | + <p>{camelToText(dt)}</p> |
| 110 | + </label> |
| 111 | + ))} |
| 112 | + </div> |
| 113 | + <p> |
| 114 | + {diffCount} change{diffCount === 1 ? '' : 's'} |
| 115 | + </p> |
| 116 | + </div> |
| 117 | + |
| 118 | + <section |
| 119 | + className="w-full min-h-full bg-gray-100 p-4 flex-1 rounded-md" |
| 120 | + dangerouslySetInnerHTML={{ __html: diff }} |
| 121 | + /> |
| 122 | + </section> |
| 123 | + </div> |
| 124 | + ); |
| 125 | +}; |
| 126 | + |
| 127 | +export default TextDiff; |
0 commit comments