@wordpress/editor
Version:
Enhanced block editor for WordPress posts.
123 lines (120 loc) • 4.53 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = SiteDiscussion;
var _i18n = require("@wordpress/i18n");
var _data = require("@wordpress/data");
var _coreData = require("@wordpress/core-data");
var _components = require("@wordpress/components");
var _element = require("@wordpress/element");
var _blockEditor = require("@wordpress/block-editor");
var _constants = require("../../store/constants");
var _postPanelRow = _interopRequireDefault(require("../post-panel-row"));
var _store = require("../../store");
var _jsxRuntime = require("react/jsx-runtime");
/**
* WordPress dependencies
*/
/**
* Internal dependencies
*/
const COMMENT_OPTIONS = [{
label: (0, _i18n._x)('Open', 'Adjective: e.g. "Comments are open"'),
value: 'open',
description: (0, _i18n.__)('Visitors can add new comments and replies.')
}, {
label: (0, _i18n.__)('Closed'),
value: '',
description: [(0, _i18n.__)('Visitors cannot add new comments or replies.'), (0, _i18n.__)('Existing comments remain visible.')].join(' ')
}];
function SiteDiscussion() {
const {
editEntityRecord
} = (0, _data.useDispatch)(_coreData.store);
const {
allowCommentsOnNewPosts,
isTemplate,
postSlug
} = (0, _data.useSelect)(select => {
const {
getEditedPostAttribute,
getCurrentPostType
} = select(_store.store);
const {
getEditedEntityRecord,
canUser
} = select(_coreData.store);
const siteSettings = canUser('read', {
kind: 'root',
name: 'site'
}) ? getEditedEntityRecord('root', 'site') : undefined;
return {
isTemplate: getCurrentPostType() === _constants.TEMPLATE_POST_TYPE,
postSlug: getEditedPostAttribute('slug'),
allowCommentsOnNewPosts: siteSettings?.default_comment_status || ''
};
}, []);
// Use internal state instead of a ref to make sure that the component
// re-renders when the popover's anchor updates.
const [popoverAnchor, setPopoverAnchor] = (0, _element.useState)(null);
// Memoize popoverProps to avoid returning a new object every time.
const popoverProps = (0, _element.useMemo)(() => ({
// Anchor the popover to the middle of the entire row so that it doesn't
// move around when the label changes.
anchor: popoverAnchor,
placement: 'left-start',
offset: 36,
shift: true
}), [popoverAnchor]);
if (!isTemplate || !['home', 'index'].includes(postSlug)) {
return null;
}
const setAllowCommentsOnNewPosts = newValue => {
editEntityRecord('root', 'site', undefined, {
default_comment_status: newValue ? 'open' : null
});
};
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_postPanelRow.default, {
label: (0, _i18n.__)('Discussion'),
ref: setPopoverAnchor,
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Dropdown, {
popoverProps: popoverProps,
contentClassName: "editor-site-discussion-dropdown__content",
focusOnMount: true,
renderToggle: ({
isOpen,
onToggle
}) => /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Button, {
size: "compact",
variant: "tertiary",
"aria-expanded": isOpen,
"aria-label": (0, _i18n.__)('Change discussion settings'),
onClick: onToggle,
children: allowCommentsOnNewPosts ? (0, _i18n.__)('Comments open') : (0, _i18n.__)('Comments closed')
}),
renderContent: ({
onClose
}) => /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_blockEditor.__experimentalInspectorPopoverHeader, {
title: (0, _i18n.__)('Discussion'),
onClose: onClose
}), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.__experimentalVStack, {
spacing: 3,
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_components.__experimentalText, {
children: (0, _i18n.__)('Changes will apply to new posts only. Individual posts may override these settings.')
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.RadioControl, {
className: "editor-site-discussion__options",
hideLabelFromVision: true,
label: (0, _i18n.__)('Comment status'),
options: COMMENT_OPTIONS,
onChange: setAllowCommentsOnNewPosts,
selected: allowCommentsOnNewPosts
})]
})]
})
})
});
}
//# sourceMappingURL=index.js.map