@wordpress/fields 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (135) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/README.md +12 -0
  3. package/build/actions/delete-post.js +9 -9
  4. package/build/actions/delete-post.js.map +1 -1
  5. package/build/actions/duplicate-post.js +3 -3
  6. package/build/actions/duplicate-post.js.map +1 -1
  7. package/build/actions/export-pattern.js.map +1 -1
  8. package/build/actions/permanently-delete-post.js +4 -3
  9. package/build/actions/permanently-delete-post.js.map +1 -1
  10. package/build/actions/reset-post.js +2 -2
  11. package/build/actions/reset-post.js.map +1 -1
  12. package/build/actions/restore-post.js +6 -5
  13. package/build/actions/restore-post.js.map +1 -1
  14. package/build/actions/trash-post.js +5 -4
  15. package/build/actions/trash-post.js.map +1 -1
  16. package/build/actions/view-post-revisions.js +1 -1
  17. package/build/actions/view-post-revisions.js.map +1 -1
  18. package/build/fields/featured-image/featured-image-edit.js +113 -0
  19. package/build/fields/featured-image/featured-image-edit.js.map +1 -0
  20. package/build/fields/featured-image/featured-image-view.js +41 -0
  21. package/build/fields/featured-image/featured-image-view.js.map +1 -0
  22. package/build/fields/featured-image/index.js +30 -0
  23. package/build/fields/featured-image/index.js.map +1 -0
  24. package/build/fields/index.js +21 -0
  25. package/build/fields/index.js.map +1 -1
  26. package/build/fields/parent/index.js +34 -0
  27. package/build/fields/parent/index.js.map +1 -0
  28. package/build/fields/parent/parent-edit.js +243 -0
  29. package/build/fields/parent/parent-edit.js.map +1 -0
  30. package/build/fields/parent/parent-view.js +39 -0
  31. package/build/fields/parent/parent-view.js.map +1 -0
  32. package/build/fields/parent/utils.js +20 -0
  33. package/build/fields/parent/utils.js.map +1 -0
  34. package/build/fields/slug/index.js +30 -0
  35. package/build/fields/slug/index.js.map +1 -0
  36. package/build/fields/slug/slug-edit.js +132 -0
  37. package/build/fields/slug/slug-edit.js.map +1 -0
  38. package/build/fields/slug/slug-view.js +30 -0
  39. package/build/fields/slug/slug-view.js.map +1 -0
  40. package/build/mutation/index.js +3 -2
  41. package/build/mutation/index.js.map +1 -1
  42. package/build/types.js.map +1 -1
  43. package/build-module/actions/delete-post.js +11 -12
  44. package/build-module/actions/delete-post.js.map +1 -1
  45. package/build-module/actions/duplicate-post.js +4 -5
  46. package/build-module/actions/duplicate-post.js.map +1 -1
  47. package/build-module/actions/export-pattern.js.map +1 -1
  48. package/build-module/actions/permanently-delete-post.js +4 -3
  49. package/build-module/actions/permanently-delete-post.js.map +1 -1
  50. package/build-module/actions/rename-post.js +1 -2
  51. package/build-module/actions/rename-post.js.map +1 -1
  52. package/build-module/actions/reorder-page.js +1 -2
  53. package/build-module/actions/reorder-page.js.map +1 -1
  54. package/build-module/actions/reset-post.js +3 -4
  55. package/build-module/actions/reset-post.js.map +1 -1
  56. package/build-module/actions/restore-post.js +6 -5
  57. package/build-module/actions/restore-post.js.map +1 -1
  58. package/build-module/actions/trash-post.js +6 -6
  59. package/build-module/actions/trash-post.js.map +1 -1
  60. package/build-module/actions/view-post-revisions.js +1 -1
  61. package/build-module/actions/view-post-revisions.js.map +1 -1
  62. package/build-module/fields/featured-image/featured-image-edit.js +105 -0
  63. package/build-module/fields/featured-image/featured-image-edit.js.map +1 -0
  64. package/build-module/fields/featured-image/featured-image-view.js +33 -0
  65. package/build-module/fields/featured-image/featured-image-view.js.map +1 -0
  66. package/build-module/fields/featured-image/index.js +24 -0
  67. package/build-module/fields/featured-image/index.js.map +1 -0
  68. package/build-module/fields/index.js +3 -0
  69. package/build-module/fields/index.js.map +1 -1
  70. package/build-module/fields/parent/index.js +28 -0
  71. package/build-module/fields/parent/index.js.map +1 -0
  72. package/build-module/fields/parent/parent-edit.js +230 -0
  73. package/build-module/fields/parent/parent-edit.js.map +1 -0
  74. package/build-module/fields/parent/parent-view.js +32 -0
  75. package/build-module/fields/parent/parent-view.js.map +1 -0
  76. package/build-module/fields/parent/utils.js +14 -0
  77. package/build-module/fields/parent/utils.js.map +1 -0
  78. package/build-module/fields/slug/index.js +23 -0
  79. package/build-module/fields/slug/index.js.map +1 -0
  80. package/build-module/fields/slug/slug-edit.js +125 -0
  81. package/build-module/fields/slug/slug-edit.js.map +1 -0
  82. package/build-module/fields/slug/slug-view.js +24 -0
  83. package/build-module/fields/slug/slug-view.js.map +1 -0
  84. package/build-module/mutation/index.js +3 -2
  85. package/build-module/mutation/index.js.map +1 -1
  86. package/build-module/types.js.map +1 -1
  87. package/build-style/styles-rtl.css +134 -0
  88. package/build-style/styles.css +134 -0
  89. package/build-types/actions/delete-post.d.ts.map +1 -1
  90. package/build-types/fields/featured-image/featured-image-edit.d.ts +7 -0
  91. package/build-types/fields/featured-image/featured-image-edit.d.ts.map +1 -0
  92. package/build-types/fields/featured-image/featured-image-view.d.ts +7 -0
  93. package/build-types/fields/featured-image/featured-image-view.d.ts.map +1 -0
  94. package/build-types/fields/featured-image/index.d.ts +11 -0
  95. package/build-types/fields/featured-image/index.d.ts.map +1 -0
  96. package/build-types/fields/index.d.ts +3 -0
  97. package/build-types/fields/index.d.ts.map +1 -1
  98. package/build-types/fields/parent/index.d.ts +14 -0
  99. package/build-types/fields/parent/index.d.ts.map +1 -0
  100. package/build-types/fields/parent/parent-edit.d.ts +9 -0
  101. package/build-types/fields/parent/parent-edit.d.ts.map +1 -0
  102. package/build-types/fields/parent/parent-view.d.ts +7 -0
  103. package/build-types/fields/parent/parent-view.d.ts.map +1 -0
  104. package/build-types/fields/parent/utils.d.ts +6 -0
  105. package/build-types/fields/parent/utils.d.ts.map +1 -0
  106. package/build-types/fields/slug/index.d.ts +11 -0
  107. package/build-types/fields/slug/index.d.ts.map +1 -0
  108. package/build-types/fields/slug/slug-edit.d.ts +8 -0
  109. package/build-types/fields/slug/slug-edit.d.ts.map +1 -0
  110. package/build-types/fields/slug/slug-view.d.ts +9 -0
  111. package/build-types/fields/slug/slug-view.d.ts.map +1 -0
  112. package/build-types/types.d.ts +2 -0
  113. package/build-types/types.d.ts.map +1 -1
  114. package/package.json +27 -23
  115. package/src/actions/delete-post.tsx +8 -5
  116. package/src/actions/duplicate-post.tsx +2 -2
  117. package/src/actions/view-post-revisions.tsx +1 -1
  118. package/src/fields/featured-image/featured-image-edit.tsx +122 -0
  119. package/src/fields/featured-image/featured-image-view.tsx +38 -0
  120. package/src/fields/featured-image/index.ts +24 -0
  121. package/src/fields/featured-image/style.scss +95 -0
  122. package/src/fields/index.ts +3 -0
  123. package/src/fields/parent/index.ts +27 -0
  124. package/src/fields/parent/parent-edit.tsx +348 -0
  125. package/src/fields/parent/parent-view.tsx +33 -0
  126. package/src/fields/parent/utils.ts +18 -0
  127. package/src/fields/slug/index.ts +23 -0
  128. package/src/fields/slug/slug-edit.tsx +156 -0
  129. package/src/fields/slug/slug-view.tsx +26 -0
  130. package/src/fields/slug/style.scss +22 -0
  131. package/src/mutation/index.ts +3 -3
  132. package/src/styles.scss +1 -0
  133. package/src/types.ts +2 -0
  134. package/tsconfig.json +3 -1
  135. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,348 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { ComboboxControl, ExternalLink } from '@wordpress/components';
5
+ import { useSelect } from '@wordpress/data';
6
+ import {
7
+ createInterpolateElement,
8
+ useCallback,
9
+ useMemo,
10
+ useState,
11
+ } from '@wordpress/element';
12
+ // @ts-ignore
13
+ import { store as coreStore } from '@wordpress/core-data';
14
+ import type { DataFormControlProps } from '@wordpress/dataviews';
15
+
16
+ /**
17
+ * External dependencies
18
+ */
19
+ import removeAccents from 'remove-accents';
20
+
21
+ /**
22
+ * Internal dependencies
23
+ */
24
+ import { debounce } from '@wordpress/compose';
25
+ import { decodeEntities } from '@wordpress/html-entities';
26
+ import { __, sprintf } from '@wordpress/i18n';
27
+ import type { BasePost } from '../../types';
28
+ import { getTitleWithFallbackName } from './utils';
29
+ import { filterURLForDisplay } from '@wordpress/url';
30
+
31
+ type TreeBase = {
32
+ id: number;
33
+ name: string;
34
+ [ key: string ]: any;
35
+ };
36
+
37
+ type TreeWithParent = TreeBase & {
38
+ parent: number;
39
+ };
40
+
41
+ type TreeWithoutParent = TreeBase & {
42
+ parent: null;
43
+ };
44
+
45
+ type Tree = TreeWithParent | TreeWithoutParent;
46
+
47
+ function buildTermsTree( flatTerms: Tree[] ) {
48
+ const flatTermsWithParentAndChildren = flatTerms.map( ( term ) => {
49
+ return {
50
+ children: [],
51
+ ...term,
52
+ };
53
+ } );
54
+
55
+ // All terms should have a `parent` because we're about to index them by it.
56
+ if (
57
+ flatTermsWithParentAndChildren.some(
58
+ ( { parent } ) => parent === null || parent === undefined
59
+ )
60
+ ) {
61
+ return flatTermsWithParentAndChildren as TreeWithParent[];
62
+ }
63
+
64
+ const termsByParent = (
65
+ flatTermsWithParentAndChildren as TreeWithParent[]
66
+ ).reduce(
67
+ ( acc, term ) => {
68
+ const { parent } = term;
69
+ if ( ! acc[ parent ] ) {
70
+ acc[ parent ] = [];
71
+ }
72
+ acc[ parent ].push( term );
73
+ return acc;
74
+ },
75
+ {} as Record< string, Array< TreeWithParent > >
76
+ );
77
+
78
+ const fillWithChildren = (
79
+ terms: Array< TreeWithParent >
80
+ ): Array< TreeWithParent > => {
81
+ return terms.map( ( term ) => {
82
+ const children = termsByParent[ term.id ];
83
+ return {
84
+ ...term,
85
+ children:
86
+ children && children.length
87
+ ? fillWithChildren( children )
88
+ : [],
89
+ };
90
+ } );
91
+ };
92
+
93
+ return fillWithChildren( termsByParent[ '0' ] || [] );
94
+ }
95
+
96
+ export const getItemPriority = ( name: string, searchValue: string ) => {
97
+ const normalizedName = removeAccents( name || '' ).toLowerCase();
98
+ const normalizedSearch = removeAccents( searchValue || '' ).toLowerCase();
99
+ if ( normalizedName === normalizedSearch ) {
100
+ return 0;
101
+ }
102
+
103
+ if ( normalizedName.startsWith( normalizedSearch ) ) {
104
+ return normalizedName.length;
105
+ }
106
+
107
+ return Infinity;
108
+ };
109
+
110
+ export function PageAttributesParent( {
111
+ data,
112
+ onChangeControl,
113
+ }: {
114
+ data: BasePost;
115
+ onChangeControl: ( newValue: number ) => void;
116
+ } ) {
117
+ const [ fieldValue, setFieldValue ] = useState< null | string >( null );
118
+
119
+ const pageId = data.parent;
120
+ const postId = data.id;
121
+ const postTypeSlug = data.type;
122
+
123
+ const { parentPostTitle, pageItems, isHierarchical } = useSelect(
124
+ ( select ) => {
125
+ // @ts-expect-error getPostType is not typed
126
+ const { getEntityRecord, getEntityRecords, getPostType } =
127
+ select( coreStore );
128
+
129
+ const postTypeInfo = getPostType( postTypeSlug );
130
+
131
+ const postIsHierarchical =
132
+ postTypeInfo?.hierarchical && postTypeInfo.viewable;
133
+
134
+ const parentPost = pageId
135
+ ? getEntityRecord< BasePost >(
136
+ 'postType',
137
+ postTypeSlug,
138
+ pageId
139
+ )
140
+ : null;
141
+
142
+ const query = {
143
+ per_page: 100,
144
+ exclude: postId,
145
+ parent_exclude: postId,
146
+ orderby: 'menu_order',
147
+ order: 'asc',
148
+ _fields: 'id,title,parent',
149
+ ...( fieldValue !== null && {
150
+ search: fieldValue,
151
+ } ),
152
+ };
153
+
154
+ return {
155
+ isHierarchical: postIsHierarchical,
156
+ parentPostTitle: parentPost
157
+ ? getTitleWithFallbackName( parentPost )
158
+ : '',
159
+ pageItems: postIsHierarchical
160
+ ? getEntityRecords< BasePost >(
161
+ 'postType',
162
+ postTypeSlug,
163
+ query
164
+ )
165
+ : null,
166
+ };
167
+ },
168
+ [ fieldValue, pageId, postId, postTypeSlug ]
169
+ );
170
+
171
+ /**
172
+ * This logic has been copied from https://github.com/WordPress/gutenberg/blob/0249771b519d5646171fb9fae422006c8ab773f2/packages/editor/src/components/page-attributes/parent.js#L106.
173
+ */
174
+ const parentOptions = useMemo( () => {
175
+ const getOptionsFromTree = (
176
+ tree: Array< Tree >,
177
+ level = 0
178
+ ): Array< {
179
+ value: number;
180
+ label: string;
181
+ rawName: string;
182
+ } > => {
183
+ const mappedNodes = tree.map( ( treeNode ) => [
184
+ {
185
+ value: treeNode.id,
186
+ label:
187
+ '— '.repeat( level ) + decodeEntities( treeNode.name ),
188
+ rawName: treeNode.name,
189
+ },
190
+ ...getOptionsFromTree( treeNode.children || [], level + 1 ),
191
+ ] );
192
+
193
+ const sortedNodes = mappedNodes.sort( ( [ a ], [ b ] ) => {
194
+ const priorityA = getItemPriority(
195
+ a.rawName,
196
+ fieldValue ?? ''
197
+ );
198
+ const priorityB = getItemPriority(
199
+ b.rawName,
200
+ fieldValue ?? ''
201
+ );
202
+ return priorityA >= priorityB ? 1 : -1;
203
+ } );
204
+
205
+ return sortedNodes.flat();
206
+ };
207
+
208
+ if ( ! pageItems ) {
209
+ return [];
210
+ }
211
+
212
+ let tree = pageItems.map( ( item ) => ( {
213
+ id: item.id as number,
214
+ parent: item.parent ?? null,
215
+ name: getTitleWithFallbackName( item ),
216
+ } ) );
217
+
218
+ // Only build a hierarchical tree when not searching.
219
+ if ( ! fieldValue ) {
220
+ tree = buildTermsTree( tree );
221
+ }
222
+
223
+ const opts = getOptionsFromTree( tree );
224
+
225
+ // Ensure the current parent is in the options list.
226
+ const optsHasParent = opts.find( ( item ) => item.value === pageId );
227
+ if ( pageId && parentPostTitle && ! optsHasParent ) {
228
+ opts.unshift( {
229
+ value: pageId,
230
+ label: parentPostTitle,
231
+ rawName: '',
232
+ } );
233
+ }
234
+ return opts.map( ( option ) => ( {
235
+ ...option,
236
+ value: option.value.toString(),
237
+ } ) );
238
+ }, [ pageItems, fieldValue, parentPostTitle, pageId ] );
239
+
240
+ if ( ! isHierarchical ) {
241
+ return null;
242
+ }
243
+
244
+ /**
245
+ * Handle user input.
246
+ *
247
+ * @param {string} inputValue The current value of the input field.
248
+ */
249
+ const handleKeydown = ( inputValue: string ) => {
250
+ setFieldValue( inputValue );
251
+ };
252
+
253
+ /**
254
+ * Handle author selection.
255
+ *
256
+ * @param {Object} selectedPostId The selected Author.
257
+ */
258
+ const handleChange = ( selectedPostId: string | null | undefined ) => {
259
+ if ( selectedPostId ) {
260
+ return onChangeControl( parseInt( selectedPostId, 10 ) ?? 0 );
261
+ }
262
+
263
+ onChangeControl( 0 );
264
+ };
265
+
266
+ return (
267
+ <ComboboxControl
268
+ __nextHasNoMarginBottom
269
+ __next40pxDefaultSize
270
+ label={ __( 'Parent' ) }
271
+ help={ __( 'Choose a parent page.' ) }
272
+ value={ pageId?.toString() }
273
+ options={ parentOptions }
274
+ onFilterValueChange={ debounce(
275
+ ( value: unknown ) => handleKeydown( value as string ),
276
+ 300
277
+ ) }
278
+ onChange={ handleChange }
279
+ hideLabelFromVision
280
+ />
281
+ );
282
+ }
283
+
284
+ export const ParentEdit = ( {
285
+ data,
286
+ field,
287
+ onChange,
288
+ }: DataFormControlProps< BasePost > ) => {
289
+ const { id } = field;
290
+
291
+ const homeUrl = useSelect( ( select ) => {
292
+ // @ts-expect-error getEntityRecord is not typed with unstableBase as argument.
293
+ return select( coreStore ).getEntityRecord< {
294
+ home: string;
295
+ } >( 'root', '__unstableBase' )?.home as string;
296
+ }, [] );
297
+
298
+ const onChangeControl = useCallback(
299
+ ( newValue?: number ) =>
300
+ onChange( {
301
+ [ id ]: newValue,
302
+ } ),
303
+ [ id, onChange ]
304
+ );
305
+
306
+ return (
307
+ <fieldset className="fields-controls__parent">
308
+ <div>
309
+ { createInterpolateElement(
310
+ sprintf(
311
+ /* translators: %1$s The home URL of the WordPress installation without the scheme. */
312
+ __(
313
+ 'Child pages inherit characteristics from their parent, such as URL structure. For instance, if "Pricing" is a child of "Services", its URL would be %1$s<wbr />/services<wbr />/pricing.'
314
+ ),
315
+ filterURLForDisplay( homeUrl ).replace(
316
+ /([/.])/g,
317
+ '<wbr />$1'
318
+ )
319
+ ),
320
+ {
321
+ wbr: <wbr />,
322
+ }
323
+ ) }
324
+ <p>
325
+ { createInterpolateElement(
326
+ __(
327
+ 'They also show up as sub-items in the default navigation menu. <a>Learn more.</a>'
328
+ ),
329
+ {
330
+ a: (
331
+ <ExternalLink
332
+ href={ __(
333
+ 'https://wordpress.org/documentation/article/page-post-settings-sidebar/#page-attributes'
334
+ ) }
335
+ children={ undefined }
336
+ />
337
+ ),
338
+ }
339
+ ) }
340
+ </p>
341
+ <PageAttributesParent
342
+ data={ data }
343
+ onChangeControl={ onChangeControl }
344
+ />
345
+ </div>
346
+ </fieldset>
347
+ );
348
+ };
@@ -0,0 +1,33 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { useSelect } from '@wordpress/data';
5
+ import { store as coreStore } from '@wordpress/core-data';
6
+
7
+ /**
8
+ * Internal dependencies
9
+ */
10
+ import type { BasePost } from '../../types';
11
+ import type { DataViewRenderFieldProps } from '@wordpress/dataviews';
12
+ import { getTitleWithFallbackName } from './utils';
13
+ import { __ } from '@wordpress/i18n';
14
+
15
+ export const ParentView = ( {
16
+ item,
17
+ }: DataViewRenderFieldProps< BasePost > ) => {
18
+ const parent = useSelect(
19
+ ( select ) => {
20
+ const { getEntityRecord } = select( coreStore );
21
+ return item?.parent
22
+ ? getEntityRecord( 'postType', item.type, item.parent )
23
+ : null;
24
+ },
25
+ [ item.parent, item.type ]
26
+ );
27
+
28
+ if ( parent ) {
29
+ return <>{ getTitleWithFallbackName( parent ) }</>;
30
+ }
31
+
32
+ return <>{ __( 'None' ) }</>;
33
+ };
@@ -0,0 +1,18 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { decodeEntities } from '@wordpress/html-entities';
5
+ import { __ } from '@wordpress/i18n';
6
+
7
+ /**
8
+ * Internal dependencies
9
+ */
10
+ import type { BasePost } from '../../types';
11
+
12
+ export function getTitleWithFallbackName( post: BasePost ) {
13
+ return typeof post.title === 'object' &&
14
+ 'rendered' in post.title &&
15
+ post.title.rendered
16
+ ? decodeEntities( post.title.rendered )
17
+ : `#${ post?.id } (${ __( 'no title' ) })`;
18
+ }
@@ -0,0 +1,23 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import type { Field } from '@wordpress/dataviews';
5
+
6
+ /**
7
+ * Internal dependencies
8
+ */
9
+ import type { BasePost } from '../../types';
10
+ import { __ } from '@wordpress/i18n';
11
+ import SlugEdit from './slug-edit';
12
+ import SlugView from './slug-view';
13
+
14
+ const slugField: Field< BasePost > = {
15
+ id: 'slug',
16
+ type: 'text',
17
+ label: __( 'Slug' ),
18
+ getValue: ( { item } ) => item.slug,
19
+ Edit: SlugEdit,
20
+ render: SlugView,
21
+ };
22
+
23
+ export default slugField;
@@ -0,0 +1,156 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import {
5
+ Button,
6
+ ExternalLink,
7
+ __experimentalInputControl as InputControl,
8
+ __experimentalInputControlPrefixWrapper as InputControlPrefixWrapper,
9
+ __experimentalVStack as VStack,
10
+ } from '@wordpress/components';
11
+ import { copySmall } from '@wordpress/icons';
12
+ import { useCopyToClipboard, useInstanceId } from '@wordpress/compose';
13
+ import { useDispatch } from '@wordpress/data';
14
+ import { useCallback, useEffect, useRef } from '@wordpress/element';
15
+ import { store as noticesStore } from '@wordpress/notices';
16
+ import { safeDecodeURIComponent } from '@wordpress/url';
17
+ import type { DataFormControlProps } from '@wordpress/dataviews';
18
+ import { __ } from '@wordpress/i18n';
19
+
20
+ /**
21
+ * Internal dependencies
22
+ */
23
+ import type { BasePost } from '../../types';
24
+
25
+ const SlugEdit = ( {
26
+ field,
27
+ onChange,
28
+ data,
29
+ }: DataFormControlProps< BasePost > ) => {
30
+ const { id } = field;
31
+
32
+ const slug = field.getValue( { item: data } ) ?? '';
33
+ const permalinkTemplate = data.permalink_template || '';
34
+ const PERMALINK_POSTNAME_REGEX = /%(?:postname|pagename)%/;
35
+ const [ prefix, suffix ] = permalinkTemplate.split(
36
+ PERMALINK_POSTNAME_REGEX
37
+ );
38
+ const permalinkPrefix = prefix;
39
+ const permalinkSuffix = suffix;
40
+ const isEditable = PERMALINK_POSTNAME_REGEX.test( permalinkTemplate );
41
+ const originalSlugRef = useRef( slug );
42
+ const slugToDisplay = slug || originalSlugRef.current;
43
+ const permalink = isEditable
44
+ ? `${ permalinkPrefix }${ slugToDisplay }${ permalinkSuffix }`
45
+ : safeDecodeURIComponent( data.link || '' );
46
+
47
+ useEffect( () => {
48
+ if ( slug && originalSlugRef.current === undefined ) {
49
+ originalSlugRef.current = slug;
50
+ }
51
+ }, [ slug ] );
52
+
53
+ const onChangeControl = useCallback(
54
+ ( newValue?: string ) =>
55
+ onChange( {
56
+ [ id ]: newValue,
57
+ } ),
58
+ [ id, onChange ]
59
+ );
60
+
61
+ const { createNotice } = useDispatch( noticesStore );
62
+
63
+ const copyButtonRef = useCopyToClipboard( permalink, () => {
64
+ createNotice( 'info', __( 'Copied Permalink to clipboard.' ), {
65
+ isDismissible: true,
66
+ type: 'snackbar',
67
+ } );
68
+ } );
69
+
70
+ const postUrlSlugDescriptionId =
71
+ 'editor-post-url__slug-description-' + useInstanceId( SlugEdit );
72
+
73
+ return (
74
+ <fieldset className="fields-controls__slug">
75
+ { isEditable && (
76
+ <VStack>
77
+ <VStack spacing="0px">
78
+ <span>
79
+ { __(
80
+ 'Customize the last part of the Permalink.'
81
+ ) }
82
+ </span>
83
+ <ExternalLink href="https://wordpress.org/documentation/article/page-post-settings-sidebar/#permalink">
84
+ { __( 'Learn more' ) }
85
+ </ExternalLink>
86
+ </VStack>
87
+ <InputControl
88
+ __next40pxDefaultSize
89
+ prefix={
90
+ <InputControlPrefixWrapper>
91
+ /
92
+ </InputControlPrefixWrapper>
93
+ }
94
+ suffix={
95
+ <Button
96
+ __next40pxDefaultSize
97
+ icon={ copySmall }
98
+ ref={ copyButtonRef }
99
+ label={ __( 'Copy' ) }
100
+ />
101
+ }
102
+ label={ __( 'Link' ) }
103
+ hideLabelFromVision
104
+ value={ slug }
105
+ autoComplete="off"
106
+ spellCheck="false"
107
+ type="text"
108
+ className="fields-controls__slug-input"
109
+ onChange={ ( newValue?: string ) => {
110
+ onChangeControl( newValue );
111
+ } }
112
+ onBlur={ () => {
113
+ if ( slug === '' ) {
114
+ onChangeControl( originalSlugRef.current );
115
+ }
116
+ } }
117
+ aria-describedby={ postUrlSlugDescriptionId }
118
+ help={
119
+ <>
120
+ <p className="fields-controls__slug-help">
121
+ <span className="fields-controls__slug-help-visual-label">
122
+ { __( 'Permalink:' ) }
123
+ </span>
124
+ <ExternalLink
125
+ className="fields-controls__slug-help-link"
126
+ href={ permalink }
127
+ >
128
+ <span className="fields-controls__slug-help-prefix">
129
+ { permalinkPrefix }
130
+ </span>
131
+ <span className="fields-controls__slug-help-slug">
132
+ { slugToDisplay }
133
+ </span>
134
+ <span className="fields-controls__slug-help-suffix">
135
+ { permalinkSuffix }
136
+ </span>
137
+ </ExternalLink>
138
+ </p>
139
+ </>
140
+ }
141
+ />
142
+ </VStack>
143
+ ) }
144
+ { ! isEditable && (
145
+ <ExternalLink
146
+ className="fields-controls__slug-help"
147
+ href={ permalink }
148
+ >
149
+ { permalink }
150
+ </ExternalLink>
151
+ ) }
152
+ </fieldset>
153
+ );
154
+ };
155
+
156
+ export default SlugEdit;
@@ -0,0 +1,26 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { useEffect, useRef } from '@wordpress/element';
5
+
6
+ /**
7
+ * Internal dependencies
8
+ */
9
+ import type { BasePost } from '../../types';
10
+
11
+ const SlugView = ( { item }: { item: BasePost } ) => {
12
+ const slug = item.slug;
13
+ const originalSlugRef = useRef( slug );
14
+
15
+ useEffect( () => {
16
+ if ( slug && originalSlugRef.current === undefined ) {
17
+ originalSlugRef.current = slug;
18
+ }
19
+ }, [ slug ] );
20
+
21
+ const slugToDisplay = slug || originalSlugRef.current;
22
+
23
+ return `/${ slugToDisplay ?? '' }`;
24
+ };
25
+
26
+ export default SlugView;
@@ -0,0 +1,22 @@
1
+ .fields-controls__slug {
2
+ .fields-controls__slug-external-icon {
3
+ margin-left: 5ch;
4
+ }
5
+
6
+ .fields-controls__slug-input input.components-input-control__input {
7
+ padding-inline-start: 0 !important;
8
+ }
9
+
10
+ .fields-controls__slug-help-link {
11
+ word-break: break-word;
12
+ }
13
+
14
+ .fields-controls__slug-help {
15
+ display: flex;
16
+ flex-direction: column;
17
+
18
+ .fields-controls__slug-help-slug {
19
+ font-weight: 600;
20
+ }
21
+ }
22
+ }
@@ -10,9 +10,9 @@ import { dispatch } from '@wordpress/data';
10
10
  */
11
11
  import type { CoreDataError, Post } from '../types';
12
12
 
13
- const getErrorMessagesFromPromises = < T >(
13
+ function getErrorMessagesFromPromises< T >(
14
14
  allSettledResults: PromiseSettledResult< T >[]
15
- ) => {
15
+ ) {
16
16
  const errorMessages = new Set< string >();
17
17
  // If there was at lease one failure.
18
18
  if ( allSettledResults.length === 1 ) {
@@ -36,7 +36,7 @@ const getErrorMessagesFromPromises = < T >(
36
36
  }
37
37
  }
38
38
  return errorMessages;
39
- };
39
+ }
40
40
 
41
41
  export type NoticeSettings< T extends Post > = {
42
42
  success: {
@@ -0,0 +1 @@
1
+ @import "./fields/slug/style.scss";
package/src/types.ts CHANGED
@@ -35,6 +35,8 @@ export interface BasePost extends CommonPost {
35
35
  menu_order?: number;
36
36
  ping_status?: 'open' | 'closed';
37
37
  link?: string;
38
+ slug?: string;
39
+ permalink_template?: string;
38
40
  }
39
41
 
40
42
  export interface Template extends CommonPost {
package/tsconfig.json CHANGED
@@ -23,7 +23,9 @@
23
23
  { "path": "../blob" },
24
24
  { "path": "../core-data" },
25
25
  { "path": "../hooks" },
26
- { "path": "../html-entities" }
26
+ { "path": "../html-entities" },
27
+ { "path": "../media-utils" },
28
+ { "path": "../router" }
27
29
  ],
28
30
  "include": [ "src" ]
29
31
  }