2021-04-20 04:37:17 +00:00
|
|
|
import React, { useState, useEffect } from 'react';
|
2021-04-16 08:56:07 +00:00
|
|
|
import DOMPurify from 'dompurify';
|
2024-04-19 11:52:06 +00:00
|
|
|
// eslint-disable-next-line import/no-unresolved
|
2024-02-07 07:10:55 +00:00
|
|
|
import Markdown from 'react-markdown';
|
2024-02-09 14:04:18 +00:00
|
|
|
import './text.scss';
|
2024-02-14 12:44:35 +00:00
|
|
|
import Loader from '@/ToolJetUI/Loader/Loader';
|
2021-04-04 13:11:17 +00:00
|
|
|
|
2024-02-07 07:10:55 +00:00
|
|
|
const VERTICAL_ALIGNMENT_VS_CSS_VALUE = {
|
|
|
|
|
top: 'flex-start',
|
|
|
|
|
center: 'center',
|
|
|
|
|
bottom: 'flex-end',
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const Text = function Text({ height, properties, fireEvent, styles, darkMode, setExposedVariable, dataCy }) {
|
2022-08-17 09:22:20 +00:00
|
|
|
let {
|
|
|
|
|
textSize,
|
|
|
|
|
textColor,
|
|
|
|
|
textAlign,
|
2022-09-26 09:35:46 +00:00
|
|
|
backgroundColor,
|
2022-08-17 09:22:20 +00:00
|
|
|
fontWeight,
|
|
|
|
|
decoration,
|
|
|
|
|
transformation,
|
|
|
|
|
fontStyle,
|
|
|
|
|
lineHeight,
|
|
|
|
|
textIndent,
|
|
|
|
|
letterSpacing,
|
|
|
|
|
wordSpacing,
|
|
|
|
|
fontVariant,
|
2023-06-29 09:44:05 +00:00
|
|
|
boxShadow,
|
2024-02-07 07:10:55 +00:00
|
|
|
verticalAlignment,
|
|
|
|
|
borderColor,
|
|
|
|
|
borderRadius,
|
|
|
|
|
isScrollRequired,
|
2022-08-17 09:22:20 +00:00
|
|
|
} = styles;
|
2024-02-07 07:10:55 +00:00
|
|
|
const { loadingState, textFormat, disabledState } = properties;
|
2022-07-01 11:24:34 +00:00
|
|
|
const [text, setText] = useState(() => computeText());
|
2024-02-07 07:10:55 +00:00
|
|
|
const [visibility, setVisibility] = useState(properties.visibility);
|
|
|
|
|
const [isLoading, setLoading] = useState(loadingState);
|
|
|
|
|
const [isDisabled, setIsDisabled] = useState(disabledState);
|
2022-10-14 08:01:22 +00:00
|
|
|
const color = ['#000', '#000000'].includes(textColor) ? (darkMode ? '#fff' : '#000') : textColor;
|
2021-12-14 11:17:11 +00:00
|
|
|
|
2022-09-26 09:35:46 +00:00
|
|
|
useEffect(() => {
|
2024-02-07 07:10:55 +00:00
|
|
|
if (visibility !== properties.visibility) setVisibility(properties.visibility);
|
|
|
|
|
if (isLoading !== loadingState) setLoading(loadingState);
|
|
|
|
|
if (isDisabled !== disabledState) setIsDisabled(disabledState);
|
|
|
|
|
|
2022-10-14 08:01:22 +00:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2024-02-07 07:10:55 +00:00
|
|
|
}, [properties.visibility, loadingState, disabledState]);
|
2022-09-26 09:35:46 +00:00
|
|
|
|
2022-12-27 14:40:33 +00:00
|
|
|
useEffect(() => {
|
|
|
|
|
const text = computeText();
|
|
|
|
|
setText(text);
|
|
|
|
|
setExposedVariable('text', text);
|
2021-08-30 11:43:05 +00:00
|
|
|
|
2024-02-07 07:10:55 +00:00
|
|
|
setExposedVariable('setText', async function (text) {
|
|
|
|
|
setText(text);
|
|
|
|
|
setExposedVariable('text', text);
|
|
|
|
|
});
|
|
|
|
|
setExposedVariable('clear', async function (text) {
|
|
|
|
|
setText('');
|
|
|
|
|
setExposedVariable('text', '');
|
|
|
|
|
});
|
|
|
|
|
setExposedVariable('isVisible', properties.visibility);
|
|
|
|
|
setExposedVariable('isLoading', loadingState);
|
|
|
|
|
setExposedVariable('isDisabled', disabledState);
|
|
|
|
|
|
|
|
|
|
setExposedVariable('visibility', async function (value) {
|
|
|
|
|
setVisibility(value);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
setExposedVariable('setVisibility', async function (value) {
|
|
|
|
|
setVisibility(value);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
setExposedVariable('setLoading', async function (value) {
|
|
|
|
|
setLoading(value);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
setExposedVariable('setDisable', async function (value) {
|
|
|
|
|
setIsDisabled(value);
|
|
|
|
|
});
|
2023-09-01 08:25:03 +00:00
|
|
|
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2024-02-07 07:10:55 +00:00
|
|
|
}, [
|
|
|
|
|
properties.text,
|
|
|
|
|
setText,
|
|
|
|
|
setVisibility,
|
|
|
|
|
properties.visibility,
|
|
|
|
|
loadingState,
|
|
|
|
|
disabledState,
|
|
|
|
|
setIsDisabled,
|
|
|
|
|
setLoading,
|
|
|
|
|
]);
|
2022-07-01 11:24:34 +00:00
|
|
|
|
|
|
|
|
function computeText() {
|
|
|
|
|
return properties.text === 0 || properties.text === false ? properties.text?.toString() : properties.text;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-07 07:10:55 +00:00
|
|
|
const handleClick = () => {
|
|
|
|
|
fireEvent('onClick');
|
|
|
|
|
};
|
2024-02-14 16:52:52 +00:00
|
|
|
|
2021-04-30 06:31:32 +00:00
|
|
|
const computedStyles = {
|
2024-02-07 07:10:55 +00:00
|
|
|
height: `${height}px`,
|
|
|
|
|
backgroundColor: darkMode && ['#edeff5'].includes(backgroundColor) ? '#2f3c4c' : backgroundColor,
|
2021-04-30 06:31:32 +00:00
|
|
|
color,
|
2021-12-14 11:17:11 +00:00
|
|
|
display: visibility ? 'flex' : 'none',
|
2022-08-17 09:22:20 +00:00
|
|
|
fontWeight: fontWeight ? fontWeight : fontWeight === '0' ? 0 : 'normal',
|
|
|
|
|
lineHeight: lineHeight ?? 1.5,
|
|
|
|
|
textDecoration: decoration ?? 'none',
|
|
|
|
|
textTransform: transformation ?? 'none',
|
|
|
|
|
fontStyle: fontStyle ?? 'none',
|
|
|
|
|
fontVariant: fontVariant ?? 'normal',
|
|
|
|
|
textIndent: `${textIndent}px` ?? '0px',
|
|
|
|
|
letterSpacing: `${letterSpacing}px` ?? '0px',
|
|
|
|
|
wordSpacing: `${wordSpacing}px` ?? '0px',
|
2023-06-29 09:44:05 +00:00
|
|
|
boxShadow,
|
2024-02-07 07:10:55 +00:00
|
|
|
border: '1px solid',
|
|
|
|
|
borderColor: darkMode && ['#f2f2f5'].includes(borderColor) ? '#2f3c4c' : borderColor ? borderColor : 'transparent',
|
|
|
|
|
borderRadius: borderRadius ? `${borderRadius}px` : '0px',
|
|
|
|
|
fontSize: `${textSize}px`,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const commonStyles = {
|
|
|
|
|
width: '100%',
|
|
|
|
|
height: '100%',
|
|
|
|
|
overflowY: isScrollRequired == 'enabled' ? 'auto' : 'hidden',
|
|
|
|
|
display: 'flex',
|
|
|
|
|
flexDirection: 'column',
|
|
|
|
|
justifyContent: VERTICAL_ALIGNMENT_VS_CSS_VALUE[verticalAlignment],
|
|
|
|
|
textAlign,
|
2024-02-14 16:52:52 +00:00
|
|
|
overflowX: isScrollRequired === 'disabled' && 'hidden',
|
2021-04-30 06:31:32 +00:00
|
|
|
};
|
|
|
|
|
|
2024-04-04 10:33:28 +00:00
|
|
|
const commonScrollStyle = {
|
|
|
|
|
overflowY: isScrollRequired == 'enabled' ? 'scroll' : 'hidden',
|
|
|
|
|
};
|
|
|
|
|
|
2021-04-30 06:31:32 +00:00
|
|
|
return (
|
2024-02-07 07:10:55 +00:00
|
|
|
<div
|
|
|
|
|
data-disabled={isDisabled}
|
|
|
|
|
className="text-widget"
|
|
|
|
|
style={computedStyles}
|
|
|
|
|
data-cy={dataCy}
|
|
|
|
|
onMouseOver={() => {
|
|
|
|
|
fireEvent('onHover');
|
|
|
|
|
}}
|
|
|
|
|
onClick={handleClick}
|
|
|
|
|
>
|
2024-02-09 14:04:18 +00:00
|
|
|
{!isLoading && (
|
2024-02-14 16:52:52 +00:00
|
|
|
<div style={commonStyles} className="text-widget-section">
|
2024-04-04 10:33:28 +00:00
|
|
|
{textFormat === 'plainText' && <div style={commonScrollStyle}>{text}</div>}
|
|
|
|
|
{textFormat === 'markdown' && (
|
|
|
|
|
<div style={commonScrollStyle}>
|
|
|
|
|
<Markdown className={'reactMarkdown'}>{text}</Markdown>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2024-02-09 14:04:18 +00:00
|
|
|
{(textFormat === 'html' || !textFormat) && (
|
2024-02-14 12:44:35 +00:00
|
|
|
<div
|
2024-04-04 10:33:28 +00:00
|
|
|
style={commonScrollStyle}
|
2024-02-14 12:44:35 +00:00
|
|
|
dangerouslySetInnerHTML={{
|
|
|
|
|
__html: DOMPurify.sanitize(text || ''),
|
|
|
|
|
}}
|
|
|
|
|
/>
|
2024-02-09 14:04:18 +00:00
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2024-02-14 12:44:35 +00:00
|
|
|
{isLoading === true && (
|
|
|
|
|
<div style={{ width: '100%', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
|
2022-01-04 05:03:07 +00:00
|
|
|
<center>
|
2024-02-14 17:57:43 +00:00
|
|
|
<Loader width="16" absolute={false} />
|
2022-01-04 05:03:07 +00:00
|
|
|
</center>
|
2021-04-16 08:56:07 +00:00
|
|
|
</div>
|
2021-04-30 06:31:32 +00:00
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2021-04-02 04:16:26 +00:00
|
|
|
};
|