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

Data View - Page view: Add slug field control #65196

Open
wants to merge 16 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
6 changes: 6 additions & 0 deletions docs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -1697,6 +1697,12 @@
"markdown_source": "../packages/eslint-plugin/README.md",
"parent": "packages"
},
{
"title": "@wordpress/fields",
"slug": "packages-fields",
"markdown_source": "../packages/fields/README.md",
"parent": "packages"
},
{
"title": "@wordpress/format-library",
"slug": "packages-format-library",
Expand Down
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 = [
'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,
{
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 @@ -50,6 +50,10 @@ Undocumented declaration.

Undocumented declaration.

### slugField

Undocumented declaration.

### titleField

Undocumented declaration.
Expand Down
1 change: 0 additions & 1 deletion packages/fields/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"gutenberg",
"dataviews"
],
"private": true,
"homepage": "https://github.com/WordPress/gutenberg/tree/HEAD/packages/fields/README.md",
"repository": {
"type": "git",
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',
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