-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
gigitux
wants to merge
16
commits into
WordPress:trunk
Choose a base branch
from
gigitux:add/link-control
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
5682b92
Page view: Add link field control
gigitux 395cc25
Add @wordpress/fields package
gigitux e820a6c
Merge branch 'add/@wordpress-fields' of github.com:gigitux/gutenberg …
gigitux b00ec03
remove not used references
gigitux 6e71a98
add marker to generate API documentation
gigitux 43b8a93
improve package description
gigitux 7e04069
not use lock/unlock api
gigitux e5fd361
remove apostrophe
gigitux 9bfd08f
fix build
gigitux 30def0b
Merge branch 'add/@wordpress-fields' of github.com:gigitux/gutenberg …
gigitux 0855668
move slug field control to @wordpress/fields package
gigitux bc60075
Merge branch 'trunk' of github.com:WordPress/gutenberg into add/link-…
gigitux d95c35d
Merge branch 'trunk' of github.com:WordPress/gutenberg into add/link-…
gigitux 934bc1e
improve definition
gigitux dc2e1b8
disable slug field control in bulk mode
gigitux 500b848
improve css classnames
gigitux File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
.fields-controls__slug { | ||
.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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
@import "./fields/slug/style.scss"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Does it make sense this classname? Or should we think a better one?