UNPKG

@wordpress/edit-post

Version:
159 lines (132 loc) 4.2 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.getPostEditURL = getPostEditURL; exports.getPostTrashedURL = getPostTrashedURL; exports.default = exports.BrowserURL = void 0; var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn")); var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")); var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")); var _element = require("@wordpress/element"); var _data = require("@wordpress/data"); var _url = require("@wordpress/url"); /** * WordPress dependencies */ /** * Returns the Post's Edit URL. * * @param {number} postId Post ID. * * @return {string} Post edit URL. */ function getPostEditURL(postId) { return (0, _url.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. */ function getPostTrashedURL(postId, postType) { return (0, _url.addQueryArgs)('edit.php', { trashed: 1, post_type: postType, ids: postId }); } var BrowserURL = /*#__PURE__*/ function (_Component) { (0, _inherits2.default)(BrowserURL, _Component); function BrowserURL() { var _this; (0, _classCallCheck2.default)(this, BrowserURL); _this = (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(BrowserURL).apply(this, arguments)); _this.state = { historyId: null }; return _this; } (0, _createClass2.default)(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; }(_element.Component); exports.BrowserURL = BrowserURL; var _default = (0, _data.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); exports.default = _default; //# sourceMappingURL=index.js.map