-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Caret position on WEB #17331
Merged
puneetlath
merged 17 commits into
Expensify:main
from
margelo:perunt/web-caret-position
May 23, 2023
Merged
Caret position on WEB #17331
Changes from 8 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
98ad44a
calculates the position of the caret and updates the component state …
perunt b452aa8
eslint
perunt d35ea9b
Merge branch 'main' of https://github.com/Expensify/App into perunt/w…
perunt c33ef11
Merge branch 'main' of https://github.com/margelo/expensify-app-fork …
perunt 23472c3
change func name to addCursorPositionToSelectionChange
perunt 70903e7
Merge branch 'main' of https://github.com/Expensify/App into perunt/w…
perunt b855615
add optional statement
perunt 5ae86d5
fix positionY
perunt 7ff9e1e
Merge branch 'main' of https://github.com/Expensify/App into perunt/w…
perunt 3381a58
Merge branch 'main' of https://github.com/Expensify/App into perunt/w…
perunt 92ec9ce
Merge branch 'main' of https://github.com/Expensify/App into perunt/w…
perunt aec217b
prettier
perunt 05ddf81
Merge branch 'main' of https://github.com/Expensify/App into perunt/w…
perunt 499a755
Merge branch 'main' of https://github.com/Expensify/App into perunt/w…
perunt 5240081
Merge branch 'perunt/web-caret-position' of https://github.com/margel…
perunt 49d961d
addressed comment after review
perunt d3d530a
Merge branch 'main' of https://github.com/Expensify/App into perunt/w…
perunt 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
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,5 +1,6 @@ | ||
/* eslint-disable rulesdir/onyx-props-must-have-default */ | ||
import React from 'react'; | ||
import {StyleSheet} from 'react-native'; | ||
import {StyleSheet, View} from 'react-native'; | ||
import PropTypes from 'prop-types'; | ||
import _ from 'underscore'; | ||
import ExpensiMark from 'expensify-common/lib/ExpensiMark'; | ||
|
@@ -15,6 +16,7 @@ import Clipboard from '../../libs/Clipboard'; | |
import withWindowDimensions, {windowDimensionsPropTypes} from '../withWindowDimensions'; | ||
import compose from '../../libs/compose'; | ||
import styles from '../../styles/styles'; | ||
import Text from '../Text'; | ||
|
||
const propTypes = { | ||
/** Maximum number of lines in the text input */ | ||
|
@@ -73,6 +75,9 @@ const propTypes = { | |
/** Whether the composer is full size */ | ||
isComposerFullSize: PropTypes.bool, | ||
|
||
/** Whether the composer is full size */ | ||
perunt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
shouldCalculateCaretPosition: PropTypes.bool, | ||
|
||
...withLocalizePropTypes, | ||
|
||
...windowDimensionsPropTypes, | ||
|
@@ -99,6 +104,7 @@ const defaultProps = { | |
isFullComposerAvailable: false, | ||
setIsFullComposerAvailable: () => {}, | ||
isComposerFullSize: false, | ||
shouldCalculateCaretPosition: false, | ||
}; | ||
|
||
const IMAGE_EXTENSIONS = { | ||
|
@@ -129,6 +135,7 @@ class Composer extends React.Component { | |
start: initialValue.length, | ||
end: initialValue.length, | ||
}, | ||
valueBeforeCaret: '', | ||
}; | ||
|
||
this.paste = this.paste.bind(this); | ||
|
@@ -137,6 +144,8 @@ class Composer extends React.Component { | |
this.handleWheel = this.handleWheel.bind(this); | ||
this.putSelectionInClipboard = this.putSelectionInClipboard.bind(this); | ||
this.shouldCallUpdateNumberOfLines = this.shouldCallUpdateNumberOfLines.bind(this); | ||
this.addCursorPositionToSelectionChange = this.addCursorPositionToSelectionChange.bind(this); | ||
this.textRef = React.createRef(null); | ||
} | ||
|
||
componentDidMount() { | ||
|
@@ -190,6 +199,36 @@ class Composer extends React.Component { | |
this.textInput.removeEventListener('wheel', this.handleWheel); | ||
} | ||
|
||
addCursorPositionToSelectionChange(event) { | ||
perunt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (this.props.shouldCalculateCaretPosition) { | ||
this.setState( | ||
{ | ||
valueBeforeCaret: event.target.value.slice( | ||
0, | ||
event.nativeEvent.selection.start, | ||
), | ||
}, | ||
|
||
() => { | ||
hayata-suenaga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const customEvent = { | ||
nativeEvent: { | ||
selection: { | ||
start: event.nativeEvent.selection.start, | ||
end: event.nativeEvent.selection.end, | ||
positionX: this.textRef.current.offsetLeft, | ||
positionY: this.textRef.current.offsetTop, | ||
}, | ||
}, | ||
}; | ||
this.props.onSelectionChange(customEvent); | ||
}, | ||
); | ||
return; | ||
} | ||
|
||
this.props.onSelectionChange(event); | ||
} | ||
|
||
/** | ||
* Set pasted text to clipboard | ||
* @param {String} text | ||
|
@@ -348,6 +387,7 @@ class Composer extends React.Component { | |
updateIsFullComposerAvailable(this.props, numberOfLines); | ||
this.setState({ | ||
numberOfLines, | ||
width: computedStyle.width, | ||
}); | ||
this.props.onNumberOfLinesChange(numberOfLines); | ||
}); | ||
|
@@ -358,28 +398,52 @@ class Composer extends React.Component { | |
propStyles.outline = 'none'; | ||
const propsWithoutStyles = _.omit(this.props, 'style'); | ||
|
||
// This code creates a hidden text component that helps track the caret position in the visible input. | ||
|
||
const calculationCaretPositionElement = ( | ||
perunt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<View | ||
style={{ | ||
position: 'absolute', | ||
bottom: -2000, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this changes made a regression - #26535 |
||
zIndex: -1, | ||
opacity: 0, | ||
}} | ||
> | ||
<Text | ||
hayata-suenaga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
multiline | ||
style={[styles.textInputCompose, {width: this.state.width}]} | ||
> | ||
{this.state.valueBeforeCaret} | ||
<Text ref={this.textRef} /> | ||
</Text> | ||
</View> | ||
); | ||
|
||
// We're disabling autoCorrect for iOS Safari until Safari fixes this issue. See https://github.com/Expensify/App/issues/8592 | ||
return ( | ||
<RNTextInput | ||
autoComplete="off" | ||
autoCorrect={!Browser.isMobileSafari()} | ||
placeholderTextColor={themeColors.placeholderText} | ||
ref={el => this.textInput = el} | ||
selection={this.state.selection} | ||
onChange={this.shouldCallUpdateNumberOfLines} | ||
onSelectionChange={this.onSelectionChange} | ||
hayata-suenaga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
style={[ | ||
propStyles, | ||
|
||
// We are hiding the scrollbar to prevent it from reducing the text input width, | ||
// so we can get the correct scroll height while calculating the number of lines. | ||
this.state.numberOfLines < this.props.maxLines ? styles.overflowHidden : {}, | ||
]} | ||
<> | ||
<RNTextInput | ||
autoComplete="off" | ||
autoCorrect={!Browser.isMobileSafari()} | ||
placeholderTextColor={themeColors.placeholderText} | ||
ref={el => this.textInput = el} | ||
selection={this.state.selection} | ||
onChange={this.shouldCallUpdateNumberOfLines} | ||
style={[ | ||
propStyles, | ||
|
||
// We are hiding the scrollbar to prevent it from reducing the text input width, | ||
// so we can get the correct scroll height while calculating the number of lines. | ||
this.state.numberOfLines < this.props.maxLines ? styles.overflowHidden : {}, | ||
]} | ||
/* eslint-disable-next-line react/jsx-props-no-spreading */ | ||
{...propsWithoutStyles} | ||
numberOfLines={this.state.numberOfLines} | ||
disabled={this.props.isDisabled} | ||
/> | ||
{...propsWithoutStyles} | ||
onSelectionChange={this.addCursorPositionToSelectionChange} | ||
numberOfLines={this.state.numberOfLines} | ||
disabled={this.props.isDisabled} | ||
/> | ||
{this.props.shouldCalculateCaretPosition && calculationCaretPositionElement} | ||
</> | ||
); | ||
} | ||
} | ||
|
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you need this? It looks like you did set a default for the new prop you added.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initially set a default value, but ran into linter issues. After rechecking, the specific issue is resolved. However, we're facing additional errors with the linter, unrelated to this PR. It seems to conflict with our new Prettier configuration.