-
Notifications
You must be signed in to change notification settings - Fork 4k
/
page-slug.js
154 lines (151 loc) · 4.41 KB
/
page-slug.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
/**
* WordPress dependencies
*/
import { useSelect, useDispatch } from '@wordpress/data';
import {
safeDecodeURIComponent,
filterURLForDisplay,
cleanForSlug,
} from '@wordpress/url';
import { useState, useMemo } from '@wordpress/element';
import { __experimentalInspectorPopoverHeader as InspectorPopoverHeader } from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';
import {
__experimentalInputControl as InputControl,
__experimentalHStack as HStack,
__experimentalVStack as VStack,
__experimentalText as Text,
Dropdown,
Button,
} from '@wordpress/components';
import { store as coreStore } from '@wordpress/core-data';
export const PERMALINK_POSTNAME_REGEX = /%(?:postname|pagename)%/;
function getPostPermalink( record, isEditable ) {
if ( ! record?.permalink_template ) {
return;
}
const slug = record?.slug || record?.generated_slug;
const [ prefix, suffix ] = record.permalink_template.split(
PERMALINK_POSTNAME_REGEX
);
const permalink = isEditable ? prefix + slug + suffix : record.link;
return filterURLForDisplay( safeDecodeURIComponent( permalink ) );
}
export default function PageSlug( { postType, postId } ) {
const { editEntityRecord } = useDispatch( coreStore );
const { record, savedSlug } = useSelect(
( select ) => {
const { getEntityRecord, getEditedEntityRecord } =
select( coreStore );
const savedRecord = getEntityRecord( 'postType', postType, postId );
return {
record: getEditedEntityRecord( 'postType', postType, postId ),
savedSlug: savedRecord?.slug || savedRecord?.generated_slug,
};
},
[ postType, postId ]
);
const [ popoverAnchor, setPopoverAnchor ] = useState( null );
const [ forceEmptyField, setForceEmptyField ] = useState( false );
const isEditable =
PERMALINK_POSTNAME_REGEX.test( record?.permalink_template ) &&
record?._links?.[ 'wp:action-publish' ];
const popoverProps = useMemo(
() => ( {
// Anchor the popover to the middle of the entire row so that it doesn't
// move around when the label changes.
anchor: popoverAnchor,
'aria-label': __( 'Change slug' ),
placement: 'bottom-end',
} ),
[ popoverAnchor ]
);
if ( ! record || ! isEditable ) {
return null;
}
const recordSlug = safeDecodeURIComponent(
record?.slug || record?.generated_slug
);
const permaLink = getPostPermalink( record, isEditable );
const onSlugChange = ( newValue ) => {
editEntityRecord( 'postType', postType, postId, {
slug: newValue,
} );
};
return (
<HStack className="edit-site-summary-field">
<Text className="edit-site-summary-field__label">
{ __( 'URL' ) }
</Text>
<Dropdown
contentClassName="edit-site-page-panels-edit-slug__dropdown"
popoverProps={ popoverProps }
focusOnMount
ref={ setPopoverAnchor }
onClose={ () => {
if ( forceEmptyField ) {
onSlugChange( cleanForSlug( savedSlug ) );
setForceEmptyField( false );
}
} }
renderToggle={ ( { onToggle } ) => (
<Button
className="edit-site-summary-field__trigger"
variant="tertiary"
onClick={ onToggle }
>
{ permaLink }
</Button>
) }
renderContent={ ( { onClose } ) => {
return (
<>
<InspectorPopoverHeader
title={ __( 'URL' ) }
onClose={ onClose }
/>
<VStack spacing={ 5 }>
<InputControl
__nextHasNoMarginBottom
__next40pxDefaultSize
label={ __( 'Permalink' ) }
hideLabelFromVision
value={ forceEmptyField ? '' : recordSlug }
autoComplete="off"
spellCheck="false"
help={ __( 'The last part of the URL.' ) }
onChange={ ( newValue ) => {
onSlugChange( 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 ) {
setForceEmptyField( true );
}
return;
}
if ( forceEmptyField ) {
setForceEmptyField( false );
}
} }
onBlur={ ( event ) => {
onSlugChange(
cleanForSlug(
event.target.value || savedSlug
)
);
if ( forceEmptyField ) {
setForceEmptyField( false );
}
} }
/>
</VStack>
</>
);
} }
/>
</HStack>
);
}