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

Expose getEditedPostAttribute selector #5003

Merged
merged 10 commits into from
Feb 13, 2018
4 changes: 0 additions & 4 deletions data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,6 @@ export const query = ( mapSelectorsToProps ) => ( WrappedComponent ) => {
};

return connectWithStore( ( state, ownProps ) => {
const select = ( key, selectorName, ...args ) => {
Copy link
Member

Choose a reason for hiding this comment

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

Is this code removed purposefully? I mean @aduth removes it with #5007, too. However not sure if that should be a part of this PR.

return selectors[ key ][ selectorName ]( state[ key ], ...args );
};

return mapSelectorsToProps( select, ownProps );
} );
};
Expand Down
2 changes: 1 addition & 1 deletion edit-post/components/sidebar/featured-image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function FeaturedImage( { isOpened, postType, onTogglePanel } ) {
}

const applyQuery = query( ( select ) => ( {
postTypeSlug: select( 'core/editor', 'getCurrentPostType' ),
postTypeSlug: select( 'core/editor', 'getEditedPostAttribute', 'type' ),
} ) );

const applyConnect = connect(
Expand Down
2 changes: 1 addition & 1 deletion edit-post/components/sidebar/page-attributes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function PageAttributes( { isOpened, onTogglePanel, postType } ) {
}

const applyQuery = query( ( select ) => ( {
postTypeSlug: select( 'core/editor', 'getCurrentPostType' ),
postTypeSlug: select( 'core/editor', 'getEditedPostAttribute', 'type' ),
} ) );

const applyConnect = connect(
Expand Down
4 changes: 2 additions & 2 deletions editor/components/document-outline/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { __ } from '@wordpress/i18n';
*/
import './style.scss';
import DocumentOutlineItem from './item';
import { getBlocks, getEditedPostTitle } from '../../store/selectors';
import { getBlocks, getEditedPostAttribute } from '../../store/selectors';
import { selectBlock } from '../../store/actions';

/**
Expand Down Expand Up @@ -134,7 +134,7 @@ export const DocumentOutline = ( { blocks = [], title, onSelect } ) => {
export default connect(
( state ) => {
return {
title: getEditedPostTitle( state ),
title: getEditedPostAttribute( state, 'title' ),
blocks: getBlocks( state ),
};
},
Expand Down
4 changes: 2 additions & 2 deletions editor/components/post-title/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { withContext } from '@wordpress/components';
*/
import './style.scss';
import PostPermalink from '../post-permalink';
import { getEditedPostTitle } from '../../store/selectors';
import { getEditedPostAttribute } from '../../store/selectors';
import { insertBlock, editPost, clearSelectedBlock } from '../../store/actions';

/**
Expand Down Expand Up @@ -130,7 +130,7 @@ class PostTitle extends Component {

const applyConnect = connect(
( state ) => ( {
title: getEditedPostTitle( state ),
title: getEditedPostAttribute( state, 'title' ),
} ),
{
onEnterPress() {
Expand Down
2 changes: 1 addition & 1 deletion editor/hooks/copy-content/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function CopyContentButton( { editedPostContent, hasCopied, setState } ) {

const Enhanced = compose(
query( ( select ) => ( {
editedPostContent: select( 'core/editor', 'getEditedPostContent' ),
editedPostContent: select( 'core/editor', 'getEditedPostAttribute', 'content' ),
} ) ),
withState( { hasCopied: false } )
)( CopyContentButton );
Expand Down
10 changes: 2 additions & 8 deletions editor/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ import { registerReducer, registerSelectors, withRehydratation, loadAndPersist }
import reducer from './reducer';
import applyMiddlewares from './middlewares';
import {
getCurrentPostType,
getEditedPostContent,
getEditedPostTitle,
getEditedPostAttribute,
getSelectedBlockCount,
getCurrentPostSlug,
} from './selectors';

/**
Expand All @@ -28,11 +25,8 @@ const store = applyMiddlewares(
loadAndPersist( store, reducer, 'preferences', STORAGE_KEY );

registerSelectors( MODULE_KEY, {
getCurrentPostType,
getEditedPostContent,
getEditedPostTitle,
getEditedPostAttribute,
getSelectedBlockCount,
getCurrentPostSlug,
} );

export default store;
42 changes: 9 additions & 33 deletions editor/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,17 +169,6 @@ export function getCurrentPostType( state ) {
return state.currentPost.type;
}

/**
* Returns the slug of the post currently being edited.
*
* @param {Object} state Global application state.
*
* @return {string} Slug.
*/
export function getCurrentPostSlug( state ) {
return getEditedPostAttribute( state, 'slug' );
}

/**
* Returns the ID of the post currently being edited, or null if the post has
* not yet been saved.
Expand Down Expand Up @@ -239,6 +228,13 @@ export function getPostEdits( state ) {
*/
export function getEditedPostAttribute( state, attributeName ) {
const edits = getPostEdits( state );

// Special cases
switch ( attributeName ) {
case 'content':
return getEditedPostContent( state );
}

return edits[ attributeName ] === undefined ?
state.currentPost[ attributeName ] :
edits[ attributeName ];
Expand Down Expand Up @@ -301,7 +297,7 @@ export function isEditedPostPublishable( state ) {
*/
export function isEditedPostSaveable( state ) {
return (
!! getEditedPostTitle( state ) ||
!! getEditedPostAttribute( state, 'title' ) ||
!! getEditedPostExcerpt( state ) ||
!! getEditedPostContent( state )
);
Expand All @@ -323,26 +319,6 @@ export function isEditedPostBeingScheduled( state ) {
return moment( date ).isAfter( now );
}

/**
* Returns the raw title of the post being edited, preferring the unsaved value
* if different than the saved post.
*
* @param {Object} state Global application state.
*
* @return {string} Raw post title.
*/
export function getEditedPostTitle( state ) {
const editedTitle = getPostEdits( state ).title;
if ( editedTitle !== undefined ) {
return editedTitle;
}
const currentPost = getCurrentPost( state );
if ( currentPost.title && currentPost.title ) {
return currentPost.title;
}
return '';
}

/**
* Gets the document title to be used.
*
Expand All @@ -351,7 +327,7 @@ export function getEditedPostTitle( state ) {
* @return {string} Document title.
*/
export function getDocumentTitle( state ) {
let title = getEditedPostTitle( state );
let title = getEditedPostAttribute( state, 'title' );

if ( ! title.trim() ) {
title = isCleanNewPost( state ) ? __( 'New post' ) : __( '(Untitled)' );
Expand Down
71 changes: 34 additions & 37 deletions editor/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ const {
getCurrentPostLastRevisionId,
getCurrentPostRevisionsCount,
getCurrentPostType,
Copy link
Member

Choose a reason for hiding this comment

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

Should we also remove this one in the follow-up PR?

Copy link
Member

Choose a reason for hiding this comment

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

select( 'core/editor', 'getEditedPostAttribute', 'type' ) - we use that already in this PR.

getCurrentPostSlug,
getPostEdits,
getEditedPostTitle,
getDocumentTitle,
getEditedPostExcerpt,
getEditedPostVisibility,
Expand All @@ -42,6 +40,7 @@ const {
getBlockCount,
getSelectedBlock,
getBlockRootUID,
getEditedPostAttribute,
getMultiSelectedBlockUids,
getMultiSelectedBlocksStartUid,
getMultiSelectedBlocksEndUid,
Expand Down Expand Up @@ -374,13 +373,13 @@ describe( 'selectors', () => {
} );
} );

describe( 'getCurrentPostSlug', () => {
describe( 'getEditedPostAttribute', () => {
it( 'should return the current post\'s slug if no edits have been made', () => {
const state = {
currentPost: { slug: 'post slug' },
};

expect( getCurrentPostSlug( state ) ).toBe( 'post slug' );
expect( getEditedPostAttribute( state, 'slug' ) ).toBe( 'post slug' );
} );

it( 'should return the latest slug if edits have been made to the post', () => {
Expand All @@ -395,7 +394,37 @@ describe( 'selectors', () => {
},
};

expect( getCurrentPostSlug( state ) ).toBe( 'new slug' );
expect( getEditedPostAttribute( state, 'slug' ) ).toBe( 'new slug' );
} );

it( 'should return the post saved title if the title is not edited', () => {
const state = {
currentPost: {
title: 'sassel',
},
editor: {
present: {
edits: { status: 'private' },
},
},
};

expect( getEditedPostAttribute( state, 'title' ) ).toBe( 'sassel' );
} );

it( 'should return the edited title', () => {
const state = {
currentPost: {
title: 'sassel',
},
editor: {
present: {
edits: { title: 'youcha' },
},
},
};

expect( getEditedPostAttribute( state, 'title' ) ).toBe( 'youcha' );
} );
} );

Expand Down Expand Up @@ -469,38 +498,6 @@ describe( 'selectors', () => {
} );
} );

describe( 'getEditedPostTitle', () => {
it( 'should return the post saved title if the title is not edited', () => {
const state = {
currentPost: {
title: 'sassel',
},
editor: {
present: {
edits: { status: 'private' },
},
},
};

expect( getEditedPostTitle( state ) ).toBe( 'sassel' );
} );

it( 'should return the edited title', () => {
const state = {
currentPost: {
title: 'sassel',
},
editor: {
present: {
edits: { title: 'youcha' },
},
},
};

expect( getEditedPostTitle( state ) ).toBe( 'youcha' );
} );
} );

describe( 'getDocumentTitle', () => {
const metaBoxes = {};
it( 'should return current title unedited existing post', () => {
Expand Down