@@ -112,26 +112,21 @@ export const Phrase: React.FC<PhraseProps> = ({
112112} ) => {
113113 const themeStyles = { size, theme } ;
114114
115- const onClick = ( ) => {
116- events ?. onClickPhrase ?.( phrase ) ;
117- } ;
118-
119- const onMouseEnter = ( ) => {
120- events ?. onMouseEnterPhrase ?.( phrase ) ;
121- } ;
122-
123- const onMouseLeave = ( ) => {
124- events ?. onMouseLeavePhrase ?.( phrase ) ;
125- } ;
126-
127- let defaultText = ! isEmpty ( events ) ? (
128- < span onClick = { onClick } onMouseEnter = { onMouseEnter } onMouseLeave = { onMouseLeave } >
129- { phrase . value }
130- </ span >
131- ) : (
132- < > { phrase . value } </ >
133- ) ;
134-
115+ const eventProps = ! isEmpty ( events )
116+ ? {
117+ onClick : ( ) => {
118+ events ?. onClickPhrase ?.( phrase ) ;
119+ } ,
120+ onMouseEnter : ( ) => {
121+ events ?. onMouseEnterPhrase ?.( phrase ) ;
122+ } ,
123+ onMouseLeave : ( ) => {
124+ events ?. onMouseLeavePhrase ?.( phrase ) ;
125+ } ,
126+ }
127+ : { } ;
128+
129+ let defaultText = < > { phrase . value } </ > ;
135130 if ( isTextPhrase ( phrase ) ) {
136131 if ( phrase . bold ) defaultText = < Bold > { defaultText } </ Bold > ;
137132 if ( phrase . italic ) defaultText = < Italic > { defaultText } </ Italic > ;
@@ -143,7 +138,7 @@ export const Phrase: React.FC<PhraseProps> = ({
143138 </ a >
144139 ) ;
145140 return (
146- < span style = { phrase ?. styles } className = { cx ( phrase ?. className ) } >
141+ < span { ... eventProps } style = { phrase ?. styles } className = { cx ( phrase ?. className ) } >
147142 { defaultText }
148143 </ span >
149144 ) ;
@@ -153,7 +148,7 @@ export const Phrase: React.FC<PhraseProps> = ({
153148 // 使用 pre 标签渲染特殊转义字符
154149 if ( isEscapePhrase ( phrase ) )
155150 return (
156- < pre className = { cx ( phrase . className ) } style = { phrase . styles } >
151+ < pre { ... eventProps } className = { cx ( phrase . className ) } style = { phrase . styles } >
157152 { phrase . value }
158153 </ pre >
159154 ) ;
@@ -163,6 +158,7 @@ export const Phrase: React.FC<PhraseProps> = ({
163158 if ( isFormulaPhrase ( phrase ) )
164159 return (
165160 < FormulaWrapper
161+ { ...eventProps }
166162 className = { cx ( phrase . className , `${ NTV_PREFIX_CLS } -formula` ) }
167163 style = { phrase . styles }
168164 dangerouslySetInnerHTML = { {
@@ -177,13 +173,15 @@ export const Phrase: React.FC<PhraseProps> = ({
177173 ) ;
178174
179175 if ( isImagePhrase ( phrase ) ) {
180- return < img src = { phrase . value } alt = { phrase . alt } className = { cx ( phrase . className ) } style = { phrase . styles } /> ;
176+ return (
177+ < img { ...eventProps } src = { phrase . value } alt = { phrase . alt } className = { cx ( phrase . className ) } style = { phrase . styles } />
178+ ) ;
181179 }
182180
183181 const descriptor = pluginManager ?. getPhraseDescriptorBySpec ( phrase ) ;
184182 if ( descriptor ) {
185183 return < > { renderPhraseByDescriptor ( { spec : phrase , descriptor, themeStyles, events } ) } </ > ;
186184 }
187185
188- return defaultText ;
186+ return ! isEmpty ( events ) ? < span { ... eventProps } > defaultText </ span > : defaultText ;
189187} ;
0 commit comments