-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Blocks: Move logic for generating class name to BlockEdit component #4009
Changes from 4 commits
f718d8e
1342736
6e6356a
f2bcebb
3650811
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ | |
*/ | ||
import { pickBy, noop } from 'lodash'; | ||
import { connect } from 'react-redux'; | ||
import classnames from 'classnames'; | ||
|
||
/** | ||
* WordPress dependencies | ||
|
@@ -15,7 +14,8 @@ import { __ } from '@wordpress/i18n'; | |
/** | ||
* Internal dependencies | ||
*/ | ||
import { getBlockType, registerBlockType, hasBlockSupport, getBlockDefaultClassname } from '../../api'; | ||
import BlockEdit from '../../block-edit'; | ||
import { registerBlockType } from '../../api'; | ||
import ReusableBlockEditPanel from './edit-panel'; | ||
|
||
class ReusableBlockEdit extends Component { | ||
|
@@ -86,23 +86,16 @@ class ReusableBlockEdit extends Component { | |
} | ||
|
||
const reusableBlockAttributes = { ...reusableBlock.attributes, ...attributes }; | ||
const blockType = getBlockType( reusableBlock.type ); | ||
const BlockEdit = blockType.edit || blockType.save; | ||
|
||
// Generate a class name for the block's editable form | ||
const generatedClassName = hasBlockSupport( blockType, 'className', true ) ? | ||
getBlockDefaultClassname( reusableBlock.type ) : | ||
null; | ||
const className = classnames( generatedClassName, reusableBlockAttributes.className ); | ||
|
||
return [ | ||
// We fake the block being read-only by wrapping it with an element that has pointer-events: none | ||
<div key="edit" style={ { pointerEvents: isEditing ? 'auto' : 'none' } }> | ||
<BlockEdit | ||
{ ...this.props } | ||
name={ reusableBlock.type } | ||
focus={ isEditing ? focus : null } | ||
attributes={ reusableBlockAttributes } | ||
setAttributes={ isEditing ? this.setAttributes : noop } | ||
className={ className } | ||
/> | ||
</div>, | ||
focus && ( | ||
|
@@ -167,6 +160,10 @@ registerBlockType( 'core/block', { | |
}, | ||
}, | ||
|
||
supports: { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let implementing block decide whether the custom class name is supported. |
||
customClassName: false, | ||
}, | ||
|
||
edit: ConnectedReusableBlockEdit, | ||
save: () => null, | ||
} ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,14 +8,13 @@ import { get, partial, reduce, size } from 'lodash'; | |
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Component, compose, createElement } from '@wordpress/element'; | ||
import { Component, compose } from '@wordpress/element'; | ||
import { keycodes } from '@wordpress/utils'; | ||
import { | ||
getBlockType, | ||
BlockEdit, | ||
getBlockDefaultClassname, | ||
createBlock, | ||
hasBlockSupport, | ||
getBlockType, | ||
getSaveElement, | ||
isReusableBlock, | ||
} from '@wordpress/blocks'; | ||
import { withFilters, withContext } from '@wordpress/components'; | ||
|
@@ -377,12 +376,6 @@ export class BlockListBlock extends Component { | |
wrapperProps = blockType.getEditWrapperProps( block.attributes ); | ||
} | ||
|
||
// Generate a class name for the block's editable form | ||
const generatedClassName = hasBlockSupport( blockType, 'className', true ) ? | ||
getBlockDefaultClassname( block.name ) : | ||
null; | ||
const className = classnames( generatedClassName, block.attributes.className ); | ||
|
||
// Disable reason: Each block can be selected by clicking on it | ||
/* eslint-disable jsx-a11y/no-static-element-interactions, jsx-a11y/onclick-has-role, jsx-a11y/click-events-have-key-events */ | ||
return ( | ||
|
@@ -424,7 +417,6 @@ export class BlockListBlock extends Component { | |
onReplace={ isLocked ? undefined : onReplace } | ||
setFocus={ partial( onFocus, block.uid ) } | ||
mergeBlocks={ isLocked ? undefined : this.mergeBlocks } | ||
className={ className } | ||
id={ block.uid } | ||
isSelectionEnabled={ this.props.isSelectionEnabled } | ||
toggleSelection={ this.props.toggleSelection } | ||
|
@@ -434,11 +426,7 @@ export class BlockListBlock extends Component { | |
<BlockHtml uid={ block.uid } /> | ||
) } | ||
{ ! isValid && [ | ||
createElement( blockType.save, { | ||
key: 'invalid-preview', | ||
attributes: block.attributes, | ||
className, | ||
} ), | ||
getSaveElement( blockType, block.attributes ), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Won't we have a React warning here about the missing key? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed with 3650811. |
||
<InvalidBlockWarning | ||
key="invalid-warning" | ||
block={ block } | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Is this exposed? How do we use this component when moved to the
editor
module :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it is exposed: https://github.com/WordPress/gutenberg/blob/master/blocks/index.js#L21.