|
1 | | -import { Typography } from 'antd'; |
| 1 | +import { theme, Tooltip, Typography } from 'antd'; |
| 2 | +import type { TooltipProps } from 'antd/es/tooltip'; |
| 3 | +import type { EllipsisConfig } from 'antd/es/typography/Base'; |
2 | 4 | import type { TextProps as AntdTextProps } from 'antd/es/typography/Text'; |
3 | | -import React from 'react'; |
| 5 | +import React, { useEffect, useRef, useState, ReactNode } from 'react'; |
| 6 | +import { useTranslation } from 'react-i18next'; |
4 | 7 |
|
5 | | -export interface BAITextProps extends AntdTextProps { |
| 8 | +export interface BAITextProps extends Omit<AntdTextProps, 'ellipsis'> { |
6 | 9 | monospace?: boolean; |
| 10 | + // Enable CSS-based ellipsis with Safari compatibility (multi-line via -webkit-line-clamp) |
| 11 | + ellipsis?: boolean | EllipsisConfig; |
| 12 | +} |
| 13 | + |
| 14 | +// Derive Tooltip props from the ellipsis config. |
| 15 | +function resolveTooltipProps( |
| 16 | + children: ReactNode, |
| 17 | + ellipsis: boolean | EllipsisConfig, |
| 18 | +): TooltipProps | null { |
| 19 | + if (!ellipsis || typeof ellipsis !== 'object') return null; |
| 20 | + |
| 21 | + const { tooltip } = ellipsis; |
| 22 | + if (tooltip === true) return { title: children }; |
| 23 | + if ( |
| 24 | + tooltip && |
| 25 | + typeof tooltip === 'object' && |
| 26 | + !React.isValidElement(tooltip) |
| 27 | + ) { |
| 28 | + return { title: children, ...(tooltip as TooltipProps) }; |
| 29 | + } |
| 30 | + return null; |
7 | 31 | } |
8 | 32 |
|
9 | 33 | const BAIText: React.FC<BAITextProps> = ({ |
10 | | - type, |
11 | 34 | style, |
12 | 35 | monospace, |
| 36 | + ellipsis, |
| 37 | + copyable, |
13 | 38 | children, |
| 39 | + strong, |
| 40 | + italic, |
| 41 | + underline, |
| 42 | + delete: deleteProp, |
| 43 | + mark, |
| 44 | + code, |
| 45 | + keyboard, |
14 | 46 | ...restProps |
15 | 47 | }) => { |
16 | | - // If monospace prop is true, apply monospace font styling |
17 | | - if (monospace) { |
| 48 | + const { token } = theme.useToken(); |
| 49 | + const { t } = useTranslation(); |
| 50 | + const textRef = useRef<HTMLSpanElement>(null); |
| 51 | + const [isOverflowing, setIsOverflowing] = useState(false); |
| 52 | + const [isExpanded, setIsExpanded] = useState(false); |
| 53 | + |
| 54 | + const expandable = typeof ellipsis === 'object' ? ellipsis.expandable : false; |
| 55 | + const onExpand = typeof ellipsis === 'object' ? ellipsis.onExpand : undefined; |
| 56 | + |
| 57 | + useEffect(() => { |
| 58 | + if (!ellipsis || !textRef.current || isExpanded) return; |
| 59 | + const element = textRef.current; |
| 60 | + const rows = typeof ellipsis === 'object' ? ellipsis.rows || 1 : 1; |
| 61 | + const check = () => { |
| 62 | + if (!element) return; |
| 63 | + if (rows === 1) { |
| 64 | + setIsOverflowing(element.scrollWidth > element.clientWidth); |
| 65 | + } else { |
| 66 | + setIsOverflowing(element.scrollHeight > element.clientHeight); |
| 67 | + } |
| 68 | + }; |
| 69 | + check(); |
| 70 | + const ro = new ResizeObserver(check); |
| 71 | + ro.observe(element); |
| 72 | + return () => ro.disconnect(); |
| 73 | + }, [ellipsis, children, isExpanded]); |
| 74 | + |
| 75 | + const handleExpand = (e: React.MouseEvent<HTMLElement>) => { |
| 76 | + const newExpandedState = !isExpanded; |
| 77 | + setIsExpanded(newExpandedState); |
| 78 | + onExpand?.(e, { expanded: newExpandedState }); |
| 79 | + }; |
| 80 | + |
| 81 | + if (!ellipsis) { |
18 | 82 | return ( |
19 | 83 | <Typography.Text |
20 | | - type={type} |
| 84 | + style={{ ...(monospace && { fontFamily: 'monospace' }), ...style }} |
| 85 | + copyable={copyable} |
| 86 | + strong={strong} |
| 87 | + italic={italic} |
| 88 | + underline={underline} |
| 89 | + delete={deleteProp} |
| 90 | + mark={mark} |
| 91 | + code={code} |
| 92 | + keyboard={keyboard} |
21 | 93 | {...restProps} |
22 | | - style={{ |
23 | | - fontFamily: 'monospace', |
24 | | - ...style, |
25 | | - }} |
26 | 94 | > |
27 | 95 | {children} |
28 | 96 | </Typography.Text> |
29 | 97 | ); |
30 | 98 | } |
31 | 99 |
|
32 | | - // For non-monospace text, pass all props directly to antd Text |
| 100 | + const rows = typeof ellipsis === 'object' ? ellipsis.rows || 1 : 1; |
| 101 | + const tooltipProps = resolveTooltipProps(children, ellipsis); |
| 102 | + |
| 103 | + // Styles for the container when code or keyboard is used |
| 104 | + const containerStyles: React.CSSProperties = { |
| 105 | + ...(code && { |
| 106 | + backgroundColor: token.colorFillTertiary, |
| 107 | + border: `1px solid ${token.colorBorder}`, |
| 108 | + borderRadius: token.borderRadiusSM, |
| 109 | + padding: '0.2em 0.4em', |
| 110 | + fontFamily: token.fontFamilyCode, |
| 111 | + fontSize: '0.9em', |
| 112 | + display: 'inline-flex', |
| 113 | + alignItems: 'center', |
| 114 | + gap: token.marginXXS, |
| 115 | + }), |
| 116 | + ...(keyboard && { |
| 117 | + backgroundColor: token.colorFillContent, |
| 118 | + border: `1px solid ${token.colorBorder}`, |
| 119 | + borderRadius: token.borderRadiusSM, |
| 120 | + padding: '0.15em 0.4em', |
| 121 | + fontFamily: token.fontFamilyCode, |
| 122 | + fontSize: '0.9em', |
| 123 | + boxShadow: `0 2px 0 ${token.colorBorderSecondary}`, |
| 124 | + display: 'inline-flex', |
| 125 | + alignItems: 'center', |
| 126 | + gap: token.marginXXS, |
| 127 | + }), |
| 128 | + }; |
| 129 | + |
| 130 | + // Styles for text content (excluding code/keyboard as they're on container) |
| 131 | + const textDecorationStyles: React.CSSProperties = { |
| 132 | + ...(monospace && { fontFamily: 'monospace' }), |
| 133 | + ...(strong && { fontWeight: token.fontWeightStrong }), |
| 134 | + ...(italic && { fontStyle: 'italic' }), |
| 135 | + ...(underline && deleteProp |
| 136 | + ? { textDecoration: 'underline line-through' } |
| 137 | + : underline |
| 138 | + ? { textDecoration: 'underline' } |
| 139 | + : deleteProp |
| 140 | + ? { textDecoration: 'line-through' } |
| 141 | + : {}), |
| 142 | + ...(mark && { |
| 143 | + backgroundColor: token.colorWarningBg, |
| 144 | + padding: '0 0.2em', |
| 145 | + }), |
| 146 | + }; |
| 147 | + |
33 | 148 | return ( |
34 | | - <Typography.Text type={type} style={style} {...restProps}> |
35 | | - {children} |
| 149 | + <Typography.Text |
| 150 | + {...restProps} |
| 151 | + copyable={(() => { |
| 152 | + if (!copyable) return undefined; |
| 153 | + if (typeof copyable === 'object') { |
| 154 | + return { ...copyable, text: copyable.text ?? String(children) }; |
| 155 | + } |
| 156 | + return { text: String(children) }; // copyable === true |
| 157 | + })()} |
| 158 | + style={{ |
| 159 | + display: 'inline-flex', |
| 160 | + alignItems: 'center', |
| 161 | + maxWidth: '100%', |
| 162 | + ...containerStyles, |
| 163 | + ...style, |
| 164 | + }} |
| 165 | + ellipsis={false} |
| 166 | + > |
| 167 | + <Tooltip |
| 168 | + {...tooltipProps} |
| 169 | + open={tooltipProps && isOverflowing && !isExpanded ? undefined : false} |
| 170 | + > |
| 171 | + <span |
| 172 | + ref={textRef} |
| 173 | + style={{ |
| 174 | + ...textDecorationStyles, |
| 175 | + overflow: isExpanded ? 'visible' : 'hidden', |
| 176 | + textOverflow: isExpanded ? 'clip' : 'ellipsis', |
| 177 | + flex: 1, |
| 178 | + minWidth: 0, |
| 179 | + ...(isExpanded |
| 180 | + ? { |
| 181 | + whiteSpace: 'normal', |
| 182 | + wordBreak: 'break-word', |
| 183 | + } |
| 184 | + : rows === 1 |
| 185 | + ? { |
| 186 | + whiteSpace: 'nowrap', |
| 187 | + display: 'block', |
| 188 | + } |
| 189 | + : { |
| 190 | + display: '-webkit-box', |
| 191 | + WebkitLineClamp: rows, |
| 192 | + WebkitBoxOrient: 'vertical', |
| 193 | + overflow: 'hidden', |
| 194 | + wordBreak: 'break-word', |
| 195 | + }), |
| 196 | + }} |
| 197 | + > |
| 198 | + {children} |
| 199 | + </span> |
| 200 | + </Tooltip> |
| 201 | + {expandable && isOverflowing && ( |
| 202 | + <Typography.Link |
| 203 | + onClick={handleExpand} |
| 204 | + style={{ |
| 205 | + marginLeft: token.marginXXS, |
| 206 | + flexShrink: 0, |
| 207 | + }} |
| 208 | + > |
| 209 | + {isExpanded |
| 210 | + ? t('general.button.Collapse') |
| 211 | + : t('general.button.Expand')} |
| 212 | + </Typography.Link> |
| 213 | + )} |
36 | 214 | </Typography.Text> |
37 | 215 | ); |
38 | 216 | }; |
|
0 commit comments