Skip to content

Commit

Permalink
Port heading block to mobile (#8774)
Browse files Browse the repository at this point in the history
* Adds tagName prop to RichText

Instead of forcing a paragraph, make the wrapper tag logic more generic
and allow for customization

* Adds React Native implementation of heading block

* Fix lint issues

* Remove RichText tagName default value

* Remove unused (for now) class name props
  • Loading branch information
koke authored and gziolo committed Aug 10, 2018
1 parent 6e62e8a commit dd78fd2
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
54 changes: 54 additions & 0 deletions core-blocks/heading/edit.native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* External dependencies
*/
import { View } from 'react-native';

/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { Component } from '@wordpress/element';
import { RichText } from '@wordpress/editor';

/**
* Internal dependencies
*/
import './editor.scss';

const minHeight = 50;

class HeadingEdit extends Component {
render() {
const {
attributes,
setAttributes,
} = this.props;

const {
level,
placeholder,
} = attributes;

const tagName = 'h' + level;

return (
<View>
<RichText
tagName={ tagName }
content={ { contentTree: attributes.content, eventCount: attributes.eventCount } }
style={ {
minHeight: Math.max( minHeight, typeof attributes.aztecHeight === 'undefined' ? 0 : attributes.aztecHeight ),
} }
onChange={ ( value ) => {
setAttributes( value );
} }
onContentSizeChange={ ( event ) => {
setAttributes( { aztecHeight: event.aztecHeight } );
} }
placeholder={ placeholder || __( 'Write heading…' ) }
/>
</View>
);
}
}
export default HeadingEdit;
2 changes: 2 additions & 0 deletions core-blocks/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import {
* Internal dependencies
*/
import * as code from './code';
import * as heading from './heading';
import * as more from './more';
import * as paragraph from './paragraph';

export const registerCoreBlocks = () => {
[
paragraph,
heading,
code,
more,
].forEach( ( { name, settings } ) => {
Expand Down
1 change: 1 addition & 0 deletions core-blocks/paragraph/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class ParagraphEdit extends Component {
return (
<View>
<RichText
tagName="p"
content={ { contentTree: attributes.content, eventCount: attributes.eventCount } }
style={ {
...style,
Expand Down
12 changes: 8 additions & 4 deletions packages/editor/src/components/rich-text/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ export class RichText extends Component {
this.lastEventCount = event.nativeEvent.eventCount;
// The following method just cleans up any <p> tags produced by aztec and replaces them with a br tag
// This should be removed on a later version when aztec doesn't return the top tag of the text being edited
const contentWithoutP = event.nativeEvent.text.replace( /<p>/gi, '' ).replace( /<\/p>/gi, '<br>' );
this.lastContent = contentWithoutP;
const openingTagRegexp = RegExp( '^<' + this.props.tagName + '>', 'gi' );
const closingTagRegexp = RegExp( '</' + this.props.tagName + '>$', 'gi' );
const contentWithoutRootTag = event.nativeEvent.text.replace( openingTagRegexp, '' ).replace( closingTagRegexp, '' );
this.lastContent = contentWithoutRootTag;

this.currentTimer = setTimeout( function() {
this.props.onChange( {
Expand Down Expand Up @@ -68,13 +70,15 @@ export class RichText extends Component {
}

render() {
// Save back to HTML from React tree
const html = renderToString( this.props.content.contentTree );
const {
tagName,
style,
eventCount,
} = this.props;

// Save back to HTML from React tree
const html = '<' + tagName + '>' + renderToString( this.props.content.contentTree ) + '</' + tagName + '>';

return (
<RCTAztecView
text={ { text: html, eventCount: eventCount } }
Expand Down

0 comments on commit dd78fd2

Please sign in to comment.