2024-06-23 18:59:58 +01:00
|
|
|
import { Box, Grid, Stack, Typography } from '@mui/material';
|
|
|
|
|
import React, { useContext, useEffect, useState } from 'react';
|
|
|
|
|
import { Formik, useFormikContext } from 'formik';
|
2024-06-23 00:47:12 -07:00
|
|
|
import * as Yup from 'yup';
|
|
|
|
|
import ToolTextInput from '../../../components/input/ToolTextInput';
|
|
|
|
|
import ToolTextResult from '../../../components/result/ToolTextResult';
|
2024-06-23 15:14:14 +01:00
|
|
|
import ToolOptions from '../../../components/options/ToolOptions';
|
2024-06-23 00:47:12 -07:00
|
|
|
import { mergeText } from './service';
|
|
|
|
|
import { CustomSnackBarContext } from '../../../contexts/CustomSnackBarContext';
|
2024-06-23 15:14:14 +01:00
|
|
|
import TextFieldWithDesc from '../../../components/options/TextFieldWithDesc';
|
|
|
|
|
import CheckboxWithDesc from '../../../components/options/CheckboxWithDesc';
|
2024-06-25 02:07:57 +01:00
|
|
|
import ToolOptionGroups from '../../../components/options/ToolOptionGroups';
|
2024-06-23 00:47:12 -07:00
|
|
|
|
|
|
|
|
const initialValues = {
|
2024-06-23 02:41:46 -07:00
|
|
|
joinCharacter: '',
|
2024-06-23 00:47:12 -07:00
|
|
|
deleteBlank: true,
|
|
|
|
|
deleteTrailing: true
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const validationSchema = Yup.object().shape({
|
|
|
|
|
joinCharacter: Yup.string().required('Join character is required'),
|
|
|
|
|
deleteBlank: Yup.boolean().required('Delete blank is required'),
|
|
|
|
|
deleteTrailing: Yup.boolean().required('Delete trailing is required')
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const mergeOptions = {
|
2024-06-23 02:41:46 -07:00
|
|
|
placeholder: 'Join Character',
|
2024-06-23 00:47:12 -07:00
|
|
|
description:
|
|
|
|
|
'Symbol that connects broken\n' + 'pieces of text. (Space by default.)\n',
|
|
|
|
|
accessor: 'joinCharacter' as keyof typeof initialValues
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const blankTrailingOptions: {
|
|
|
|
|
title: string;
|
|
|
|
|
description: string;
|
|
|
|
|
accessor: keyof typeof initialValues;
|
|
|
|
|
}[] = [
|
|
|
|
|
{
|
|
|
|
|
title: 'Delete Blank Lines',
|
2024-06-23 02:41:46 -07:00
|
|
|
description: "Delete lines that don't have\n text symbols.\n",
|
2024-06-23 00:47:12 -07:00
|
|
|
accessor: 'deleteBlank'
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
title: 'Delete Trailing Spaces',
|
2024-06-23 02:41:46 -07:00
|
|
|
description: 'Remove spaces and tabs at\n the end of the lines.\n',
|
2024-06-23 00:47:12 -07:00
|
|
|
accessor: 'deleteTrailing'
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
export default function JoinText() {
|
|
|
|
|
const [input, setInput] = useState<string>('');
|
|
|
|
|
const { showSnackBar } = useContext(CustomSnackBarContext);
|
|
|
|
|
const [result, setResult] = useState<string>('');
|
|
|
|
|
|
|
|
|
|
const FormikListenerComponent = ({ input }: { input: string }) => {
|
|
|
|
|
const { values } = useFormikContext<typeof initialValues>();
|
|
|
|
|
const { joinCharacter, deleteBlank, deleteTrailing } = values;
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
try {
|
|
|
|
|
setResult(mergeText(input, deleteBlank, deleteTrailing, joinCharacter));
|
|
|
|
|
} catch (exception: unknown) {
|
|
|
|
|
if (exception instanceof Error)
|
|
|
|
|
showSnackBar(exception.message, 'error');
|
|
|
|
|
}
|
|
|
|
|
}, [values, input]);
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Box>
|
|
|
|
|
<Grid container spacing={2}>
|
|
|
|
|
<Grid item xs={6}>
|
|
|
|
|
<ToolTextInput
|
|
|
|
|
title={'Text Pieces'}
|
|
|
|
|
value={input}
|
|
|
|
|
onChange={setInput}
|
|
|
|
|
/>
|
|
|
|
|
</Grid>
|
|
|
|
|
<Grid item xs={6}>
|
|
|
|
|
<ToolTextResult title={'Joined Text'} value={result} />
|
|
|
|
|
</Grid>
|
|
|
|
|
</Grid>
|
|
|
|
|
<ToolOptions>
|
|
|
|
|
<Formik
|
|
|
|
|
initialValues={initialValues}
|
|
|
|
|
validationSchema={validationSchema}
|
|
|
|
|
onSubmit={() => {}}
|
|
|
|
|
>
|
|
|
|
|
{({ setFieldValue, values }) => (
|
|
|
|
|
<Stack direction={'row'} spacing={2}>
|
|
|
|
|
<FormikListenerComponent input={input} />
|
2024-06-25 02:07:57 +01:00
|
|
|
<ToolOptionGroups
|
|
|
|
|
groups={[
|
|
|
|
|
{
|
|
|
|
|
title: 'Text Merged Options',
|
|
|
|
|
component: (
|
|
|
|
|
<TextFieldWithDesc
|
|
|
|
|
placeholder={mergeOptions.placeholder}
|
|
|
|
|
value={values['joinCharacter']}
|
|
|
|
|
onChange={(value) =>
|
|
|
|
|
setFieldValue(mergeOptions.accessor, value)
|
|
|
|
|
}
|
|
|
|
|
description={mergeOptions.description}
|
|
|
|
|
/>
|
|
|
|
|
)
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
title: 'Blank Lines and Trailing Spaces',
|
|
|
|
|
component: blankTrailingOptions.map((option) => (
|
|
|
|
|
<CheckboxWithDesc
|
|
|
|
|
key={option.accessor}
|
|
|
|
|
title={option.title}
|
|
|
|
|
checked={!!values[option.accessor]}
|
|
|
|
|
onChange={(value) =>
|
|
|
|
|
setFieldValue(option.accessor, value)
|
|
|
|
|
}
|
|
|
|
|
description={option.description}
|
|
|
|
|
/>
|
|
|
|
|
))
|
2024-06-23 02:41:46 -07:00
|
|
|
}
|
2024-06-25 02:07:57 +01:00
|
|
|
]}
|
|
|
|
|
/>
|
2024-06-23 00:47:12 -07:00
|
|
|
</Stack>
|
|
|
|
|
)}
|
|
|
|
|
</Formik>
|
|
|
|
|
</ToolOptions>
|
|
|
|
|
</Box>
|
|
|
|
|
);
|
|
|
|
|
}
|