-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26374 from teneeto/chore/16157-migrate-index-file…
…-to-function-component Chore: migrate index.js class to function component
- Loading branch information
Showing
1 changed file
with
43 additions
and
44 deletions.
There are no files selected for viewing
87 changes: 43 additions & 44 deletions
87
src/components/HTMLEngineProvider/HTMLRenderers/PreRenderer/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,69 @@ | ||
import React from 'react'; | ||
import React, {useCallback, useEffect, useRef} from 'react'; | ||
import _ from 'underscore'; | ||
import withLocalize from '../../../withLocalize'; | ||
|
||
import ControlSelection from '../../../../libs/ControlSelection'; | ||
import * as DeviceCapabilities from '../../../../libs/DeviceCapabilities'; | ||
import htmlRendererPropTypes from '../htmlRendererPropTypes'; | ||
import BasePreRenderer from './BasePreRenderer'; | ||
import * as DeviceCapabilities from '../../../../libs/DeviceCapabilities'; | ||
import ControlSelection from '../../../../libs/ControlSelection'; | ||
|
||
class PreRenderer extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.scrollNode = this.scrollNode.bind(this); | ||
this.debouncedIsScrollingVertically = _.debounce(this.isScrollingVertically.bind(this), 100, true); | ||
} | ||
const isScrollingVertically = (event) => | ||
// Mark as vertical scrolling only when absolute value of deltaY is more than the double of absolute | ||
// value of deltaX, so user can use trackpad scroll on the code block horizontally at a wide angle. | ||
Math.abs(event.deltaY) > Math.abs(event.deltaX) * 2; | ||
|
||
componentDidMount() { | ||
if (!this.ref) { | ||
return; | ||
} | ||
this.ref.getScrollableNode().addEventListener('wheel', this.scrollNode); | ||
} | ||
const debouncedIsScrollingVertically = _.debounce(isScrollingVertically, 100, true); | ||
|
||
componentWillUnmount() { | ||
this.ref.getScrollableNode().removeEventListener('wheel', this.scrollNode); | ||
} | ||
function PreRenderer(props) { | ||
const scrollViewRef = useRef(); | ||
|
||
/** | ||
* Check if user is scrolling vertically based on deltaX and deltaY. We debounce this | ||
* method in the constructor to make sure it's called only for the first event. | ||
* Checks if user is scrolling vertically based on deltaX and deltaY. We debounce this | ||
* method in order to make sure it's called only for the first event. | ||
* @param {WheelEvent} event Wheel event | ||
* @returns {Boolean} true if user is scrolling vertically | ||
*/ | ||
isScrollingVertically(event) { | ||
// Mark as vertical scrolling only when absolute value of deltaY is more than the double of absolute | ||
// value of deltaX, so user can use trackpad scroll on the code block horizontally at a wide angle. | ||
return Math.abs(event.deltaY) > Math.abs(event.deltaX) * 2; | ||
} | ||
|
||
/** | ||
* Manually scrolls the code block if code block horizontal scrollable, then prevents the event from being passed up to the parent. | ||
* @param {Object} event native event | ||
*/ | ||
scrollNode(event) { | ||
const node = this.ref.getScrollableNode(); | ||
const scrollNode = useCallback((event) => { | ||
const node = scrollViewRef.current.getScrollableNode(); | ||
const horizontalOverflow = node.scrollWidth > node.offsetWidth; | ||
const isScrollingVertically = this.debouncedIsScrollingVertically(event); | ||
if (event.currentTarget === node && horizontalOverflow && !isScrollingVertically) { | ||
if (event.currentTarget === node && horizontalOverflow && !debouncedIsScrollingVertically(event)) { | ||
node.scrollLeft += event.deltaX; | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
} | ||
} | ||
}, []); | ||
|
||
useEffect(() => { | ||
const eventListenerRefValue = scrollViewRef.current; | ||
if (!eventListenerRefValue) { | ||
return; | ||
} | ||
eventListenerRefValue.getScrollableNode().addEventListener('wheel', scrollNode); | ||
|
||
return () => { | ||
if (!eventListenerRefValue.getScrollableNode()) { | ||
return; | ||
} | ||
eventListenerRefValue.getScrollableNode().removeEventListener('wheel', scrollNode); | ||
}; | ||
}, [scrollNode]); | ||
|
||
render() { | ||
return ( | ||
<BasePreRenderer | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
{...this.props} | ||
ref={(el) => (this.ref = el)} | ||
onPressIn={() => DeviceCapabilities.canUseTouchScreen() && ControlSelection.block()} | ||
onPressOut={() => ControlSelection.unblock()} | ||
/> | ||
); | ||
} | ||
return ( | ||
<BasePreRenderer | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
{...props} | ||
ref={scrollViewRef} | ||
onPressIn={() => DeviceCapabilities.canUseTouchScreen() && ControlSelection.block()} | ||
onPressOut={ControlSelection.unblock} | ||
/> | ||
); | ||
} | ||
|
||
PreRenderer.propTypes = htmlRendererPropTypes; | ||
PreRenderer.displayName = 'PreRenderer'; | ||
|
||
export default withLocalize(PreRenderer); | ||
export default PreRenderer; |