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