2025-03-10 13:09:22 +03:00
|
|
|
import { Box } from '@mui/material';
|
|
|
|
|
import { useState } from 'react';
|
|
|
|
|
import ToolTextResult from '@components/result/ToolTextResult';
|
|
|
|
|
import { GetGroupsType } from '@components/options/ToolOptions';
|
|
|
|
|
import { truncateText } from './service';
|
|
|
|
|
import TextFieldWithDesc from '@components/options/TextFieldWithDesc';
|
|
|
|
|
import ToolTextInput from '@components/input/ToolTextInput';
|
|
|
|
|
import { initialValues, InitialValuesType } from './initialValues';
|
|
|
|
|
import ToolContent from '@components/ToolContent';
|
|
|
|
|
import { CardExampleType } from '@components/examples/ToolExamples';
|
|
|
|
|
import { ToolComponentProps } from '@tools/defineTool';
|
|
|
|
|
import SimpleRadio from '@components/options/SimpleRadio';
|
2025-03-11 08:17:50 +03:00
|
|
|
import CheckboxWithDesc from '@components/options/CheckboxWithDesc';
|
2025-07-12 23:02:35 -07:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2025-03-10 13:09:22 +03:00
|
|
|
|
|
|
|
|
const exampleCards: CardExampleType<InitialValuesType>[] = [
|
|
|
|
|
{
|
|
|
|
|
title: 'Basic Truncation on the Right',
|
|
|
|
|
description: 'Truncate text from the right side based on max length.',
|
|
|
|
|
sampleText: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
|
|
|
|
|
sampleResult: 'Lorem ipsum dolor...',
|
|
|
|
|
sampleOptions: {
|
|
|
|
|
...initialValues,
|
|
|
|
|
textToTruncate:
|
|
|
|
|
'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
|
|
|
|
|
maxLength: '20',
|
|
|
|
|
truncationSide: 'right',
|
|
|
|
|
addIndicator: true,
|
|
|
|
|
indicator: '...'
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
title: 'Truncation on the Left with Indicator',
|
|
|
|
|
description: 'Truncate text from the left side and add an indicator.',
|
|
|
|
|
sampleText: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
|
|
|
|
|
sampleResult: '...is dolor sit amet, consectetur adipiscing elit.',
|
|
|
|
|
sampleOptions: {
|
|
|
|
|
...initialValues,
|
|
|
|
|
textToTruncate:
|
|
|
|
|
'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
|
|
|
|
|
maxLength: '40',
|
|
|
|
|
truncationSide: 'left',
|
|
|
|
|
addIndicator: true,
|
|
|
|
|
indicator: '...'
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
title: 'Multi-line Truncation with Indicator',
|
|
|
|
|
description:
|
|
|
|
|
'Truncate text line by line, adding an indicator to each line.',
|
|
|
|
|
sampleText: `Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
|
|
|
|
|
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
|
|
|
|
|
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.`,
|
|
|
|
|
sampleResult: `Lorem ipsum dolor sit amet, consectetur...
|
|
|
|
|
Ut enim ad minim veniam, quis nostrud...
|
|
|
|
|
Duis aute irure dolor in reprehenderit...`,
|
|
|
|
|
sampleOptions: {
|
|
|
|
|
...initialValues,
|
|
|
|
|
textToTruncate: `Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
|
|
|
|
|
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
|
|
|
|
|
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.`,
|
|
|
|
|
maxLength: '50',
|
|
|
|
|
lineByLine: true,
|
|
|
|
|
addIndicator: true,
|
|
|
|
|
indicator: '...'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
export default function Truncate({ title }: ToolComponentProps) {
|
2025-07-12 23:02:35 -07:00
|
|
|
const { t } = useTranslation();
|
2025-03-10 13:09:22 +03:00
|
|
|
const [input, setInput] = useState<string>('');
|
|
|
|
|
const [result, setResult] = useState<string>('');
|
|
|
|
|
|
|
|
|
|
function compute(optionsValues: InitialValuesType, input: string) {
|
|
|
|
|
setResult(truncateText(optionsValues, input));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const getGroups: GetGroupsType<InitialValuesType> = ({
|
|
|
|
|
values,
|
|
|
|
|
updateField
|
|
|
|
|
}) => [
|
|
|
|
|
{
|
2025-07-12 23:02:35 -07:00
|
|
|
title: t('string.truncate.truncationSide'),
|
2025-03-10 13:09:22 +03:00
|
|
|
component: (
|
|
|
|
|
<Box>
|
|
|
|
|
<SimpleRadio
|
|
|
|
|
onClick={() => updateField('truncationSide', 'right')}
|
|
|
|
|
checked={values.truncationSide === 'right'}
|
2025-07-12 23:02:35 -07:00
|
|
|
title={t('string.truncate.rightSideTruncation')}
|
|
|
|
|
description={t('string.truncate.rightSideDescription')}
|
2025-03-10 13:09:22 +03:00
|
|
|
/>
|
|
|
|
|
<SimpleRadio
|
|
|
|
|
onClick={() => updateField('truncationSide', 'left')}
|
|
|
|
|
checked={values.truncationSide === 'left'}
|
2025-07-12 23:02:35 -07:00
|
|
|
title={t('string.truncate.leftSideTruncation')}
|
|
|
|
|
description={t('string.truncate.leftSideDescription')}
|
2025-03-10 13:09:22 +03:00
|
|
|
/>
|
|
|
|
|
</Box>
|
|
|
|
|
)
|
|
|
|
|
},
|
|
|
|
|
{
|
2025-07-12 23:02:35 -07:00
|
|
|
title: t('string.truncate.lengthAndLines'),
|
2025-03-10 13:09:22 +03:00
|
|
|
component: (
|
|
|
|
|
<Box>
|
|
|
|
|
<TextFieldWithDesc
|
2025-07-12 23:02:35 -07:00
|
|
|
description={t('string.truncate.maxLengthDescription')}
|
|
|
|
|
placeholder={t('string.truncate.numberPlaceholder')}
|
2025-03-10 13:09:22 +03:00
|
|
|
value={values.maxLength}
|
|
|
|
|
onOwnChange={(val) => updateField('maxLength', val)}
|
|
|
|
|
type={'number'}
|
|
|
|
|
/>
|
2025-03-11 08:17:50 +03:00
|
|
|
<CheckboxWithDesc
|
|
|
|
|
onChange={(val) => updateField('lineByLine', val)}
|
2025-03-10 13:09:22 +03:00
|
|
|
checked={values.lineByLine}
|
2025-07-12 23:02:35 -07:00
|
|
|
title={t('string.truncate.lineByLineTruncating')}
|
|
|
|
|
description={t('string.truncate.lineByLineDescription')}
|
2025-03-10 13:09:22 +03:00
|
|
|
/>
|
|
|
|
|
</Box>
|
|
|
|
|
)
|
|
|
|
|
},
|
|
|
|
|
{
|
2025-07-12 23:02:35 -07:00
|
|
|
title: t('string.truncate.suffixAndAffix'),
|
2025-03-10 13:09:22 +03:00
|
|
|
component: (
|
|
|
|
|
<Box>
|
2025-03-11 08:17:50 +03:00
|
|
|
<CheckboxWithDesc
|
|
|
|
|
onChange={(val) => updateField('addIndicator', val)}
|
2025-03-10 13:09:22 +03:00
|
|
|
checked={values.addIndicator}
|
2025-07-12 23:02:35 -07:00
|
|
|
title={t('string.truncate.addTruncationIndicator')}
|
2025-03-11 08:17:50 +03:00
|
|
|
description={''}
|
2025-03-10 13:09:22 +03:00
|
|
|
/>
|
|
|
|
|
<TextFieldWithDesc
|
2025-07-12 23:02:35 -07:00
|
|
|
description={t('string.truncate.indicatorDescription')}
|
|
|
|
|
placeholder={t('string.truncate.charactersPlaceholder')}
|
2025-03-10 13:09:22 +03:00
|
|
|
value={values.indicator}
|
|
|
|
|
onOwnChange={(val) => updateField('indicator', val)}
|
|
|
|
|
type={'text'}
|
|
|
|
|
/>
|
|
|
|
|
</Box>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<ToolContent
|
|
|
|
|
title={title}
|
|
|
|
|
initialValues={initialValues}
|
|
|
|
|
getGroups={getGroups}
|
|
|
|
|
compute={compute}
|
|
|
|
|
input={input}
|
|
|
|
|
setInput={setInput}
|
|
|
|
|
inputComponent={
|
2025-07-12 23:02:35 -07:00
|
|
|
<ToolTextInput
|
|
|
|
|
title={t('string.truncate.inputTitle')}
|
|
|
|
|
value={input}
|
|
|
|
|
onChange={setInput}
|
|
|
|
|
/>
|
2025-03-10 13:09:22 +03:00
|
|
|
}
|
|
|
|
|
resultComponent={
|
2025-07-12 23:02:35 -07:00
|
|
|
<ToolTextResult
|
|
|
|
|
title={t('string.truncate.resultTitle')}
|
|
|
|
|
value={result}
|
|
|
|
|
/>
|
2025-03-10 13:09:22 +03:00
|
|
|
}
|
|
|
|
|
toolInfo={{
|
2025-07-12 23:02:35 -07:00
|
|
|
title: t('string.truncate.toolInfo.title'),
|
|
|
|
|
description: t('string.truncate.toolInfo.description')
|
2025-03-10 13:09:22 +03:00
|
|
|
}}
|
|
|
|
|
exampleCards={exampleCards}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|