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

Add permalinks to the document sidebar #11874

Merged
merged 4 commits into from
Nov 15, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
149 changes: 149 additions & 0 deletions packages/edit-post/src/components/sidebar/post-link/index.js
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' ) }
Copy link
Member

@Soean Soean Nov 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only show it to user roles who are able to change the 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 packages/edit-post/src/components/sidebar/post-link/style.scss
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import LastRevision from '../last-revision';
import PostTaxonomies from '../post-taxonomies';
import FeaturedImage from '../featured-image';
import PostExcerpt from '../post-excerpt';
import PostLink from '../post-link';
import DiscussionPanel from '../discussion-panel';
import PageAttributes from '../page-attributes';
import MetaBoxes from '../../meta-boxes';
Expand All @@ -33,6 +34,7 @@ const SettingsSidebar = ( { sidebarName } ) => (
<PostStatus />
<LastRevision />
<PostTaxonomies />
<PostLink />
Soean marked this conversation as resolved.
Show resolved Hide resolved
<FeaturedImage />
<PostExcerpt />
<DiscussionPanel />
Expand Down
1 change: 1 addition & 0 deletions packages/edit-post/src/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
@import "./components/sidebar/style.scss";
@import "./components/sidebar/last-revision/style.scss";
@import "./components/sidebar/post-author/style.scss";
@import "./components/sidebar/post-link/style.scss";
@import "./components/sidebar/post-schedule/style.scss";
@import "./components/sidebar/post-status/style.scss";
@import "./components/sidebar/post-visibility/style.scss";
Expand Down