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

Open
wants to merge 17 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/edit-site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"@wordpress/editor": "file:../editor",
"@wordpress/element": "file:../element",
"@wordpress/escape-html": "file:../escape-html",
"@wordpress/fields": "file:../fields",
"@wordpress/hooks": "file:../hooks",
"@wordpress/html-entities": "file:../html-entities",
"@wordpress/i18n": "file:../i18n",
Expand Down
29 changes: 27 additions & 2 deletions packages/edit-site/src/components/post-edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,24 @@ function PostEditForm( { postType, postId } ) {
);
const form = {
type: 'panel',
fields: [ 'title', 'status', 'date', 'author', 'comment_status' ],
fields: [
'title',
'status',
'date',
'author',
'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 @@ -95,7 +111,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
2 changes: 2 additions & 0 deletions packages/edit-site/src/components/post-fields/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
import { __experimentalHStack as HStack, Icon } from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import { useEntityRecords, store as coreStore } from '@wordpress/core-data';
import { slugField } from '@wordpress/fields';

/**
* Internal dependencies
Expand Down Expand Up @@ -369,6 +370,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.

{
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 "./components/add-new-template/style.scss";
@import "./components/block-editor/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 @@ -66,6 +66,10 @@ Undocumented declaration.

Undocumented declaration.

### slugField

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,2 +1,3 @@
export { default as slugField } from './slug';
export { default as titleField } from './title';
export { default as orderField } from './order';
22 changes: 22 additions & 0 deletions packages/fields/src/fields/slug/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* 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?

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

export default slugField;
141 changes: 141 additions & 0 deletions packages/fields/src/fields/slug/slug-edit.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/**
* WordPress dependencies
*/
import {
Button,
ExternalLink,
__experimentalInputControl as InputControl,
__experimentalInputControlPrefixWrapper as InputControlPrefixWrapper,
__experimentalVStack as VStack,
} from '@wordpress/components';
import { copySmall } from '@wordpress/icons';
import { useCopyToClipboard } 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 URL to clipboard.' ), {
isDismissible: true,
type: 'snackbar',
} );
} );

return (
<fieldset className="fields-controls__slug">
{ isEditable && (
<VStack>
<VStack spacing="0px">
<span>
{ __( 'Customize the last part of the URL.' ) }
</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 );
}
} }
help={
<ExternalLink
className="fields-controls__slug-help"
href={ permalink }
>
<span>{ permalinkPrefix }</span>
<span className="fields-controls__slug-help-slug">
{ slugToDisplay }
</span>
<span className="fields-controls__slug-help-suffix">
{ permalinkSuffix }
</span>
</ExternalLink>
}
/>
</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;
18 changes: 18 additions & 0 deletions packages/fields/src/fields/slug/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.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?

.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 {
color: $gray-700;

.fields-controls__slug-help-slug,
.fields-controls__slug-help-suffix {
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