Skip to content

Commit

Permalink
Move Reusable Block definition from blocks/library to editor/library
Browse files Browse the repository at this point in the history
By having the definition in the `editor` module, we are able to easily
access the redux store using the selectors and actions defined in
`editor/selectors.js` and `editor/actions.js`.
  • Loading branch information
noisysocks committed Dec 13, 2017
1 parent 8777e86 commit f1fc7b6
Show file tree
Hide file tree
Showing 12 changed files with 24 additions and 61 deletions.
1 change: 0 additions & 1 deletion blocks/library/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,4 @@ import './text-columns';
import './verse';
import './video';
import './audio';
import './block';
import './paragraph';
1 change: 0 additions & 1 deletion blocks/test/fixtures/core__block.html

This file was deleted.

11 changes: 0 additions & 11 deletions blocks/test/fixtures/core__block.json

This file was deleted.

14 changes: 0 additions & 14 deletions blocks/test/fixtures/core__block.parsed.json

This file was deleted.

1 change: 0 additions & 1 deletion blocks/test/fixtures/core__block.serialized.html

This file was deleted.

1 change: 1 addition & 0 deletions editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { settings as dateSettings } from '@wordpress/date';
* Internal dependencies
*/
import './assets/stylesheets/main.scss';
import './library';
import Layout from './edit-post/layout';
import { EditorProvider, ErrorBoundary } from './components';
import { initializeMetaBoxState } from './actions';
Expand Down
File renamed without changes.
File renamed without changes.
53 changes: 21 additions & 32 deletions blocks/library/block/index.js → editor/library/block/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import classnames from 'classnames';
import { Component } from '@wordpress/element';
import { Placeholder, Spinner } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { getBlockType, registerBlockType, hasBlockSupport, getBlockDefaultClassname } from '@wordpress/blocks';

/**
* Internal dependencies
*/
import { getBlockType, registerBlockType, hasBlockSupport, getBlockDefaultClassname } from '../../api';
import ReusableBlockEditPanel from './edit-panel';
import { fetchReusableBlocks, updateReusableBlock, saveReusableBlock, convertBlockToStatic } from '../../actions';
import { getReusableBlock, isSavingReusableBlock } from '../../selectors';

class ReusableBlockEdit extends Component {
constructor() {
Expand All @@ -26,7 +28,7 @@ class ReusableBlockEdit extends Component {
this.stopEditing = this.stopEditing.bind( this );
this.setAttributes = this.setAttributes.bind( this );
this.setName = this.setName.bind( this );
this.updateReusableBlock = this.updateReusableBlock.bind( this );
this.saveChanges = this.saveChanges.bind( this );

this.state = {
isEditing: false,
Expand All @@ -37,7 +39,7 @@ class ReusableBlockEdit extends Component {

componentDidMount() {
if ( ! this.props.reusableBlock ) {
this.props.fetchReusableBlock();
this.props.onFetch();
}
}

Expand All @@ -63,7 +65,7 @@ class ReusableBlockEdit extends Component {
this.setState( { name } );
}

updateReusableBlock() {
saveChanges() {
const { name, attributes } = this.state;

// Use pickBy to include only changed (assigned) values in payload
Expand All @@ -72,13 +74,13 @@ class ReusableBlockEdit extends Component {
attributes,
} );

this.props.updateReusableBlock( payload );
this.props.saveReusableBlock();
this.props.onUpdate( payload );
this.props.onSave();
this.stopEditing();
}

render() {
const { focus, reusableBlock, isSaving, convertBlockToStatic } = this.props;
const { focus, reusableBlock, isSaving, onConvert } = this.props;
const { isEditing, name, attributes } = this.state;

if ( ! reusableBlock ) {
Expand Down Expand Up @@ -112,9 +114,9 @@ class ReusableBlockEdit extends Component {
name={ name !== null ? name : reusableBlock.name }
isSaving={ isSaving }
onEdit={ this.startEditing }
onDetach={ convertBlockToStatic }
onDetach={ onConvert }
onChangeName={ this.setName }
onSave={ this.updateReusableBlock }
onSave={ this.saveChanges }
onCancel={ this.stopEditing }
/>
),
Expand All @@ -124,34 +126,21 @@ class ReusableBlockEdit extends Component {

const ConnectedReusableBlockEdit = connect(
( state, ownProps ) => ( {
reusableBlock: state.reusableBlocks.data[ ownProps.attributes.ref ],
isSaving: state.reusableBlocks.isSaving[ ownProps.attributes.ref ],
reusableBlock: getReusableBlock( state, ownProps.attributes.ref ),
isSaving: isSavingReusableBlock( state, ownProps.attributes.ref ),
} ),
( dispatch, ownProps ) => ( {
fetchReusableBlock() {
dispatch( {
type: 'FETCH_REUSABLE_BLOCKS',
id: ownProps.attributes.ref,
} );
onFetch() {
dispatch( fetchReusableBlocks( ownProps.attributes.ref ) );
},
updateReusableBlock( reusableBlock ) {
dispatch( {
type: 'UPDATE_REUSABLE_BLOCK',
id: ownProps.attributes.ref,
reusableBlock,
} );
onUpdate( reusableBlock ) {
dispatch( updateReusableBlock( ownProps.attributes.ref, reusableBlock ) );
},
saveReusableBlock() {
dispatch( {
type: 'SAVE_REUSABLE_BLOCK',
id: ownProps.attributes.ref,
} );
onSave() {
dispatch( saveReusableBlock( ownProps.attributes.ref ) );
},
convertBlockToStatic() {
dispatch( {
type: 'CONVERT_BLOCK_TO_STATIC',
uid: ownProps.id,
} );
onConvert() {
dispatch( convertBlockToStatic( ownProps.id ) );
},
} )
)( ReusableBlockEdit );
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions editor/library/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './block';
2 changes: 1 addition & 1 deletion lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
require dirname( __FILE__ ) . '/register.php';

// Register server-side code for individual blocks.
foreach ( glob( dirname( __FILE__ ) . '/../blocks/library/*/index.php' ) as $block_logic ) {
foreach ( glob( dirname( __FILE__ ) . '/../{blocks,editor}/library/*/index.php', GLOB_BRACE ) as $block_logic ) {
require $block_logic;
}

0 comments on commit f1fc7b6

Please sign in to comment.