-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Introduces RichText component for mobile and ports Para block for mobile #8231
Merged
daniloercoli
merged 21 commits into
master
from
rnmobile/port-para-block-use-latest-gb-master-july
Aug 1, 2018
Merged
Changes from 14 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
3318475
Introduce RichText component for mobile, and port the Paragraph block…
daniloercoli 1ffdf7c
Fix lint
daniloercoli 73d1a38
Use an edit file to share more common code with the main Paragraph we…
SergioEstevao 1669996
Fix lint
daniloercoli ec0f897
Remove log calls
daniloercoli 20d7082
Revert renaming of custom-class-name.native.js → custom-class-name-na…
daniloercoli b918423
Cleaning the code / Refactoring by "nativizing" `utils.js` (& `index.…
daniloercoli 3348926
Remove any extra P.
SergioEstevao 804b52a
Replace <p> by <br>
SergioEstevao 8e75df3
Merge branch 'master' into rnmobile/port-para-block-use-latest-gb-mas…
SergioEstevao bed493b
The RichText mobile component now returns plain HTML, and the transfo…
daniloercoli c9373d2
Merge remote-tracking branch 'origin/master' into rnmobile/port-para-…
gziolo f0e83ec
Update code to work with the lastest master changes
gziolo b275731
Correctly check if minHeight is undefined when setting the style of m…
daniloercoli f214c76
Blocks: Extract phrasing content to its own file to allow reuse with …
gziolo 762b582
Merge branch 'rnmobile/port-para-block-use-latest-gb-master-july' of …
gziolo 9651c70
Remove method `setForceUpdate` that is not used. Also remove the conv…
daniloercoli 248a2c8
Remove `aria-label` that is not used in RichText mobile
daniloercoli b34049a
Convert private instance variable `lastContentSizeHeight`, to local m…
daniloercoli 131e3e7
Use built-in `forceUpdate` method call, instead of using a private bo…
daniloercoli 3351260
Add comment for P replacement for BR
SergioEstevao 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
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,243 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import classnames from 'classnames'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { | ||
Component, | ||
Fragment, | ||
} from '@wordpress/element'; | ||
import { | ||
PanelBody, | ||
ToggleControl, | ||
withFallbackStyles, | ||
} from '@wordpress/components'; | ||
import { | ||
withColors, | ||
AlignmentToolbar, | ||
BlockControls, | ||
ContrastChecker, | ||
FontSizePicker, | ||
InspectorControls, | ||
PanelColorSettings, | ||
RichText, | ||
withFontSizes, | ||
} from '@wordpress/editor'; | ||
import { createBlock } from '@wordpress/blocks'; | ||
import { compose } from '@wordpress/compose'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import './style.scss'; | ||
|
||
const { getComputedStyle } = window; | ||
|
||
const name = 'core/paragraph'; | ||
|
||
const applyFallbackStyles = withFallbackStyles( ( node, ownProps ) => { | ||
const { textColor, backgroundColor, fontSize, customFontSize } = ownProps.attributes; | ||
const editableNode = node.querySelector( '[contenteditable="true"]' ); | ||
//verify if editableNode is available, before using getComputedStyle. | ||
const computedStyles = editableNode ? getComputedStyle( editableNode ) : null; | ||
return { | ||
fallbackBackgroundColor: backgroundColor || ! computedStyles ? undefined : computedStyles.backgroundColor, | ||
fallbackTextColor: textColor || ! computedStyles ? undefined : computedStyles.color, | ||
fallbackFontSize: fontSize || customFontSize || ! computedStyles ? undefined : parseInt( computedStyles.fontSize ) || undefined, | ||
}; | ||
} ); | ||
|
||
class ParagraphBlock extends Component { | ||
constructor() { | ||
super( ...arguments ); | ||
|
||
this.onReplace = this.onReplace.bind( this ); | ||
this.toggleDropCap = this.toggleDropCap.bind( this ); | ||
this.splitBlock = this.splitBlock.bind( this ); | ||
} | ||
|
||
onReplace( blocks ) { | ||
const { attributes, onReplace } = this.props; | ||
onReplace( blocks.map( ( block, index ) => ( | ||
index === 0 && block.name === name ? | ||
{ ...block, | ||
attributes: { | ||
...attributes, | ||
...block.attributes, | ||
}, | ||
} : | ||
block | ||
) ) ); | ||
} | ||
|
||
toggleDropCap() { | ||
const { attributes, setAttributes } = this.props; | ||
setAttributes( { dropCap: ! attributes.dropCap } ); | ||
} | ||
|
||
getDropCapHelp( checked ) { | ||
return checked ? __( 'Showing large initial letter.' ) : __( 'Toggle to show a large initial letter.' ); | ||
} | ||
|
||
/** | ||
* Split handler for RichText value, namely when content is pasted or the | ||
* user presses the Enter key. | ||
* | ||
* @param {?Array} before Optional before value, to be used as content | ||
* in place of what exists currently for the | ||
* block. If undefined, the block is deleted. | ||
* @param {?Array} after Optional after value, to be appended in a new | ||
* paragraph block to the set of blocks passed | ||
* as spread. | ||
* @param {...WPBlock} blocks Optional blocks inserted between the before | ||
* and after value blocks. | ||
*/ | ||
splitBlock( before, after, ...blocks ) { | ||
const { | ||
attributes, | ||
insertBlocksAfter, | ||
setAttributes, | ||
onReplace, | ||
} = this.props; | ||
|
||
if ( after ) { | ||
// Append "After" content as a new paragraph block to the end of | ||
// any other blocks being inserted after the current paragraph. | ||
blocks.push( createBlock( name, { content: after } ) ); | ||
} | ||
|
||
if ( blocks.length && insertBlocksAfter ) { | ||
insertBlocksAfter( blocks ); | ||
} | ||
|
||
const { content } = attributes; | ||
if ( ! before ) { | ||
// If before content is omitted, treat as intent to delete block. | ||
onReplace( [] ); | ||
} else if ( content !== before ) { | ||
// Only update content if it has in-fact changed. In case that user | ||
// has created a new paragraph at end of an existing one, the value | ||
// of before will be strictly equal to the current content. | ||
setAttributes( { content: before } ); | ||
} | ||
} | ||
|
||
render() { | ||
const { | ||
attributes, | ||
setAttributes, | ||
mergeBlocks, | ||
onReplace, | ||
className, | ||
backgroundColor, | ||
textColor, | ||
setBackgroundColor, | ||
setTextColor, | ||
fallbackBackgroundColor, | ||
fallbackTextColor, | ||
fallbackFontSize, | ||
fontSize, | ||
setFontSize, | ||
} = this.props; | ||
|
||
const { | ||
align, | ||
content, | ||
dropCap, | ||
placeholder, | ||
} = attributes; | ||
|
||
return ( | ||
<Fragment> | ||
<BlockControls> | ||
<AlignmentToolbar | ||
value={ align } | ||
onChange={ ( nextAlign ) => { | ||
setAttributes( { align: nextAlign } ); | ||
} } | ||
/> | ||
</BlockControls> | ||
<InspectorControls> | ||
<PanelBody title={ __( 'Text Settings' ) } className="blocks-font-size"> | ||
<FontSizePicker | ||
fallbackFontSize={ fallbackFontSize } | ||
value={ fontSize.size } | ||
onChange={ setFontSize } | ||
/> | ||
<ToggleControl | ||
label={ __( 'Drop Cap' ) } | ||
checked={ !! dropCap } | ||
onChange={ this.toggleDropCap } | ||
help={ this.getDropCapHelp } | ||
/> | ||
</PanelBody> | ||
<PanelColorSettings | ||
title={ __( 'Color Settings' ) } | ||
initialOpen={ false } | ||
colorSettings={ [ | ||
{ | ||
value: backgroundColor.value, | ||
onChange: setBackgroundColor, | ||
label: __( 'Background Color' ), | ||
}, | ||
{ | ||
value: textColor.value, | ||
onChange: setTextColor, | ||
label: __( 'Text Color' ), | ||
}, | ||
] } | ||
> | ||
<ContrastChecker | ||
{ ...{ | ||
textColor: textColor.value, | ||
backgroundColor: backgroundColor.value, | ||
fallbackTextColor, | ||
fallbackBackgroundColor, | ||
} } | ||
fontSize={ fontSize.size } | ||
/> | ||
</PanelColorSettings> | ||
</InspectorControls> | ||
<RichText | ||
tagName="p" | ||
className={ classnames( 'wp-block-paragraph', className, { | ||
'has-background': backgroundColor.value, | ||
'has-drop-cap': dropCap, | ||
[ backgroundColor.class ]: backgroundColor.class, | ||
[ textColor.class ]: textColor.class, | ||
[ fontSize.class ]: fontSize.class, | ||
} ) } | ||
style={ { | ||
backgroundColor: backgroundColor.value, | ||
color: textColor.value, | ||
fontSize: fontSize.size ? fontSize.size + 'px' : undefined, | ||
textAlign: align, | ||
} } | ||
value={ content } | ||
onChange={ ( nextContent ) => { | ||
setAttributes( { | ||
content: nextContent, | ||
} ); | ||
} } | ||
onSplit={ this.splitBlock } | ||
onMerge={ mergeBlocks } | ||
onReplace={ this.onReplace } | ||
onRemove={ () => onReplace( [] ) } | ||
placeholder={ placeholder || __( 'Add text or type / to add content' ) } | ||
/> | ||
</Fragment> | ||
); | ||
} | ||
} | ||
|
||
const ParagraphEdit = compose( [ | ||
withColors( 'backgroundColor', { textColor: 'color' } ), | ||
withFontSizes( 'fontSize' ), | ||
applyFallbackStyles, | ||
] )( ParagraphBlock ); | ||
|
||
export default ParagraphEdit; |
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,61 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { View } from 'react-native'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { Component } from '@wordpress/element'; | ||
import { parse } from '@wordpress/blocks'; | ||
import { RichText } from '@wordpress/editor'; | ||
|
||
const minHeight = 50; | ||
|
||
class ParagraphEdit extends Component { | ||
render() { | ||
const { | ||
attributes, | ||
setAttributes, | ||
style, | ||
} = this.props; | ||
|
||
const { | ||
placeholder, | ||
} = attributes; | ||
|
||
return ( | ||
<View> | ||
<RichText | ||
content={ { contentTree: attributes.content, eventCount: attributes.eventCount } } | ||
style={ { | ||
...style, | ||
minHeight: Math.max( minHeight, attributes.aztecHeight !== null ? attributes.aztecHeight : 0 ), | ||
} } | ||
onChange={ ( event ) => { | ||
// Create a React Tree from the new HTML | ||
const newParaBlock = parse( '<!-- wp:paragraph --><p>' + event.content + '</p><!-- /wp:paragraph -->' )[ 0 ]; | ||
setAttributes( { | ||
...this.props.attributes, | ||
content: newParaBlock.attributes.content, | ||
eventCount: event.eventCount, | ||
} ); | ||
} | ||
} | ||
onContentSizeChange={ ( event ) => { | ||
setAttributes( { | ||
...this.props.attributes, | ||
aztecHeight: event.aztecHeight, | ||
} ); | ||
} | ||
} | ||
placeholder={ placeholder || __( 'Add text or type / to add content' ) } | ||
aria-label={ __( 'test' ) } | ||
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. Do we need this 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. No, it's not needed at the moment. |
||
/> | ||
</View> | ||
); | ||
} | ||
} | ||
|
||
export default ParagraphEdit; |
Oops, something went wrong.
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.
I noticed some differences withh the
master
branch. There were some changes applied to this code. I will fix them myself.