From d8caf7912c0c7d49fe94d36a8d0148f8b3b38b68 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Wed, 1 Nov 2023 16:00:59 +0200 Subject: [PATCH] [Site editor]: Add edit page slug field --- .../page-panels/page-slug.js | 159 ++++++++++++++++++ .../page-panels/page-summary.js | 2 + .../sidebar-edit-mode/page-panels/style.scss | 7 + 3 files changed, 168 insertions(+) create mode 100644 packages/edit-site/src/components/sidebar-edit-mode/page-panels/page-slug.js diff --git a/packages/edit-site/src/components/sidebar-edit-mode/page-panels/page-slug.js b/packages/edit-site/src/components/sidebar-edit-mode/page-panels/page-slug.js new file mode 100644 index 0000000000000..149d8f4942d78 --- /dev/null +++ b/packages/edit-site/src/components/sidebar-edit-mode/page-panels/page-slug.js @@ -0,0 +1,159 @@ +/** + * WordPress dependencies + */ +import { useSelect, useDispatch } from '@wordpress/data'; +import { safeDecodeURIComponent, filterURLForDisplay } from '@wordpress/url'; +import { useState, useMemo } from '@wordpress/element'; +import { __experimentalInspectorPopoverHeader as InspectorPopoverHeader } from '@wordpress/block-editor'; +import { __ } from '@wordpress/i18n'; +import { + TextControl, + ExternalLink, + __experimentalHStack as HStack, + __experimentalVStack as VStack, + __experimentalText as Text, + Dropdown, + Button, +} from '@wordpress/components'; +import { useEntityRecord, store as coreStore } from '@wordpress/core-data'; +import { store as noticesStore } from '@wordpress/notices'; + +/** + * Internal dependencies + */ +import useEditedEntityRecord from '../../use-edited-entity-record'; + +export const PERMALINK_POSTNAME_REGEX = /%(?:postname|pagename)%/; + +export default function PageSlug( { postType, postId } ) { + const { createErrorNotice } = useDispatch( noticesStore ); + const { editEntityRecord } = useDispatch( coreStore ); + const { record, isLoaded } = useEditedEntityRecord( postType, postId ); + const { record: savedRecord } = useEntityRecord( postType, postId ); + const [ popoverAnchor, setPopoverAnchor ] = useState( null ); + const isEditable = + PERMALINK_POSTNAME_REGEX.test( record?.permalink_template ) && + record?._links?.[ 'wp:action-publish' ]; + const viewPostLabel = useSelect( + ( select ) => { + const postTypeObject = select( coreStore ).getPostType( postType ); + return postTypeObject?.labels.view_item; + }, + [ postType ] + ); + 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 ( ! isLoaded ) { + return null; + } + const onSlugChange = async ( newValue ) => { + try { + await editEntityRecord( 'postType', postType, postId, { + slug: newValue, + } ); + } catch ( error ) { + const errorMessage = + error.message && error.code !== 'unknown_error' + ? error.message + : __( 'An error occurred while updating the status' ); + + createErrorNotice( errorMessage, { + type: 'snackbar', + } ); + } + }; + return ( + + + { __( 'URL' ) } + + ( + + ) } + renderContent={ ( { onClose } ) => { + return ( + <> + + + { isEditable && ( + + { __( + 'The last part of the URL.' + ) }{ ' ' } + + { __( 'Learn more.' ) } + + + } + onChange={ onSlugChange } + onBlur={ ( event ) => { + if ( ! event.target.value ) { + onSlugChange( + savedRecord?.slug + ); + } + } } + /> + ) } + + + { viewPostLabel || __( 'View post' ) } + + + { record.link } + + + + + ); + } } + /> + + ); +} diff --git a/packages/edit-site/src/components/sidebar-edit-mode/page-panels/page-summary.js b/packages/edit-site/src/components/sidebar-edit-mode/page-panels/page-summary.js index c4dafeab6cb37..25b69985bcbd6 100644 --- a/packages/edit-site/src/components/sidebar-edit-mode/page-panels/page-summary.js +++ b/packages/edit-site/src/components/sidebar-edit-mode/page-panels/page-summary.js @@ -8,6 +8,7 @@ import { __experimentalVStack as VStack } from '@wordpress/components'; import PageStatus from './page-status'; import PublishDate from './publish-date'; import EditTemplate from './edit-template'; +import PageSlug from './page-slug'; export default function PageSummary( { status, @@ -32,6 +33,7 @@ export default function PageSummary( { postType={ postType } /> + ); } diff --git a/packages/edit-site/src/components/sidebar-edit-mode/page-panels/style.scss b/packages/edit-site/src/components/sidebar-edit-mode/page-panels/style.scss index 5501fe49e5876..e1a8e4acb7227 100644 --- a/packages/edit-site/src/components/sidebar-edit-mode/page-panels/style.scss +++ b/packages/edit-site/src/components/sidebar-edit-mode/page-panels/style.scss @@ -85,3 +85,10 @@ min-width: 240px; } } + +.edit-site-page-panels-edit-slug__dropdown { + .components-popover__content { + min-width: 320px; + padding: $grid-unit-20; + } +}