@wordpress/block-library
Version:
Block library for the WordPress editor.
102 lines (92 loc) • 3.34 kB
JavaScript
import { createElement } from "@wordpress/element";
/**
* External dependencies
*/
import { View } from 'react-native';
/**
* WordPress dependencies
*/
import { withSelect } from '@wordpress/data';
import { compose, withPreferredColorScheme, useResizeObserver } from '@wordpress/compose';
import { InnerBlocks, store as blockEditorStore } from '@wordpress/block-editor';
import { useCallback } from '@wordpress/element';
import { alignmentHelpers } from '@wordpress/components';
/**
* Internal dependencies
*/
import styles from './editor.scss';
const {
isFullWidth
} = alignmentHelpers;
function GroupEdit(_ref) {
let {
attributes,
hasInnerBlocks,
isSelected,
isLastInnerBlockSelected,
getStylesFromColorScheme,
style,
blockWidth
} = _ref;
const {
align
} = attributes;
const [resizeObserver, sizes] = useResizeObserver();
const {
width
} = sizes || {
width: 0
};
const renderAppender = useCallback(() => createElement(View, {
style: [!hasInnerBlocks && styles.groupAppender, isFullWidth(align) && !hasInnerBlocks && styles.fullwidthGroupAppender, isFullWidth(align) && hasInnerBlocks && styles.fullwidthHasInnerGroupAppender]
}, createElement(InnerBlocks.ButtonBlockAppender, null)), [align, hasInnerBlocks]);
if (!isSelected && !hasInnerBlocks) {
return createElement(View, {
style: [getStylesFromColorScheme(styles.groupPlaceholder, styles.groupPlaceholderDark), !hasInnerBlocks && { ...styles.marginVerticalDense,
...styles.marginHorizontalNone
}]
});
}
return createElement(View, {
style: [isSelected && hasInnerBlocks && styles.innerBlocks, style, isSelected && hasInnerBlocks && (style === null || style === void 0 ? void 0 : style.backgroundColor) && styles.hasBackgroundAppender, isLastInnerBlockSelected && (style === null || style === void 0 ? void 0 : style.backgroundColor) && styles.isLastInnerBlockSelected]
}, resizeObserver, createElement(InnerBlocks, {
renderAppender: isSelected && renderAppender,
parentWidth: width,
blockWidth: blockWidth
}));
}
export default compose([withSelect((select, _ref2) => {
var _getBlockAttributes;
let {
clientId
} = _ref2;
const {
getBlock,
getBlockIndex,
hasSelectedInnerBlock,
getBlockRootClientId,
getSelectedBlockClientId,
getBlockAttributes
} = select(blockEditorStore);
const block = getBlock(clientId);
const hasInnerBlocks = !!(block && block.innerBlocks.length);
const isInnerBlockSelected = hasInnerBlocks && hasSelectedInnerBlock(clientId, true);
let isLastInnerBlockSelected = false;
if (isInnerBlockSelected) {
const {
innerBlocks
} = block;
const selectedBlockClientId = getSelectedBlockClientId();
const totalInnerBlocks = innerBlocks.length - 1;
const blockIndex = getBlockIndex(selectedBlockClientId);
isLastInnerBlockSelected = totalInnerBlocks === blockIndex;
}
const parentId = getBlockRootClientId(clientId);
const parentBlockAlignment = (_getBlockAttributes = getBlockAttributes(parentId)) === null || _getBlockAttributes === void 0 ? void 0 : _getBlockAttributes.align;
return {
hasInnerBlocks,
isLastInnerBlockSelected,
parentBlockAlignment
};
}), withPreferredColorScheme])(GroupEdit);
//# sourceMappingURL=edit.native.js.map