Skip to content

Commit

Permalink
Revert "Revert "Use nested blocks for quotes (#6054)" (#6501)"
Browse files Browse the repository at this point in the history
This reverts commit 230ad0d.
  • Loading branch information
ellatrix committed May 1, 2018
1 parent 88f48b8 commit b928ac9
Show file tree
Hide file tree
Showing 35 changed files with 426 additions and 444 deletions.
4 changes: 2 additions & 2 deletions blocks/inner-blocks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import { withContext } from '@wordpress/components';

function InnerBlocks( { BlockList, layouts } ) {
return <BlockList layouts={ layouts } />;
return BlockList ? <BlockList layouts={ layouts } /> : null;
}

InnerBlocks = withContext( 'BlockList' )()( InnerBlocks );

InnerBlocks.Content = ( { BlockContent } ) => {
return <BlockContent />;
return BlockContent ? <BlockContent /> : null;
};

InnerBlocks.Content = withContext( 'BlockContent' )()( InnerBlocks.Content );
Expand Down
2 changes: 1 addition & 1 deletion blocks/rich-text/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ a traditional `input` field, usually when the user exits the field.

### `onSplit( before: Array|String, after: Array|String, ...blocks: Object ): Function`

*Optional.* Called when the content can be split with `before` and `after`. There might be blocks present, which should be inserted in between.
*Optional.* Called when the content can be split with `after` as the split off value. There might be blocks present, which should be inserted before the `after` value. Note: the `before` value should no longer be used.

### `onReplace( blocks: Array ): Function`

Expand Down
42 changes: 26 additions & 16 deletions blocks/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ import { EVENTS } from './constants';
import { withBlockEditContext } from '../block-edit/context';
import { domToFormat, valueToString, isEmpty } from './format';

/**
* Browser dependencies
*/
const { console } = window;

const { BACKSPACE, DELETE, ENTER } = keycodes;

/**
Expand Down Expand Up @@ -121,6 +126,7 @@ export class RichText extends Component {
};

this.isEmpty = ! value || ! value.length;
this.savedContent = value;
}

/**
Expand Down Expand Up @@ -272,7 +278,7 @@ export class RichText extends Component {
} );

// Allows us to ask for this information when we get a report.
window.console.log( 'Received item:\n\n', blob );
console.log( 'Received item:\n\n', blob );

if ( isEmptyEditor && this.props.onReplace ) {
// Necessary to allow the paste bin to be removed without errors.
Expand Down Expand Up @@ -303,8 +309,8 @@ export class RichText extends Component {
onPastePreProcess( event ) {
const HTML = this.isPlainTextPaste ? '' : event.content;
// Allows us to ask for this information when we get a report.
window.console.log( 'Received HTML:\n\n', HTML );
window.console.log( 'Received plain text:\n\n', this.pastedPlainText );
console.log( 'Received HTML:\n\n', HTML );
console.log( 'Received plain text:\n\n', this.pastedPlainText );

// There is a selection, check if a link is pasted.
if ( ! this.editor.selection.isCollapsed() ) {
Expand All @@ -319,7 +325,7 @@ export class RichText extends Component {
} );

// Allows us to ask for this information when we get a report.
window.console.log( 'Created link:\n\n', pastedText );
console.log( 'Created link:\n\n', pastedText );

event.preventDefault();

Expand Down Expand Up @@ -487,6 +493,10 @@ export class RichText extends Component {
if ( event.shiftKey || ! this.props.onSplit ) {
this.editor.execCommand( 'InsertLineBreak', false, event );
} else {
// Splitting the content might destroy the editor, so it's
// important that we stop other handlers (e.g. ones
// registered by TinyMCE) from also handling this event.
event.stopImmediatePropagation();
this.splitContent();
}
}
Expand Down Expand Up @@ -619,10 +629,8 @@ export class RichText extends Component {
return memo;
}, [] );

// Splitting into two blocks
this.setContent( this.props.value );

const { format } = this.props;

this.restoreContentAndSplit(
domToFormat( before, format, this.editor ),
domToFormat( after, format, this.editor )
Expand Down Expand Up @@ -701,15 +709,16 @@ export class RichText extends Component {
!! this.editor &&
this.props.tagName === prevProps.tagName &&
this.props.value !== prevProps.value &&
this.props.value !== this.savedContent &&

// Comparing using isEqual is necessary especially to avoid unnecessary updateContent calls
// This fixes issues in multi richText blocks like quotes when moving the focus between
// the different editables.
! isEqual( this.props.value, prevProps.value ) &&
! isEqual( this.props.value, this.savedContent )
this.props.value !== this.savedContent
) {
this.updateContent();

if (
'development' === process.env.NODE_ENV &&
isEqual( this.props.value, prevProps.value )
) {
console.warn( 'The current and previous value props are not strictly equal but the contents are the same. Please ensure the value prop reference does not change.' );
}
}
}

Expand Down Expand Up @@ -766,14 +775,15 @@ export class RichText extends Component {

/**
* Calling onSplit means we need to abort the change done by TinyMCE.
* we need to call updateContent to restore the initial content before calling onSplit.
* we need to call setContent to restore the initial content before calling onSplit.
*
* @param {Array} before content before the split position
* @param {Array} after content after the split position
* @param {?Array} blocks blocks to insert at the split position
*/
restoreContentAndSplit( before, after, blocks = [] ) {
this.updateContent();
this.setContent( before );
this.onChange();
this.props.onSplit( before, after, ...blocks );
}

Expand Down
3 changes: 1 addition & 2 deletions core-blocks/heading/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ export const settings = {
onMerge={ mergeBlocks }
onSplit={
insertBlocksAfter ?
( before, after, ...blocks ) => {
setAttributes( { content: before } );
( unused, after, ...blocks ) => {
insertBlocksAfter( [
...blocks,
createBlock( 'core/paragraph', { content: after } ),
Expand Down
33 changes: 2 additions & 31 deletions core-blocks/list/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { find, compact, get, initial, last, isEmpty } from 'lodash';
import { find, compact, get, isEmpty } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -63,21 +63,6 @@ export const settings = {
} );
},
},
{
type: 'block',
blocks: [ 'core/quote' ],
transform: ( { value, citation } ) => {
const items = value.map( ( p ) => get( p, 'children.props.children' ) );
if ( ! isEmpty( citation ) ) {
items.push( citation );
}
const hasItems = ! items.every( isEmpty );
return createBlock( 'core/list', {
nodeName: 'UL',
values: hasItems ? items.map( ( content, index ) => <li key={ index }>{ content }</li> ) : [],
} );
},
},
{
type: 'raw',
isMatch: ( node ) => node.nodeName === 'OL' || node.nodeName === 'UL',
Expand Down Expand Up @@ -113,18 +98,6 @@ export const settings = {
content: [ content ],
} ) ),
},
{
type: 'block',
blocks: [ 'core/quote' ],
transform: ( { values } ) => {
return createBlock( 'core/quote', {
value: compact( ( values.length === 1 ? values : initial( values ) )
.map( ( value ) => get( value, 'props.children', null ) ) )
.map( ( children ) => ( { children: <p>{ children }</p> } ) ),
citation: ( values.length === 1 ? undefined : [ get( last( values ), 'props.children' ) ] ),
} );
},
},
],
},

Expand Down Expand Up @@ -232,7 +205,6 @@ export const settings = {
const {
attributes,
insertBlocksAfter,
setAttributes,
mergeBlocks,
onReplace,
className,
Expand Down Expand Up @@ -280,7 +252,7 @@ export const settings = {
onMerge={ mergeBlocks }
onSplit={
insertBlocksAfter ?
( before, after, ...blocks ) => {
( unused, after, ...blocks ) => {
if ( ! blocks.length ) {
blocks.push( createBlock( 'core/paragraph' ) );
}
Expand All @@ -292,7 +264,6 @@ export const settings = {
} ) );
}

setAttributes( { values: before } );
insertBlocksAfter( blocks );
} :
undefined
Expand Down
3 changes: 1 addition & 2 deletions core-blocks/paragraph/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,7 @@ class ParagraphBlock extends Component {
} );
} }
onSplit={ insertBlocksAfter ?
( before, after, ...blocks ) => {
setAttributes( { content: before } );
( unused, after, ...blocks ) => {
insertBlocksAfter( [
...blocks,
createBlock( 'core/paragraph', { content: after } ),
Expand Down
91 changes: 58 additions & 33 deletions core-blocks/pullquote/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { map } from 'lodash';
import { castArray } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -10,9 +10,11 @@ import { __ } from '@wordpress/i18n';
import { withState } from '@wordpress/components';
import { Fragment } from '@wordpress/element';
import {
createBlock,
BlockControls,
BlockAlignmentToolbar,
RichText,
InnerBlocks,
} from '@wordpress/blocks';

/**
Expand All @@ -21,21 +23,7 @@ import {
import './editor.scss';
import './style.scss';

const toRichTextValue = ( value ) => map( value, ( ( subValue ) => subValue.children ) );
const fromRichTextValue = ( value ) => map( value, ( subValue ) => ( {
children: subValue,
} ) );
const blockAttributes = {
value: {
type: 'array',
source: 'query',
selector: 'blockquote > p',
query: {
children: {
source: 'node',
},
},
},
citation: {
type: 'array',
source: 'children',
Expand Down Expand Up @@ -71,7 +59,7 @@ export const settings = {
edit: withState( {
editable: 'content',
} )( ( { attributes, setAttributes, isSelected, className, editable, setState } ) => {
const { value, citation, align } = attributes;
const { citation, align } = attributes;
const updateAlignment = ( nextAlign ) => setAttributes( { align: nextAlign } );
const onSetActiveEditable = ( newEditable ) => () => setState( { editable: newEditable } );

Expand All @@ -84,20 +72,7 @@ export const settings = {
/>
</BlockControls>
<blockquote className={ className }>
<RichText
multiline="p"
value={ toRichTextValue( value ) }
onChange={
( nextValue ) => setAttributes( {
value: fromRichTextValue( nextValue ),
} )
}
/* translators: the text of the quotation */
placeholder={ __( 'Write quote…' ) }
wrapperClassName="blocks-pullquote__content"
isSelected={ isSelected && editable === 'content' }
onFocus={ onSetActiveEditable( 'content' ) }
/>
<InnerBlocks />
{ ( citation || isSelected ) && (
<RichText
tagName="cite"
Expand All @@ -119,11 +94,11 @@ export const settings = {
} ),

save( { attributes } ) {
const { value, citation, align } = attributes;
const { citation, align } = attributes;

return (
<blockquote className={ `align${ align }` }>
<RichText.Content value={ toRichTextValue( value ) } />
<InnerBlocks.Content />
{ citation && citation.length > 0 && <RichText.Content tagName="cite" value={ citation } /> }
</blockquote>
);
Expand All @@ -132,6 +107,54 @@ export const settings = {
deprecated: [ {
attributes: {
...blockAttributes,
value: {
type: 'array',
source: 'query',
selector: 'blockquote > p',
query: {
children: {
source: 'node',
},
},
},
},

migrate( { value = [], ...attributes } ) {
return [
attributes,
value.map( ( { children: paragraph } ) =>
createBlock( 'core/paragraph', {
content: castArray( paragraph.props.children ),
} )
),
];
},

save( { attributes } ) {
const { value, citation, align } = attributes;

return (
<blockquote className={ `align${ align }` }>
{ value && value.map( ( paragraph, i ) =>
<p key={ i }>{ paragraph.children && paragraph.children.props.children }</p>
) }
{ citation && citation.length > 0 && <RichText.Content tagName="cite" value={ citation } /> }
</blockquote>
);
},
}, {
attributes: {
...blockAttributes,
value: {
type: 'array',
source: 'query',
selector: 'blockquote > p',
query: {
children: {
source: 'node',
},
},
},
citation: {
type: 'array',
source: 'children',
Expand All @@ -144,7 +167,9 @@ export const settings = {

return (
<blockquote className={ `align${ align }` }>
<RichText.Content value={ toRichTextValue( value ) } />
{ value && value.map( ( paragraph, i ) =>
<p key={ i }>{ paragraph.children && paragraph.children.props.children }</p>
) }
{ citation && citation.length > 0 && <RichText.Content tagName="footer" value={ citation } /> }
</blockquote>
);
Expand Down
Loading

0 comments on commit b928ac9

Please sign in to comment.