Skip to content
This repository has been archived by the owner on Jul 30, 2024. It is now read-only.

Commit

Permalink
feat: Add PostPublish override
Browse files Browse the repository at this point in the history
  • Loading branch information
SofiaSousa committed Nov 16, 2018
1 parent 9174394 commit 46f88f1
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/js/gutenberg-overrides/@wordpress/editor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
@@ -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 = <PublishButtonLabel forceIsSaving={ forceIsSaving } />;

const componentProps = isToggle ? toggleProps : buttonProps;
const componentChildren = isToggle ? toggleChildren : buttonChildren;
return (
<Button
ref={ this.buttonNode }
{ ...componentProps }
>
{ componentChildren }
<DotTip tipId="core/editor.publish">
{ __('Finished writing? That’s great, let’s get this published right now. Just click “Publish” and you’re good to go.') }
</DotTip>
</Button>
);
}
}

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);

0 comments on commit 46f88f1

Please sign in to comment.