Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an alternative block appender when the container doesn't support the default block #10136

Merged
merged 7 commits into from
Oct 4, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions packages/editor/src/components/block-list-appender/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* External dependencies
*/
import { last } from 'lodash';
youknowriad marked this conversation as resolved.
Show resolved Hide resolved

/**
* WordPress dependencies
*/
import { withSelect } from '@wordpress/data';
import { getDefaultBlockName } from '@wordpress/blocks';

/**
* Internal dependencies
*/
import IgnoreNestedEvents from '../ignore-nested-events';
import DefaultBlockAppender from '../default-block-appender';
import Inserter from '../inserter';

function BlockListAppender( {
blockClientIds,
layout,
isGroupedByLayout,
rootClientId,
canInsertDefaultBlock,
isLocked,
} ) {
if ( isLocked ) {
return null;
}

let defaultLayout;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see this being redefined, would it be better to just have this be a ternary?

const defaultLayout = isGroupedByLayout ? layout : undefined;
// Or even?
const defaultLayout = isGroupedByLayout && layout;

if ( isGroupedByLayout ) {
defaultLayout = layout;
}

if ( canInsertDefaultBlock ) {
return (
<IgnoreNestedEvents childHandledEvents={ [ 'onFocus', 'onClick', 'onKeyDown' ] }>
<DefaultBlockAppender
rootClientId={ rootClientId }
lastBlockClientId={ last( blockClientIds ) }
youknowriad marked this conversation as resolved.
Show resolved Hide resolved
layout={ defaultLayout }
/>
</IgnoreNestedEvents>
);
}

return (
<div className="block-list-appender">
<Inserter rootClientId={ rootClientId } layout={ defaultLayout } />
</div>
);
}

export default withSelect( ( select, { rootClientId } ) => {
const {
getBlockOrder,
canInsertBlockType,
getTemplateLock,
} = select( 'core/editor' );

return {
isLocked: !! getTemplateLock( rootClientId ),
blockClientIds: getBlockOrder( rootClientId ),
canInsertDefaultBlock: canInsertBlockType( getDefaultBlockName(), rootClientId ),
};
} )( BlockListAppender );
7 changes: 7 additions & 0 deletions packages/editor/src/components/block-list-appender/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.block-list-appender {
display: flex;
align-items: center;
justify-content: center;
padding: 10px;
border: 1px dotted $light-gray-500;
}
2 changes: 1 addition & 1 deletion packages/editor/src/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import BlockContextualToolbar from './block-contextual-toolbar';
import BlockMultiControls from './multi-controls';
import BlockMobileToolbar from './block-mobile-toolbar';
import BlockInsertionPoint from './insertion-point';
import IgnoreNestedEvents from './ignore-nested-events';
import IgnoreNestedEvents from '../ignore-nested-events';
import InserterWithShortcuts from '../inserter-with-shortcuts';
import Inserter from '../inserter';
import withHoverAreas from './with-hover-areas';
Expand Down
23 changes: 7 additions & 16 deletions packages/editor/src/components/block-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
mapValues,
sortBy,
throttle,
last,
} from 'lodash';
import classnames from 'classnames';

Expand All @@ -17,15 +16,13 @@ import classnames from 'classnames';
*/
import { Component } from '@wordpress/element';
import { withSelect, withDispatch } from '@wordpress/data';
import { getDefaultBlockName } from '@wordpress/blocks';
import { compose } from '@wordpress/compose';

/**
* Internal dependencies
*/
import BlockListBlock from './block';
import IgnoreNestedEvents from './ignore-nested-events';
import DefaultBlockAppender from '../default-block-appender';
import BlockListAppender from '../block-list-appender';

class BlockList extends Component {
constructor( props ) {
Expand Down Expand Up @@ -194,7 +191,6 @@ class BlockList extends Component {
layout,
isGroupedByLayout,
rootClientId,
canInsertDefaultBlock,
isDraggable,
} = this.props;

Expand Down Expand Up @@ -224,15 +220,12 @@ class BlockList extends Component {
isDraggable={ isDraggable }
/>
) ) }
{ canInsertDefaultBlock && (
<IgnoreNestedEvents childHandledEvents={ [ 'onFocus', 'onClick', 'onKeyDown' ] }>
<DefaultBlockAppender
rootClientId={ rootClientId }
lastBlockClientId={ last( blockClientIds ) }
layout={ defaultLayout }
/>
</IgnoreNestedEvents>
) }

<BlockListAppender
rootClientId={ rootClientId }
layout={ layout }
isGroupedByLayout={ isGroupedByLayout }
/>
</div>
);
}
Expand All @@ -247,7 +240,6 @@ export default compose( [
getMultiSelectedBlocksStartClientId,
getMultiSelectedBlocksEndClientId,
getBlockSelectionStart,
canInsertBlockType,
} = select( 'core/editor' );
const { rootClientId } = ownProps;

Expand All @@ -258,7 +250,6 @@ export default compose( [
selectionStartClientId: getBlockSelectionStart(),
isSelectionEnabled: isSelectionEnabled(),
isMultiSelecting: isMultiSelecting(),
canInsertDefaultBlock: canInsertBlockType( getDefaultBlockName(), rootClientId ),
};
} ),
withDispatch( ( dispatch ) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { mount } from 'enzyme';
/**
* Internal dependencies
*/
import IgnoreNestedEvents from '../ignore-nested-events';
import IgnoreNestedEvents from '../';

describe( 'IgnoreNestedEvents', () => {
it( 'passes props to its rendered div', () => {
Expand Down
17 changes: 11 additions & 6 deletions packages/editor/src/components/inserter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,26 +88,31 @@ class Inserter extends Component {
}

export default compose( [
withSelect( ( select ) => {
withSelect( ( select, { rootClientId, layout } ) => {
const {
getEditedPostAttribute,
getBlockInsertionPoint,
getSelectedBlock,
getInserterItems,
getBlockOrder,
} = select( 'core/editor' );
const insertionPoint = getBlockInsertionPoint();
const { rootClientId } = insertionPoint;
const parentId = rootClientId || insertionPoint.rootClientId;
return {
title: getEditedPostAttribute( 'title' ),
insertionPoint,
insertionPoint: {
Copy link
Member

@aduth aduth Oct 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should almost never want to create a new object to be returned as a value of mapSelectToProps return value object. It forces a component render on every single change to any registered store.

See: #11028

rootClientId: parentId,
layout: rootClientId ? layout : insertionPoint.layout,
index: rootClientId ? getBlockOrder( rootClientId ).length : insertionPoint.index,
},
selectedBlock: getSelectedBlock(),
items: getInserterItems( rootClientId ),
rootClientId,
items: getInserterItems( parentId ),
rootClientId: parentId,
};
} ),
withDispatch( ( dispatch, ownProps ) => ( {
onInsertBlock: ( item ) => {
const { insertionPoint, selectedBlock } = ownProps;
const { selectedBlock, insertionPoint } = ownProps;
const { index, rootClientId, layout } = insertionPoint;
const { name, initialAttributes } = item;
const insertedBlock = createBlock( name, { ...initialAttributes, layout } );
Expand Down
1 change: 1 addition & 0 deletions packages/editor/src/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
@import "./components/block-icon/style.scss";
@import "./components/block-inspector/style.scss";
@import "./components/block-list/style.scss";
@import "./components/block-list-appender/style.scss";
@import "./components/block-compare/style.scss";
@import "./components/block-mover/style.scss";
@import "./components/block-preview/style.scss";
Expand Down