@@ -8,6 +8,7 @@ type RenderDescription = (embed: any, description: string, headings: boolean) =>
88
99const logger = moonlight . getLogger ( "Better YouTube Embeds - Description" ) ;
1010const descriptionCache = new Map < string , string > ( ) ;
11+ const sizeCache = new Map < string , number > ( ) ;
1112
1213const API_KEY = "AIzaSyCpphGplamUhCCEIcum1VyDXBt0i1nOqac" ; // one of Google's own
1314const FAKE_EMBED = { type : "rich" } ;
@@ -27,6 +28,7 @@ function YTDescription({
2728 const [ fullDescription , setFullDescription ] = React . useState (
2829 descriptionCache . has ( videoId ) ? descriptionCache . get ( videoId ) : description
2930 ) ;
31+ const [ firstLineLength , setFirstLineLength ] = React . useState ( sizeCache . has ( videoId ) ? sizeCache . get ( videoId ) : - 1 ) ;
3032
3133 React . useEffect ( ( ) => {
3234 if ( ! descriptionCache . has ( videoId ) )
@@ -53,15 +55,35 @@ function YTDescription({
5355 }
5456 } ) ;
5557
56- const lines = fullDescription ! . split ( "\n" ) ;
58+ const lines = fullDescription ! . trim ( ) . split ( "\n" ) ;
59+ const descClass = EmbedClasses . embedDescription + " " + EmbedClasses . embedMargin ;
60+
61+ if ( lines . length === 1 && firstLineLength === - 1 ) {
62+ const inner = document . createElement ( "span" ) ;
63+ inner . innerText = fullDescription ! ;
64+ const wrapper = document . createElement ( "div" ) ;
65+ wrapper . className = descClass ;
66+ wrapper . appendChild ( inner ) ;
67+ wrapper . style . position = "absolute" ;
68+ wrapper . style . opacity = "0" ;
69+ wrapper . style . pointerEvents = "none" ;
70+ document . body . appendChild ( wrapper ) ;
71+
72+ const width = wrapper . clientWidth ;
73+
74+ setFirstLineLength ( width ) ;
75+ sizeCache . set ( videoId , width ) ;
76+
77+ setTimeout ( ( ) => wrapper . remove ( ) , 0 ) ;
78+ }
5779
5880 const rendered = renderDescription ( FAKE_EMBED , fullDescription ! , false ) ;
5981 const firstLine = renderDescription ( FAKE_EMBED , lines [ 0 ] , false ) ;
6082
61- return lines . length === 1 && description . length <= 40 ? (
62- < div className = { EmbedClasses . embedDescription + " " + EmbedClasses . embedMargin } > { rendered } </ div >
83+ return lines . length === 1 && firstLineLength ! > 0 && firstLineLength ! < 400 ? (
84+ < div className = { descClass } > { rendered } </ div >
6385 ) : (
64- < div className = { EmbedClasses . embedDescription + " " + EmbedClasses . embedMargin } >
86+ < div className = { descClass } >
6587 { expanded ? rendered : < div className = "betterEmbedsYT-description-firstLine" > { firstLine } </ div > }
6688 < Clickable className = "betterEmbedsYT-description-button" onClick = { ( ) => setExpanded ( ! expanded ) } >
6789 { expanded ? "Show less" : "Show more" }
0 commit comments