Skip to content

Commit

Permalink
Quick Edit - Slug Field: improve slug preview
Browse files Browse the repository at this point in the history
  • Loading branch information
gigitux committed Oct 29, 2024
1 parent 648c458 commit d871796
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
45 changes: 21 additions & 24 deletions packages/fields/src/fields/slug/slug-edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { __ } from '@wordpress/i18n';
* Internal dependencies
*/
import type { BasePost } from '../../types';
import { getSlug } from './utils';

const SlugEdit = ( {
field,
Expand All @@ -29,7 +30,7 @@ const SlugEdit = ( {
}: DataFormControlProps< BasePost > ) => {
const { id } = field;

const slug = field.getValue( { item: data } ) ?? '';
const slug = field.getValue( { item: data } ) || getSlug( data );
const permalinkTemplate = data.permalink_template || '';
const PERMALINK_POSTNAME_REGEX = /%(?:postname|pagename)%/;
const [ prefix, suffix ] = permalinkTemplate.split(
Expand Down Expand Up @@ -115,30 +116,26 @@ const SlugEdit = ( {
}
} }
aria-describedby={ postUrlSlugDescriptionId }
help={
<>
<p className="fields-controls__slug-help">
<span className="fields-controls__slug-help-visual-label">
{ __( 'Permalink:' ) }
</span>
<ExternalLink
className="fields-controls__slug-help-link"
href={ permalink }
>
<span className="fields-controls__slug-help-prefix">
{ permalinkPrefix }
</span>
<span className="fields-controls__slug-help-slug">
{ slugToDisplay }
</span>
<span className="fields-controls__slug-help-suffix">
{ permalinkSuffix }
</span>
</ExternalLink>
</p>
</>
}
/>
<div className="fields-controls__slug-help">
<span className="fields-controls__slug-help-visual-label">
{ __( 'Permalink:' ) }
</span>
<ExternalLink
className="fields-controls__slug-help-link"
href={ permalink }
>
<span className="fields-controls__slug-help-prefix">
{ permalinkPrefix }
</span>
<span className="fields-controls__slug-help-slug">
{ slugToDisplay }
</span>
<span className="fields-controls__slug-help-suffix">
{ permalinkSuffix }
</span>
</ExternalLink>
</div>
</VStack>
) }
{ ! isEditable && (
Expand Down
5 changes: 3 additions & 2 deletions packages/fields/src/fields/slug/slug-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import { useEffect, useRef } from '@wordpress/element';
* Internal dependencies
*/
import type { BasePost } from '../../types';
import { getSlug } from './utils';

const SlugView = ( { item }: { item: BasePost } ) => {
const slug = item.slug;
const slug = item ? getSlug( item ) : '';
const originalSlugRef = useRef( slug );

useEffect( () => {
Expand All @@ -20,7 +21,7 @@ const SlugView = ( { item }: { item: BasePost } ) => {

const slugToDisplay = slug || originalSlugRef.current;

return `/${ slugToDisplay ?? '' }`;
return `${ slugToDisplay ?? '' }`;
};

export default SlugView;
15 changes: 15 additions & 0 deletions packages/fields/src/fields/slug/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* WordPress dependencies
*/
import { cleanForSlug } from '@wordpress/url';
/**
* Internal dependencies
*/
import type { BasePost } from '../../types';
import { getItemTitle } from '../../actions/utils';

export const getSlug = ( item: BasePost ) => {
return (
item.slug || cleanForSlug( getItemTitle( item ) ) || item.id.toString()
);
};

0 comments on commit d871796

Please sign in to comment.