@@ -8,8 +8,7 @@ import { EventHandler } from "./bootstrap-helper";
88 * @return {void } This function does not return any value.
99 */
1010function initCardBodies ( ) {
11-
12- const cardBodies = document . querySelectorAll ( '.card-body' ) ;
11+ const cardBodies = document . querySelectorAll ( ".card-body" ) ;
1312
1413 // If there are no cardBodies and you have not yet exceeded 3 retries
1514 let retryCount = 0 ;
@@ -20,12 +19,12 @@ function initCardBodies() {
2019
2120 // If after 3 attempts there are still no cardBodies, we simply exit
2221 if ( cardBodies . length === 0 ) {
23- console . warn ( ' No .card-body found after 3 retries.' ) ;
22+ console . warn ( " No .card-body found after 3 retries." ) ;
2423 return ;
2524 }
2625
2726 cardBodies . forEach ( ( cardBody , index ) => {
28- const paragraph = cardBody . querySelector ( ' div p' ) ;
27+ const paragraph = cardBody . querySelector ( " div p" ) ;
2928 const originalText = paragraph . textContent ;
3029 const style = window . getComputedStyle ( cardBody ) ;
3130
@@ -37,51 +36,58 @@ function initCardBodies() {
3736 const fontSize = parseFloat ( style . fontSize ) ;
3837
3938 // Calculate the actual line height. If lineHeight is not a number, estimate using fontSize
40- const actualLineHeight = isNaN ( lineHeight ) ? parseFloat ( style . lineHeight ) * fontSize : lineHeight ;
39+ const actualLineHeight = isNaN ( lineHeight )
40+ ? parseFloat ( style . lineHeight ) * fontSize
41+ : lineHeight ;
4142
4243 // Calculate the maximum height allowed for the paragraph (lines × line height)
4344 const maxHeight = lineClamp * actualLineHeight ;
4445
4546 // Check if the paragraph exceeds the maximum allowed height
4647 if ( paragraph . offsetHeight >= maxHeight ) {
47- let visibleText = '' ;
48- const words = originalText . split ( ' ' ) ;
48+ let visibleText = "" ;
49+ const words = originalText . split ( " " ) ;
4950 let visibleWordCount = 0 ;
50- let tempText = '' ;
51+ let tempText = "" ;
5152
5253 // Add words one by one until the text height exceeds the max allowed height
53- while ( visibleWordCount < words . length && getTextHeight ( tempText + ( tempText ? ' ' : '' ) + words [ visibleWordCount ] , paragraph ) <= maxHeight ) {
54- tempText += ( tempText ? ' ' : '' ) + words [ visibleWordCount ] ;
54+ while (
55+ visibleWordCount < words . length &&
56+ getTextHeight (
57+ tempText + ( tempText ? " " : "" ) + words [ visibleWordCount ] ,
58+ paragraph
59+ ) <= maxHeight
60+ ) {
61+ tempText += ( tempText ? " " : "" ) + words [ visibleWordCount ] ;
5562 visibleWordCount ++ ;
5663 }
57- visibleText = tempText + ' ...' ;
64+ visibleText = tempText + " ..." ;
5865
5966 // Create a new hidden element to store the truncated text
60- const visibleTextElementId = `visible-text-${ Math . random ( ) . toString ( 36 ) . substring ( 7 ) } ` ;
61- const visibleTextElement = document . createElement ( 'div' ) ;
67+ const visibleTextElementId = `visible-text-${ Math . random ( )
68+ . toString ( 36 )
69+ . substring ( 7 ) } `;
70+ const visibleTextElement = document . createElement ( "div" ) ;
6271 visibleTextElement . id = visibleTextElementId ;
6372 visibleTextElement . textContent = visibleText ;
64- visibleTextElement . style . position = ' absolute' ;
73+ visibleTextElement . style . position = " absolute" ;
6574 visibleTextElement . style . top = `${ paragraph . offsetTop } px` ;
6675 visibleTextElement . style . left = `${ paragraph . offsetLeft } px` ;
6776 visibleTextElement . style . width = `${ paragraph . offsetWidth } px` ;
6877 visibleTextElement . style . height = `${ paragraph . offsetHeight } px` ;
69- visibleTextElement . style . opacity = '0' ;
70- visibleTextElement . style . pointerEvents = ' none' ;
71- visibleTextElement . style . zIndex = '1' ;
78+ visibleTextElement . style . opacity = "0" ;
79+ visibleTextElement . style . pointerEvents = " none" ;
80+ visibleTextElement . style . zIndex = "1" ;
7281
7382 // Add the hidden element to the DOM
7483 cardBody . appendChild ( visibleTextElement ) ;
7584
76- paragraph . setAttribute ( ' aria-describedby' , visibleTextElementId ) ;
85+ paragraph . setAttribute ( " aria-describedby" , visibleTextElementId ) ;
7786
7887 // Hide the original paragraph from screen readers
79- paragraph . setAttribute ( 'aria-hidden' , 'true' ) ;
80-
88+ paragraph . setAttribute ( "aria-hidden" , "true" ) ;
8189 }
8290 } ) ;
83-
84-
8591}
8692
8793/**
@@ -96,14 +102,14 @@ function getTextHeight(text, element) {
96102 const tempElement = document . createElement ( element . tagName ) ;
97103 tempElement . style . font = window . getComputedStyle ( element ) . font ;
98104 tempElement . style . width = window . getComputedStyle ( element ) . width ;
99- tempElement . style . whiteSpace = ' pre-wrap' ;
105+ tempElement . style . whiteSpace = " pre-wrap" ;
100106 tempElement . textContent = text ;
101107 document . body . appendChild ( tempElement ) ;
102108 const height = tempElement . offsetHeight ;
103109 document . body . removeChild ( tempElement ) ;
104110 return height ;
105111}
106112
107- EventHandler . on ( window , ' load.uds.card-bodies' , initCardBodies ) ;
113+ EventHandler . on ( window , " load.uds.card-bodies" , initCardBodies ) ;
108114
109115export { initCardBodies } ;
0 commit comments