Skip to content

Commit 0fc429f

Browse files
authored
fix(highlight-code): handle mousewheel events properly (#7554)
SyntaxHighlighter component doesn't support ref. We had to use different approach to finds it's DOM Node using ref of the root Node of the render tree for HighlightCode component. Refs #7497
1 parent 5ada9ef commit 0fc429f

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

src/core/components/highlight-code.jsx

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)