Skip to content

Commit

Permalink
Revert "Use nested blocks for quotes (#6054)" (#6501)
Browse files Browse the repository at this point in the history
This reverts commit 853900d.
  • Loading branch information
ellatrix authored Apr 30, 2018
1 parent 414d850 commit 230ad0d
Show file tree
Hide file tree
Showing 35 changed files with 444 additions and 426 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 ? <BlockList layouts={ layouts } /> : null;
return <BlockList layouts={ layouts } />;
}

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

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

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 `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.
*Optional.* Called when the content can be split with `before` and `after`. There might be blocks present, which should be inserted in between.

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

Expand Down
42 changes: 16 additions & 26 deletions blocks/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ 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 @@ -126,7 +121,6 @@ export class RichText extends Component {
};

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

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

// Allows us to ask for this information when we get a report.
console.log( 'Received item:\n\n', blob );
window.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 @@ -309,8 +303,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.
console.log( 'Received HTML:\n\n', HTML );
console.log( 'Received plain text:\n\n', this.pastedPlainText );
window.console.log( 'Received HTML:\n\n', HTML );
window.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 @@ -325,7 +319,7 @@ export class RichText extends Component {
} );

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

event.preventDefault();

Expand Down Expand Up @@ -493,10 +487,6 @@ 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 @@ -629,8 +619,10 @@ export class RichText extends Component {
return memo;
}, [] );

const { format } = this.props;
// 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 @@ -709,16 +701,15 @@ export class RichText extends Component {
!! this.editor &&
this.props.tagName === prevProps.tagName &&
this.props.value !== prevProps.value &&
this.props.value !== this.savedContent
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.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 @@ -775,15 +766,14 @@ export class RichText extends Component {

/**
* Calling onSplit means we need to abort the change done by TinyMCE.
* we need to call setContent to restore the initial content before calling onSplit.
* we need to call updateContent 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.setContent( before );
this.onChange();
this.updateContent();
this.props.onSplit( before, after, ...blocks );
}

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

/**
* WordPress dependencies
Expand Down Expand Up @@ -63,6 +63,21 @@ 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 @@ -98,6 +113,18 @@ 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 @@ -205,6 +232,7 @@ export const settings = {
const {
attributes,
insertBlocksAfter,
setAttributes,
mergeBlocks,
onReplace,
className,
Expand Down Expand Up @@ -252,7 +280,7 @@ export const settings = {
onMerge={ mergeBlocks }
onSplit={
insertBlocksAfter ?
( unused, after, ...blocks ) => {
( before, after, ...blocks ) => {
if ( ! blocks.length ) {
blocks.push( createBlock( 'core/paragraph' ) );
}
Expand All @@ -264,6 +292,7 @@ export const settings = {
} ) );
}

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

/**
* WordPress dependencies
Expand All @@ -10,11 +10,9 @@ 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 @@ -23,7 +21,21 @@ 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 @@ -59,7 +71,7 @@ export const settings = {
edit: withState( {
editable: 'content',
} )( ( { attributes, setAttributes, isSelected, className, editable, setState } ) => {
const { citation, align } = attributes;
const { value, citation, align } = attributes;
const updateAlignment = ( nextAlign ) => setAttributes( { align: nextAlign } );
const onSetActiveEditable = ( newEditable ) => () => setState( { editable: newEditable } );

Expand All @@ -72,7 +84,20 @@ export const settings = {
/>
</BlockControls>
<blockquote className={ className }>
<InnerBlocks />
<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' ) }
/>
{ ( citation || isSelected ) && (
<RichText
tagName="cite"
Expand All @@ -94,11 +119,11 @@ export const settings = {
} ),

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

return (
<blockquote className={ `align${ align }` }>
<InnerBlocks.Content />
<RichText.Content value={ toRichTextValue( value ) } />
{ citation && citation.length > 0 && <RichText.Content tagName="cite" value={ citation } /> }
</blockquote>
);
Expand All @@ -107,54 +132,6 @@ 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 @@ -167,9 +144,7 @@ export const settings = {

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

0 comments on commit 230ad0d

Please sign in to comment.