From 46f88f141e6c0ea342542bf159b289fdb9f9a3f6 Mon Sep 17 00:00:00 2001 From: Sofia Sousa Date: Fri, 16 Nov 2018 15:01:23 +0000 Subject: [PATCH] feat: Add PostPublish override --- .../@wordpress/editor/CHANGELOG.md | 2 + .../components/post-publish-button/index.js | 168 ++++++++++++++++++ 2 files changed, 170 insertions(+) create mode 100644 src/js/gutenberg-overrides/@wordpress/editor/build-module/components/post-publish-button/index.js diff --git a/src/js/gutenberg-overrides/@wordpress/editor/CHANGELOG.md b/src/js/gutenberg-overrides/@wordpress/editor/CHANGELOG.md index 4d69282..c243e89 100644 --- a/src/js/gutenberg-overrides/@wordpress/editor/CHANGELOG.md +++ b/src/js/gutenberg-overrides/@wordpress/editor/CHANGELOG.md @@ -14,6 +14,8 @@ ### Added +- 'post-publish-button' override + - 'post-preview-button' override - `addQueryArgs` to 'Manage All Reusable Blocks' link (Inserter Menu) ([packages/editor/build-module/components/inserter/menu.js](https://github.com/front/gutenberg-js/blob/v2.7.0/src/js/gutenberg-overrides/packages/editor/build-module/components/inserter/menu.js)) diff --git a/src/js/gutenberg-overrides/@wordpress/editor/build-module/components/post-publish-button/index.js b/src/js/gutenberg-overrides/@wordpress/editor/build-module/components/post-publish-button/index.js new file mode 100644 index 0000000..9f5df5e --- /dev/null +++ b/src/js/gutenberg-overrides/@wordpress/editor/build-module/components/post-publish-button/index.js @@ -0,0 +1,168 @@ +/** + * External dependencies + */ +import { noop, get } from 'lodash'; + +/** + * WordPress dependencies + */ +import { Button } from '@wordpress/components'; +import { Component, createRef } from '@wordpress/element'; +import { withSelect, withDispatch } from '@wordpress/data'; +import { compose, ifCondition } from '@wordpress/compose'; +import { __ } from '@wordpress/i18n'; +import { DotTip } from '@wordpress/nux'; + +/** + * Internal dependencies + */ +import PublishButtonLabel from './label'; +export class PostPublishButton extends Component { + constructor (props) { + super(props); + this.buttonNode = createRef(); + } + componentDidMount () { + if (this.props.focusOnMount) { + this.buttonNode.current.focus(); + } + } + + render () { + const { + forceIsDirty, + forceIsSaving, + hasPublishAction, + isBeingScheduled, + isOpen, + isPostSavingLocked, + isPublishable, + isPublished, + isSaveable, + isSaving, + isToggle, + onSave, + onStatusChange, + onSubmit = noop, + onToggle, + visibility, + } = 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 (isBeingScheduled) { + publishStatus = 'future'; + } + else if (visibility === 'private') { + publishStatus = 'private'; + } + else { + publishStatus = 'publish'; + } + + const onClick = () => { + onSubmit(); + onStatusChange(publishStatus); + onSave(); + }; + + const buttonProps = { + 'aria-disabled': isButtonDisabled, + className: 'editor-post-publish-button', + isBusy: isSaving && isPublished, + isLarge: true, + isPrimary: true, + onClick, + }; + + const toggleProps = { + 'aria-disabled': isToggleDisabled, + 'aria-expanded': isOpen, + className: 'editor-post-publish-panel__toggle', + isBusy: isSaving && isPublished, + isPrimary: true, + onClick: onToggle, + }; + + const toggleChildren = isBeingScheduled ? __('Schedule…') : __('Publish…'); + const buttonChildren = ; + + const componentProps = isToggle ? toggleProps : buttonProps; + const componentChildren = isToggle ? toggleChildren : buttonChildren; + return ( + + ); + } +} + +export default compose([ + withSelect(select => { + const { + isSavingPost, + isEditedPostBeingScheduled, + getEditedPostVisibility, + isCurrentPostPublished, + isEditedPostSaveable, + isEditedPostPublishable, + isPostSavingLocked, + getCurrentPost, + getCurrentPostType, + + // GUTENBERG JS + getEditorSettings, + } = select('core/editor'); + + // GUTENBERG JS + const { canPublish } = getEditorSettings(); + + return { + isSaving: isSavingPost(), + isBeingScheduled: isEditedPostBeingScheduled(), + visibility: getEditedPostVisibility(), + isSaveable: isEditedPostSaveable(), + isPostSavingLocked: isPostSavingLocked(), + isPublishable: isEditedPostPublishable(), + isPublished: isCurrentPostPublished(), + hasPublishAction: get(getCurrentPost(), [ '_links', 'wp:action-publish' ], false), + postType: getCurrentPostType(), + + // GUTENBERG + canPublish, + }; + }), + withDispatch(dispatch => { + const { editPost, savePost } = dispatch('core/editor'); + return { + onStatusChange: status => editPost({ status }), + onSave: savePost, + }; + }), + + // GUTENBERG JS + // added the ifCondition to enable/disable + // the publish button according 'canPublish' setting + ifCondition(({ canPublish }) => canPublish), +])(PostPublishButton);