-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Chore: migrate index.js class to function component #26374
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
Merged
marcochavezf
merged 15 commits into
Expensify:main
from
teneeto:chore/16157-migrate-index-file-to-function-component
Oct 2, 2023
+43
−44
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
2adafc5
migrate index.js class to function component
teneeto 494cf78
use arrow function and useCallback
teneeto 4d748f1
use current in ref and revert props destructure
teneeto 84f0b52
Update src/components/HTMLEngineProvider/HTMLRenderers/PreRenderer/in…
teneeto 8b84242
Merge branch 'main' of github.com:Expensify/App into chore/16157-migr…
teneeto 568afb8
fix review comments
teneeto a577bc5
Update src/components/HTMLEngineProvider/HTMLRenderers/PreRenderer/in…
teneeto f31bde1
Merge branch 'main' of github.com:Expensify/App into chore/16157-migr…
teneeto 4556c53
Merge branch 'main' of github.com:Expensify/App into chore/16157-migr…
teneeto 16dec4b
Merge branch 'main' of github.com:Expensify/App into chore/16157-migr…
teneeto a9f7bc4
Merge branch 'main' of github.com:Expensify/App into chore/16157-migr…
teneeto 0aaed3e
Merge branch 'main' of github.com:Expensify/App into chore/16157-migr…
teneeto ecda5f3
Update src/components/HTMLEngineProvider/HTMLRenderers/PreRenderer/in…
teneeto 9a2055b
Update src/components/HTMLEngineProvider/HTMLRenderers/PreRenderer/in…
teneeto 473b703
Merge branch 'main' of github.com:Expensify/App into chore/16157-migr…
teneeto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
87 changes: 43 additions & 44 deletions
87
src/components/HTMLEngineProvider/HTMLRenderers/PreRenderer/index.js
This file contains hidden or 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); | ||
|
teneeto marked this conversation as resolved.
teneeto marked this conversation as resolved.
|
||
| }; | ||
| }, [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} | ||
| /> | ||
| ); | ||
|
teneeto marked this conversation as resolved.
|
||
| } | ||
|
|
||
| PreRenderer.propTypes = htmlRendererPropTypes; | ||
| PreRenderer.displayName = 'PreRenderer'; | ||
|
|
||
| export default withLocalize(PreRenderer); | ||
| export default PreRenderer; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.