@@ -16,17 +16,26 @@ export default class HighlightCode extends Component {
1616 canCopy : PropTypes . bool
1717 }
1818
19- #syntaxHighlighter;
20- #pre;
19+ #childNodes
2120
2221 downloadText = ( ) => {
2322 saveAs ( this . props . value , this . props . fileName || "response.txt" )
2423 }
2524
25+ handleRootRef = ( node ) => {
26+ if ( node === null ) {
27+ this . #childNodes = node
28+ } else {
29+ this . #childNodes = Array
30+ . from ( node . childNodes )
31+ . filter ( node => ! ! node . nodeType && node . classList . contains ( "microlight" ) )
32+ }
33+ }
34+
2635 preventYScrollingBeyondElement = ( e ) => {
2736 const target = e . target
2837
29- var deltaY = e . nativeEvent . deltaY
38+ var deltaY = e . deltaY
3039 var contentHeight = target . scrollHeight
3140 var visibleHeight = target . offsetHeight
3241 var scrollTop = target . scrollTop
@@ -43,13 +52,11 @@ export default class HighlightCode extends Component {
4352 }
4453
4554 componentDidMount ( ) {
46- [ this . #syntaxHighlighter, this . #pre]
47- . map ( element => element ?. addEventListener ( "mousewheel" , this . preventYScrollingBeyondElement , { passive : false } ) )
55+ this . #childNodes?. forEach ( node => node . addEventListener ( "mousewheel" , this . preventYScrollingBeyondElement , { passive : false } ) )
4856 }
4957
5058 componentWillUnmount ( ) {
51- [ this . #syntaxHighlighter, this . #pre]
52- . map ( element => element ?. removeEventListener ( "mousewheel" , this . preventYScrollingBeyondElement ) )
59+ this . #childNodes?. forEach ( node => node . removeEventListener ( "mousewheel" , this . preventYScrollingBeyondElement ) )
5360 }
5461
5562 render ( ) {
@@ -61,17 +68,16 @@ export default class HighlightCode extends Component {
6168
6269 const codeBlock = get ( config , "syntaxHighlight.activated" )
6370 ? < SyntaxHighlighter
64- ref = { elem => this . #syntaxHighlighter = elem }
6571 language = { language }
6672 className = { className + " microlight" }
6773 style = { getStyle ( get ( config , "syntaxHighlight.theme" ) ) }
6874 >
6975 { value }
7076 </ SyntaxHighlighter >
71- : < pre ref = { elem => this . #pre = elem } className = { className + " microlight" } > { value } </ pre >
77+ : < pre className = { className + " microlight" } > { value } </ pre >
7278
7379 return (
74- < div className = "highlight-code" >
80+ < div className = "highlight-code" ref = { this . handleRootRef } >
7581 { ! downloadable ? null :
7682 < div className = "download-contents" onClick = { this . downloadText } >
7783 Download
0 commit comments