UNPKG

3.63 kBJavaScriptView Raw
1import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
2import _createClass from "@babel/runtime/helpers/esm/createClass";
3import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
4import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
5import _inherits from "@babel/runtime/helpers/esm/inherits";
6
7/**
8 * WordPress dependencies
9 */
10import { Component } from '@wordpress/element';
11import { withSelect } from '@wordpress/data';
12import { addQueryArgs } from '@wordpress/url';
13/**
14 * Returns the Post's Edit URL.
15 *
16 * @param {number} postId Post ID.
17 *
18 * @return {string} Post edit URL.
19 */
20
21export function getPostEditURL(postId) {
22 return addQueryArgs('post.php', {
23 post: postId,
24 action: 'edit'
25 });
26}
27/**
28 * Returns the Post's Trashed URL.
29 *
30 * @param {number} postId Post ID.
31 * @param {string} postType Post Type.
32 *
33 * @return {string} Post trashed URL.
34 */
35
36export function getPostTrashedURL(postId, postType) {
37 return addQueryArgs('edit.php', {
38 trashed: 1,
39 post_type: postType,
40 ids: postId
41 });
42}
43export var BrowserURL =
44/*#__PURE__*/
45function (_Component) {
46 _inherits(BrowserURL, _Component);
47
48 function BrowserURL() {
49 var _this;
50
51 _classCallCheck(this, BrowserURL);
52
53 _this = _possibleConstructorReturn(this, _getPrototypeOf(BrowserURL).apply(this, arguments));
54 _this.state = {
55 historyId: null
56 };
57 return _this;
58 }
59
60 _createClass(BrowserURL, [{
61 key: "componentDidUpdate",
62 value: function componentDidUpdate(prevProps) {
63 var _this$props = this.props,
64 postId = _this$props.postId,
65 postStatus = _this$props.postStatus,
66 postType = _this$props.postType;
67 var historyId = this.state.historyId;
68
69 if (postStatus === 'trash') {
70 this.setTrashURL(postId, postType);
71 return;
72 }
73
74 if ((postId !== prevProps.postId || postId !== historyId) && postStatus !== 'auto-draft') {
75 this.setBrowserURL(postId);
76 }
77 }
78 /**
79 * Navigates the browser to the post trashed URL to show a notice about the trashed post.
80 *
81 * @param {number} postId Post ID.
82 * @param {string} postType Post Type.
83 */
84
85 }, {
86 key: "setTrashURL",
87 value: function setTrashURL(postId, postType) {
88 window.location.href = getPostTrashedURL(postId, postType);
89 }
90 /**
91 * Replaces the browser URL with a post editor link for the given post ID.
92 *
93 * Note it is important that, since this function may be called when the
94 * editor first loads, the result generated `getPostEditURL` matches that
95 * produced by the server. Otherwise, the URL will change unexpectedly.
96 *
97 * @param {number} postId Post ID for which to generate post editor URL.
98 */
99
100 }, {
101 key: "setBrowserURL",
102 value: function setBrowserURL(postId) {
103 window.history.replaceState({
104 id: postId
105 }, 'Post ' + postId, getPostEditURL(postId));
106 this.setState(function () {
107 return {
108 historyId: postId
109 };
110 });
111 }
112 }, {
113 key: "render",
114 value: function render() {
115 return null;
116 }
117 }]);
118
119 return BrowserURL;
120}(Component);
121export default withSelect(function (select) {
122 var _select = select('core/editor'),
123 getCurrentPost = _select.getCurrentPost;
124
125 var _getCurrentPost = getCurrentPost(),
126 id = _getCurrentPost.id,
127 status = _getCurrentPost.status,
128 type = _getCurrentPost.type;
129
130 return {
131 postId: id,
132 postStatus: status,
133 postType: type
134 };
135})(BrowserURL);
136//# sourceMappingURL=index.js.map
\No newline at end of file