UNPKG

@wordpress/edit-post

Version:
136 lines (122 loc) 3.63 kB
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck"; import _createClass from "@babel/runtime/helpers/esm/createClass"; import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf"; import _inherits from "@babel/runtime/helpers/esm/inherits"; /** * WordPress dependencies */ import { Component } from '@wordpress/element'; import { withSelect } from '@wordpress/data'; import { addQueryArgs } from '@wordpress/url'; /** * Returns the Post's Edit URL. * * @param {number} postId Post ID. * * @return {string} Post edit URL. */ export function getPostEditURL(postId) { return addQueryArgs('post.php', { post: postId, action: 'edit' }); } /** * Returns the Post's Trashed URL. * * @param {number} postId Post ID. * @param {string} postType Post Type. * * @return {string} Post trashed URL. */ export function getPostTrashedURL(postId, postType) { return addQueryArgs('edit.php', { trashed: 1, post_type: postType, ids: postId }); } export var BrowserURL = /*#__PURE__*/ function (_Component) { _inherits(BrowserURL, _Component); function BrowserURL() { var _this; _classCallCheck(this, BrowserURL); _this = _possibleConstructorReturn(this, _getPrototypeOf(BrowserURL).apply(this, arguments)); _this.state = { historyId: null }; return _this; } _createClass(BrowserURL, [{ key: "componentDidUpdate", value: function componentDidUpdate(prevProps) { var _this$props = this.props, postId = _this$props.postId, postStatus = _this$props.postStatus, postType = _this$props.postType; var historyId = this.state.historyId; if (postStatus === 'trash') { this.setTrashURL(postId, postType); return; } if ((postId !== prevProps.postId || postId !== historyId) && postStatus !== 'auto-draft') { this.setBrowserURL(postId); } } /** * Navigates the browser to the post trashed URL to show a notice about the trashed post. * * @param {number} postId Post ID. * @param {string} postType Post Type. */ }, { key: "setTrashURL", value: function setTrashURL(postId, postType) { window.location.href = getPostTrashedURL(postId, postType); } /** * Replaces the browser URL with a post editor link for the given post ID. * * Note it is important that, since this function may be called when the * editor first loads, the result generated `getPostEditURL` matches that * produced by the server. Otherwise, the URL will change unexpectedly. * * @param {number} postId Post ID for which to generate post editor URL. */ }, { key: "setBrowserURL", value: function setBrowserURL(postId) { window.history.replaceState({ id: postId }, 'Post ' + postId, getPostEditURL(postId)); this.setState(function () { return { historyId: postId }; }); } }, { key: "render", value: function render() { return null; } }]); return BrowserURL; }(Component); export default withSelect(function (select) { var _select = select('core/editor'), getCurrentPost = _select.getCurrentPost; var _getCurrentPost = getCurrentPost(), id = _getCurrentPost.id, status = _getCurrentPost.status, type = _getCurrentPost.type; return { postId: id, postStatus: status, postType: type }; })(BrowserURL); //# sourceMappingURL=index.js.map