-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Changes from all commits
5682b92
395cc25
e820a6c
b00ec03
6e71a98
43b8a93
7e04069
e5fd361
9bfd08f
30def0b
0855668
bc60075
d95c35d
934bc1e
dc2e1b8
500b848
4d1f022
d7e0e6f
4efda74
f53239c
075a2b0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -320,6 +320,7 @@ function usePostFields( viewType ) { | |
return <time>{ getFormattedDate( item.date ) }</time>; | ||
}, | ||
}, | ||
slugField, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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
On this, we are full aligned! We will plan to work on this API. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've prepared a few small PRs to move the remaining field definition to
|
||
{ | ||
id: 'comment_status', | ||
label: __( 'Discussion' ), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,6 +70,10 @@ Undocumented declaration. | |
|
||
Undocumented declaration. | ||
|
||
### slugField | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
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'; |
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', | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would say gutenberg/packages/fields/src/fields/slug/slug-edit.tsx Lines 38 to 46 in 4d1f022
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Would it make sense for each field to have a There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; |
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; |
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; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
.fields-controls__slug { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps I'd go with |
||
.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; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
@import "./fields/slug/style.scss"; |
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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! 👍