|
| 1 | +import React, { useEffect, useState } from 'react'; |
| 2 | +import { clipboard } from 'electron'; |
| 3 | +import { loremIpsum } from 'lorem-ipsum'; |
| 4 | + |
| 5 | +type Unit = 'words' | 'sentences' | 'paragraphs'; |
| 6 | +type Format = 'html' | 'plain'; |
| 7 | + |
| 8 | +const LoremIpsum = () => { |
| 9 | + const [output, setOutput] = useState(''); |
| 10 | + const [copied, setCopied] = useState(false); |
| 11 | + const [units, setUnits] = useState<Unit>('paragraphs'); |
| 12 | + const [format, setFormat] = useState<Format>('plain'); |
| 13 | + const [count, setCount] = useState(5); |
| 14 | + |
| 15 | + const handleCopyOutput = () => { |
| 16 | + setCopied(true); |
| 17 | + clipboard.write({ text: output }); |
| 18 | + setTimeout(() => setCopied(false), 500); |
| 19 | + }; |
| 20 | + |
| 21 | + const unitsList = ['words', 'sentences', 'paragraphs']; |
| 22 | + const formatList = [ |
| 23 | + { |
| 24 | + f: 'html', |
| 25 | + l: 'HTML', |
| 26 | + }, |
| 27 | + { |
| 28 | + f: 'plain', |
| 29 | + l: 'Text', |
| 30 | + }, |
| 31 | + ]; |
| 32 | + |
| 33 | + useEffect(() => { |
| 34 | + setOutput(loremIpsum({ units, count, format })); |
| 35 | + }, [units, count, format]); |
| 36 | + |
| 37 | + return ( |
| 38 | + <div className="flex flex-col min-h-full"> |
| 39 | + <div className="flex justify-between mb-1"> |
| 40 | + <span className="flex space-x-4"> |
| 41 | + <input |
| 42 | + type="number" |
| 43 | + className="w-20 text-center" |
| 44 | + value={count} |
| 45 | + onChange={(e) => setCount(parseInt(e.target.value, 10) || 5)} |
| 46 | + /> |
| 47 | + |
| 48 | + {unitsList.map((unit) => ( |
| 49 | + <label |
| 50 | + htmlFor={unit} |
| 51 | + className="flex items-center space-x-1" |
| 52 | + key={unit} |
| 53 | + > |
| 54 | + <input |
| 55 | + type="radio" |
| 56 | + className="btn" |
| 57 | + name="unit" |
| 58 | + id={unit} |
| 59 | + checked={unit === units} |
| 60 | + onChange={() => setUnits(unit as Unit)} |
| 61 | + /> |
| 62 | + <p>{unit}</p> |
| 63 | + </label> |
| 64 | + ))} |
| 65 | + </span> |
| 66 | + |
| 67 | + <span className="flex items-center justify-center space-x-4"> |
| 68 | + {formatList.map(({ f, l }) => ( |
| 69 | + <label htmlFor={f} className="flex items-center space-x-1" key={f}> |
| 70 | + <input |
| 71 | + type="radio" |
| 72 | + className="btn" |
| 73 | + name="format" |
| 74 | + id={f} |
| 75 | + checked={f === format} |
| 76 | + onChange={() => setFormat(f as Format)} |
| 77 | + /> |
| 78 | + <p>{l}</p> |
| 79 | + </label> |
| 80 | + ))} |
| 81 | + </span> |
| 82 | + |
| 83 | + <button |
| 84 | + type="button" |
| 85 | + className="w-16 btn" |
| 86 | + onClick={handleCopyOutput} |
| 87 | + disabled={copied} |
| 88 | + > |
| 89 | + {copied ? 'Copied' : 'Copy'} |
| 90 | + </button> |
| 91 | + </div> |
| 92 | + <div className="flex flex-1 min-h-full space-x-2"> |
| 93 | + <textarea |
| 94 | + className="flex-1 min-h-full p-2 bg-gray-100 rounded-md" |
| 95 | + value={output} |
| 96 | + readOnly |
| 97 | + /> |
| 98 | + </div> |
| 99 | + </div> |
| 100 | + ); |
| 101 | +}; |
| 102 | + |
| 103 | +export default LoremIpsum; |
0 commit comments