-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add logic for basic (temporary) wp_template editing UI (#17625)
* Templates: Add logic for basic temporary editing UI. * Templates: Fix menu filter. * Post Slug: Follow class name convention.
- Loading branch information
1 parent
53eca43
commit 6cf43ac
Showing
10 changed files
with
270 additions
and
10 deletions.
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
17 changes: 17 additions & 0 deletions
17
packages/edit-post/src/components/sidebar/post-slug/index.js
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,17 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { PanelRow } from '@wordpress/components'; | ||
import { PostSlug as PostSlugForm, PostSlugCheck } from '@wordpress/editor'; | ||
|
||
export function PostSlug() { | ||
return ( | ||
<PostSlugCheck> | ||
<PanelRow> | ||
<PostSlugForm /> | ||
</PanelRow> | ||
</PostSlugCheck> | ||
); | ||
} | ||
|
||
export default PostSlug; |
4 changes: 4 additions & 0 deletions
4
packages/edit-post/src/components/sidebar/post-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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.editor-post-slug__input { | ||
margin: -5px 0; | ||
padding: 2px; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import PostTypeSupportCheck from '../post-type-support-check'; | ||
|
||
export default function PostSlugCheck( { children } ) { | ||
return ( | ||
<PostTypeSupportCheck supportKeys="slug">{ children }</PostTypeSupportCheck> | ||
); | ||
} |
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,85 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { withDispatch, withSelect } from '@wordpress/data'; | ||
import { Component } from '@wordpress/element'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { withInstanceId, compose } from '@wordpress/compose'; | ||
import { safeDecodeURIComponent } from '@wordpress/url'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import PostSlugCheck from './check'; | ||
import { cleanForSlug } from '../../utils/url'; | ||
|
||
export class PostSlug extends Component { | ||
constructor( { postSlug, postTitle, postID } ) { | ||
super( ...arguments ); | ||
|
||
this.state = { | ||
editedSlug: safeDecodeURIComponent( postSlug ) || cleanForSlug( postTitle ) || postID, | ||
}; | ||
|
||
this.setSlug = this.setSlug.bind( this ); | ||
} | ||
|
||
setSlug( event ) { | ||
const { postSlug, onUpdateSlug } = this.props; | ||
const { value } = event.target; | ||
|
||
const editedSlug = cleanForSlug( value ); | ||
|
||
if ( editedSlug === postSlug ) { | ||
return; | ||
} | ||
|
||
onUpdateSlug( editedSlug ); | ||
} | ||
|
||
render() { | ||
const { instanceId } = this.props; | ||
const { editedSlug } = this.state; | ||
|
||
const inputId = 'editor-post-slug-' + instanceId; | ||
|
||
return ( | ||
<PostSlugCheck> | ||
<label htmlFor={ inputId }>{ __( 'Slug' ) }</label> | ||
<input | ||
type="text" | ||
id={ inputId } | ||
value={ editedSlug } | ||
onChange={ ( event ) => this.setState( { editedSlug: event.target.value } ) } | ||
onBlur={ this.setSlug } | ||
className="editor-post-slug__input" | ||
/> | ||
</PostSlugCheck> | ||
); | ||
} | ||
} | ||
|
||
export default compose( [ | ||
withSelect( ( select ) => { | ||
const { | ||
getCurrentPost, | ||
getEditedPostAttribute, | ||
} = select( 'core/editor' ); | ||
|
||
const { id } = getCurrentPost(); | ||
return { | ||
postSlug: getEditedPostAttribute( 'slug' ), | ||
postTitle: getEditedPostAttribute( 'title' ), | ||
postID: id, | ||
}; | ||
} ), | ||
withDispatch( ( dispatch ) => { | ||
const { editPost } = dispatch( 'core/editor' ); | ||
return { | ||
onUpdateSlug( slug ) { | ||
editPost( { slug } ); | ||
}, | ||
}; | ||
} ), | ||
withInstanceId, | ||
] )( PostSlug ); |
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,21 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { shallow } from 'enzyme'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import PostSlugCheck from '../check'; | ||
|
||
describe( 'PostSlugCheck', () => { | ||
it( 'should render control', () => { | ||
const wrapper = shallow( | ||
<PostSlugCheck> | ||
slug | ||
</PostSlugCheck> | ||
); | ||
|
||
expect( wrapper.type() ).not.toBe( null ); | ||
} ); | ||
} ); |
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,45 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { shallow } from 'enzyme'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { PostSlug } from '../'; | ||
|
||
describe( 'PostSlug', () => { | ||
describe( '#render()', () => { | ||
it( 'should update internal slug', () => { | ||
const wrapper = shallow( | ||
<PostSlug | ||
postSlug="index" /> | ||
); | ||
|
||
wrapper.find( 'input' ).simulate( 'change', { | ||
target: { | ||
value: 'single-post', | ||
}, | ||
} ); | ||
|
||
expect( wrapper.state().editedSlug ).toEqual( 'single-post' ); | ||
} ); | ||
|
||
it( 'should update slug', () => { | ||
const onUpdateSlug = jest.fn(); | ||
const wrapper = shallow( | ||
<PostSlug | ||
postSlug="index" | ||
onUpdateSlug={ onUpdateSlug } /> | ||
); | ||
|
||
wrapper.find( 'input' ).simulate( 'blur', { | ||
target: { | ||
value: 'single-post', | ||
}, | ||
} ); | ||
|
||
expect( onUpdateSlug ).toHaveBeenCalledWith( 'single-post' ); | ||
} ); | ||
} ); | ||
} ); |