Debian Codegraph @

node/@patternfly/react-core/dist/js/components/Truncate/Truncate.js

raw
1 "use strict";
2 Object.defineProperty(exports, "__esModule", { value: true });
3 exports.Truncate = exports.TruncatePosition = void 0;
4 const tslib_1 = require("tslib");
5 const jsx_runtime_1 = require("react/jsx-runtime");
6 const react_1 = require("react");
7 const truncate_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/Truncate/truncate"));
8 const react_styles_1 = require("@patternfly/react-styles");
9 const Tooltip_1 = require("../Tooltip");
10 const helpers_1 = require("../../helpers");
11 const resizeObserver_1 = require("../../helpers/resizeObserver");
12 const util_1 = require("../../helpers/util");
13 var TruncatePosition;
14 (function (TruncatePosition) {
15 TruncatePosition["start"] = "start";
16 TruncatePosition["end"] = "end";
17 TruncatePosition["middle"] = "middle";
18 })(TruncatePosition || (exports.TruncatePosition = TruncatePosition = {}));
19 const truncateStyles = {
20 start: truncate_1.default.truncateEnd,
21 end: truncate_1.default.truncateStart
22 };
23 const minWidthCharacters = 12;
24 const sliceTrailingContent = (str, slice) => [str.slice(0, str.length - slice), str.slice(-slice)];
25 const TruncateBase = (_a) => {
26 var { className, href, position = 'end', tooltipPosition = 'top', tooltipProps, trailingNumChars = 7, maxCharsDisplayed, omissionContent = '\u2026', content, innerRef } = _a, props = tslib_1.__rest(_a, ["className", "href", "position", "tooltipPosition", "tooltipProps", "trailingNumChars", "maxCharsDisplayed", "omissionContent", "content", "innerRef"]);
27 const [isTruncated, setIsTruncated] = (0, react_1.useState)(true);
28 const [parentElement, setParentElement] = (0, react_1.useState)(null);
29 const [textElement, setTextElement] = (0, react_1.useState)(null);
30 const [shouldRenderByMaxChars, setShouldRenderByMaxChars] = (0, react_1.useState)(maxCharsDisplayed > 0);
31 const textRef = (0, react_1.useRef)(null);
32 (0, react_1.useImperativeHandle)(innerRef, () => textRef.current);
33 const defaultSubParentRef = (0, react_1.useRef)(null);
34 const subParentRef = (tooltipProps === null || tooltipProps === void 0 ? void 0 : tooltipProps.triggerRef) || defaultSubParentRef;
35 const observer = (0, react_1.useRef)(null);
36 if (maxCharsDisplayed <= 0) {
37 // eslint-disable-next-line no-console
38 console.warn('Truncate: the maxCharsDisplayed must be greater than 0, otherwise no content will be visible.');
39 }
40 const getActualWidth = (element) => {
41 const computedStyle = getComputedStyle(element);
42 return (parseFloat(computedStyle.width) -
43 parseFloat(computedStyle.paddingLeft) -
44 parseFloat(computedStyle.paddingRight) -
45 parseFloat(computedStyle.borderRight) -
46 parseFloat(computedStyle.borderLeft));
47 };
48 const calculateTotalTextWidth = (element, trailingNumChars, content) => {
49 const firstTextWidth = element.scrollWidth;
50 const firstTextLength = content.length;
51 return (firstTextWidth / firstTextLength) * trailingNumChars + firstTextWidth;
52 };
53 (0, react_1.useEffect)(() => {
54 if (textRef && textRef.current && !textElement) {
55 setTextElement(textRef.current);
56 }
57 }, [textRef, textElement]);
58 (0, react_1.useEffect)(() => {
59 const refElement = (0, helpers_1.getReferenceElement)(subParentRef);
60 if ((refElement === null || refElement === void 0 ? void 0 : refElement.parentElement) && !parentElement) {
61 setParentElement(refElement.parentElement);
62 }
63 }, [subParentRef, parentElement]);
64 (0, react_1.useEffect)(() => {
65 if (textElement && parentElement && !observer.current && !shouldRenderByMaxChars) {
66 const totalTextWidth = calculateTotalTextWidth(textElement, trailingNumChars, content);
67 const textWidth = position === 'middle' ? totalTextWidth : textElement.scrollWidth;
68 const debouncedHandleResize = (0, util_1.debounce)(() => {
69 const parentWidth = getActualWidth(parentElement);
70 setIsTruncated(textWidth >= parentWidth);
71 }, 500);
72 const observer = (0, resizeObserver_1.getResizeObserver)(parentElement, debouncedHandleResize);
73 return () => {
74 observer();
75 };
76 }
77 }, [textElement, parentElement, trailingNumChars, content, position, shouldRenderByMaxChars]);
78 (0, react_1.useEffect)(() => {
79 if (shouldRenderByMaxChars) {
80 setIsTruncated(content.length > maxCharsDisplayed);
81 }
82 }, [shouldRenderByMaxChars, content.length, maxCharsDisplayed]);
83 (0, react_1.useEffect)(() => {
84 setShouldRenderByMaxChars(maxCharsDisplayed > 0);
85 }, [maxCharsDisplayed]);
86 const lrmEntity = (0, jsx_runtime_1.jsx)(react_1.Fragment, { children: "\u200E" });
87 const isStartPosition = position === TruncatePosition.start;
88 const isEndPosition = position === TruncatePosition.end;
89 const renderResizeObserverContent = () => {
90 if (isEndPosition || isStartPosition) {
91 return ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: (0, jsx_runtime_1.jsxs)("span", { ref: textRef, className: truncateStyles[position], children: [isStartPosition && lrmEntity, content, isStartPosition && lrmEntity] }) }));
92 }
93 const shouldSliceContent = content.length - trailingNumChars > minWidthCharacters;
94 return ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: (0, jsx_runtime_1.jsxs)(react_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("span", { ref: textRef, className: truncate_1.default.truncateStart, children: shouldSliceContent ? sliceTrailingContent(content, trailingNumChars)[0] : content }), shouldSliceContent && ((0, jsx_runtime_1.jsx)("span", { className: truncate_1.default.truncateEnd, children: sliceTrailingContent(content, trailingNumChars)[1] }))] }) }));
95 };
96 const renderMaxDisplayContent = () => {
97 const renderVisibleContent = (contentToRender) => ((0, jsx_runtime_1.jsx)("span", { className: `${truncate_1.default.truncate}__text`, children: contentToRender }));
98 if (!isTruncated) {
99 return renderVisibleContent(content);
100 }
101 const omissionElement = ((0, jsx_runtime_1.jsx)("span", { className: `${truncate_1.default.truncate}__omission`, "aria-hidden": "true", children: omissionContent }));
102 const renderVisuallyHiddenContent = (contentToHide) => ((0, jsx_runtime_1.jsx)("span", { className: "pf-v6-screen-reader", children: contentToHide }));
103 if (position === TruncatePosition.start) {
104 return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [renderVisuallyHiddenContent(content.slice(0, maxCharsDisplayed * -1)), omissionElement, renderVisibleContent(content.slice(maxCharsDisplayed * -1))] }));
105 }
106 if (isEndPosition) {
107 return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [renderVisibleContent(content.slice(0, maxCharsDisplayed)), omissionElement, renderVisuallyHiddenContent(content.slice(maxCharsDisplayed))] }));
108 }
109 const trueMiddleStart = Math.floor(maxCharsDisplayed / 2);
110 const trueMiddleEnd = Math.ceil(maxCharsDisplayed / 2) * -1;
111 return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [renderVisibleContent(content.slice(0, trueMiddleStart)), omissionElement, renderVisuallyHiddenContent(content.slice(trueMiddleStart, trueMiddleEnd)), renderVisibleContent(content.slice(trueMiddleEnd))] }));
112 };
113 const TruncateWrapper = href ? 'a' : 'span';
114 const truncateBody = ((0, jsx_runtime_1.jsx)(TruncateWrapper, Object.assign({ ref: !(tooltipProps === null || tooltipProps === void 0 ? void 0 : tooltipProps.triggerRef) ? subParentRef : null, href: href, className: (0, react_styles_1.css)(truncate_1.default.truncate, shouldRenderByMaxChars && truncate_1.default.modifiers.fixed, className) }, (isTruncated && !href && !(tooltipProps === null || tooltipProps === void 0 ? void 0 : tooltipProps.triggerRef) && { tabIndex: 0 }), props, { children: !shouldRenderByMaxChars ? renderResizeObserverContent() : renderMaxDisplayContent() })));
115 return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [isTruncated && ((0, jsx_runtime_1.jsx)(Tooltip_1.Tooltip, Object.assign({ hidden: !isTruncated, position: tooltipPosition, content: content, triggerRef: subParentRef }, tooltipProps))), truncateBody] }));
116 };
117 exports.Truncate = (0, react_1.forwardRef)((props, ref) => ((0, jsx_runtime_1.jsx)(TruncateBase, Object.assign({ innerRef: ref }, props))));
118 exports.Truncate.displayName = 'Truncate';
119 //# sourceMappingURL=Truncate.js.map