Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(highlight-code): handle mousewheel events properly #7554

Merged
merged 5 commits into from
Oct 12, 2021
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions src/core/components/highlight-code.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,26 @@ export default class HighlightCode extends Component {
canCopy: PropTypes.bool
}

#syntaxHighlighter;
#pre;
#childNodes

downloadText = () => {
saveAs(this.props.value, this.props.fileName || "response.txt")
}

handleRootRef = (node) => {
if (node === null) {
this.#childNodes = node;
char0n marked this conversation as resolved.
Show resolved Hide resolved
} else {
this.#childNodes = Array
.from(node.childNodes)
.filter(node => !!node.nodeType && node.classList.contains("microlight"))
}
}

preventYScrollingBeyondElement = (e) => {
const target = e.target

var deltaY = e.nativeEvent.deltaY
var deltaY = e.deltaY
var contentHeight = target.scrollHeight
var visibleHeight = target.offsetHeight
var scrollTop = target.scrollTop
Expand All @@ -43,13 +52,11 @@ export default class HighlightCode extends Component {
}

componentDidMount() {
[this.#syntaxHighlighter, this.#pre]
.map(element => element?.addEventListener("mousewheel", this.preventYScrollingBeyondElement, { passive: false }))
this.#childNodes.forEach(node => node.addEventListener("mousewheel", this.preventYScrollingBeyondElement, { passive: false }))
char0n marked this conversation as resolved.
Show resolved Hide resolved
}

componentWillUnmount() {
[this.#syntaxHighlighter, this.#pre]
.map(element => element?.removeEventListener("mousewheel", this.preventYScrollingBeyondElement))
this.#childNodes.forEach(node => node.removeEventListener("mousewheel", this.preventYScrollingBeyondElement))
char0n marked this conversation as resolved.
Show resolved Hide resolved
}

render () {
Expand All @@ -61,17 +68,16 @@ export default class HighlightCode extends Component {

const codeBlock = get(config, "syntaxHighlight.activated")
? <SyntaxHighlighter
ref={elem => this.#syntaxHighlighter = elem}
language={language}
className={className + " microlight"}
style={getStyle(get(config, "syntaxHighlight.theme"))}
>
{value}
</SyntaxHighlighter>
: <pre ref={elem => this.#pre = elem} className={className + " microlight"}>{value}</pre>
: <pre className={className + " microlight"}>{value}</pre>

return (
<div className="highlight-code">
<div className="highlight-code" ref={this.handleRootRef}>
{ !downloadable ? null :
<div className="download-contents" onClick={this.downloadText}>
Download
Expand Down