Skip to content

Commit

Permalink
Blocks: Show controls for newly created block
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Apr 30, 2018
1 parent e59ef0e commit e80d22f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 6 deletions.
8 changes: 7 additions & 1 deletion blocks/block-edit/context.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
/**
* External dependencies
*/
import { noop } from 'lodash';

/**
* WordPress dependencies
*/
import { createContext, createHigherOrderComponent } from '@wordpress/element';

const { Consumer, Provider } = createContext( {
name: '',
isSelected: false,
focusedElement: null,
setFocusedElement: () => {},
initFocusedElement: noop,
setFocusedElement: noop,
} );

export { Provider as BlockEditContextProvider };
Expand Down
11 changes: 11 additions & 0 deletions blocks/block-edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ import { BlockEditContextProvider } from './context';
export class BlockEdit extends Component {
constructor( props ) {
super( props );
this.initFocusedElement = this.initFocusedElement.bind( this );
this.setFocusedElement = this.setFocusedElement.bind( this );
this.state = {
focusedElement: null,
initFocusedElement: this.initFocusedElement,
setFocusedElement: this.setFocusedElement,
};
}
Expand All @@ -42,6 +44,15 @@ export class BlockEdit extends Component {
};
}

initFocusedElement( focusedElement ) {
this.setState( ( prevState ) => {
if ( prevState.focusedElement !== null ) {
return null;
}
return { focusedElement };
} );
}

setFocusedElement( focusedElement ) {
this.setState( ( prevState ) => {
if ( prevState.focusedElement === focusedElement ) {
Expand Down
7 changes: 6 additions & 1 deletion blocks/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,12 @@ const RichTextContainer = compose( [
// Ensures that only one RichText component can be focused.
return {
isSelected: context.isSelected && context.focusedElement === ownProps.instanceId,
onFocus: () => context.setFocusedElement( ownProps.instanceId ),
onSetup: () => {
context.initFocusedElement( ownProps.instanceId );
},
onFocus: () => {
context.setFocusedElement( ownProps.instanceId );
},
};
} ),
withSelect( ( select ) => {
Expand Down
1 change: 0 additions & 1 deletion core-blocks/pullquote/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { map } from 'lodash';
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { withState } from '@wordpress/components';
import { Fragment } from '@wordpress/element';
import {
BlockControls,
Expand Down
6 changes: 3 additions & 3 deletions core-blocks/text-columns/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { times } from 'lodash';
import { get, times } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -90,7 +90,7 @@ export const settings = {
<div className="wp-block-column" key={ `column-${ index }` }>
<RichText
tagName="p"
value={ content && content[ index ] && content[ index ].children }
value={ get( content, [ index, 'children' ] ) }
onChange={ ( nextContent ) => {
setAttributes( {
content: [
Expand All @@ -116,7 +116,7 @@ export const settings = {
<div className={ `align${ width } columns-${ columns }` }>
{ times( columns, ( index ) =>
<div className="wp-block-column" key={ `column-${ index }` }>
<RichText.Content tagName="p" value={ content && content[ index ].children } />
<RichText.Content tagName="p" value={ get( content, [ index, 'children' ] ) } />
</div>
) }
</div>
Expand Down

0 comments on commit e80d22f

Please sign in to comment.