pb-sxp-ui 1.15.22 → 1.15.23

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/dist/pb-ui.js CHANGED
@@ -10202,69 +10202,49 @@
10202
10202
  };
10203
10203
  var Modal$1 = React.memo(Modal);
10204
10204
 
10205
- /*
10206
- * @Author: binruan@chatlabs.com
10207
- * @Date: 2023-12-26 16:11:34
10208
- * @LastEditors: binruan@chatlabs.com
10209
- * @LastEditTime: 2024-11-07 14:27:18
10210
- * @FilePath: \pb-sxp-ui\src\core\components\SxpPageRender\ExpandableText.tsx
10211
- *
10212
- */
10213
- const limitTextLastWholeWord = (originalText = '', limit) => {
10214
- // 匹配到中文
10215
- const chineseRegex = /[\u4e00-\u9fa5]+/;
10216
- if (chineseRegex.test(originalText)) {
10217
- return originalText.slice(0, 54);
10218
- }
10219
- const words = originalText.split(' ');
10220
- const newWords = [];
10221
- for (let i = 0; i < words.length; i++) {
10222
- newWords.push(words[i]);
10223
- const tempText = newWords.join(' ');
10224
- if (tempText.length >= limit)
10225
- break;
10226
- }
10227
- // const _words = newWords.length === words.length ? newWords : newWords.slice(0, newWords.length - 1);
10228
- const _words = newWords.length > 1 && newWords.length < words.length ? newWords.slice(0, newWords.length - 1) : newWords;
10229
- return _words.join(' ') + ' ';
10230
- };
10231
10205
  const ExpandableText = ({ text, maxStr = 108, style, className, onClick, foldText, unfoldText, isPost, onChange }) => {
10232
10206
  const [isShowMore, setIsShowMore] = React.useState(false);
10233
10207
  const [isShow, setIsShow] = React.useState(false);
10234
- const lineClamp = Number((style === null || style === void 0 ? void 0 : style.lineClamp) || 2);
10208
+ const _a = style || {}, { lineClamp } = _a, textStyle = __rest(_a, ["lineClamp"]);
10209
+ const textLineClamp = Number(lineClamp || 2);
10235
10210
  const multiRow = React.useRef(null);
10236
- const multiRowCopy = React.useRef(null);
10237
- const handleClick = React.useCallback(() => {
10211
+ const observerRef = React.useRef();
10212
+ const handleClick = () => {
10238
10213
  setIsShowMore(!isShowMore);
10239
- // onChange?.(!isShowMore);
10240
- }, [isShowMore, onChange]);
10241
- React.useMemo(() => {
10242
- return !isShowMore && text.length > maxStr ? limitTextLastWholeWord(text, maxStr) + '...' : text;
10243
- }, [text, maxStr, isShowMore]);
10214
+ };
10215
+ const checkOverflow = React.useCallback(() => {
10216
+ if (!multiRow.current || !isPost)
10217
+ return;
10218
+ const lineHeight = parseInt(getComputedStyle(multiRow.current).lineHeight) || 20;
10219
+ const maxHeight = lineHeight * textLineClamp;
10220
+ // 添加1px容错,解决部分机型计算误差
10221
+ setIsShow(multiRow.current.scrollHeight > maxHeight + 1);
10222
+ }, [isPost, text, textLineClamp]);
10244
10223
  React.useEffect(() => {
10245
- var _a, _b;
10246
- if (multiRowCopy === null || multiRowCopy === void 0 ? void 0 : multiRowCopy.current)
10247
- multiRowCopy.current.style.display = 'block';
10248
- if (((_a = multiRowCopy === null || multiRowCopy === void 0 ? void 0 : multiRowCopy.current) === null || _a === void 0 ? void 0 : _a.offsetHeight) > ((_b = multiRow === null || multiRow === void 0 ? void 0 : multiRow.current) === null || _b === void 0 ? void 0 : _b.offsetHeight) && isPost) {
10249
- setIsShow(true);
10250
- }
10251
- else {
10252
- setIsShow(false);
10253
- }
10254
- multiRowCopy.current.style.display = 'none';
10255
- }, [isPost, text]);
10256
- return (React.createElement("div", { className: className, style: Object.assign(Object.assign({}, style), { transform: 'translate3d(0px, 0px, 0px)' }), hidden: !text || text === '' },
10257
- React.createElement("div", { ref: multiRow, style: {
10258
- overflow: 'hidden',
10259
- WebkitLineClamp: !isPost || isShowMore ? '' : lineClamp,
10224
+ if (!multiRow.current)
10225
+ return;
10226
+ // 使用ResizeObserver监听尺寸变化
10227
+ observerRef.current = new ResizeObserver(checkOverflow);
10228
+ observerRef.current.observe(multiRow.current);
10229
+ // 初始检查
10230
+ checkOverflow();
10231
+ return () => {
10232
+ var _a;
10233
+ (_a = observerRef.current) === null || _a === void 0 ? void 0 : _a.disconnect();
10234
+ };
10235
+ }, [checkOverflow]);
10236
+ return (React.createElement("div", { className: className, style: Object.assign(Object.assign({}, textStyle), { transform: 'translate3d(0px, 0px, 0px)' }), hidden: !text || text === '' },
10237
+ React.createElement("div", { ref: multiRow, style: Object.assign(Object.assign({}, (!isShowMore &&
10238
+ isPost && {
10239
+ WebkitLineClamp: textLineClamp,
10240
+ lineClamp: textLineClamp,
10260
10241
  textOverflow: 'ellipsis',
10242
+ overflow: 'hidden',
10261
10243
  display: '-webkit-box',
10262
- WebkitBoxOrient: 'vertical',
10263
- wordBreak: 'break-word'
10264
- }, dangerouslySetInnerHTML: { __html: setFontForText(text === null || text === void 0 ? void 0 : text.replace(/\n/g, '</br>'), style) } }),
10265
- React.createElement("div", { ref: multiRowCopy, dangerouslySetInnerHTML: { __html: setFontForText(text === null || text === void 0 ? void 0 : text.replace(/\n/g, '</br>'), style) } }),
10244
+ WebkitBoxOrient: 'vertical'
10245
+ })), { wordBreak: 'break-word' }), dangerouslySetInnerHTML: { __html: setFontForText(text === null || text === void 0 ? void 0 : text.replace(/\n/g, '</br>'), textStyle) } }),
10266
10246
  text && isPost && isShow && (React.createElement("button", { "aria-label": isShowMore ? unfoldText || 'show less' : foldText || 'show more', style: { textDecoration: 'underline', cursor: 'pointer' }, onClick: onClick !== null && onClick !== void 0 ? onClick : handleClick, dangerouslySetInnerHTML: {
10267
- __html: setFontForText(isShowMore ? unfoldText || 'show less' : foldText || 'show more', style)
10247
+ __html: setFontForText(isShowMore ? unfoldText || 'show less' : foldText || 'show more', textStyle)
10268
10248
  } }))));
10269
10249
  };
10270
10250
  var ExpandableText$1 = React.memo(ExpandableText);