-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add permalinks to the document sidebar (#11874)
## Description Closes: #11858 This PR adds a permalink panel to the document sidebar, trying to follow the design specified in #11858. This PR is the first iteration so that we can iterate on the design in parallel. One missing nice feature is the ability to auto refresh the permalink settings (prefix/suffix) if the settings are changed in another tab, I'm not sure if this feature is a blocker and I wanted to think a little bit better about this part. ## How has this been tested? Verify that if the post is new, the panel does not appear. Some behavior we have on the existing permalink edit UI (close to title). Verify that if the post does not contains editable permalinks (e.g.: Plain permalink setting) the input is not available and only the post link is shown. Verify that if the permalink is editable it is possible to edit the permalink without problems and after saving the post the permalink is correctly changed/saved. ## Screenshots <!-- if applicable --> <img width="281" alt="screenshot 2018-11-14 at 17 57 30" src="https://user-images.githubusercontent.com/11271197/48504970-ea847b00-e83d-11e8-8c19-484c7007b67c.png"> <img width="277" alt="screenshot 2018-11-14 at 18 19 20" src="https://user-images.githubusercontent.com/11271197/48504971-ea847b00-e83d-11e8-87f5-37d62da62203.png"> <img width="285" alt="screenshot 2018-11-14 at 18 45 37" src="https://user-images.githubusercontent.com/11271197/48504972-eb1d1180-e83d-11e8-82fa-087666d8b6c6.png">
- Loading branch information
1 parent
e2d08a4
commit b1e11e5
Showing
4 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
149 changes: 149 additions & 0 deletions
149
packages/edit-post/src/components/sidebar/post-link/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,149 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { get } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Fragment } from '@wordpress/element'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { PanelBody, TextControl } from '@wordpress/components'; | ||
import { withSelect, withDispatch } from '@wordpress/data'; | ||
import { compose, ifCondition, withState } from '@wordpress/compose'; | ||
import { addQueryArgs } from '@wordpress/url'; | ||
|
||
/** | ||
* Module Constants | ||
*/ | ||
const PANEL_NAME = 'post-link'; | ||
|
||
function PostLink( { | ||
isOpened, | ||
onTogglePanel, | ||
isEditable, | ||
postLink, | ||
permalinkParts, | ||
editPermalink, | ||
forceEmptyField, | ||
setState, | ||
} ) { | ||
const { prefix, postName, suffix } = permalinkParts; | ||
let prefixElement, postNameElement, suffixElement; | ||
if ( isEditable ) { | ||
prefixElement = prefix && ( | ||
<span className="edit-post-post-link__link-prefix">{ prefix }</span> | ||
); | ||
postNameElement = postName && ( | ||
<span className="edit-post-post-link__link-post-name">{ postName }</span> | ||
); | ||
suffixElement = suffix && ( | ||
<span className="edit-post-post-link__link-suffix">{ suffix }</span> | ||
); | ||
} | ||
|
||
return ( | ||
<PanelBody | ||
title={ __( 'Permalink' ) } | ||
opened={ isOpened } | ||
onToggle={ onTogglePanel } | ||
> | ||
{ isEditable && ( | ||
<TextControl | ||
label={ __( 'URL' ) } | ||
value={ forceEmptyField ? '' : postName } | ||
onChange={ ( newValue ) => { | ||
editPermalink( newValue ); | ||
// When we delete the field the permalink gets | ||
// reverted to the original value. | ||
// The forceEmptyField logic allows the user to have | ||
// the field temporarily empty while typing. | ||
if ( ! newValue ) { | ||
if ( ! forceEmptyField ) { | ||
setState( { | ||
forceEmptyField: true, | ||
} ); | ||
} | ||
return; | ||
} | ||
if ( forceEmptyField ) { | ||
setState( { | ||
forceEmptyField: false, | ||
} ); | ||
} | ||
} } | ||
/> | ||
) } | ||
<p className="edit-post-post-link__preview-label"> | ||
{ __( 'Preview' ) } | ||
</p> | ||
<a | ||
className="edit-post-post-link__link" | ||
href={ postLink } | ||
target="_blank" | ||
> | ||
{ isEditable ? | ||
( <Fragment> | ||
{ prefixElement }{ postNameElement }{ suffixElement } | ||
</Fragment> ) : | ||
postLink | ||
} | ||
</a> | ||
<a | ||
className="edit-post-post-link__permalink-settings" | ||
href={ addQueryArgs( 'options-permalink.php' ) } | ||
target="_blank" | ||
> | ||
{ __( 'Permalink Settings' ) } | ||
</a> | ||
</PanelBody> | ||
); | ||
} | ||
|
||
export default compose( [ | ||
withSelect( ( select ) => { | ||
const { | ||
isEditedPostNew, | ||
isPermalinkEditable, | ||
getCurrentPost, | ||
isCurrentPostPublished, | ||
getPermalinkParts, | ||
getEditedPostAttribute, | ||
} = select( 'core/editor' ); | ||
const { | ||
isEditorPanelOpened, | ||
} = select( 'core/edit-post' ); | ||
const { | ||
getPostType, | ||
} = select( 'core' ); | ||
|
||
const { link } = getCurrentPost(); | ||
const postTypeName = getEditedPostAttribute( 'type' ); | ||
const postType = getPostType( postTypeName ); | ||
return { | ||
isNew: isEditedPostNew(), | ||
postLink: link, | ||
isEditable: isPermalinkEditable(), | ||
isPublished: isCurrentPostPublished(), | ||
isOpened: isEditorPanelOpened( PANEL_NAME ), | ||
permalinkParts: getPermalinkParts(), | ||
isViewable: get( postType, [ 'viewable' ], false ), | ||
}; | ||
} ), | ||
ifCondition( ( { isNew, postLink, isViewable } ) => { | ||
return ! isNew && postLink && isViewable; | ||
} ), | ||
withDispatch( ( dispatch ) => { | ||
const { toggleEditorPanelOpened } = dispatch( 'core/edit-post' ); | ||
const { editPost } = dispatch( 'core/editor' ); | ||
return { | ||
onTogglePanel: () => toggleEditorPanelOpened( PANEL_NAME ), | ||
editPermalink: ( newSlug ) => { | ||
editPost( { slug: newSlug } ); | ||
}, | ||
}; | ||
} ), | ||
withState( { | ||
forceEmptyField: false, | ||
} ), | ||
] )( PostLink ); |
21 changes: 21 additions & 0 deletions
21
packages/edit-post/src/components/sidebar/post-link/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,21 @@ | ||
.edit-post-post-link__link { | ||
font-weight: 600; | ||
} | ||
|
||
.edit-post-post-link__link-post-name { | ||
font-weight: 800; | ||
} | ||
|
||
.edit-post-post-link__preview-label { | ||
margin: 0; | ||
} | ||
|
||
.edit-post-post-link__link { | ||
word-wrap: break-word; | ||
} | ||
|
||
.edit-post-post-link__permalink-settings { | ||
margin-top: 1em; | ||
font-weight: 500; | ||
display: block; | ||
} |
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