From c989bce15b4815258b8bd619bb87936263095626 Mon Sep 17 00:00:00 2001 From: Danilo Ercoli Date: Wed, 31 Oct 2018 15:50:07 +0100 Subject: [PATCH] Port quote block to native app --- packages/block-library/src/index.native.js | 2 + packages/block-library/src/quote/edit.js | 59 ++++++++++ .../block-library/src/quote/edit.native.js | 108 ++++++++++++++++++ packages/block-library/src/quote/index.js | 58 +--------- 4 files changed, 172 insertions(+), 55 deletions(-) create mode 100644 packages/block-library/src/quote/edit.js create mode 100644 packages/block-library/src/quote/edit.native.js diff --git a/packages/block-library/src/index.native.js b/packages/block-library/src/index.native.js index 3da15242c7cfb..706df00a4fb36 100644 --- a/packages/block-library/src/index.native.js +++ b/packages/block-library/src/index.native.js @@ -14,6 +14,7 @@ import * as more from './more'; import * as paragraph from './paragraph'; import * as image from './image'; import * as nextpage from './nextpage'; +import * as quote from './quote'; export const registerCoreBlocks = () => { [ @@ -23,6 +24,7 @@ export const registerCoreBlocks = () => { more, image, nextpage, + quote, ].forEach( ( { name, settings } ) => { registerBlockType( name, settings ); } ); diff --git a/packages/block-library/src/quote/edit.js b/packages/block-library/src/quote/edit.js new file mode 100644 index 0000000000000..af0fce21d8ebe --- /dev/null +++ b/packages/block-library/src/quote/edit.js @@ -0,0 +1,59 @@ +/** + * WordPress dependencies + */ +import { __ } from '@wordpress/i18n'; +import { Fragment } from '@wordpress/element'; +import { + BlockControls, + AlignmentToolbar, + RichText, +} from '@wordpress/editor'; + +export default function edit( { attributes, setAttributes, isSelected, mergeBlocks, onReplace, className } ) { + const { align, value, citation } = attributes; + return ( + + + { + setAttributes( { align: nextAlign } ); + } } + /> + +
+ setAttributes( { + value: nextValue, + } ) + } + onMerge={ mergeBlocks } + onRemove={ ( forward ) => { + const hasEmptyCitation = ! citation || citation.length === 0; + if ( ! forward && hasEmptyCitation ) { + onReplace( [] ); + } + } } + /* translators: the text of the quotation */ + placeholder={ __( 'Write quote…' ) } + /> + { ( ! RichText.isEmpty( citation ) || isSelected ) && ( + setAttributes( { + citation: nextCitation, + } ) + } + /* translators: the individual or entity quoted */ + placeholder={ __( 'Write citation…' ) } + className="wp-block-quote__citation" + /> + ) } +
+
+ ); +} diff --git a/packages/block-library/src/quote/edit.native.js b/packages/block-library/src/quote/edit.native.js new file mode 100644 index 0000000000000..94d6f23c839e5 --- /dev/null +++ b/packages/block-library/src/quote/edit.native.js @@ -0,0 +1,108 @@ +/** + * 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 QuoteEdit extends Component { + render() { + const { + attributes, + setAttributes, + style, + mergeBlocks, + onReplace, + isSelected, + } = this.props; + + const { + placeholder, + align, + value, + citation, + } = attributes; + + return ( + + { + // Create a React Tree from the new HTML + const newQuoteBlock = parse( '
' + event.content + '
' )[ 0 ]; + setAttributes( { + ...this.props.attributes, + value: newQuoteBlock.attributes.value, + } ); + } + } + onContentSizeChange={ ( event ) => { + setAttributes( { + ...this.props.attributes, + aztecHeight: event.aztecHeight, + } ); + } + } + onMerge={ mergeBlocks } + onRemove={ ( forward ) => { + const hasEmptyCitation = ! citation || citation.length === 0; + if ( ! forward && hasEmptyCitation ) { + onReplace( [] ); + } + } } + /* translators: the text of the quotation */ + placeholder={ placeholder || __( 'Write quote…' ) } + /> + { ( ! RichText.isEmpty( citation ) || isSelected ) && ( + { + // TODO: remove this fix + const openingTagRegexp = RegExp( '', 'gim' ); + const closingTagRegexp = RegExp( '', 'gim' ); + let newCiteVaule = event.content; + newCiteVaule = newCiteVaule.replace( openingTagRegexp, '' ).replace( closingTagRegexp, '' ); + setAttributes( { + ...this.props.attributes, + citation: newCiteVaule, + } ); + } + } + onContentSizeChange={ ( event ) => { + setAttributes( { + ...this.props.attributes, + aztecHeight: event.aztecHeight, + } ); + } + } + /* translators: the individual or entity quoted */ + placeholder={ placeholder || __( 'Write citation…' ) } + className="wp-block-quote__citation" + /> + ) } +
+ ); + } +} + +export default QuoteEdit; diff --git a/packages/block-library/src/quote/index.js b/packages/block-library/src/quote/index.js index a87b0dba27453..4e2d635b5ea8a 100644 --- a/packages/block-library/src/quote/index.js +++ b/packages/block-library/src/quote/index.js @@ -7,15 +7,11 @@ import { omit } from 'lodash'; * WordPress dependencies */ import { __, _x } from '@wordpress/i18n'; -import { Fragment } from '@wordpress/element'; import { createBlock, getPhrasingContentSchema } from '@wordpress/blocks'; -import { - BlockControls, - AlignmentToolbar, - RichText, -} from '@wordpress/editor'; +import { RichText } from '@wordpress/editor'; import { join, split, create, toHTMLString } from '@wordpress/rich-text'; import { G, Path, SVG } from '@wordpress/components'; +import edit from './edit'; const blockAttributes = { value: { @@ -187,55 +183,7 @@ export const settings = { ], }, - edit( { attributes, setAttributes, isSelected, mergeBlocks, onReplace, className } ) { - const { align, value, citation } = attributes; - - return ( - - - { - setAttributes( { align: nextAlign } ); - } } - /> - -
- setAttributes( { - value: nextValue, - } ) - } - onMerge={ mergeBlocks } - onRemove={ ( forward ) => { - const hasEmptyCitation = ! citation || citation.length === 0; - if ( ! forward && hasEmptyCitation ) { - onReplace( [] ); - } - } } - /* translators: the text of the quotation */ - placeholder={ __( 'Write quote…' ) } - /> - { ( ! RichText.isEmpty( citation ) || isSelected ) && ( - setAttributes( { - citation: nextCitation, - } ) - } - /* translators: the individual or entity quoted */ - placeholder={ __( 'Write citation…' ) } - className="wp-block-quote__citation" - /> - ) } -
-
- ); - }, + edit, save( { attributes } ) { const { align, value, citation } = attributes;