|
| 1 | +import { clipboard } from 'electron'; |
| 2 | +import React, { useState } from 'react'; |
| 3 | + |
| 4 | +interface OneToOneProps { |
| 5 | + fromDefault: string; |
| 6 | + fromFunc: (f: string) => string; |
| 7 | + inverseFunc: (r: string) => string; |
| 8 | +} |
| 9 | + |
| 10 | +const OneToOne = ({ fromDefault, fromFunc, inverseFunc }: OneToOneProps) => { |
| 11 | + const [from, setFrom] = useState(fromDefault); |
| 12 | + const [to, setTo] = useState(fromFunc(from)); |
| 13 | + |
| 14 | + const [fromCopied, setFromCopied] = useState(false); |
| 15 | + const [toCopied, setToCopied] = useState(false); |
| 16 | + |
| 17 | + const changeFrom = (value: string) => { |
| 18 | + setFrom(value); |
| 19 | + setTo(fromFunc(value)); |
| 20 | + }; |
| 21 | + |
| 22 | + const changeTo = (value: string) => { |
| 23 | + setTo(value); |
| 24 | + setFrom(inverseFunc(value)); |
| 25 | + }; |
| 26 | + |
| 27 | + const handleChangeFrom = (evt: { target: { value: string } }) => |
| 28 | + changeFrom(evt.target.value); |
| 29 | + |
| 30 | + const handleChangeTo = (evt: { target: { value: string } }) => |
| 31 | + changeTo(evt.target.value); |
| 32 | + |
| 33 | + const handleCopyFrom = () => { |
| 34 | + setFromCopied(true); |
| 35 | + clipboard.write({ text: from }); |
| 36 | + setTimeout(() => setFromCopied(false), 500); |
| 37 | + }; |
| 38 | + |
| 39 | + const handleCopyTo = () => { |
| 40 | + setToCopied(true); |
| 41 | + clipboard.write({ text: to }); |
| 42 | + setTimeout(() => setToCopied(false), 500); |
| 43 | + }; |
| 44 | + |
| 45 | + const handleClipboardFrom = () => { |
| 46 | + changeFrom(clipboard.readText()); |
| 47 | + }; |
| 48 | + |
| 49 | + const handleClipboardTo = () => { |
| 50 | + changeTo(clipboard.readText()); |
| 51 | + }; |
| 52 | + |
| 53 | + return ( |
| 54 | + <div className="flex flex-col min-h-full"> |
| 55 | + <div className="flex justify-between mb-1"> |
| 56 | + <section className="flex items-center justify-start space-x-2"> |
| 57 | + <button type="button" className="btn" onClick={handleClipboardFrom}> |
| 58 | + Clipboard |
| 59 | + </button> |
| 60 | + <button |
| 61 | + type="button" |
| 62 | + className="w-16 btn" |
| 63 | + onClick={handleCopyFrom} |
| 64 | + disabled={fromCopied} |
| 65 | + > |
| 66 | + {fromCopied ? 'Copied' : 'Copy'} |
| 67 | + </button> |
| 68 | + </section> |
| 69 | + <section className="flex items-center justify-start space-x-2"> |
| 70 | + <button type="button" className="btn" onClick={handleClipboardTo}> |
| 71 | + Clipboard |
| 72 | + </button> |
| 73 | + <button |
| 74 | + type="button" |
| 75 | + className="w-16 btn" |
| 76 | + onClick={handleCopyTo} |
| 77 | + disabled={toCopied} |
| 78 | + > |
| 79 | + {toCopied ? 'Copied' : 'Copy'} |
| 80 | + </button> |
| 81 | + </section> |
| 82 | + </div> |
| 83 | + <div className="flex flex-1 min-h-full space-x-2"> |
| 84 | + <textarea |
| 85 | + onChange={handleChangeFrom} |
| 86 | + className="flex-1 min-h-full p-2 bg-white rounded-md" |
| 87 | + value={from} |
| 88 | + /> |
| 89 | + <textarea |
| 90 | + onChange={handleChangeTo} |
| 91 | + className="flex-1 min-h-full p-2 bg-white rounded-md" |
| 92 | + value={to} |
| 93 | + /> |
| 94 | + </div> |
| 95 | + </div> |
| 96 | + ); |
| 97 | +}; |
| 98 | + |
| 99 | +export default OneToOne; |
0 commit comments