2023-09-25 16:48:53 +08:00
|
|
|
import * as React from 'react';
|
2022-01-18 14:13:41 +08:00
|
|
|
import toArray from 'rc-util/lib/Children/toArray';
|
2024-02-27 16:56:24 +08:00
|
|
|
import useLayoutEffect from 'rc-util/lib/hooks/useLayoutEffect';
|
2022-01-18 14:13:41 +08:00
|
|
|
|
2024-02-27 16:56:24 +08:00
|
|
|
interface MeasureTextProps {
|
|
|
|
style?: React.CSSProperties;
|
|
|
|
children: React.ReactNode;
|
2022-01-18 14:13:41 +08:00
|
|
|
}
|
|
|
|
|
2024-02-27 16:56:24 +08:00
|
|
|
interface MeasureTextRef {
|
|
|
|
isExceed: () => boolean;
|
|
|
|
getHeight: () => number;
|
|
|
|
}
|
|
|
|
|
|
|
|
const MeasureText = React.forwardRef<MeasureTextRef, MeasureTextProps>(
|
|
|
|
({ style, children }, ref) => {
|
|
|
|
const spanRef = React.useRef<HTMLSpanElement>(null);
|
|
|
|
|
|
|
|
React.useImperativeHandle(ref, () => ({
|
|
|
|
isExceed: () => {
|
|
|
|
const span = spanRef.current!;
|
|
|
|
return span.scrollHeight > span.clientHeight;
|
|
|
|
},
|
|
|
|
getHeight: () => spanRef.current!.clientHeight,
|
|
|
|
}));
|
|
|
|
|
|
|
|
return (
|
|
|
|
<span
|
|
|
|
aria-hidden
|
|
|
|
ref={spanRef}
|
|
|
|
style={{
|
|
|
|
position: 'fixed',
|
|
|
|
display: 'block',
|
|
|
|
left: 0,
|
|
|
|
top: 0,
|
|
|
|
pointerEvents: 'none',
|
|
|
|
backgroundColor: 'rgba(255, 0, 0, 0.65)',
|
|
|
|
...style,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2022-01-18 14:13:41 +08:00
|
|
|
function cuttable(node: React.ReactElement) {
|
|
|
|
const type = typeof node;
|
|
|
|
return type === 'string' || type === 'number';
|
|
|
|
}
|
|
|
|
|
|
|
|
function getNodesLen(nodeList: React.ReactElement[]) {
|
|
|
|
let totalLen = 0;
|
|
|
|
|
2022-11-19 13:47:33 +08:00
|
|
|
nodeList.forEach((node) => {
|
2022-01-18 14:13:41 +08:00
|
|
|
if (cuttable(node)) {
|
|
|
|
totalLen += String(node).length;
|
|
|
|
} else {
|
|
|
|
totalLen += 1;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return totalLen;
|
|
|
|
}
|
|
|
|
|
|
|
|
function sliceNodes(nodeList: React.ReactElement[], len: number) {
|
|
|
|
let currLen = 0;
|
|
|
|
const currentNodeList: React.ReactNode[] = [];
|
|
|
|
|
|
|
|
for (let i = 0; i < nodeList.length; i += 1) {
|
|
|
|
// Match to return
|
|
|
|
if (currLen === len) {
|
|
|
|
return currentNodeList;
|
|
|
|
}
|
|
|
|
|
|
|
|
const node = nodeList[i];
|
|
|
|
const canCut = cuttable(node);
|
|
|
|
const nodeLen = canCut ? String(node).length : 1;
|
|
|
|
const nextLen = currLen + nodeLen;
|
|
|
|
|
|
|
|
// Exceed but current not which means we need cut this
|
|
|
|
// This will not happen on validate ReactElement
|
|
|
|
if (nextLen > len) {
|
|
|
|
const restLen = len - currLen;
|
|
|
|
currentNodeList.push(String(node).slice(0, restLen));
|
|
|
|
return currentNodeList;
|
|
|
|
}
|
|
|
|
|
|
|
|
currentNodeList.push(node);
|
|
|
|
currLen = nextLen;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodeList;
|
|
|
|
}
|
|
|
|
|
2024-02-27 16:56:24 +08:00
|
|
|
export interface EllipsisProps {
|
2024-03-11 10:33:05 +08:00
|
|
|
enableMeasure?: boolean;
|
2024-02-27 16:56:24 +08:00
|
|
|
text?: React.ReactNode;
|
|
|
|
width: number;
|
|
|
|
// fontSize: number;
|
|
|
|
rows: number;
|
|
|
|
children: (
|
|
|
|
cutChildren: React.ReactNode[],
|
|
|
|
/** Tell current `text` is exceed the `rows` which can be ellipsis */
|
|
|
|
canEllipsis: boolean,
|
|
|
|
) => React.ReactNode;
|
|
|
|
onEllipsis: (isEllipsis: boolean) => void;
|
2024-03-20 11:39:03 +08:00
|
|
|
expanded: boolean;
|
2024-02-27 16:56:24 +08:00
|
|
|
/**
|
|
|
|
* Mark for misc update. Which will not affect ellipsis content length.
|
|
|
|
* e.g. tooltip content update.
|
|
|
|
*/
|
|
|
|
miscDeps: any[];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Measure for the `text` is exceed the `rows` or not
|
|
|
|
const STATUS_MEASURE_NONE = 0;
|
|
|
|
const STATUS_MEASURE_START = 1;
|
|
|
|
const STATUS_MEASURE_NEED_ELLIPSIS = 2;
|
|
|
|
const STATUS_MEASURE_NO_NEED_ELLIPSIS = 3;
|
|
|
|
|
|
|
|
const lineClipStyle: React.CSSProperties = {
|
|
|
|
display: '-webkit-box',
|
|
|
|
overflow: 'hidden',
|
|
|
|
WebkitBoxOrient: 'vertical',
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function EllipsisMeasure(props: EllipsisProps) {
|
2024-03-20 11:39:03 +08:00
|
|
|
const { enableMeasure, width, text, children, rows, expanded, miscDeps, onEllipsis } = props;
|
2022-01-18 14:13:41 +08:00
|
|
|
|
|
|
|
const nodeList = React.useMemo(() => toArray(text), [text]);
|
2024-02-27 16:56:24 +08:00
|
|
|
const nodeLen = React.useMemo(() => getNodesLen(nodeList), [text]);
|
|
|
|
|
|
|
|
// ========================= Full Content =========================
|
2024-03-20 11:39:03 +08:00
|
|
|
// Used for measure only, which means it's always render as no need ellipsis
|
|
|
|
const fullContent = React.useMemo(() => children(nodeList, false), [text]);
|
2022-01-18 14:13:41 +08:00
|
|
|
|
2024-02-27 16:56:24 +08:00
|
|
|
// ========================= Cut Content ==========================
|
|
|
|
const [ellipsisCutIndex, setEllipsisCutIndex] = React.useState<[number, number] | null>(null);
|
|
|
|
const cutMidRef = React.useRef<MeasureTextRef>(null);
|
2023-08-30 23:24:19 +08:00
|
|
|
|
2024-02-27 16:56:24 +08:00
|
|
|
// ========================= NeedEllipsis =========================
|
|
|
|
const needEllipsisRef = React.useRef<MeasureTextRef>(null);
|
|
|
|
|
2024-03-15 13:56:03 +08:00
|
|
|
// Measure for `rows-1` height, to avoid operation exceed the line height
|
|
|
|
const descRowsEllipsisRef = React.useRef<MeasureTextRef>(null);
|
|
|
|
const symbolRowEllipsisRef = React.useRef<MeasureTextRef>(null);
|
|
|
|
|
2024-03-20 11:39:03 +08:00
|
|
|
const [canEllipsis, setCanEllipsis] = React.useState(false);
|
2024-02-27 16:56:24 +08:00
|
|
|
const [needEllipsis, setNeedEllipsis] = React.useState(STATUS_MEASURE_NONE);
|
|
|
|
const [ellipsisHeight, setEllipsisHeight] = React.useState(0);
|
|
|
|
|
|
|
|
// Trigger start measure
|
|
|
|
useLayoutEffect(() => {
|
2024-03-11 10:33:05 +08:00
|
|
|
if (enableMeasure && width && nodeLen) {
|
2024-02-27 16:56:24 +08:00
|
|
|
setNeedEllipsis(STATUS_MEASURE_START);
|
|
|
|
} else {
|
|
|
|
setNeedEllipsis(STATUS_MEASURE_NONE);
|
2022-01-18 14:13:41 +08:00
|
|
|
}
|
2024-03-11 10:33:05 +08:00
|
|
|
}, [width, text, rows, enableMeasure, nodeList]);
|
2022-01-18 14:13:41 +08:00
|
|
|
|
2024-02-27 16:56:24 +08:00
|
|
|
// Measure process
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
if (needEllipsis === STATUS_MEASURE_START) {
|
|
|
|
const isOverflow = !!needEllipsisRef.current?.isExceed();
|
2022-01-18 14:13:41 +08:00
|
|
|
|
2024-02-27 16:56:24 +08:00
|
|
|
setNeedEllipsis(isOverflow ? STATUS_MEASURE_NEED_ELLIPSIS : STATUS_MEASURE_NO_NEED_ELLIPSIS);
|
|
|
|
setEllipsisCutIndex(isOverflow ? [0, nodeLen] : null);
|
2024-03-20 11:39:03 +08:00
|
|
|
setCanEllipsis(isOverflow);
|
2024-02-27 16:56:24 +08:00
|
|
|
|
2024-03-15 13:56:03 +08:00
|
|
|
// Get the basic height of ellipsis rows
|
|
|
|
const baseRowsEllipsisHeight = needEllipsisRef.current?.getHeight() || 0;
|
|
|
|
|
|
|
|
// Get the height of `rows - 1` + symbol height
|
|
|
|
const descRowsEllipsisHeight = rows === 1 ? 0 : descRowsEllipsisRef.current?.getHeight() || 0;
|
|
|
|
const symbolRowEllipsisHeight = symbolRowEllipsisRef.current?.getHeight() || 0;
|
|
|
|
const rowsWithEllipsisHeight = descRowsEllipsisHeight + symbolRowEllipsisHeight;
|
|
|
|
|
|
|
|
const maxRowsHeight = Math.max(baseRowsEllipsisHeight, rowsWithEllipsisHeight);
|
|
|
|
|
|
|
|
setEllipsisHeight(maxRowsHeight + 1);
|
2024-02-27 16:56:24 +08:00
|
|
|
|
|
|
|
onEllipsis(isOverflow);
|
2022-01-18 14:13:41 +08:00
|
|
|
}
|
2024-02-27 16:56:24 +08:00
|
|
|
}, [needEllipsis]);
|
|
|
|
|
|
|
|
// ========================= Cut Measure ==========================
|
|
|
|
const cutMidIndex = ellipsisCutIndex
|
|
|
|
? Math.ceil((ellipsisCutIndex[0] + ellipsisCutIndex[1]) / 2)
|
|
|
|
: 0;
|
2022-01-18 14:13:41 +08:00
|
|
|
|
2024-02-27 16:56:24 +08:00
|
|
|
useLayoutEffect(() => {
|
|
|
|
const [minIndex, maxIndex] = ellipsisCutIndex || [0, 0];
|
|
|
|
if (minIndex !== maxIndex) {
|
|
|
|
const midHeight = cutMidRef.current?.getHeight() || 0;
|
|
|
|
|
|
|
|
const isOverflow = midHeight > ellipsisHeight;
|
|
|
|
let targetMidIndex = cutMidIndex;
|
|
|
|
|
|
|
|
if (maxIndex - minIndex === 1) {
|
|
|
|
targetMidIndex = isOverflow ? minIndex : maxIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isOverflow) {
|
|
|
|
setEllipsisCutIndex([minIndex, targetMidIndex]);
|
|
|
|
} else {
|
|
|
|
setEllipsisCutIndex([targetMidIndex, maxIndex]);
|
|
|
|
}
|
2022-01-18 14:13:41 +08:00
|
|
|
}
|
2024-02-27 16:56:24 +08:00
|
|
|
}, [ellipsisCutIndex, cutMidIndex]);
|
|
|
|
|
|
|
|
// ========================= Text Content =========================
|
|
|
|
const finalContent = React.useMemo(() => {
|
|
|
|
if (
|
|
|
|
needEllipsis !== STATUS_MEASURE_NEED_ELLIPSIS ||
|
|
|
|
!ellipsisCutIndex ||
|
|
|
|
ellipsisCutIndex[0] !== ellipsisCutIndex[1]
|
|
|
|
) {
|
2024-03-20 11:39:03 +08:00
|
|
|
const content = children(nodeList, false);
|
2024-02-27 16:56:24 +08:00
|
|
|
|
|
|
|
// Limit the max line count to avoid scrollbar blink
|
|
|
|
// https://github.com/ant-design/ant-design/issues/42958
|
|
|
|
if (
|
|
|
|
needEllipsis !== STATUS_MEASURE_NO_NEED_ELLIPSIS &&
|
|
|
|
needEllipsis !== STATUS_MEASURE_NONE
|
|
|
|
) {
|
|
|
|
return (
|
|
|
|
<span
|
|
|
|
style={{
|
|
|
|
...lineClipStyle,
|
|
|
|
WebkitLineClamp: rows,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{content}
|
|
|
|
</span>
|
|
|
|
);
|
2022-01-18 14:13:41 +08:00
|
|
|
}
|
2024-02-27 16:56:24 +08:00
|
|
|
|
|
|
|
return content;
|
2022-01-18 14:13:41 +08:00
|
|
|
}
|
|
|
|
|
2024-03-20 11:39:03 +08:00
|
|
|
return children(expanded ? nodeList : sliceNodes(nodeList, ellipsisCutIndex[0]), canEllipsis);
|
|
|
|
}, [expanded, needEllipsis, ellipsisCutIndex, nodeList, ...miscDeps]);
|
2024-02-27 16:56:24 +08:00
|
|
|
|
|
|
|
// ============================ Render ============================
|
2022-01-18 14:13:41 +08:00
|
|
|
const measureStyle: React.CSSProperties = {
|
|
|
|
width,
|
|
|
|
whiteSpace: 'normal',
|
|
|
|
margin: 0,
|
|
|
|
padding: 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2024-02-27 16:56:24 +08:00
|
|
|
{/* Final show content */}
|
|
|
|
{finalContent}
|
|
|
|
|
|
|
|
{/* Measure if current content is exceed the rows */}
|
|
|
|
{needEllipsis === STATUS_MEASURE_START && (
|
2024-03-15 13:56:03 +08:00
|
|
|
<>
|
|
|
|
{/** With `rows` */}
|
|
|
|
<MeasureText
|
|
|
|
style={{
|
|
|
|
...measureStyle,
|
|
|
|
...lineClipStyle,
|
|
|
|
WebkitLineClamp: rows,
|
|
|
|
}}
|
|
|
|
ref={needEllipsisRef}
|
|
|
|
>
|
|
|
|
{fullContent}
|
|
|
|
</MeasureText>
|
|
|
|
|
|
|
|
{/** With `rows - 1` */}
|
|
|
|
<MeasureText
|
|
|
|
style={{
|
|
|
|
...measureStyle,
|
|
|
|
...lineClipStyle,
|
|
|
|
WebkitLineClamp: rows - 1,
|
|
|
|
}}
|
|
|
|
ref={descRowsEllipsisRef}
|
|
|
|
>
|
|
|
|
{fullContent}
|
|
|
|
</MeasureText>
|
|
|
|
|
|
|
|
{/** With `rows - 1` */}
|
|
|
|
<MeasureText
|
|
|
|
style={{
|
|
|
|
...measureStyle,
|
|
|
|
...lineClipStyle,
|
|
|
|
WebkitLineClamp: 1,
|
|
|
|
}}
|
|
|
|
ref={symbolRowEllipsisRef}
|
|
|
|
>
|
2024-03-20 11:39:03 +08:00
|
|
|
{children([], true)}
|
2024-03-15 13:56:03 +08:00
|
|
|
</MeasureText>
|
|
|
|
</>
|
2024-02-27 16:56:24 +08:00
|
|
|
)}
|
|
|
|
|
|
|
|
{/* Real size overflow measure */}
|
|
|
|
{needEllipsis === STATUS_MEASURE_NEED_ELLIPSIS &&
|
|
|
|
ellipsisCutIndex &&
|
|
|
|
ellipsisCutIndex[0] !== ellipsisCutIndex[1] && (
|
|
|
|
<MeasureText
|
|
|
|
style={{
|
|
|
|
...measureStyle,
|
|
|
|
top: 400,
|
|
|
|
}}
|
|
|
|
ref={cutMidRef}
|
|
|
|
>
|
2024-03-20 11:39:03 +08:00
|
|
|
{children(sliceNodes(nodeList, cutMidIndex), true)}
|
2024-02-27 16:56:24 +08:00
|
|
|
</MeasureText>
|
2022-01-18 14:13:41 +08:00
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|