@wordpress/block-editor
Version:
74 lines (68 loc) • 1.84 kB
JavaScript
import { createElement } from "@wordpress/element";
/**
* External dependencies
*/
import { noop } from 'lodash';
/**
* WordPress dependencies
*/
import { store as blocksStore } from '@wordpress/blocks';
import { Toolbar, ToolbarButton } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { withSelect, withDispatch } from '@wordpress/data';
import { compose } from '@wordpress/compose';
/**
* Internal dependencies
*/
import UngroupIcon from './icon';
import { store as blockEditorStore } from '../../store';
export function UngroupButton({
onConvertFromGroup,
isUngroupable = false
}) {
if (!isUngroupable) {
return null;
}
return createElement(Toolbar, null, createElement(ToolbarButton, {
title: __('Ungroup'),
icon: UngroupIcon,
onClick: onConvertFromGroup
}));
}
export default compose([withSelect(select => {
const {
getSelectedBlockClientId,
getBlock
} = select(blockEditorStore);
const {
getGroupingBlockName
} = select(blocksStore);
const selectedId = getSelectedBlockClientId();
const selectedBlock = getBlock(selectedId);
const groupingBlockName = getGroupingBlockName();
const isUngroupable = selectedBlock && selectedBlock.innerBlocks && !!selectedBlock.innerBlocks.length && selectedBlock.name === groupingBlockName;
const innerBlocks = isUngroupable ? selectedBlock.innerBlocks : [];
return {
isUngroupable,
clientId: selectedId,
innerBlocks
};
}), withDispatch((dispatch, {
clientId,
innerBlocks,
onToggle = noop
}) => {
const {
replaceBlocks
} = dispatch(blockEditorStore);
return {
onConvertFromGroup() {
if (!innerBlocks.length) {
return;
}
replaceBlocks(clientId, innerBlocks);
onToggle();
}
};
})])(UngroupButton);
//# sourceMappingURL=index.native.js.map