@@ -22,31 +22,6 @@ export const CommentText: React.FunctionComponent<ICommentTextProps> = (
2222 const { text, mentions } = props ;
2323 const mentionsResults : Mention [ ] = mentions ;
2424
25- useEffect ( ( ) => {
26- const hasMentions = mentions ?. length ? true : false ;
27- let result : string | JSX . Element [ ] = text ;
28- if ( hasMentions ) {
29- result = regexifyString ( {
30- pattern : / @ m e n t i o n & # 1 2 3 ; \d + & # 1 2 5 ; / g,
31- decorator : ( match , index ) => {
32- const mention = mentionsResults [ index ] ;
33- const _name = `@${ mention . name } ` ;
34- return (
35- < >
36- < LivePersona
37- serviceScope = { serviceScope }
38- upn = { mention . email }
39- template = { < span style = { { color : theme . themePrimary , whiteSpace : "nowrap" } } > { _name } </ span > }
40- />
41- </ >
42- ) ;
43- } ,
44- input : text ,
45- } ) as JSX . Element [ ] ;
46- }
47- setCommentText ( result ) ;
48- } , [ ] ) ;
49-
5025 const convertTextToLinksAndText = (
5126 text : string
5227 ) : ( string | JSX . Element ) [ ] => {
@@ -69,34 +44,80 @@ export const CommentText: React.FunctionComponent<ICommentTextProps> = (
6944 } ) ;
7045 } ;
7146
72- return (
73- < >
74- < Stack wrap horizontal horizontalAlign = "start" verticalAlign = "center" >
75- { isArray ( commentText ) ? (
76- // eslint-disable-next-line @typescript-eslint/no-explicit-any
77- ( commentText as any [ ] ) . map ( ( el , i ) => {
78- if ( isObject ( el ) ) {
79- return (
80- < span key = { i } style = { { paddingRight : 5 } } >
81- { el }
82- </ span >
83- ) ;
84- } else {
85- const _el : string = el . trim ( ) ;
86- if ( _el . length ) {
47+ useEffect ( ( ) => {
48+ const hasMentions = mentions ?. length ? true : false ;
49+ const lines = text . split ( / \r ? \n / ) ;
50+ const result : JSX . Element [ ] = [ ] ;
51+ lines . forEach ( ( line , idx ) => {
52+ let lineContent : string | JSX . Element [ ] = line ;
53+ if ( hasMentions ) {
54+ lineContent = regexifyString ( {
55+ pattern : / @ m e n t i o n & # 1 2 3 ; \d + & # 1 2 5 ; / g,
56+ decorator : ( match , index ) => {
57+ const mention = mentionsResults [ index ] ;
58+ const _name = `@${ mention . name } ` ;
59+ return (
60+ < LivePersona
61+ serviceScope = { serviceScope }
62+ upn = { mention . email }
63+ template = {
64+ < span
65+ style = { { color : theme . themePrimary , whiteSpace : 'nowrap' } }
66+ >
67+ { _name }
68+ </ span >
69+ }
70+ />
71+ ) ;
72+ } ,
73+ input : line ,
74+ } ) as JSX . Element [ ] ;
75+ }
76+ result . push (
77+ < Stack
78+ horizontal
79+ key = { `stack-${ idx } ` }
80+ horizontalAlign = "start"
81+ verticalAlign = "center"
82+ >
83+ { isArray ( lineContent ) ? (
84+ lineContent . length > 0 ?
85+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
86+ ( lineContent as any [ ] ) . map ( ( el , i ) => {
87+ if ( isObject ( el ) ) {
8788 return (
88- < Text style = { { paddingRight : 5 } } variant = "small" key = { i } >
89- { convertTextToLinksAndText ( he . decode ( _el ) ) }
90- </ Text >
89+ < span key = { i } style = { { paddingRight : 5 } } >
90+ { el }
91+ </ span >
9192 ) ;
93+ } else {
94+ const _el : string = el . trim ( ) ;
95+ if ( _el . length ) {
96+ return (
97+ < Text style = { { paddingRight : 5 } } variant = "small" key = { i } >
98+ { convertTextToLinksAndText ( he . decode ( _el ) ) }
99+ </ Text >
100+ ) ;
101+ }
92102 }
93- }
94- } )
95- ) : (
96- < Text variant = "small" >
97- { convertTextToLinksAndText ( he . decode ( commentText ) ) }
98- </ Text >
99- ) }
103+ } ) : < br />
104+ ) : lineContent . trim ( ) . length > 0 ? (
105+ < Text variant = "small" >
106+ { convertTextToLinksAndText ( he . decode ( lineContent ) ) }
107+ </ Text >
108+ ) : (
109+ < br />
110+ ) }
111+ </ Stack >
112+ ) ;
113+ } ) ;
114+ setCommentText ( result ) ;
115+ } , [ ] ) ;
116+
117+ return (
118+ < >
119+ < Stack wrap verticalAlign = "center" >
120+ { commentText }
100121 </ Stack >
101122 </ >
102123 ) ;
0 commit comments