@wordpress/block-library
Version:
Block library for the WordPress editor.
84 lines (76 loc) • 3.25 kB
JavaScript
import { createElement, Fragment } from "@wordpress/element";
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { InspectorControls, useBlockProps, useInnerBlocksProps, store as blockEditorStore, Warning } from '@wordpress/block-editor';
import { useSelect } from '@wordpress/data';
import { getBlockSupport } from '@wordpress/blocks';
import { PanelBody } from '@wordpress/components';
/**
* Internal dependencies
*/
import { CommentsPaginationArrowControls } from './comments-pagination-arrow-controls';
const TEMPLATE = [['core/comments-pagination-previous'], ['core/comments-pagination-numbers'], ['core/comments-pagination-next']];
const ALLOWED_BLOCKS = ['core/comments-pagination-previous', 'core/comments-pagination-numbers', 'core/comments-pagination-next'];
const getDefaultBlockLayout = blockTypeOrName => {
const layoutBlockSupportConfig = getBlockSupport(blockTypeOrName, '__experimentalLayout');
return layoutBlockSupportConfig === null || layoutBlockSupportConfig === void 0 ? void 0 : layoutBlockSupportConfig.default;
};
export default function QueryPaginationEdit(_ref) {
let {
attributes: {
paginationArrow,
layout
},
setAttributes,
clientId,
name
} = _ref;
const usedLayout = layout || getDefaultBlockLayout(name);
const hasNextPreviousBlocks = useSelect(select => {
const {
getBlocks
} = select(blockEditorStore);
const innerBlocks = getBlocks(clientId);
/**
* Show the `paginationArrow` control only if a
* Comments Pagination Next or Comments Pagination Previous
* block exists.
*/
return innerBlocks === null || innerBlocks === void 0 ? void 0 : innerBlocks.find(innerBlock => {
return ['core/comments-pagination-previous', 'core/comments-pagination-next'].includes(innerBlock.name);
});
}, []);
const blockProps = useBlockProps();
const innerBlocksProps = useInnerBlocksProps(blockProps, {
template: TEMPLATE,
allowedBlocks: ALLOWED_BLOCKS,
__experimentalLayout: usedLayout
}); // Get the Discussion settings
const pageComments = useSelect(select => {
const {
getSettings
} = select(blockEditorStore);
const {
__experimentalDiscussionSettings
} = getSettings();
return __experimentalDiscussionSettings === null || __experimentalDiscussionSettings === void 0 ? void 0 : __experimentalDiscussionSettings.pageComments;
}, []); // If paging comments is not enabled in the Discussion Settings then hide the pagination
// controls. We don't want to remove them from the template so that when the user enables
// paging comments, the controls will be visible.
if (!pageComments) {
return createElement(Warning, null, __('Comments Pagination block: paging comments is disabled in the Discussion Settings'));
}
return createElement(Fragment, null, hasNextPreviousBlocks && createElement(InspectorControls, null, createElement(PanelBody, {
title: __('Settings')
}, createElement(CommentsPaginationArrowControls, {
value: paginationArrow,
onChange: value => {
setAttributes({
paginationArrow: value
});
}
}))), createElement("div", innerBlocksProps));
}
//# sourceMappingURL=edit.js.map