2024-06-21 20:57:05 +01:00
|
|
|
import { Box, Stack, TextField } from '@mui/material';
|
|
|
|
|
import Grid from '@mui/material/Grid';
|
|
|
|
|
import Typography from '@mui/material/Typography';
|
2024-06-21 22:35:56 +01:00
|
|
|
import React, { useContext, useEffect, useRef, useState } from 'react';
|
2024-06-21 20:57:05 +01:00
|
|
|
import ToolTextInput from '../../../components/input/ToolTextInput';
|
|
|
|
|
import ToolTextResult from '../../../components/result/ToolTextResult';
|
2024-06-21 21:59:17 +01:00
|
|
|
import { Field, Formik, FormikProps, useFormikContext } from 'formik';
|
2024-06-21 20:57:05 +01:00
|
|
|
import * as Yup from 'yup';
|
2024-06-23 15:14:14 +01:00
|
|
|
import ToolOptions from '../../../components/options/ToolOptions';
|
2024-06-22 03:29:34 +01:00
|
|
|
import { compute, SplitOperatorType } from './service';
|
2024-06-21 22:35:56 +01:00
|
|
|
import { CustomSnackBarContext } from '../../../contexts/CustomSnackBarContext';
|
2024-06-23 15:14:14 +01:00
|
|
|
import RadioWithTextField from '../../../components/options/RadioWithTextField';
|
|
|
|
|
import TextFieldWithDesc from '../../../components/options/TextFieldWithDesc';
|
2024-06-19 21:18:35 +01:00
|
|
|
|
2024-06-21 20:06:07 +01:00
|
|
|
const initialValues = {
|
2024-06-21 21:59:17 +01:00
|
|
|
splitSeparatorType: 'symbol' as SplitOperatorType,
|
2024-06-21 20:24:00 +01:00
|
|
|
symbolValue: ' ',
|
|
|
|
|
regexValue: '/\\s+/',
|
|
|
|
|
lengthValue: '16',
|
2024-06-21 20:57:05 +01:00
|
|
|
chunksValue: '4',
|
|
|
|
|
|
|
|
|
|
outputSeparator: '\\n',
|
|
|
|
|
charBeforeChunk: '',
|
|
|
|
|
charAfterChunk: ''
|
|
|
|
|
};
|
|
|
|
|
const splitOperators: {
|
2024-06-21 20:06:07 +01:00
|
|
|
title: string;
|
|
|
|
|
description: string;
|
2024-06-21 20:57:05 +01:00
|
|
|
type: SplitOperatorType;
|
|
|
|
|
}[] = [
|
|
|
|
|
{
|
|
|
|
|
title: 'Use a Symbol for Splitting',
|
|
|
|
|
description:
|
|
|
|
|
'Character that will be used to\n' +
|
|
|
|
|
'break text into parts.\n' +
|
|
|
|
|
'(Space by default.)',
|
|
|
|
|
type: 'symbol'
|
|
|
|
|
},
|
2024-06-21 20:06:07 +01:00
|
|
|
{
|
|
|
|
|
title: 'Use a Regex for Splitting',
|
|
|
|
|
type: 'regex',
|
2024-06-21 20:57:05 +01:00
|
|
|
description:
|
|
|
|
|
'Regular expression that will be\n' +
|
2024-06-21 20:06:07 +01:00
|
|
|
'used to break text into parts.\n' +
|
|
|
|
|
'(Multiple spaces by default.)'
|
|
|
|
|
},
|
|
|
|
|
{
|
2024-06-21 20:57:05 +01:00
|
|
|
title: 'Use Length for Splitting',
|
|
|
|
|
description:
|
|
|
|
|
'Number of symbols that will be\n' + 'put in each output chunk.',
|
2024-06-21 20:06:07 +01:00
|
|
|
type: 'length'
|
|
|
|
|
},
|
|
|
|
|
{
|
2024-06-21 20:57:05 +01:00
|
|
|
title: 'Use a Number of Chunks',
|
|
|
|
|
description: 'Number of chunks of equal\n' + 'length in the output.',
|
2024-06-21 20:06:07 +01:00
|
|
|
type: 'chunks'
|
2024-06-21 20:57:05 +01:00
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
const outputOptions: {
|
|
|
|
|
description: string;
|
|
|
|
|
accessor: keyof typeof initialValues;
|
|
|
|
|
}[] = [
|
|
|
|
|
{
|
|
|
|
|
description:
|
|
|
|
|
'Character that will be put\n' +
|
|
|
|
|
'between the split chunks.\n' +
|
|
|
|
|
'(It\'s newline "\\n" by default.)',
|
|
|
|
|
accessor: 'outputSeparator'
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
description: 'Character before each chunk',
|
|
|
|
|
accessor: 'charBeforeChunk'
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
description: 'Character after each chunk',
|
|
|
|
|
accessor: 'charAfterChunk'
|
|
|
|
|
}
|
|
|
|
|
];
|
2024-06-21 21:59:17 +01:00
|
|
|
|
2024-06-19 21:18:35 +01:00
|
|
|
export default function SplitText() {
|
2024-06-21 20:57:05 +01:00
|
|
|
const [input, setInput] = useState<string>('');
|
|
|
|
|
const [result, setResult] = useState<string>('');
|
2024-06-23 15:14:14 +01:00
|
|
|
// const formRef = useRef<FormikProps<typeof initialValues>>(null);
|
2024-06-21 22:35:56 +01:00
|
|
|
const { showSnackBar } = useContext(CustomSnackBarContext);
|
|
|
|
|
|
2024-06-21 21:59:17 +01:00
|
|
|
const FormikListenerComponent = () => {
|
|
|
|
|
const { values } = useFormikContext<typeof initialValues>();
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-06-21 22:35:56 +01:00
|
|
|
try {
|
|
|
|
|
const {
|
|
|
|
|
splitSeparatorType,
|
|
|
|
|
outputSeparator,
|
|
|
|
|
charBeforeChunk,
|
|
|
|
|
charAfterChunk,
|
|
|
|
|
chunksValue,
|
|
|
|
|
symbolValue,
|
|
|
|
|
regexValue,
|
|
|
|
|
lengthValue
|
|
|
|
|
} = values;
|
2024-06-22 03:29:34 +01:00
|
|
|
|
|
|
|
|
setResult(
|
|
|
|
|
compute(
|
|
|
|
|
splitSeparatorType,
|
|
|
|
|
input,
|
|
|
|
|
symbolValue,
|
|
|
|
|
regexValue,
|
|
|
|
|
Number(lengthValue),
|
|
|
|
|
Number(chunksValue),
|
|
|
|
|
charBeforeChunk,
|
|
|
|
|
charAfterChunk,
|
|
|
|
|
outputSeparator
|
|
|
|
|
)
|
|
|
|
|
);
|
2024-06-21 22:35:56 +01:00
|
|
|
} catch (exception: unknown) {
|
|
|
|
|
if (exception instanceof Error)
|
|
|
|
|
showSnackBar(exception.message, 'error');
|
2024-06-21 21:59:17 +01:00
|
|
|
}
|
|
|
|
|
}, [values, input]);
|
2024-06-21 20:06:07 +01:00
|
|
|
|
2024-06-21 21:59:17 +01:00
|
|
|
return null; // This component doesn't render anything
|
|
|
|
|
};
|
2024-06-21 20:06:07 +01:00
|
|
|
const validationSchema = Yup.object({
|
2024-06-21 20:57:05 +01:00
|
|
|
// splitSeparator: Yup.string().required('The separator is required')
|
|
|
|
|
});
|
2024-06-21 20:06:07 +01:00
|
|
|
|
2024-06-19 21:18:35 +01:00
|
|
|
return (
|
2024-06-22 22:06:16 +01:00
|
|
|
<Box>
|
2024-06-19 21:18:35 +01:00
|
|
|
<Grid container spacing={2}>
|
|
|
|
|
<Grid item xs={6}>
|
2024-06-21 20:06:07 +01:00
|
|
|
<ToolTextInput value={input} onChange={setInput} />
|
2024-06-19 21:18:35 +01:00
|
|
|
</Grid>
|
|
|
|
|
<Grid item xs={6}>
|
2024-06-21 20:06:07 +01:00
|
|
|
<ToolTextResult title={'Text pieces'} value={result} />
|
2024-06-19 21:18:35 +01:00
|
|
|
</Grid>
|
|
|
|
|
</Grid>
|
2024-06-21 20:24:00 +01:00
|
|
|
<ToolOptions>
|
2024-06-21 20:06:07 +01:00
|
|
|
<Formik
|
|
|
|
|
initialValues={initialValues}
|
|
|
|
|
validationSchema={validationSchema}
|
2024-06-21 20:57:05 +01:00
|
|
|
onSubmit={() => {}}
|
2024-06-21 20:06:07 +01:00
|
|
|
>
|
2024-06-21 20:24:00 +01:00
|
|
|
{({ setFieldValue, values }) => (
|
2024-06-21 20:57:05 +01:00
|
|
|
<Stack direction={'row'} spacing={2}>
|
2024-06-21 21:59:17 +01:00
|
|
|
<FormikListenerComponent />
|
2024-06-21 20:06:07 +01:00
|
|
|
<Box>
|
|
|
|
|
<Typography fontSize={22}>Split separator options</Typography>
|
2024-06-21 20:57:05 +01:00
|
|
|
{splitOperators.map(({ title, description, type }) => (
|
2024-06-23 15:14:14 +01:00
|
|
|
<RadioWithTextField
|
2024-06-21 20:57:05 +01:00
|
|
|
key={type}
|
2024-06-21 20:24:00 +01:00
|
|
|
type={type}
|
|
|
|
|
title={title}
|
|
|
|
|
fieldName={'splitSeparatorType'}
|
|
|
|
|
description={description}
|
|
|
|
|
value={values[`${type}Value`]}
|
2024-06-21 20:57:05 +01:00
|
|
|
onTypeChange={(type) =>
|
|
|
|
|
setFieldValue('splitSeparatorType', type)
|
|
|
|
|
}
|
2024-06-21 20:24:00 +01:00
|
|
|
onTextChange={(val) => setFieldValue(`${type}Value`, val)}
|
2024-06-21 20:57:05 +01:00
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
</Box>
|
|
|
|
|
<Box>
|
|
|
|
|
<Typography fontSize={22}>Output separator options</Typography>
|
|
|
|
|
{outputOptions.map((option) => (
|
2024-06-23 15:14:14 +01:00
|
|
|
<TextFieldWithDesc
|
2024-06-21 20:57:05 +01:00
|
|
|
key={option.accessor}
|
|
|
|
|
value={values[option.accessor]}
|
|
|
|
|
onChange={(value) => setFieldValue(option.accessor, value)}
|
|
|
|
|
description={option.description}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
2024-06-21 20:06:07 +01:00
|
|
|
</Box>
|
|
|
|
|
</Stack>
|
|
|
|
|
)}
|
|
|
|
|
</Formik>
|
2024-06-21 20:24:00 +01:00
|
|
|
</ToolOptions>
|
2024-06-22 22:06:16 +01:00
|
|
|
</Box>
|
2024-06-21 20:57:05 +01:00
|
|
|
);
|
2024-06-19 21:18:35 +01:00
|
|
|
}
|