Skip to content

Commit

Permalink
Blocks: Reimplement shared block as embedded editor
Browse files Browse the repository at this point in the history
- Publish shared blocks when creating and editing them
- Fix exception when converting a shared block to static
- Fix 'Convert to Regular Block' button
- Remove the block when deleting a shared block
  • Loading branch information
aduth authored and noisysocks committed Aug 21, 2018
1 parent 4a184d5 commit 9e7b7a3
Show file tree
Hide file tree
Showing 23 changed files with 275 additions and 1,168 deletions.
43 changes: 43 additions & 0 deletions core-blocks/block/selection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { withSelect, withDispatch } from '@wordpress/data';
import { compose } from '@wordpress/compose';

class SharedBlockSelection extends Component {
componentDidUpdate( prevProps ) {
const {
isSharedBlockSelected,
hasSelection,
clearSelectedBlock,
onBlockSelection,
} = this.props;

if ( ! isSharedBlockSelected && prevProps.isSharedBlockSelected ) {
clearSelectedBlock();
}

if ( hasSelection && ! prevProps.hasSelection ) {
onBlockSelection();
}
}

render() {
return this.props.children;
}
}

export default compose( [
withSelect( ( select ) => {
const { getBlockSelectionStart } = select( 'core/editor' );

return {
hasSelection: !! getBlockSelectionStart(),
};
} ),
withDispatch( ( dispatch ) => {
const { clearSelectedBlock } = dispatch( 'core/editor' );
return { clearSelectedBlock };
} ),
] )( SharedBlockSelection );
123 changes: 0 additions & 123 deletions lib/class-wp-rest-blocks-controller.php

This file was deleted.

1 change: 0 additions & 1 deletion lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// These files only need to be loaded if within a rest server instance
// which this class will exist if that is the case.
if ( class_exists( 'WP_REST_Controller' ) ) {
require dirname( __FILE__ ) . '/class-wp-rest-blocks-controller.php';
require dirname( __FILE__ ) . '/class-wp-rest-autosaves-controller.php';
require dirname( __FILE__ ) . '/class-wp-rest-block-renderer-controller.php';
require dirname( __FILE__ ) . '/class-wp-rest-search-controller.php';
Expand Down
15 changes: 7 additions & 8 deletions lib/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -461,21 +461,20 @@ function gutenberg_register_post_types() {
register_post_type(
'wp_block',
array(
'labels' => array(
'labels' => array(
'name' => 'Blocks',
'singular_name' => 'Block',
),
'public' => false,
'rewrite' => false,
'show_in_rest' => true,
'rest_base' => 'blocks',
'rest_controller_class' => 'WP_REST_Blocks_Controller',
'capability_type' => 'block',
'public' => false,
'rewrite' => false,
'show_in_rest' => true,
'rest_base' => 'blocks',
'capability_type' => 'block',
'capabilities' => array(
'read' => 'read_blocks',
'create_posts' => 'create_blocks',
),
'map_meta_cap' => true,
'map_meta_cap' => true,
)
);

Expand Down
39 changes: 37 additions & 2 deletions packages/block-library/src/block/edit-panel/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
/**
* External dependencies
*/
import { over, compact } from 'lodash';

/**
* WordPress dependencies
*/
import { Button } from '@wordpress/components';
import { Component, Fragment, createRef } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { ESCAPE } from '@wordpress/keycodes';
import { withInstanceId } from '@wordpress/compose';
import { withSelect, withDispatch } from '@wordpress/data';
import { withInstanceId, compose } from '@wordpress/compose';

class ReusableBlockEditPanel extends Component {
constructor() {
Expand Down Expand Up @@ -115,4 +121,33 @@ class ReusableBlockEditPanel extends Component {
}
}

export default withInstanceId( ReusableBlockEditPanel );
export default compose( [
withInstanceId,
withSelect( ( select ) => {
const { getEditedPostAttribute } = select( 'core/editor' );

return {
title: getEditedPostAttribute( 'title' ),
};
} ),
withDispatch( ( dispatch, ownProps ) => {
const {
editPost,
undoAll,
savePost,
clearSelectedBlock,
} = dispatch( 'core/editor' );

const withClearAndFinish = ( fn ) => over( compact( [
clearSelectedBlock,
ownProps.onFinishedEditing,
fn,
] ) );

return {
onChangeTitle: ( title ) => editPost( { title } ),
onSave: withClearAndFinish( savePost ),
onCancel: withClearAndFinish( undoAll ),
};
} ),
] )( ReusableBlockEditPanel );
Loading

0 comments on commit 9e7b7a3

Please sign in to comment.