-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
177 lines (154 loc) · 4.58 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
* External dependencies
*/
import classnames from 'classnames';
import { get } from 'lodash';
/**
* WordPress dependencies
*/
import { withDispatch, withSelect } from '@wordpress/data';
import { Component } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { compose } from '@wordpress/compose';
import { ClipboardButton, Button, ExternalLink } from '@wordpress/components';
import { safeDecodeURI, safeDecodeURIComponent } from '@wordpress/url';
/**
* Internal dependencies
*/
import PostPermalinkEditor from './editor.js';
import { getWPAdminURL, cleanForSlug } from '../../utils/url';
class PostPermalink extends Component {
constructor() {
super( ...arguments );
this.addVisibilityCheck = this.addVisibilityCheck.bind( this );
this.onVisibilityChange = this.onVisibilityChange.bind( this );
this.state = {
isCopied: false,
isEditingPermalink: false,
};
}
addVisibilityCheck() {
window.addEventListener( 'visibilitychange', this.onVisibilityChange );
}
onVisibilityChange() {
const { isEditable, refreshPost } = this.props;
// If the user just returned after having clicked the "Change Permalinks" button,
// fetch a new copy of the post from the server, just in case they enabled permalinks.
if ( ! isEditable && 'visible' === document.visibilityState ) {
refreshPost();
}
}
componentDidUpdate( prevProps, prevState ) {
// If we've just stopped editing the permalink, focus on the new permalink.
if ( prevState.isEditingPermalink && ! this.state.isEditingPermalink ) {
this.linkElement.focus();
}
}
componentWillUnmount() {
window.removeEventListener( 'visibilitychange', this.addVisibilityCheck );
}
render() {
const {
isEditable,
isNew,
isPublished,
isViewable,
permalinkParts,
postLink,
postSlug,
postID,
postTitle,
} = this.props;
if ( isNew || ! isViewable || ! permalinkParts || ! postLink ) {
return null;
}
const { isCopied, isEditingPermalink } = this.state;
const ariaLabel = isCopied ? __( 'Permalink copied' ) : __( 'Copy the permalink' );
const { prefix, suffix } = permalinkParts;
const slug = safeDecodeURIComponent( postSlug ) || cleanForSlug( postTitle ) || postID;
const samplePermalink = ( isEditable ) ? prefix + slug + suffix : prefix;
return (
<div className="editor-post-permalink">
<ClipboardButton
className={ classnames( 'editor-post-permalink__copy', { 'is-copied': isCopied } ) }
text={ samplePermalink }
label={ ariaLabel }
onCopy={ () => this.setState( { isCopied: true } ) }
aria-disabled={ isCopied }
icon="admin-links"
/>
<span className="editor-post-permalink__label">{ __( 'Permalink:' ) }</span>
{ ! isEditingPermalink &&
<ExternalLink
className="editor-post-permalink__link"
href={ ! isPublished ? postLink : samplePermalink }
target="_blank"
ref={ ( linkElement ) => this.linkElement = linkElement }
>
{ safeDecodeURI( samplePermalink ) }
‎
</ExternalLink>
}
{ isEditingPermalink &&
<PostPermalinkEditor
slug={ slug }
onSave={ () => this.setState( { isEditingPermalink: false } ) }
/>
}
{ isEditable && ! isEditingPermalink &&
<Button
className="editor-post-permalink__edit"
isLarge
onClick={ () => this.setState( { isEditingPermalink: true } ) }
>
{ __( 'Edit' ) }
</Button>
}
{ ! isEditable &&
<Button
className="editor-post-permalink__change"
isLarge
href={ getWPAdminURL( 'options-permalink.php' ) }
onClick={ this.addVisibilityCheck }
target="_blank"
>
{ __( 'Change Permalinks' ) }
</Button>
}
</div>
);
}
}
export default compose( [
withSelect( ( select ) => {
const {
isEditedPostNew,
isPermalinkEditable,
getCurrentPost,
getPermalinkParts,
getEditedPostAttribute,
isCurrentPostPublished,
} = select( 'core/editor' );
const {
getPostType,
} = select( 'core' );
const { id, link } = getCurrentPost();
const postTypeName = getEditedPostAttribute( 'type' );
const postType = getPostType( postTypeName );
return {
isNew: isEditedPostNew(),
postLink: link,
permalinkParts: getPermalinkParts(),
postSlug: getEditedPostAttribute( 'slug' ),
isEditable: isPermalinkEditable(),
isPublished: isCurrentPostPublished(),
postTitle: getEditedPostAttribute( 'title' ),
postID: id,
isViewable: get( postType, [ 'viewable' ], false ),
};
} ),
withDispatch( ( dispatch ) => {
const { refreshPost } = dispatch( 'core/editor' );
return { refreshPost };
} ),
] )( PostPermalink );