Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QuickEdit: Add slug field control #65196

Merged
merged 21 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion packages/edit-site/src/components/post-edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,19 @@ function PostEditForm( { postType, postId } ) {
'title',
'author',
'date',
'slug',
'comment_status',
],
};

const fieldsWithBulkEditSupport = [
Copy link
Member

@oandregal oandregal Sep 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has a few facets and it's broken in trunk. I've created a new issue to discuss how to move forward at #65685 Would you available to tackle that one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I can work on it! 👍

'title',
'status',
'date',
'author',
'comment_status',
];

const onChange = ( edits ) => {
for ( const id of ids ) {
if (
Expand Down Expand Up @@ -106,7 +116,16 @@ function PostEditForm( { postType, postId } ) {
<DataForm
data={ ids.length === 1 ? record : multiEdits }
fields={ fields }
form={ form }
form={
ids.length === 1
? form
: {
...form,
fields: form.fields.filter( ( field ) =>
fieldsWithBulkEditSupport.includes( field )
),
}
}
onChange={ onChange }
/>
</VStack>
Expand Down
3 changes: 2 additions & 1 deletion packages/edit-site/src/components/post-fields/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import clsx from 'clsx';
*/
import { __, sprintf } from '@wordpress/i18n';
import { decodeEntities } from '@wordpress/html-entities';
import { featuredImageField } from '@wordpress/fields';
import { featuredImageField, slugField } from '@wordpress/fields';
import {
createInterpolateElement,
useMemo,
Expand Down Expand Up @@ -320,6 +320,7 @@ function usePostFields( viewType ) {
return <time>{ getFormattedDate( item.date ) }</time>;
},
},
slugField,
Copy link
Member

@oandregal oandregal Sep 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's ok for this PR going this way. I'd like to offer some thoughts about next steps.

Initially, the wordpress/fields package exported every specific part of a field (edit, label, etc.) and we changed it to the whole field. Now that I see this, I think we have to offer an even higher-level API:

const postFields = usePostFields();
const pageFields = usePageFields();
// etc.

Essentially, the same we do for actions. In the future, we'll want 3rd parties to be able to register/unregister fields, so we can't use individual fields in every screen — otherwise, every screen will have to provide a filter/registry for that. Instead, we should offer an API that gives all the fields that are registered for a given post type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially, the wordpress/fields package exported every specific part of a field (edit, label, etc.) and we changed it to the whole field. Now that I see this, I think we have to offer an even higher-level API:

I see what you mean. I think that we will start to work on similar API when we will create a fields store in the @wordpress/fields package.

In the future, we'll want 3rd parties to be able to register/unregister fields, so we can't use individual fields in every screen — otherwise, every screen will have to provide a filter/registry for that

On this, we are full aligned! We will plan to work on this API.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
id: 'comment_status',
label: __( 'Discussion' ),
Expand Down
1 change: 1 addition & 0 deletions packages/edit-site/src/style.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@import "../../dataviews/src/style.scss";
@import "../../fields/src/styles.scss";
@import "../../fields/src/fields/featured-image/style.scss";

@import "./components/add-new-template/style.scss";
Expand Down
4 changes: 4 additions & 0 deletions packages/fields/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ Undocumented declaration.

Undocumented declaration.

### slugField
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we start adding some JSDoc comments to the exported entities so this is filled up with actual info? It's enough to add a JSDoc to this statement.


Undocumented declaration.

### titleField

Undocumented declaration.
Expand Down
1 change: 1 addition & 0 deletions packages/fields/src/fields/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { default as slugField } from './slug';
export { default as titleField } from './title';
export { default as orderField } from './order';
export { default as featuredImageField } from './featured-image';
23 changes: 23 additions & 0 deletions packages/fields/src/fields/slug/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* WordPress dependencies
*/
import type { Field } from '@wordpress/dataviews';

/**
* Internal dependencies
*/
import type { BasePost } from '../../types';
import { __ } from '@wordpress/i18n';
import SlugEdit from './slug-edit';
import SlugView from './slug-view';

const slugField: Field< BasePost > = {
id: 'slug',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What type would this field be? I feel we need to make progress on making type obligatory and every new field we introduce should have a type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say slug, what do you think? Mostly, because this field contains specific logic to preview the final link based on the slug setting:

const permalinkPrefix = prefix;
const permalinkSuffix = suffix;
const isEditable = PERMALINK_POSTNAME_REGEX.test( permalinkTemplate );
const originalSlug = useRef( slug );
const slugToDisplay = slug || originalSlug.current;
const permalink = isEditable
? `${ permalinkPrefix }${ slugToDisplay }${ permalinkSuffix }`
: safeDecodeURIComponent( data.link || '' );

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After further consideration, the type should always represent a generic type, not strictly tied to WordPress. In this case, it should be link. In the future, we might have:

  • A generic field for the link type.
  • Other fields that work with the link type but have additional functionalities that depend on specific settings like this one.

Would it make sense for each field to have a name?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on this discussion, I updated the type with 4efda74

type: 'text',
label: __( 'Slug' ),
getValue: ( { item } ) => item.slug,
Edit: SlugEdit,
render: SlugView,
};

export default slugField;
156 changes: 156 additions & 0 deletions packages/fields/src/fields/slug/slug-edit.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/**
* WordPress dependencies
*/
import {
Button,
ExternalLink,
__experimentalInputControl as InputControl,
__experimentalInputControlPrefixWrapper as InputControlPrefixWrapper,
__experimentalVStack as VStack,
} from '@wordpress/components';
import { copySmall } from '@wordpress/icons';
import { useCopyToClipboard, useInstanceId } from '@wordpress/compose';
import { useDispatch } from '@wordpress/data';
import { useCallback, useEffect, useRef } from '@wordpress/element';
import { store as noticesStore } from '@wordpress/notices';
import { safeDecodeURIComponent } from '@wordpress/url';
import type { DataFormControlProps } from '@wordpress/dataviews';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import type { BasePost } from '../../types';

const SlugEdit = ( {
field,
onChange,
data,
}: DataFormControlProps< BasePost > ) => {
const { id } = field;

const slug = field.getValue( { item: data } ) ?? '';
const permalinkTemplate = data.permalink_template || '';
const PERMALINK_POSTNAME_REGEX = /%(?:postname|pagename)%/;
const [ prefix, suffix ] = permalinkTemplate.split(
PERMALINK_POSTNAME_REGEX
);
const permalinkPrefix = prefix;
const permalinkSuffix = suffix;
const isEditable = PERMALINK_POSTNAME_REGEX.test( permalinkTemplate );
const originalSlug = useRef( slug );
const slugToDisplay = slug || originalSlug.current;
const permalink = isEditable
? `${ permalinkPrefix }${ slugToDisplay }${ permalinkSuffix }`
: safeDecodeURIComponent( data.link || '' );

useEffect( () => {
if ( slug && originalSlug.current === undefined ) {
originalSlug.current = slug;
}
}, [ slug ] );

const onChangeControl = useCallback(
( newValue?: string ) =>
onChange( {
[ id ]: newValue,
} ),
[ id, onChange ]
);

const { createNotice } = useDispatch( noticesStore );

const copyButtonRef = useCopyToClipboard( permalink, () => {
createNotice( 'info', __( 'Copied Permalink to clipboard.' ), {
isDismissible: true,
type: 'snackbar',
} );
} );

const postUrlSlugDescriptionId =
'editor-post-url__slug-description-' + useInstanceId( SlugEdit );

return (
<fieldset className="fields-controls__slug">
{ isEditable && (
<VStack>
<VStack spacing="0px">
<span>
{ __(
'Customize the last part of the Permalink.'
) }
</span>
<ExternalLink href="https://wordpress.org/documentation/article/page-post-settings-sidebar/#permalink">
{ __( 'Learn more' ) }
</ExternalLink>
</VStack>
<InputControl
__next40pxDefaultSize
prefix={
<InputControlPrefixWrapper>
/
</InputControlPrefixWrapper>
}
suffix={
<Button
__next40pxDefaultSize
icon={ copySmall }
ref={ copyButtonRef }
label={ __( 'Copy' ) }
/>
}
label={ __( 'Link' ) }
hideLabelFromVision
value={ slug }
autoComplete="off"
spellCheck="false"
type="text"
className="fields-controls__slug-input"
onChange={ ( newValue?: string ) => {
onChangeControl( newValue );
} }
onBlur={ () => {
if ( slug === '' ) {
onChangeControl( originalSlug.current );
}
} }
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>
</>
}
/>
</VStack>
) }
{ ! isEditable && (
<ExternalLink
className="fields-controls__slug-help"
href={ permalink }
>
{ permalink }
</ExternalLink>
) }
</fieldset>
);
};

export default SlugEdit;
26 changes: 26 additions & 0 deletions packages/fields/src/fields/slug/slug-view.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* WordPress dependencies
*/
import { useEffect, useRef } from '@wordpress/element';

/**
* Internal dependencies
*/
import type { BasePost } from '../../types';

const SlugView = ( { item }: { item: BasePost } ) => {
const slug = item.slug;
const originalSlug = useRef( slug );

useEffect( () => {
if ( slug && originalSlug.current === undefined ) {
originalSlug.current = slug;
}
}, [ slug ] );

const slugToDisplay = slug || originalSlug.current;

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

export default SlugView;
22 changes: 22 additions & 0 deletions packages/fields/src/fields/slug/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.fields-controls__slug {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense this classname? Or should we think a better one?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps I'd go with .field-slug as a prefix.

.fields-controls__slug-external-icon {
margin-left: 5ch;
}

.fields-controls__slug-input input.components-input-control__input {
padding-inline-start: 0 !important;
}

.fields-controls__slug-help-link {
word-break: break-word;
}

.fields-controls__slug-help {
display: flex;
flex-direction: column;

.fields-controls__slug-help-slug {
font-weight: 600;
}
}
}
1 change: 1 addition & 0 deletions packages/fields/src/styles.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import "./fields/slug/style.scss";
2 changes: 2 additions & 0 deletions packages/fields/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export interface BasePost extends CommonPost {
menu_order?: number;
ping_status?: 'open' | 'closed';
link?: string;
slug?: string;
permalink_template?: string;
}

export interface Template extends CommonPost {
Expand Down
Loading