@wordpress/editor
Version:
Enhanced block editor for WordPress posts.
167 lines (140 loc) • 4.69 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _element = require("@wordpress/element");
var _lodash = require("lodash");
var _components = require("@wordpress/components");
var _i18n = require("@wordpress/i18n");
var _data = require("@wordpress/data");
var _url = require("@wordpress/url");
var _htmlEntities = require("@wordpress/html-entities");
var _compose = require("@wordpress/compose");
var _label = _interopRequireDefault(require("../post-schedule/label"));
/**
* External dependencies
*/
/**
* WordPress dependencies
*/
/**
* Internal dependencies
*/
const POSTNAME = '%postname%';
/**
* Returns URL for a future post.
*
* @param {Object} post Post object.
*
* @return {string} PostPublish URL.
*/
const getFuturePostUrl = post => {
const {
slug
} = post;
if (post.permalink_template.includes(POSTNAME)) {
return post.permalink_template.replace(POSTNAME, slug);
}
return post.permalink_template;
};
function CopyButton({
text,
onCopy,
children
}) {
const ref = (0, _compose.useCopyToClipboard)(text, onCopy);
return (0, _element.createElement)(_components.Button, {
isSecondary: true,
ref: ref
}, children);
}
class PostPublishPanelPostpublish extends _element.Component {
constructor() {
super(...arguments);
this.state = {
showCopyConfirmation: false
};
this.onCopy = this.onCopy.bind(this);
this.onSelectInput = this.onSelectInput.bind(this);
this.postLink = (0, _element.createRef)();
}
componentDidMount() {
if (this.props.focusOnMount) {
this.postLink.current.focus();
}
}
componentWillUnmount() {
clearTimeout(this.dismissCopyConfirmation);
}
onCopy() {
this.setState({
showCopyConfirmation: true
});
clearTimeout(this.dismissCopyConfirmation);
this.dismissCopyConfirmation = setTimeout(() => {
this.setState({
showCopyConfirmation: false
});
}, 4000);
}
onSelectInput(event) {
event.target.select();
}
render() {
const {
children,
isScheduled,
post,
postType
} = this.props;
const postLabel = (0, _lodash.get)(postType, ['labels', 'singular_name']);
const viewPostLabel = (0, _lodash.get)(postType, ['labels', 'view_item']);
const link = post.status === 'future' ? getFuturePostUrl(post) : post.link;
const postPublishNonLinkHeader = isScheduled ? (0, _element.createElement)(_element.Fragment, null, (0, _i18n.__)('is now scheduled. It will go live on'), ' ', (0, _element.createElement)(_label.default, null), ".") : (0, _i18n.__)('is now live.');
return (0, _element.createElement)("div", {
className: "post-publish-panel__postpublish"
}, (0, _element.createElement)(_components.PanelBody, {
className: "post-publish-panel__postpublish-header"
}, (0, _element.createElement)("a", {
ref: this.postLink,
href: link
}, (0, _htmlEntities.decodeEntities)(post.title) || (0, _i18n.__)('(no title)')), ' ', postPublishNonLinkHeader), (0, _element.createElement)(_components.PanelBody, null, (0, _element.createElement)("p", {
className: "post-publish-panel__postpublish-subheader"
}, (0, _element.createElement)("strong", null, (0, _i18n.__)('What’s next?'))), (0, _element.createElement)(_components.TextControl, {
className: "post-publish-panel__postpublish-post-address",
readOnly: true,
label: (0, _i18n.sprintf)(
/* translators: %s: post type singular name */
(0, _i18n.__)('%s address'), postLabel),
value: (0, _url.safeDecodeURIComponent)(link),
onFocus: this.onSelectInput
}), (0, _element.createElement)("div", {
className: "post-publish-panel__postpublish-buttons"
}, !isScheduled && (0, _element.createElement)(_components.Button, {
isSecondary: true,
href: link
}, viewPostLabel), (0, _element.createElement)(CopyButton, {
text: link,
onCopy: this.onCopy
}, this.state.showCopyConfirmation ? (0, _i18n.__)('Copied!') : (0, _i18n.__)('Copy Link')))), children);
}
}
var _default = (0, _data.withSelect)(select => {
const {
getEditedPostAttribute,
getCurrentPost,
isCurrentPostScheduled
} = select('core/editor');
const {
getPostType
} = select('core');
return {
post: getCurrentPost(),
postType: getPostType(getEditedPostAttribute('type')),
isScheduled: isCurrentPostScheduled()
};
})(PostPublishPanelPostpublish);
exports.default = _default;
//# sourceMappingURL=postpublish.js.map