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

Avoid getBlock in block-list/block #11899

Merged
merged 10 commits into from
Nov 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 14 additions & 0 deletions docs/designers-developers/developers/data/data-core-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,20 @@ Returns whether a block is valid or not.

Is Valid.

### getBlockAttributes

Returns a block's attributes given its client ID, or null if no block exists with
the client ID.

*Parameters*

* state: Editor state.
* clientId: Block client ID.

*Returns*

Block attributes.

### getBlock

Returns a block given its client ID. This is a parsed copy of the block,
Expand Down
31 changes: 8 additions & 23 deletions docs/designers-developers/developers/filters/block-filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,6 @@ wp.hooks.addFilter(
);
```

#### `blocks.isUnmodifiedDefaultBlock.attributes`

Used internally by the default block (paragraph) to exclude the attributes from the check if the block was modified.

#### `blocks.switchToBlockType.transformedBlock`

Used to filters an individual transform result from block transformation. All of the original blocks are passed, since transformations are many-to-many, not one-to-one.
Expand Down Expand Up @@ -198,19 +194,13 @@ _Example:_
```js
var el = wp.element.createElement;

var withDataAlign = wp.compose.createHigherOrderComponent( function( BlockListBlock ) {
var withClientIdClassName = wp.compose.createHigherOrderComponent( function( BlockListBlock ) {
return function( props ) {
var newProps = lodash.assign(
{},
props,
{
wrapperProps: lodash.assign(
{},
props.wrapperProps,
{
'data-align': props.block.attributes.align
}
)
classsName: "block-" + props.clientId,
}
);

Expand All @@ -219,27 +209,22 @@ var withDataAlign = wp.compose.createHigherOrderComponent( function( BlockListBl
newProps
);
};
}, 'withAlign' );
}, 'withClientIdClassName' );

wp.hooks.addFilter( 'editor.BlockListBlock', 'my-plugin/with-data-align', withDataAlign );
wp.hooks.addFilter( 'editor.BlockListBlock', 'my-plugin/with-client-id-class-name', withClientIdClassName );

```
{% ESNext %}
```js
const { createHigherOrderComponent } = wp.compose;

const withDataAlign = createHigherOrderComponent( ( BlockListBlock ) => {
const withClientIdClassName = createHigherOrderComponent( ( BlockListBlock ) => {
return ( props ) => {
const { align } = props.block.attributes;

let wrapperProps = props.wrapperProps;
wrapperProps = { ...wrapperProps, 'data-align': align };

return <BlockListBlock { ...props } wrapperProps={ wrapperProps } />;
return <BlockListBlock { ...props } className={ "block-" + props.clientId } />;
};
}, 'withDataAlign' );
}, 'withClientIdClassName' );

wp.hooks.addFilter( 'editor.BlockListBlock', 'my-plugin/with-data-align', withDataAlign );
wp.hooks.addFilter( 'editor.BlockListBlock', 'my-plugin/with-client-id-class-name', withClientIdClassName );
```

{% end %}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 4 additions & 9 deletions packages/blocks/src/api/utils.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/**
* External dependencies
*/
import { every, has, keys, isEqual, isFunction, isString } from 'lodash';
import { every, has, isFunction, isString } from 'lodash';
import { default as tinycolor, mostReadable } from 'tinycolor2';

/**
* WordPress dependencies
*/
import { applyFilters } from '@wordpress/hooks';
import { Component, isValidElement } from '@wordpress/element';

/**
Expand Down Expand Up @@ -40,14 +39,10 @@ export function isUnmodifiedDefaultBlock( block ) {
}

const newDefaultBlock = createBlock( defaultBlockName );
const blockType = getBlockType( defaultBlockName );

const attributeKeys = applyFilters( 'blocks.isUnmodifiedDefaultBlock.attributes', [
...keys( newDefaultBlock.attributes ),
...keys( block.attributes ),
] );

return every( attributeKeys, ( key ) =>
isEqual( newDefaultBlock.attributes[ key ], block.attributes[ key ] )
return every( blockType.attributes, ( value, key ) =>
newDefaultBlock.attributes[ key ] === block.attributes[ key ]
);
}

Expand Down
36 changes: 21 additions & 15 deletions packages/editor/src/components/block-list/block-invalid-warning.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
createBlock,
rawHandler,
} from '@wordpress/blocks';
import { withDispatch } from '@wordpress/data';
import { compose } from '@wordpress/compose';
import { withDispatch, withSelect } from '@wordpress/data';

/**
* Internal dependencies
Expand Down Expand Up @@ -96,18 +97,23 @@ const blockToBlocks = ( block ) => rawHandler( {
HTML: block.originalContent,
} );

export default withDispatch( ( dispatch, { block } ) => {
const { replaceBlock } = dispatch( 'core/editor' );
export default compose( [
withSelect( ( select, { clientId } ) => ( {
block: select( 'core/editor' ).getBlock( clientId ),
Copy link
Member

Choose a reason for hiding this comment

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

This looks like a good candidate for optimization when #11851 lands.

Edit: I guess it probably won't have much an impact in common usage, if the block warning is not expected to be frequently shown. I don't see any downside to standardizing on a convention of using the registry argument if the selected prop is only used in the callback of a withDispatch function though.

} ) ),
withDispatch( ( dispatch, { block } ) => {
const { replaceBlock } = dispatch( 'core/editor' );

return {
convertToClassic() {
replaceBlock( block.clientId, blockToClassic( block ) );
},
convertToHTML() {
replaceBlock( block.clientId, blockToHTML( block ) );
},
convertToBlocks() {
replaceBlock( block.clientId, blockToBlocks( block ) );
},
};
} )( BlockInvalidWarning );
return {
convertToClassic() {
replaceBlock( block.clientId, blockToClassic( block ) );
},
convertToHTML() {
replaceBlock( block.clientId, blockToHTML( block ) );
},
convertToBlocks() {
replaceBlock( block.clientId, blockToBlocks( block ) );
},
};
} ),
] )( BlockInvalidWarning );
Loading