antd-mobile 5.38.0 → 5.38.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/2x/bundle/antd-mobile.cjs.development.js +126 -119
- package/2x/bundle/antd-mobile.cjs.js +7 -7
- package/2x/bundle/antd-mobile.es.development.js +127 -120
- package/2x/bundle/antd-mobile.es.js +4458 -4441
- package/2x/bundle/antd-mobile.umd.development.js +126 -119
- package/2x/bundle/antd-mobile.umd.js +7 -7
- package/2x/bundle/style.css +1 -0
- package/2x/cjs/components/ellipsis/ellipsis.css +1 -0
- package/2x/cjs/components/ellipsis/ellipsis.d.ts +1 -1
- package/2x/cjs/components/ellipsis/ellipsis.js +28 -124
- package/2x/cjs/components/ellipsis/useMeasure.d.ts +2 -0
- package/2x/cjs/components/ellipsis/useMeasure.js +108 -0
- package/2x/cjs/components/ellipsis/~ellipsis.d.ts +15 -0
- package/2x/cjs/components/ellipsis/~ellipsis.js +161 -0
- package/2x/es/components/ellipsis/ellipsis.css +1 -0
- package/2x/es/components/ellipsis/ellipsis.d.ts +1 -1
- package/2x/es/components/ellipsis/ellipsis.js +28 -122
- package/2x/es/components/ellipsis/useMeasure.d.ts +2 -0
- package/2x/es/components/ellipsis/useMeasure.js +101 -0
- package/2x/es/components/ellipsis/~ellipsis.d.ts +15 -0
- package/2x/es/components/ellipsis/~ellipsis.js +151 -0
- package/2x/package.json +1 -1
- package/bundle/antd-mobile.cjs.development.js +126 -119
- package/bundle/antd-mobile.cjs.js +7 -7
- package/bundle/antd-mobile.compatible.umd.js +1 -1
- package/bundle/antd-mobile.es.development.js +127 -120
- package/bundle/antd-mobile.es.js +4458 -4441
- package/bundle/antd-mobile.umd.development.js +126 -119
- package/bundle/antd-mobile.umd.js +7 -7
- package/bundle/style.css +1 -1
- package/cjs/components/ellipsis/ellipsis.css +1 -0
- package/cjs/components/ellipsis/ellipsis.d.ts +1 -1
- package/cjs/components/ellipsis/ellipsis.js +28 -124
- package/cjs/components/ellipsis/useMeasure.d.ts +2 -0
- package/cjs/components/ellipsis/useMeasure.js +108 -0
- package/cjs/components/ellipsis/~ellipsis.d.ts +15 -0
- package/cjs/components/ellipsis/~ellipsis.js +161 -0
- package/es/components/ellipsis/ellipsis.css +1 -0
- package/es/components/ellipsis/ellipsis.d.ts +1 -1
- package/es/components/ellipsis/ellipsis.js +28 -122
- package/es/components/ellipsis/useMeasure.d.ts +2 -0
- package/es/components/ellipsis/useMeasure.js +101 -0
- package/es/components/ellipsis/~ellipsis.d.ts +15 -0
- package/es/components/ellipsis/~ellipsis.js +151 -0
- package/package.json +1 -1
- package/umd/antd-mobile.js +1 -1
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import React
|
|
2
|
-
import runes from 'runes2';
|
|
3
|
-
import { mergeProps } from '../../utils/with-default-props';
|
|
1
|
+
import React from 'react';
|
|
4
2
|
import { withNativeProps } from '../../utils/native-props';
|
|
5
3
|
import { useResizeEffect } from '../../utils/use-resize-effect';
|
|
6
|
-
import {
|
|
4
|
+
import { mergeProps } from '../../utils/with-default-props';
|
|
7
5
|
import { withStopPropagation } from '../../utils/with-stop-propagation';
|
|
6
|
+
import useMeasure from './useMeasure';
|
|
8
7
|
const classPrefix = `adm-ellipsis`;
|
|
9
8
|
const defaultProps = {
|
|
10
9
|
direction: 'end',
|
|
@@ -18,134 +17,41 @@ const defaultProps = {
|
|
|
18
17
|
};
|
|
19
18
|
export const Ellipsis = p => {
|
|
20
19
|
const props = mergeProps(defaultProps, p);
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
root.style.display = 'block';
|
|
37
|
-
const originStyle = window.getComputedStyle(root);
|
|
38
|
-
const container = document.createElement('div');
|
|
39
|
-
const styleNames = Array.prototype.slice.apply(originStyle);
|
|
40
|
-
styleNames.forEach(name => {
|
|
41
|
-
container.style.setProperty(name, originStyle.getPropertyValue(name));
|
|
42
|
-
});
|
|
43
|
-
root.style.display = originDisplay;
|
|
44
|
-
container.style.height = 'auto';
|
|
45
|
-
container.style.minHeight = 'auto';
|
|
46
|
-
container.style.maxHeight = 'auto';
|
|
47
|
-
container.style.textOverflow = 'clip';
|
|
48
|
-
container.style.webkitLineClamp = 'unset';
|
|
49
|
-
container.style.display = 'block';
|
|
50
|
-
const lineHeight = pxToNumber(originStyle.lineHeight);
|
|
51
|
-
const maxHeight = Math.floor(lineHeight * (props.rows + 0.5) + pxToNumber(originStyle.paddingTop) + pxToNumber(originStyle.paddingBottom));
|
|
52
|
-
container.innerText = props.content;
|
|
53
|
-
document.body.appendChild(container);
|
|
54
|
-
if (container.offsetHeight <= maxHeight) {
|
|
55
|
-
setExceeded(false);
|
|
56
|
-
} else {
|
|
57
|
-
setExceeded(true);
|
|
58
|
-
const end = props.content.length;
|
|
59
|
-
const collapseEl = typeof props.collapseText === 'string' ? props.collapseText : (_a = collapseElRef.current) === null || _a === void 0 ? void 0 : _a.innerHTML;
|
|
60
|
-
const expandEl = typeof props.expandText === 'string' ? props.expandText : (_b = expandElRef.current) === null || _b === void 0 ? void 0 : _b.innerHTML;
|
|
61
|
-
const actionText = expanded ? collapseEl : expandEl;
|
|
62
|
-
function check(left, right) {
|
|
63
|
-
if (right - left <= 1) {
|
|
64
|
-
if (props.direction === 'end') {
|
|
65
|
-
return {
|
|
66
|
-
leading: getSubString(0, left) + '...'
|
|
67
|
-
};
|
|
68
|
-
} else {
|
|
69
|
-
return {
|
|
70
|
-
tailing: '...' + getSubString(right, end)
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
const middle = Math.round((left + right) / 2);
|
|
75
|
-
if (props.direction === 'end') {
|
|
76
|
-
container.innerHTML = getSubString(0, middle) + '...' + actionText;
|
|
77
|
-
} else {
|
|
78
|
-
container.innerHTML = actionText + '...' + getSubString(middle, end);
|
|
79
|
-
}
|
|
80
|
-
if (container.offsetHeight <= maxHeight) {
|
|
81
|
-
if (props.direction === 'end') {
|
|
82
|
-
return check(middle, right);
|
|
83
|
-
} else {
|
|
84
|
-
return check(left, middle);
|
|
85
|
-
}
|
|
86
|
-
} else {
|
|
87
|
-
if (props.direction === 'end') {
|
|
88
|
-
return check(left, middle);
|
|
89
|
-
} else {
|
|
90
|
-
return check(middle, right);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
function checkMiddle(leftPart, rightPart) {
|
|
95
|
-
if (leftPart[1] - leftPart[0] <= 1 && rightPart[1] - rightPart[0] <= 1) {
|
|
96
|
-
return {
|
|
97
|
-
leading: getSubString(0, leftPart[0]) + '...',
|
|
98
|
-
tailing: '...' + getSubString(rightPart[1], end)
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
const leftPartMiddle = Math.floor((leftPart[0] + leftPart[1]) / 2);
|
|
102
|
-
const rightPartMiddle = Math.ceil((rightPart[0] + rightPart[1]) / 2);
|
|
103
|
-
container.innerHTML = getSubString(0, leftPartMiddle) + '...' + actionText + '...' + getSubString(rightPartMiddle, end);
|
|
104
|
-
if (container.offsetHeight <= maxHeight) {
|
|
105
|
-
return checkMiddle([leftPartMiddle, leftPart[1]], [rightPart[0], rightPartMiddle]);
|
|
106
|
-
} else {
|
|
107
|
-
return checkMiddle([leftPart[0], leftPartMiddle], [rightPartMiddle, rightPart[1]]);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
const middle = Math.floor((0 + end) / 2);
|
|
111
|
-
const ellipsised = props.direction === 'middle' ? checkMiddle([0, middle], [middle, end]) : check(0, end);
|
|
112
|
-
setEllipsised(ellipsised);
|
|
113
|
-
}
|
|
114
|
-
document.body.removeChild(container);
|
|
115
|
-
}
|
|
116
|
-
useResizeEffect(calcEllipsised, rootRef);
|
|
117
|
-
useIsomorphicLayoutEffect(() => {
|
|
118
|
-
calcEllipsised();
|
|
119
|
-
}, [props.content, props.direction, props.rows, props.expandText, props.collapseText]);
|
|
120
|
-
const expandActionElement = !!props.expandText && withStopPropagation(props.stopPropagationForActionButtons, React.createElement("a", {
|
|
121
|
-
ref: expandElRef,
|
|
20
|
+
const {
|
|
21
|
+
content,
|
|
22
|
+
direction,
|
|
23
|
+
rows,
|
|
24
|
+
expandText,
|
|
25
|
+
collapseText,
|
|
26
|
+
stopPropagationForActionButtons,
|
|
27
|
+
onContentClick,
|
|
28
|
+
defaultExpanded
|
|
29
|
+
} = props;
|
|
30
|
+
// ============================ Refs ============================
|
|
31
|
+
const rootRef = React.useRef(null);
|
|
32
|
+
// ========================== Expanded ==========================
|
|
33
|
+
const [expanded, setExpanded] = React.useState(defaultExpanded);
|
|
34
|
+
const expandNode = expandText ? withStopPropagation(stopPropagationForActionButtons, React.createElement("a", {
|
|
122
35
|
onClick: () => {
|
|
123
36
|
setExpanded(true);
|
|
124
37
|
}
|
|
125
|
-
},
|
|
126
|
-
const
|
|
127
|
-
ref: collapseElRef,
|
|
38
|
+
}, expandText)) : null;
|
|
39
|
+
const collapseNode = collapseText ? withStopPropagation(stopPropagationForActionButtons, React.createElement("a", {
|
|
128
40
|
onClick: () => {
|
|
129
41
|
setExpanded(false);
|
|
130
42
|
}
|
|
131
|
-
},
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
};
|
|
43
|
+
}, collapseText)) : null;
|
|
44
|
+
// ========================== Ellipsis ==========================
|
|
45
|
+
const [measureNodes, forceResize] = useMeasure(rootRef, content, rows, direction, expanded, expandNode, collapseNode);
|
|
46
|
+
useResizeEffect(forceResize, rootRef);
|
|
47
|
+
// =========================== Render ===========================
|
|
137
48
|
return withNativeProps(props, React.createElement("div", {
|
|
138
49
|
ref: rootRef,
|
|
139
50
|
className: classPrefix,
|
|
140
51
|
onClick: e => {
|
|
141
52
|
if (e.target === e.currentTarget) {
|
|
142
|
-
|
|
53
|
+
onContentClick(e);
|
|
143
54
|
}
|
|
144
55
|
}
|
|
145
|
-
},
|
|
146
|
-
};
|
|
147
|
-
function pxToNumber(value) {
|
|
148
|
-
if (!value) return 0;
|
|
149
|
-
const match = value.match(/^\d*(\.\d*)?/);
|
|
150
|
-
return match ? Number(match[0]) : 0;
|
|
151
|
-
}
|
|
56
|
+
}, measureNodes));
|
|
57
|
+
};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
export default function useMeasure(containerRef: React.RefObject<HTMLDivElement>, content: string, rows: number, direction: 'start' | 'end' | 'middle', expanded: boolean, expandNode: React.ReactElement | null, collapseNode: React.ReactElement | null): readonly [React.JSX.Element, () => void];
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { useEvent } from 'rc-util';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import runes from 'runes2';
|
|
4
|
+
const ELLIPSIS_TEXT = '...';
|
|
5
|
+
const measureStyle = {
|
|
6
|
+
visibility: 'hidden',
|
|
7
|
+
whiteSpace: 'inherit',
|
|
8
|
+
lineHeight: 'inherit',
|
|
9
|
+
fontSize: 'inherit'
|
|
10
|
+
};
|
|
11
|
+
export default function useMeasure(containerRef, content, rows, direction, expanded, expandNode, collapseNode) {
|
|
12
|
+
const contentChars = React.useMemo(() => runes(content), [content]);
|
|
13
|
+
const [maxHeight, setMaxHeight] = React.useState(0);
|
|
14
|
+
const [walkingIndexes, setWalkingIndexes] = React.useState([0, 0]);
|
|
15
|
+
const midIndex = Math.ceil((walkingIndexes[0] + walkingIndexes[1]) / 2);
|
|
16
|
+
const [status, setStatus] = React.useState(100 /* STABLE_NO_ELLIPSIS */);
|
|
17
|
+
// ============================ Refs ============================
|
|
18
|
+
const singleRowMeasureRef = React.useRef(null);
|
|
19
|
+
const fullMeasureRef = React.useRef(null);
|
|
20
|
+
const midMeasureRef = React.useRef(null);
|
|
21
|
+
const startMeasure = useEvent(() => {
|
|
22
|
+
setStatus(1 /* PREPARE */);
|
|
23
|
+
setWalkingIndexes([0, direction === 'middle' ? Math.ceil(contentChars.length / 2) : contentChars.length]);
|
|
24
|
+
});
|
|
25
|
+
// Initialize
|
|
26
|
+
React.useLayoutEffect(() => {
|
|
27
|
+
startMeasure();
|
|
28
|
+
}, [contentChars, rows]);
|
|
29
|
+
// Measure element height
|
|
30
|
+
React.useLayoutEffect(() => {
|
|
31
|
+
var _a, _b;
|
|
32
|
+
if (status === 1 /* PREPARE */) {
|
|
33
|
+
const fullMeasureHeight = ((_a = fullMeasureRef.current) === null || _a === void 0 ? void 0 : _a.offsetHeight) || 0;
|
|
34
|
+
const singleRowMeasureHeight = ((_b = singleRowMeasureRef.current) === null || _b === void 0 ? void 0 : _b.offsetHeight) || 0;
|
|
35
|
+
const rowMeasureHeight = singleRowMeasureHeight * rows;
|
|
36
|
+
if (fullMeasureHeight <= rowMeasureHeight) {
|
|
37
|
+
setStatus(100 /* STABLE_NO_ELLIPSIS */);
|
|
38
|
+
} else {
|
|
39
|
+
setMaxHeight(rowMeasureHeight);
|
|
40
|
+
setStatus(2 /* MEASURE_WALKING */);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}, [status]);
|
|
44
|
+
// Walking measure
|
|
45
|
+
React.useLayoutEffect(() => {
|
|
46
|
+
var _a;
|
|
47
|
+
if (status === 2 /* MEASURE_WALKING */) {
|
|
48
|
+
const diff = walkingIndexes[1] - walkingIndexes[0];
|
|
49
|
+
const midHeight = ((_a = midMeasureRef.current) === null || _a === void 0 ? void 0 : _a.offsetHeight) || 0;
|
|
50
|
+
if (diff > 1) {
|
|
51
|
+
if (midHeight > maxHeight) {
|
|
52
|
+
setWalkingIndexes([walkingIndexes[0], midIndex]);
|
|
53
|
+
} else {
|
|
54
|
+
setWalkingIndexes([midIndex, walkingIndexes[1]]);
|
|
55
|
+
}
|
|
56
|
+
} else {
|
|
57
|
+
if (midHeight > maxHeight) {
|
|
58
|
+
setWalkingIndexes([walkingIndexes[0], walkingIndexes[0]]);
|
|
59
|
+
} else {
|
|
60
|
+
setWalkingIndexes([walkingIndexes[1], walkingIndexes[1]]);
|
|
61
|
+
}
|
|
62
|
+
setStatus(99 /* STABLE_ELLIPSIS */);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}, [status, walkingIndexes]);
|
|
66
|
+
// =========================== Render ===========================
|
|
67
|
+
/** Render by cut index */
|
|
68
|
+
const renderContent = index => {
|
|
69
|
+
const prefixContent = contentChars.slice(0, index);
|
|
70
|
+
const suffixContent = contentChars.slice(contentChars.length - index);
|
|
71
|
+
return React.createElement(React.Fragment, null, direction === 'start' && React.createElement(React.Fragment, null, expandNode, ELLIPSIS_TEXT), direction !== 'start' && prefixContent.join(''), direction === 'middle' && React.createElement(React.Fragment, null, ELLIPSIS_TEXT, expandNode, ELLIPSIS_TEXT), direction !== 'end' && suffixContent.join(''), direction === 'end' && React.createElement(React.Fragment, null, ELLIPSIS_TEXT, expandNode));
|
|
72
|
+
};
|
|
73
|
+
const finalContent = React.useMemo(() => {
|
|
74
|
+
if (expanded || status === 100 /* STABLE_NO_ELLIPSIS */) {
|
|
75
|
+
return React.createElement(React.Fragment, {
|
|
76
|
+
key: 'display'
|
|
77
|
+
}, content, status === 99 /* STABLE_ELLIPSIS */ && collapseNode);
|
|
78
|
+
}
|
|
79
|
+
if (status === 99 /* STABLE_ELLIPSIS */) {
|
|
80
|
+
return renderContent(midIndex);
|
|
81
|
+
}
|
|
82
|
+
return null;
|
|
83
|
+
}, [expanded, status, content, collapseNode, midIndex]);
|
|
84
|
+
const allNodes = React.createElement(React.Fragment, null, status === 1 /* PREPARE */ && React.createElement("div", {
|
|
85
|
+
key: 'full',
|
|
86
|
+
"aria-hidden": true,
|
|
87
|
+
ref: fullMeasureRef,
|
|
88
|
+
style: measureStyle
|
|
89
|
+
}, content, expandNode), status === 1 /* PREPARE */ && React.createElement("div", {
|
|
90
|
+
key: 'stable',
|
|
91
|
+
"aria-hidden": true,
|
|
92
|
+
ref: singleRowMeasureRef,
|
|
93
|
+
style: measureStyle
|
|
94
|
+
}, '\u00A0'), status === 2 /* MEASURE_WALKING */ && React.createElement("div", {
|
|
95
|
+
key: 'walking-mid',
|
|
96
|
+
"aria-hidden": true,
|
|
97
|
+
ref: midMeasureRef,
|
|
98
|
+
style: measureStyle
|
|
99
|
+
}, renderContent(midIndex)), finalContent);
|
|
100
|
+
return [allNodes, startMeasure];
|
|
101
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import type { FC, ReactNode } from 'react';
|
|
3
|
+
import { NativeProps } from '../../utils/native-props';
|
|
4
|
+
import { PropagationEvent } from '../../utils/with-stop-propagation';
|
|
5
|
+
export declare type EllipsisProps = {
|
|
6
|
+
content: string;
|
|
7
|
+
direction?: 'start' | 'end' | 'middle';
|
|
8
|
+
rows?: number;
|
|
9
|
+
expandText?: ReactNode;
|
|
10
|
+
collapseText?: ReactNode;
|
|
11
|
+
stopPropagationForActionButtons?: PropagationEvent[];
|
|
12
|
+
onContentClick?: (e: React.MouseEvent<HTMLDivElement, MouseEvent>) => void;
|
|
13
|
+
defaultExpanded?: boolean;
|
|
14
|
+
} & NativeProps;
|
|
15
|
+
export declare const Ellipsis: FC<EllipsisProps>;
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import React, { useMemo, useRef, useState } from 'react';
|
|
2
|
+
import runes from 'runes2';
|
|
3
|
+
import { mergeProps } from '../../utils/with-default-props';
|
|
4
|
+
import { withNativeProps } from '../../utils/native-props';
|
|
5
|
+
import { useResizeEffect } from '../../utils/use-resize-effect';
|
|
6
|
+
import { useIsomorphicLayoutEffect } from 'ahooks';
|
|
7
|
+
import { withStopPropagation } from '../../utils/with-stop-propagation';
|
|
8
|
+
const classPrefix = `adm-ellipsis`;
|
|
9
|
+
const defaultProps = {
|
|
10
|
+
direction: 'end',
|
|
11
|
+
rows: 1,
|
|
12
|
+
expandText: '',
|
|
13
|
+
content: '',
|
|
14
|
+
collapseText: '',
|
|
15
|
+
stopPropagationForActionButtons: [],
|
|
16
|
+
onContentClick: () => {},
|
|
17
|
+
defaultExpanded: false
|
|
18
|
+
};
|
|
19
|
+
export const Ellipsis = p => {
|
|
20
|
+
const props = mergeProps(defaultProps, p);
|
|
21
|
+
const rootRef = useRef(null);
|
|
22
|
+
const expandElRef = useRef(null);
|
|
23
|
+
const collapseElRef = useRef(null);
|
|
24
|
+
const [ellipsised, setEllipsised] = useState({});
|
|
25
|
+
const [expanded, setExpanded] = useState(props.defaultExpanded);
|
|
26
|
+
const [exceeded, setExceeded] = useState(false);
|
|
27
|
+
const chars = useMemo(() => runes(props.content), [props.content]);
|
|
28
|
+
function getSubString(start, end) {
|
|
29
|
+
return chars.slice(start, end).join('');
|
|
30
|
+
}
|
|
31
|
+
function calcEllipsised() {
|
|
32
|
+
var _a, _b;
|
|
33
|
+
const root = rootRef.current;
|
|
34
|
+
if (!root) return;
|
|
35
|
+
const originDisplay = root.style.display;
|
|
36
|
+
root.style.display = 'block';
|
|
37
|
+
const originStyle = window.getComputedStyle(root);
|
|
38
|
+
const container = document.createElement('div');
|
|
39
|
+
const styleNames = Array.prototype.slice.apply(originStyle);
|
|
40
|
+
styleNames.forEach(name => {
|
|
41
|
+
container.style.setProperty(name, originStyle.getPropertyValue(name));
|
|
42
|
+
});
|
|
43
|
+
root.style.display = originDisplay;
|
|
44
|
+
container.style.height = 'auto';
|
|
45
|
+
container.style.minHeight = 'auto';
|
|
46
|
+
container.style.maxHeight = 'auto';
|
|
47
|
+
container.style.textOverflow = 'clip';
|
|
48
|
+
container.style.webkitLineClamp = 'unset';
|
|
49
|
+
container.style.display = 'block';
|
|
50
|
+
const lineHeight = pxToNumber(originStyle.lineHeight);
|
|
51
|
+
const maxHeight = Math.floor(lineHeight * (props.rows + 0.5) + pxToNumber(originStyle.paddingTop) + pxToNumber(originStyle.paddingBottom));
|
|
52
|
+
container.innerText = props.content;
|
|
53
|
+
document.body.appendChild(container);
|
|
54
|
+
if (container.offsetHeight <= maxHeight) {
|
|
55
|
+
setExceeded(false);
|
|
56
|
+
} else {
|
|
57
|
+
setExceeded(true);
|
|
58
|
+
const end = props.content.length;
|
|
59
|
+
const collapseEl = typeof props.collapseText === 'string' ? props.collapseText : (_a = collapseElRef.current) === null || _a === void 0 ? void 0 : _a.innerHTML;
|
|
60
|
+
const expandEl = typeof props.expandText === 'string' ? props.expandText : (_b = expandElRef.current) === null || _b === void 0 ? void 0 : _b.innerHTML;
|
|
61
|
+
const actionText = expanded ? collapseEl : expandEl;
|
|
62
|
+
function check(left, right) {
|
|
63
|
+
if (right - left <= 1) {
|
|
64
|
+
if (props.direction === 'end') {
|
|
65
|
+
return {
|
|
66
|
+
leading: getSubString(0, left) + '...'
|
|
67
|
+
};
|
|
68
|
+
} else {
|
|
69
|
+
return {
|
|
70
|
+
tailing: '...' + getSubString(right, end)
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const middle = Math.round((left + right) / 2);
|
|
75
|
+
if (props.direction === 'end') {
|
|
76
|
+
container.innerHTML = getSubString(0, middle) + '...' + actionText;
|
|
77
|
+
} else {
|
|
78
|
+
container.innerHTML = actionText + '...' + getSubString(middle, end);
|
|
79
|
+
}
|
|
80
|
+
if (container.offsetHeight <= maxHeight) {
|
|
81
|
+
if (props.direction === 'end') {
|
|
82
|
+
return check(middle, right);
|
|
83
|
+
} else {
|
|
84
|
+
return check(left, middle);
|
|
85
|
+
}
|
|
86
|
+
} else {
|
|
87
|
+
if (props.direction === 'end') {
|
|
88
|
+
return check(left, middle);
|
|
89
|
+
} else {
|
|
90
|
+
return check(middle, right);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
function checkMiddle(leftPart, rightPart) {
|
|
95
|
+
if (leftPart[1] - leftPart[0] <= 1 && rightPart[1] - rightPart[0] <= 1) {
|
|
96
|
+
return {
|
|
97
|
+
leading: getSubString(0, leftPart[0]) + '...',
|
|
98
|
+
tailing: '...' + getSubString(rightPart[1], end)
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
const leftPartMiddle = Math.floor((leftPart[0] + leftPart[1]) / 2);
|
|
102
|
+
const rightPartMiddle = Math.ceil((rightPart[0] + rightPart[1]) / 2);
|
|
103
|
+
container.innerHTML = getSubString(0, leftPartMiddle) + '...' + actionText + '...' + getSubString(rightPartMiddle, end);
|
|
104
|
+
if (container.offsetHeight <= maxHeight) {
|
|
105
|
+
return checkMiddle([leftPartMiddle, leftPart[1]], [rightPart[0], rightPartMiddle]);
|
|
106
|
+
} else {
|
|
107
|
+
return checkMiddle([leftPart[0], leftPartMiddle], [rightPartMiddle, rightPart[1]]);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
const middle = Math.floor((0 + end) / 2);
|
|
111
|
+
const ellipsised = props.direction === 'middle' ? checkMiddle([0, middle], [middle, end]) : check(0, end);
|
|
112
|
+
setEllipsised(ellipsised);
|
|
113
|
+
}
|
|
114
|
+
document.body.removeChild(container);
|
|
115
|
+
}
|
|
116
|
+
useResizeEffect(calcEllipsised, rootRef);
|
|
117
|
+
useIsomorphicLayoutEffect(() => {
|
|
118
|
+
calcEllipsised();
|
|
119
|
+
}, [props.content, props.direction, props.rows, props.expandText, props.collapseText]);
|
|
120
|
+
const expandActionElement = !!props.expandText && withStopPropagation(props.stopPropagationForActionButtons, React.createElement("a", {
|
|
121
|
+
ref: expandElRef,
|
|
122
|
+
onClick: () => {
|
|
123
|
+
setExpanded(true);
|
|
124
|
+
}
|
|
125
|
+
}, props.expandText));
|
|
126
|
+
const collapseActionElement = !!props.collapseText && withStopPropagation(props.stopPropagationForActionButtons, React.createElement("a", {
|
|
127
|
+
ref: collapseElRef,
|
|
128
|
+
onClick: () => {
|
|
129
|
+
setExpanded(false);
|
|
130
|
+
}
|
|
131
|
+
}, props.collapseText));
|
|
132
|
+
const renderContent = () => {
|
|
133
|
+
if (!exceeded) return props.content;
|
|
134
|
+
if (expanded) return React.createElement(React.Fragment, null, props.content, collapseActionElement);
|
|
135
|
+
return React.createElement(React.Fragment, null, ellipsised.leading, expandActionElement, ellipsised.tailing);
|
|
136
|
+
};
|
|
137
|
+
return withNativeProps(props, React.createElement("div", {
|
|
138
|
+
ref: rootRef,
|
|
139
|
+
className: classPrefix,
|
|
140
|
+
onClick: e => {
|
|
141
|
+
if (e.target === e.currentTarget) {
|
|
142
|
+
props.onContentClick(e);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}, renderContent()));
|
|
146
|
+
};
|
|
147
|
+
function pxToNumber(value) {
|
|
148
|
+
if (!value) return 0;
|
|
149
|
+
const match = value.match(/^\d*(\.\d*)?/);
|
|
150
|
+
return match ? Number(match[0]) : 0;
|
|
151
|
+
}
|