|
| 1 | +import React, { useState, useCallback, useEffect } from 'react'; |
| 2 | +import { Link } from 'react-router-dom'; |
| 3 | +import { ArrowLeftIcon } from '@phosphor-icons/react'; |
| 4 | +import colors from '../../config/colors'; |
| 5 | +import useSeo from "../../hooks/useSeo"; |
| 6 | +import { diff_match_patch } from 'diff-match-patch'; |
| 7 | + |
| 8 | +const dmp = new diff_match_patch(); |
| 9 | + |
| 10 | +function TextDiffCheckerPage() { |
| 11 | + useSeo({ |
| 12 | + title: 'Text Diff Checker | Fezcodex', |
| 13 | + description: 'Compare two texts and highlight the differences (additions, deletions, changes).', |
| 14 | + keywords: ['Fezcodex', 'text diff', 'diff checker', 'text comparison', 'code diff'], |
| 15 | + ogTitle: 'Text Diff Checker | Fezcodex', |
| 16 | + ogDescription: 'Compare two texts and highlight the differences (additions, deletions, changes).', |
| 17 | + ogImage: 'https://fezcode.github.io/logo512.png', |
| 18 | + twitterCard: 'summary_large_image', |
| 19 | + twitterTitle: 'Text Diff Checker | Fezcodex', |
| 20 | + twitterDescription: 'Compare two texts and highlight the differences (additions, deletions, changes).', |
| 21 | + twitterImage: 'https://fezcode.github.io/logo512.png' |
| 22 | + }); |
| 23 | + |
| 24 | + const [textA, setTextA] = useState(''); |
| 25 | + const [textB, setTextB] = useState(''); |
| 26 | + const [diffOutput, setDiffOutput] = useState([]); |
| 27 | + |
| 28 | + const computeDiff = useCallback(() => { |
| 29 | + if (!textA && !textB) { |
| 30 | + setDiffOutput([]); |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + const diffs = dmp.diff_main(textA, textB); |
| 35 | + dmp.diff_cleanupSemantic(diffs); // Optional: improve human readability |
| 36 | + setDiffOutput(diffs); |
| 37 | + }, [textA, textB]); |
| 38 | + |
| 39 | + useEffect(() => { |
| 40 | + // Automatically compute diff when texts change |
| 41 | + computeDiff(); |
| 42 | + }, [textA, textB, computeDiff]); |
| 43 | + |
| 44 | + const renderDiff = () => { |
| 45 | + return diffOutput.map((part, i) => { |
| 46 | + const [type, text] = part; |
| 47 | + let style = {}; |
| 48 | + switch (type) { |
| 49 | + case DIFF_INSERT: |
| 50 | + style = { backgroundColor: '#aaffaa', color: '#000' }; // Green for additions |
| 51 | + break; |
| 52 | + case DIFF_DELETE: |
| 53 | + style = { backgroundColor: '#ffaaaa', color: '#000', textDecoration: 'line-through' }; // Red for deletions |
| 54 | + break; |
| 55 | + case DIFF_EQUAL: |
| 56 | + default: |
| 57 | + style = { color: colors['app-light'] }; // Default for unchanged |
| 58 | + break; |
| 59 | + } |
| 60 | + return <span key={i} style={style}>{text.split('\n').map((item, key) => { |
| 61 | + return ( |
| 62 | + <React.Fragment key={key}> |
| 63 | + {item} |
| 64 | + {key < text.split('\n').length - 1 && <br />} |
| 65 | + </React.Fragment> |
| 66 | + ); |
| 67 | + })}</span>; |
| 68 | + }); |
| 69 | + }; |
| 70 | + |
| 71 | + const DIFF_DELETE = -1; |
| 72 | + const DIFF_INSERT = 1; |
| 73 | + const DIFF_EQUAL = 0; |
| 74 | + |
| 75 | + const cardStyle = { |
| 76 | + backgroundColor: colors['app-alpha-10'], |
| 77 | + borderColor: colors['app-alpha-50'], |
| 78 | + color: colors.app, |
| 79 | + }; |
| 80 | + |
| 81 | + const textareaStyle = `w-full h-48 p-4 bg-gray-900/50 font-mono resize-y border rounded-md border-app-alpha-50 text-app-light focus:ring-0`; |
| 82 | + |
| 83 | + return ( |
| 84 | + <div className="py-16 sm:py-24"> |
| 85 | + <div className="mx-auto max-w-7xl px-6 lg:px-8 text-gray-300"> |
| 86 | + <Link |
| 87 | + to="/apps" |
| 88 | + className="text-article hover:underline flex items-center justify-center gap-2 text-lg mb-4" |
| 89 | + > |
| 90 | + <ArrowLeftIcon size={24} /> Back to Apps |
| 91 | + </Link> |
| 92 | + <h1 className="text-4xl font-bold tracking-tight sm:text-6xl mb-4 flex items-center"> |
| 93 | + <span className="codex-color">fc</span> |
| 94 | + <span className="separator-color">::</span> |
| 95 | + <span className="apps-color">apps</span> |
| 96 | + <span className="separator-color">::</span> |
| 97 | + <span className="single-app-color">tdc</span> |
| 98 | + </h1> |
| 99 | + <hr className="border-gray-700" /> |
| 100 | + <div className="flex justify-center items-center mt-16"> |
| 101 | + <div |
| 102 | + className="group bg-transparent border rounded-lg shadow-2xl p-6 flex flex-col justify-between relative overflow-hidden h-full w-full max-w-6xl" |
| 103 | + style={cardStyle} |
| 104 | + > |
| 105 | + <div |
| 106 | + className="absolute top-0 left-0 w-full h-full opacity-10" |
| 107 | + style={{ |
| 108 | + backgroundImage: |
| 109 | + 'radial-gradient(circle, white 1px, transparent 1px)', |
| 110 | + backgroundSize: '10px 10px', |
| 111 | + }} |
| 112 | + ></div> |
| 113 | + <div className="relative z-10 p-1"> |
| 114 | + <h1 className="text-3xl font-arvo font-normal mb-4 text-app"> Text Diff Checker </h1> |
| 115 | + <hr className="border-gray-700 mb-4" /> |
| 116 | + |
| 117 | + <div className="grid grid-cols-1 md:grid-cols-2 gap-4 mb-6"> |
| 118 | + <div> |
| 119 | + <label htmlFor="textA" className="block text-lg font-semibold mb-2 text-app">Original Text (A)</label> |
| 120 | + <textarea |
| 121 | + id="textA" |
| 122 | + value={textA} |
| 123 | + onChange={(e) => setTextA(e.target.value)} |
| 124 | + placeholder="Enter original text here..." |
| 125 | + className={textareaStyle} |
| 126 | + /> |
| 127 | + </div> |
| 128 | + <div> |
| 129 | + <label htmlFor="textB" className="block text-lg font-semibold mb-2 text-app">New Text (B)</label> |
| 130 | + <textarea |
| 131 | + id="textB" |
| 132 | + value={textB} |
| 133 | + onChange={(e) => setTextB(e.target.value)} |
| 134 | + placeholder="Enter new text here..." |
| 135 | + className={textareaStyle} |
| 136 | + /> |
| 137 | + </div> |
| 138 | + </div> |
| 139 | + |
| 140 | + <div className="flex justify-center mb-6"> |
| 141 | + <button |
| 142 | + onClick={computeDiff} |
| 143 | + className="px-6 py-2 rounded-md text-lg font-arvo font-normal transition-colors duration-300 ease-in-out border bg-tb text-app border-app-alpha-50 hover:bg-app/15" |
| 144 | + > |
| 145 | + Compare Texts |
| 146 | + </button> |
| 147 | + </div> |
| 148 | + |
| 149 | + <div className="mt-4"> |
| 150 | + <label className="block text-lg font-semibold mb-2 text-app">Differences</label> |
| 151 | + <div className="w-full p-4 bg-gray-800/50 font-mono resize-y border rounded-md border-app-alpha-50 text-app-light overflow-auto" style={{ minHeight: '100px' }}> |
| 152 | + {diffOutput.length > 0 ? renderDiff() : <p className="text-gray-500">No differences or empty input.</p>} |
| 153 | + </div> |
| 154 | + </div> |
| 155 | + </div> |
| 156 | + </div> |
| 157 | + </div> |
| 158 | + </div> |
| 159 | + </div> |
| 160 | + ); |
| 161 | +} |
| 162 | + |
| 163 | +export default TextDiffCheckerPage; |
0 commit comments