This repository has been archived by the owner on Mar 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add button to remove only special characters
- Loading branch information
Showing
4 changed files
with
78 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/pages/Home/components/ActionSection/RemoveSpecialCharacterAction.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { useState } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { useSetRecoilState, useRecoilValue } from 'recoil'; | ||
import { styled } from '@mui/system'; | ||
import MuiButton from '@mui/material/Button'; | ||
import Snackbar from '@mui/material/Snackbar'; | ||
import Alert from '@mui/material/Alert'; | ||
import { debounce as _debounce } from 'lodash'; | ||
import * as mainState from '~/store/atoms/main'; | ||
|
||
const Button = styled(MuiButton)(({ theme }) => ({ | ||
marginRight: theme.spacing(1), | ||
marginBottom: theme.spacing(1), | ||
})); | ||
|
||
export default function UniqueStopwordsAction() { | ||
const { t } = useTranslation(); | ||
const [snackbarOpen, setSnackbarOpen] = useState(false); | ||
const setPreviewText = useSetRecoilState(mainState['previewTextState']); | ||
const states = { | ||
shuffleText: useRecoilValue(mainState['shuffleTextState']), | ||
specialCharacters: useRecoilValue(mainState['specialCharactersState']), | ||
specialCharactersEnabled: useRecoilValue(mainState['specialCharactersEnabledState']), | ||
}; | ||
|
||
const handleClick = _debounce(() => { | ||
let oldLines = states.shuffleText.replace(/\s+$/, '').replace(/\r\n/g, '\n'); | ||
let newLines = oldLines.split('\n'); | ||
|
||
// console.log("특수 문자를 제거하는 중 입니다..."); | ||
newLines = removeSpecialCharacters(newLines, states); | ||
|
||
// console.log("작업이 완료 되었습니다."); | ||
setPreviewText(newLines.join('\n')); | ||
setSnackbarOpen(true); | ||
}, 100); | ||
|
||
return ( | ||
<> | ||
<Button variant="outlined" size="large" onClick={handleClick}> | ||
{t('Remove special character')} | ||
</Button> | ||
<Snackbar | ||
anchorOrigin={{ vertical: 'top', horizontal: 'center' }} | ||
open={snackbarOpen} | ||
autoHideDuration={1000} | ||
onClose={() => setSnackbarOpen(false)} | ||
> | ||
<Alert severity="success">{t('Special characters have been removed!')}</Alert> | ||
</Snackbar> | ||
</> | ||
); | ||
} | ||
|
||
function removeSpecialCharacters(lines, states) { | ||
const { specialCharacters, specialCharactersEnabled } = states; | ||
|
||
if (!specialCharactersEnabled) { | ||
return lines; | ||
} | ||
|
||
const escapeSpecialCharacters = specialCharacters.replace(/./g, '\\$&'); | ||
const re = new RegExp(`[${escapeSpecialCharacters}]`, 'g'); | ||
|
||
for (let i = 0, l = lines.length; i < l; i++) { | ||
lines[i] = lines[i].replace(re, ' ').replace(/\s+/g, ' ').trim(); | ||
} | ||
|
||
return lines; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters