Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

优化了一下文本框的复制功能 #95

Merged
merged 2 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/components/TextFieldWithClean/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Box, TextField, OutlinedTextFieldProps } from '@mui/material';
import React from 'react';
import DeleteOutlineIcon from '@mui/icons-material/DeleteOutline';

interface CustomTextFieldProps extends OutlinedTextFieldProps {
onClean: React.MouseEventHandler<HTMLDivElement>;
}

const TextFieldWithClean: React.FC<CustomTextFieldProps> = (props) => {
const { onClean, ...otherProps } = props;

return (
<Box sx={{ position: 'relative', width: '100%' }}>
<TextField
{...otherProps}
sx={{ width: '100%', ...otherProps.sx }}
variant='outlined'
/>
<Box
sx={{
position: 'absolute',
width: '32px',
height: '32px',
right: '4px',
top: '4px',
paddingTop: '6px',
textAlign: 'center',
bgcolor: 'rgba(0,0,0,0.1)',
display: otherProps.value ? '' : 'none',
borderRadius: '50%',
cursor: 'pointer',
color: '#fff',
'&:hover': {
bgcolor: 'rgba(0,0,0,0.25)',
},
}}
>
<Box onClick={onClean}>
<DeleteOutlineIcon fontSize='small' />
</Box>
</Box>
</Box>
);
};

export default TextFieldWithClean;
51 changes: 51 additions & 0 deletions src/components/TextFieldWithCopy/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Box, TextField, OutlinedTextFieldProps } from '@mui/material';
import React, { useCallback } from 'react';
import CopyToClipboard from 'react-copy-to-clipboard';
import ContentCopyIcon from '@mui/icons-material/ContentCopy';
import alert from '@/components/Alert';

const TextFieldWithCopy: React.FC<OutlinedTextFieldProps> = (props) => {
const { ...otherProps } = props;

const handleCopyClick = useCallback(() => {
alert.success('已复制到剪切板');
}, []);

return (
<Box sx={{ position: 'relative', width: '100%' }}>
<TextField
{...otherProps}
sx={{ width: '100%', ...otherProps.sx }}
variant='outlined'
/>
<Box
sx={{
position: 'absolute',
width: '30px',
height: '30px',
right: '4px',
top: '4px',
paddingTop: '6px',
textAlign: 'center',
bgcolor: 'rgba(0,0,0,0.1)',
display: otherProps.value ? '' : 'none',
borderRadius: '50%',
cursor: 'pointer',
color: '#fff',
'&:hover': {
bgcolor: 'rgba(0,0,0,0.25)',
},
}}
>
<CopyToClipboard
text={otherProps.value as string}
onCopy={handleCopyClick}
>
<ContentCopyIcon fontSize='small' />
</CopyToClipboard>
</Box>
</Box>
);
};

export default TextFieldWithCopy;
54 changes: 20 additions & 34 deletions src/pages/3des.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import MenuView from '@/components/MainContent';
import TextFieldWithClean from '@/components/TextFieldWithClean';
import TextFieldWithCopy from '@/components/TextFieldWithCopy';
import TabContext from '@mui/lab/TabContext';
import TabList from '@mui/lab/TabList';
import {
Box,
FormControlLabel,
FormLabel,
InputAdornment,
Radio,
RadioGroup,
Stack,
Tab,
TextField,
Typography,
} from '@mui/material';
import crypto from 'crypto-js';
import React, { useCallback } from 'react';
Expand Down Expand Up @@ -97,7 +96,7 @@ const TripleDES: React.FC = () => {
let k = crypto.enc.Utf8.parse(key);

console.log(src, key, mode, padding, encoding, iv);

let result;
if (method === 'encrypt') {
let plaintext = crypto.enc.Utf8.parse(src);
Expand Down Expand Up @@ -190,6 +189,12 @@ const TripleDES: React.FC = () => {
[]
);

const handleCleanSrcClick = useCallback(() => {
setSrc('');
setKey('');
setIV('');
}, []);

return (
<MenuView>
<>
Expand All @@ -209,43 +214,27 @@ const TripleDES: React.FC = () => {
</TabList>
</Box>
</TabContext>
<TextField
<TextFieldWithClean
size='small'
value={key}
label={(method === 'encrypt' ? '加密' : '解密') + '密钥'}
variant='outlined'
onChange={onKeyChange}
onClean={handleCleanSrcClick}
placeholder='建议为 24 位字符'
InputProps={{
startAdornment: (
<InputAdornment
position='start'
sx={{ width: '100px', fontFamily: 'Mono' }}
>
{method === 'encrypt' ? '加密' : '解密'}密钥
</InputAdornment>
),
}}
sx={{ input: { fontSize: '14px', fontFamily: 'Mono' } }}
/>
{mode !== 'ecb' ? (
<TextField
<TextFieldWithClean
size='small'
value={iv}
label={mode === 'ctr' ? 'Nonce' : 'IV'}
variant='outlined'
onChange={onIVChange}
onClean={handleCleanSrcClick}
placeholder={
mode === 'ctr' ? '建议为 8 位字符的 Nonce' : '建议为 8 位字符'
}
InputProps={{
startAdornment: (
<InputAdornment
position='start'
sx={{ width: '100px', fontFamily: 'Mono' }}
>
{mode === 'ctr' ? 'Nonce' : 'IV'}
</InputAdornment>
),
}}
sx={{ input: { fontSize: '14px', fontFamily: 'Mono' } }}
/>
) : (
Expand Down Expand Up @@ -327,23 +316,20 @@ const TripleDES: React.FC = () => {
<FormControlLabel value='Hex' control={<Radio />} label='Hex' />
</RadioGroup>
</Stack>
<Typography sx={{ marginTop: '10px' }}>
{method === 'encrypt' ? '明文' : '密文'}
</Typography>
<TextField
<TextFieldWithClean
value={src}
variant='outlined'
label={method === 'encrypt' ? '明文' : '密文'}
multiline
rows={3}
onClean={handleCleanSrcClick}
onChange={onSrcChange}
sx={{ textarea: { fontSize: '14px', fontFamily: 'Mono' } }}
/>
<Typography sx={{ marginTop: '10px' }}>
{method === 'encrypt' ? '密文' : '明文'}
</Typography>
<TextField
<TextFieldWithCopy
value={dst}
variant='outlined'
label={method === 'encrypt' ? '密文' : '明文'}
multiline
rows={3}
onChange={onDstChange}
Expand Down
62 changes: 27 additions & 35 deletions src/pages/aes.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import MainContent from '@/components/MainContent';
import TextFieldWithClean from '@/components/TextFieldWithClean';
import TextFieldWithCopy from '@/components/TextFieldWithCopy';
import TabContext from '@mui/lab/TabContext';
import TabList from '@mui/lab/TabList';
import {
Box,
FormControlLabel,
FormLabel,
InputAdornment,
Radio,
RadioGroup,
Stack,
Tab,
TextField,
Typography,
} from '@mui/material';
import crypto from 'crypto-js';
import React, { useCallback } from 'react';
Expand Down Expand Up @@ -187,6 +186,12 @@ const AES: React.FC = () => {
[]
);

const handleCleanSrcClick = useCallback(() => {
setSrc('');
setKey('');
setIV('');
}, []);

return (
<MainContent>
<>
Expand All @@ -206,43 +211,27 @@ const AES: React.FC = () => {
</TabList>
</Box>
</TabContext>
<TextField
<TextFieldWithClean
size='small'
value={key}
label={(method === 'encrypt' ? '加密' : '解密') + '密钥'}
variant='outlined'
onChange={onKeyChange}
onClean={handleCleanSrcClick}
placeholder='建议为 16、24或32 位字符'
InputProps={{
startAdornment: (
<InputAdornment
position='start'
sx={{ width: '100px', fontFamily: 'Mono' }}
>
{method === 'encrypt' ? '加密' : '解密'}密钥
</InputAdornment>
),
}}
sx={{ input: { fontSize: '14px', fontFamily: 'Mono' } }}
/>
{mode !== 'ecb' ? (
<TextField
<TextFieldWithClean
size='small'
value={iv}
label={mode === 'ctr' ? 'Nonce' : 'IV'}
variant='outlined'
onChange={onIVChange}
onClean={handleCleanSrcClick}
placeholder={
mode === 'ctr' ? '建议为 16 位字符的 Nonce' : '建议为 16 位字符'
}
InputProps={{
startAdornment: (
<InputAdornment
position='start'
sx={{ width: '100px', fontFamily: 'Mono' }}
>
{mode === 'ctr' ? 'Nonce' : 'IV'}
</InputAdornment>
),
}}
sx={{ input: { fontSize: '14px', fontFamily: 'Mono' } }}
/>
) : (
Expand Down Expand Up @@ -324,28 +313,31 @@ const AES: React.FC = () => {
<FormControlLabel value='Hex' control={<Radio />} label='Hex' />
</RadioGroup>
</Stack>
<Typography sx={{ marginTop: '10px' }}>
{method === 'encrypt' ? '明文' : '密文'}
</Typography>
<TextField
<TextFieldWithClean
label={method === 'encrypt' ? '明文' : '密文'}
value={src}
variant='outlined'
multiline
rows={3}
onChange={onSrcChange}
sx={{ textarea: { fontSize: '14px', fontFamily: 'Mono' } }}
onClean={handleCleanSrcClick}
sx={{
width: '100%',
textarea: { fontSize: '14px', fontFamily: 'Mono' },
}}
/>
<Typography sx={{ marginTop: '10px' }}>
{method === 'encrypt' ? '密文' : '明文'}
</Typography>
<TextField
<TextFieldWithCopy
value={dst}
label={method === 'encrypt' ? '密文' : '明文'}
variant='outlined'
multiline
rows={3}
onChange={onDstChange}
InputProps={{ readOnly: true }}
sx={{ textarea: { fontSize: '14px', fontFamily: 'Mono' } }}
sx={{
width: '100%',
textarea: { fontSize: '14px', fontFamily: 'Mono' },
}}
/>
</>
</MainContent>
Expand Down
Loading