Skip to content
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
Merged
Show file tree
Hide file tree
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 Jul 26, 2018
1ffdf7c
Fix lint
daniloercoli Jul 26, 2018
73d1a38
Use an edit file to share more common code with the main Paragraph we…
SergioEstevao Jul 26, 2018
1669996
Fix lint
daniloercoli Jul 27, 2018
ec0f897
Remove log calls
daniloercoli Jul 27, 2018
20d7082
Revert renaming of custom-class-name.native.js → custom-class-name-na…
daniloercoli Jul 27, 2018
b918423
Cleaning the code / Refactoring by "nativizing" `utils.js` (& `index.…
daniloercoli Jul 27, 2018
3348926
Remove any extra P.
SergioEstevao Jul 27, 2018
804b52a
Replace <p> by <br>
SergioEstevao Jul 27, 2018
8e75df3
Merge branch 'master' into rnmobile/port-para-block-use-latest-gb-mas…
SergioEstevao Jul 30, 2018
bed493b
The RichText mobile component now returns plain HTML, and the transfo…
daniloercoli Jul 30, 2018
c9373d2
Merge remote-tracking branch 'origin/master' into rnmobile/port-para-…
gziolo Aug 1, 2018
f0e83ec
Update code to work with the lastest master changes
gziolo Aug 1, 2018
b275731
Correctly check if minHeight is undefined when setting the style of m…
daniloercoli Aug 1, 2018
f214c76
Blocks: Extract phrasing content to its own file to allow reuse with …
gziolo Aug 1, 2018
762b582
Merge branch 'rnmobile/port-para-block-use-latest-gb-master-july' of …
gziolo Aug 1, 2018
9651c70
Remove method `setForceUpdate` that is not used. Also remove the conv…
daniloercoli Aug 1, 2018
248a2c8
Remove `aria-label` that is not used in RichText mobile
daniloercoli Aug 1, 2018
b34049a
Convert private instance variable `lastContentSizeHeight`, to local m…
daniloercoli Aug 1, 2018
131e3e7
Use built-in `forceUpdate` method call, instead of using a private bo…
daniloercoli Aug 1, 2018
3351260
Add comment for P replacement for BR
SergioEstevao Aug 1, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core-blocks/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import {
*/
import * as code from './code';
import * as more from './more';
import * as paragraph from './paragraph';

export const registerCoreBlocks = () => {
[
paragraph,
code,
more,
].forEach( ( { name, settings } ) => {
Expand Down
243 changes: 243 additions & 0 deletions core-blocks/paragraph/edit.js
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( [
Copy link
Member

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.

withColors( 'backgroundColor', { textColor: 'color' } ),
withFontSizes( 'fontSize' ),
applyFallbackStyles,
] )( ParagraphBlock );

export default ParagraphEdit;
61 changes: 61 additions & 0 deletions core-blocks/paragraph/edit.native.js
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' ) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this test to have included?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's not needed at the moment.

/>
</View>
);
}
}

export default ParagraphEdit;
Loading