| 1 |
/** |
| 2 |
* External dependencies |
| 3 |
*/ |
| 4 |
import classnames from 'classnames'; |
| 5 |
|
| 6 |
/** |
| 7 |
* WordPress dependencies |
| 8 |
*/ |
| 9 |
const { |
| 10 |
__, |
| 11 |
} = wp.i18n; |
| 12 |
|
| 13 |
const { |
| 14 |
Component, |
| 15 |
Fragment, |
| 16 |
} = wp.element; |
| 17 |
|
| 18 |
const { |
| 19 |
InnerBlocks, |
| 20 |
} = wp.blockEditor; |
| 21 |
|
| 22 |
const { |
| 23 |
withSelect, |
| 24 |
} = wp.data; |
| 25 |
|
| 26 |
/** |
| 27 |
* Component |
| 28 |
*/ |
| 29 |
class AlertBlockEdit extends Component { |
| 30 |
constructor() { |
| 31 |
super(...arguments); |
| 32 |
|
| 33 |
this.state = { |
| 34 |
// fix for WP 5.2 |
| 35 |
// styles control generates error |
| 36 |
showInnerBlocks: !!this.props.clientId, |
| 37 |
}; |
| 38 |
} |
| 39 |
|
| 40 |
render() { |
| 41 |
const { |
| 42 |
setAttributes, |
| 43 |
hasChildBlocks, |
| 44 |
} = this.props; |
| 45 |
|
| 46 |
let { |
| 47 |
className, |
| 48 |
attributes, |
| 49 |
} = this.props; |
| 50 |
|
| 51 |
const { |
| 52 |
dismissible, |
| 53 |
canvasClassName, |
| 54 |
} = attributes; |
| 55 |
|
| 56 |
className = classnames( |
| 57 |
'cnvs-block-alert', |
| 58 |
{ |
| 59 |
'cnvs-block-alert-dismissible': dismissible, |
| 60 |
}, |
| 61 |
canvasClassName, |
| 62 |
className |
| 63 |
); |
| 64 |
|
| 65 |
return ( |
| 66 |
<Fragment> |
| 67 |
<div className={className}> |
| 68 |
<div className="cnvs-block-alert-inner"> |
| 69 |
{this.state.showInnerBlocks ? ( |
| 70 |
<InnerBlocks |
| 71 |
templateLock={false} |
| 72 |
renderAppender={( |
| 73 |
hasChildBlocks ? |
| 74 |
undefined : |
| 75 |
() => <InnerBlocks.ButtonBlockAppender /> |
| 76 |
)} |
| 77 |
/> |
| 78 |
) : __('Alert content')} |
| 79 |
</div> |
| 80 |
{dismissible ? ( |
| 81 |
<button className="cnvs-close" type="button" data-dismiss="alert" aria-label={__('Close')}> |
| 82 |
<i className="cnvs-icon-x" /> |
| 83 |
</button> |
| 84 |
) : ''} |
| 85 |
</div> |
| 86 |
</Fragment> |
| 87 |
); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
const AlertBlockEditWithSelect = withSelect((select, ownProps) => { |
| 92 |
const { clientId } = ownProps; |
| 93 |
const blockEditor = select('core/block-editor'); |
| 94 |
|
| 95 |
return { |
| 96 |
hasChildBlocks: blockEditor ? blockEditor.getBlockOrder(clientId).length > 0 : false, |
| 97 |
}; |
| 98 |
})(AlertBlockEdit); |
| 99 |
|
| 100 |
export default AlertBlockEditWithSelect; |
| 101 |
|