-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
[RNMobile] Refactor Caption component #19118
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e5af32d
RNMobile - Refactor of Caption component into two: BlockCaption and C…
8657395
RNMobile - Refactor of Caption component
09d4700
Merge branch 'master' of github.com:WordPress/gutenberg into refactor…
319307c
RNMobile - Caption refactor passing accessibility props and adding mi…
a7c1d9a
Merge branch 'master' of github.com:WordPress/gutenberg into refactor…
1359e55
RNMobile - Caption: Specify p tag for both platforms
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
65 changes: 65 additions & 0 deletions
65
packages/block-editor/src/components/block-caption/index.native.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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { View } from 'react-native'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Caption, RichText } from '@wordpress/block-editor'; | ||
import { compose } from '@wordpress/compose'; | ||
import { withDispatch, withSelect } from '@wordpress/data'; | ||
|
||
const BlockCaption = ( { | ||
accessible, | ||
accessibilityLabelCreator, | ||
onBlur, | ||
onChange, | ||
onFocus, | ||
isSelected, | ||
shouldDisplay, | ||
text, | ||
} ) => ( | ||
<View | ||
style={ { flex: 1, padding: 12 } } | ||
> | ||
<Caption | ||
accessibilityLabelCreator={ accessibilityLabelCreator } | ||
accessible={ accessible } | ||
isSelected={ isSelected } | ||
onBlur={ onBlur } | ||
onChange={ onChange } | ||
onFocus={ onFocus } | ||
shouldDisplay={ shouldDisplay } | ||
value={ text } | ||
/> | ||
</View> | ||
); | ||
|
||
export default compose( [ | ||
withSelect( ( select, { clientId } ) => { | ||
const { | ||
getBlockAttributes, | ||
getSelectedBlockClientId, | ||
} = select( 'core/block-editor' ); | ||
const { caption } = getBlockAttributes( clientId ); | ||
const isBlockSelected = getSelectedBlockClientId() === clientId; | ||
|
||
// We'll render the caption so that the soft keyboard is not forced to close on Android | ||
// but still hide it by setting its display style to none. See wordpress-mobile/gutenberg-mobile#1221 | ||
const shouldDisplay = ! RichText.isEmpty( caption ) > 0 || isBlockSelected; | ||
|
||
return { | ||
shouldDisplay, | ||
text: caption, | ||
}; | ||
} ), | ||
withDispatch( ( dispatch, { clientId } ) => { | ||
const { updateBlockAttributes } = dispatch( 'core/block-editor' ); | ||
return { | ||
onChange: ( caption ) => { | ||
updateBlockAttributes( clientId, { caption } ); | ||
}, | ||
}; | ||
} ), | ||
] )( BlockCaption ); |
86 changes: 40 additions & 46 deletions
86
packages/block-editor/src/components/caption/index.native.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,65 +1,59 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { View } from 'react-native'; | ||
import { Platform, View } from 'react-native'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { RichText } from '@wordpress/block-editor'; | ||
import { compose } from '@wordpress/compose'; | ||
import { withDispatch, withSelect } from '@wordpress/data'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
const Caption = ( { accessible, accessibilityLabel, onBlur, onChange, onFocus, isSelected, shouldDisplay, text } ) => ( | ||
<View style={ { padding: 12, flex: 1, display: shouldDisplay ? 'flex' : 'none' } } | ||
// this platform difference is needed to avoid a regression described here: | ||
// https://github.com/WordPress/gutenberg/pull/18818#issuecomment-559818548 | ||
const CAPTION_TAG_NAME = Platform.select( { | ||
ios: 'p', | ||
android: '', | ||
} ); | ||
|
||
const Caption = ( { | ||
accessibilityLabelCreator, | ||
accessible, | ||
inlineToolbar, | ||
isSelected, | ||
onBlur, | ||
onChange, | ||
onFocus, | ||
placeholder = __( 'Write caption…' ), | ||
placeholderTextColor, | ||
shouldDisplay = true, | ||
style, | ||
value, | ||
} ) => ( | ||
<View | ||
accessibilityLabel={ accessibilityLabelCreator ? accessibilityLabelCreator( value ) : undefined } | ||
accessibilityRole="button" | ||
accessible={ accessible } | ||
accessibilityLabel={ accessibilityLabel } | ||
accessibilityRole={ 'button' } | ||
style={ { flex: 1, display: shouldDisplay ? 'flex' : 'none' } } | ||
> | ||
<RichText | ||
rootTagsToEliminate={ [ 'p' ] } | ||
placeholder={ __( 'Write caption…' ) } | ||
value={ text } | ||
onChange={ onChange } | ||
unstableOnFocus={ onFocus } | ||
onBlur={ onBlur } // always assign onBlur as props | ||
isSelected={ isSelected } | ||
__unstableMobileNoFocusOnMount | ||
fontSize={ 14 } | ||
fontSize={ style && style.fontSize ? style.fontSize : 14 } | ||
inlineToolbar={ inlineToolbar } | ||
isSelected={ isSelected } | ||
onBlur={ onBlur } | ||
onChange={ onChange } | ||
placeholder={ placeholder } | ||
placeholderTextColor={ placeholderTextColor } | ||
rootTagsToEliminate={ [ 'p' ] } | ||
style={ style } | ||
tagName={ CAPTION_TAG_NAME } | ||
textAlign="center" | ||
underlineColorAndroid="transparent" | ||
textAlign={ 'center' } | ||
tagName={ 'p' } | ||
unstableOnFocus={ onFocus } | ||
value={ value } | ||
/> | ||
</View> | ||
); | ||
|
||
export default compose( [ | ||
withSelect( ( select, { accessibilityLabelCreator, clientId } ) => { | ||
const { | ||
getBlockAttributes, | ||
getSelectedBlockClientId, | ||
} = select( 'core/block-editor' ); | ||
const { caption } = getBlockAttributes( clientId ); | ||
const accessibilityLabel = accessibilityLabelCreator ? accessibilityLabelCreator( caption ) : undefined; | ||
const isBlockSelected = getSelectedBlockClientId() === clientId; | ||
|
||
// We'll render the caption so that the soft keyboard is not forced to close on Android | ||
// but still hide it by setting its display style to none. See wordpress-mobile/gutenberg-mobile#1221 | ||
const shouldDisplay = ! RichText.isEmpty( caption ) > 0 || isBlockSelected; | ||
|
||
return { | ||
accessibilityLabel, | ||
shouldDisplay, | ||
text: caption, | ||
}; | ||
} ), | ||
withDispatch( ( dispatch, { clientId } ) => { | ||
const { updateBlockAttributes } = dispatch( 'core/block-editor' ); | ||
return { | ||
onChange: ( caption ) => { | ||
updateBlockAttributes( clientId, { caption } ); | ||
}, | ||
}; | ||
} ), | ||
] )( Caption ); | ||
export default Caption; |
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
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
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
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
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.
This was in the Gallery block to make it work correctly on Android due to the mentioned regression so I moved it to the Caption component itself since it was affecting the rest of Blocks using it.
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.
👍 Linking this up with the issue that was filed: wordpress-mobile/gutenberg-mobile#1651
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.
I like that this platform divergence is consolidated into one place, OTOH, it seems we currently have to "choose" between two regressions for all captions: Enter key not working or First word not style-able. This PR does not introduce these issues, but we do lose some granularity in how we can "choose which regression" 🙈 for the different use cases. I'm not suggesting a change here, just noting, and hopefully those issues will be resolved, and this will soon be a moot point. 😄
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.
Yeah you're right, by moving this to the Caption component it will affect Block level components with the First word not style-able regression, but it will allow users to add new lines on Android... I wonder what users use the most, styling in captions or entering new lines 😅