UNPKG

@wordpress/editor

Version:
235 lines (202 loc) 6.68 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = exports.PostPublishButton = void 0; var _element = require("@wordpress/element"); var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _lodash = require("lodash"); var _classnames = _interopRequireDefault(require("classnames")); var _components = require("@wordpress/components"); var _data = require("@wordpress/data"); var _compose = require("@wordpress/compose"); var _i18n = require("@wordpress/i18n"); var _label = _interopRequireDefault(require("./label")); /** * External dependencies */ /** * WordPress dependencies */ /** * Internal dependencies */ class PostPublishButton extends _element.Component { constructor(props) { super(props); this.buttonNode = (0, _element.createRef)(); this.createOnClick = this.createOnClick.bind(this); this.closeEntitiesSavedStates = this.closeEntitiesSavedStates.bind(this); this.state = { entitiesSavedStatesCallback: false }; } componentDidMount() { if (this.props.focusOnMount) { this.buttonNode.current.focus(); } } createOnClick(callback) { return (...args) => { const { hasNonPostEntityChanges } = this.props; if (hasNonPostEntityChanges) { // The modal for multiple entity saving will open, // hold the callback for saving/publishing the post // so that we can call it if the post entity is checked. this.setState({ entitiesSavedStatesCallback: () => callback(...args) }); // Open the save panel by setting its callback. // To set a function on the useState hook, we must set it // with another function (() => myFunction). Passing the // function on its own will cause an error when called. this.props.setEntitiesSavedStatesCallback(() => this.closeEntitiesSavedStates); return _lodash.noop; } return callback(...args); }; } closeEntitiesSavedStates(savedEntities) { const { postType, postId } = this.props; const { entitiesSavedStatesCallback } = this.state; this.setState({ entitiesSavedStatesCallback: false }, () => { if (savedEntities && (0, _lodash.some)(savedEntities, elt => elt.kind === 'postType' && elt.name === postType && elt.key === postId)) { // The post entity was checked, call the held callback from `createOnClick`. entitiesSavedStatesCallback(); } }); } render() { const { forceIsDirty, forceIsSaving, hasPublishAction, isBeingScheduled, isOpen, isPostSavingLocked, isPublishable, isPublished, isSaveable, isSaving, isAutoSaving, isToggle, onSave, onStatusChange, onSubmit = _lodash.noop, onToggle, visibility, hasNonPostEntityChanges } = this.props; const isButtonDisabled = isSaving || forceIsSaving || !isSaveable || isPostSavingLocked || !isPublishable && !forceIsDirty; const isToggleDisabled = isPublished || isSaving || forceIsSaving || !isSaveable || !isPublishable && !forceIsDirty; let publishStatus; if (!hasPublishAction) { publishStatus = 'pending'; } else if (visibility === 'private') { publishStatus = 'private'; } else if (isBeingScheduled) { publishStatus = 'future'; } else { publishStatus = 'publish'; } const onClickButton = () => { if (isButtonDisabled) { return; } onSubmit(); onStatusChange(publishStatus); onSave(); }; const onClickToggle = () => { if (isToggleDisabled) { return; } onToggle(); }; const buttonProps = { 'aria-disabled': isButtonDisabled && !hasNonPostEntityChanges, className: 'editor-post-publish-button', isBusy: !isAutoSaving && isSaving && isPublished, isPrimary: true, onClick: this.createOnClick(onClickButton) }; const toggleProps = { 'aria-disabled': isToggleDisabled && !hasNonPostEntityChanges, 'aria-expanded': isOpen, className: 'editor-post-publish-panel__toggle', isBusy: isSaving && isPublished, isPrimary: true, onClick: this.createOnClick(onClickToggle) }; const toggleChildren = isBeingScheduled ? (0, _i18n.__)('Schedule…') : (0, _i18n.__)('Publish'); const buttonChildren = (0, _element.createElement)(_label.default, { forceIsSaving: forceIsSaving, hasNonPostEntityChanges: hasNonPostEntityChanges }); const componentProps = isToggle ? toggleProps : buttonProps; const componentChildren = isToggle ? toggleChildren : buttonChildren; return (0, _element.createElement)(_element.Fragment, null, (0, _element.createElement)(_components.Button, (0, _extends2.default)({ ref: this.buttonNode }, componentProps, { className: (0, _classnames.default)(componentProps.className, 'editor-post-publish-button__button', { 'has-changes-dot': hasNonPostEntityChanges }) }), componentChildren)); } } exports.PostPublishButton = PostPublishButton; var _default = (0, _compose.compose)([(0, _data.withSelect)(select => { const { isSavingPost, isAutosavingPost, isEditedPostBeingScheduled, getEditedPostVisibility, isCurrentPostPublished, isEditedPostSaveable, isEditedPostPublishable, isPostSavingLocked, getCurrentPost, getCurrentPostType, getCurrentPostId, hasNonPostEntityChanges } = select('core/editor'); const _isAutoSaving = isAutosavingPost(); return { isSaving: isSavingPost() || _isAutoSaving, isAutoSaving: _isAutoSaving, isBeingScheduled: isEditedPostBeingScheduled(), visibility: getEditedPostVisibility(), isSaveable: isEditedPostSaveable(), isPostSavingLocked: isPostSavingLocked(), isPublishable: isEditedPostPublishable(), isPublished: isCurrentPostPublished(), hasPublishAction: (0, _lodash.get)(getCurrentPost(), ['_links', 'wp:action-publish'], false), postType: getCurrentPostType(), postId: getCurrentPostId(), hasNonPostEntityChanges: hasNonPostEntityChanges() }; }), (0, _data.withDispatch)(dispatch => { const { editPost, savePost } = dispatch('core/editor'); return { onStatusChange: status => editPost({ status }, { undoIgnore: true }), onSave: savePost }; })])(PostPublishButton); exports.default = _default; //# sourceMappingURL=index.js.map