-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
index.js
169 lines (161 loc) · 4.28 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
/**
* External dependencies
*/
import { get } from 'lodash';
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { PanelBody, TextControl, ExternalLink } from '@wordpress/components';
import { withSelect, withDispatch } from '@wordpress/data';
import { compose, ifCondition, withState } from '@wordpress/compose';
import { cleanForSlug } from '@wordpress/editor';
import { safeDecodeURIComponent } from '@wordpress/url';
/**
* Module Constants
*/
const PANEL_NAME = 'post-link';
function PostLink( {
isOpened,
onTogglePanel,
isEditable,
postLink,
permalinkParts,
editPermalink,
forceEmptyField,
setState,
postTitle,
postSlug,
postID,
} ) {
const { prefix, suffix } = permalinkParts;
let prefixElement, postNameElement, suffixElement;
const currentSlug = safeDecodeURIComponent( postSlug ) || cleanForSlug( postTitle ) || postID;
if ( isEditable ) {
prefixElement = prefix && (
<span className="edit-post-post-link__link-prefix">{ prefix }</span>
);
postNameElement = currentSlug && (
<span className="edit-post-post-link__link-post-name">{ currentSlug }</span>
);
suffixElement = suffix && (
<span className="edit-post-post-link__link-suffix">{ suffix }</span>
);
}
return (
<PanelBody
title={ __( 'Permalink' ) }
opened={ isOpened }
onToggle={ onTogglePanel }
>
{ isEditable && (
<div className="editor-post-link">
<TextControl
label={ __( 'URL Slug' ) }
value={ forceEmptyField ? '' : currentSlug }
onChange={ ( newValue ) => {
editPermalink( newValue );
// When we delete the field the permalink gets
// reverted to the original value.
// The forceEmptyField logic allows the user to have
// the field temporarily empty while typing.
if ( ! newValue ) {
if ( ! forceEmptyField ) {
setState( {
forceEmptyField: true,
} );
}
return;
}
if ( forceEmptyField ) {
setState( {
forceEmptyField: false,
} );
}
} }
onBlur={ ( event ) => {
editPermalink( cleanForSlug( event.target.value ) );
if ( forceEmptyField ) {
setState( {
forceEmptyField: false,
} );
}
} }
/>
<p>
{ __( 'The last part of the URL. ' ) }
<ExternalLink href="https://codex.wordpress.org/Posts_Add_New_Screen">
{ __( 'Read about permalinks' ) }
</ExternalLink>
</p>
</div>
) }
<p className="edit-post-post-link__preview-label">
{ __( 'Preview' ) }
</p>
<ExternalLink
className="edit-post-post-link__link"
href={ postLink }
target="_blank"
>
{ isEditable ?
( <>
{ prefixElement }{ postNameElement }{ suffixElement }
</> ) :
postLink
}
</ExternalLink>
</PanelBody>
);
}
export default compose( [
withSelect( ( select ) => {
const {
isEditedPostNew,
isPermalinkEditable,
getCurrentPost,
isCurrentPostPublished,
getPermalinkParts,
getEditedPostAttribute,
} = select( 'core/editor' );
const {
isEditorPanelEnabled,
isEditorPanelOpened,
} = select( 'core/edit-post' );
const {
getPostType,
} = select( 'core' );
const { link, id } = getCurrentPost();
const postTypeName = getEditedPostAttribute( 'type' );
const postType = getPostType( postTypeName );
return {
isNew: isEditedPostNew(),
postLink: link,
isEditable: isPermalinkEditable(),
isPublished: isCurrentPostPublished(),
isOpened: isEditorPanelOpened( PANEL_NAME ),
permalinkParts: getPermalinkParts(),
isEnabled: isEditorPanelEnabled( PANEL_NAME ),
isViewable: get( postType, [ 'viewable' ], false ),
postTitle: getEditedPostAttribute( 'title' ),
postSlug: getEditedPostAttribute( 'slug' ),
postID: id,
};
} ),
ifCondition( ( { isEnabled, isNew, postLink, isViewable, permalinkParts } ) => {
return isEnabled && ! isNew && postLink && isViewable && permalinkParts;
} ),
withDispatch( ( dispatch ) => {
const { toggleEditorPanelOpened } = dispatch( 'core/edit-post' );
const { editPost } = dispatch( 'core/editor' );
return {
onTogglePanel: () => toggleEditorPanelOpened( PANEL_NAME ),
editPermalink: ( newSlug ) => {
editPost( { slug: newSlug } );
},
};
} ),
withState( {
forceEmptyField: false,
} ),
] )( PostLink );