Skip to content

Commit

Permalink
Expose getEditedPostAttribute selector (#5003)
Browse files Browse the repository at this point in the history
* Exposed getEditedPostAttribute and removed unnecessary selectors

* Updated tests

* Removed getEditedPostContent from public api

* Fixed mistake in featured-image

* Implemented getEditedPostAttributes

* Implemented getEditedPostAttributes selector

* Fixed error when opening editor menu

* Removed getEditedPostSttributes selector

* Reverted changes in data/index.js
  • Loading branch information
Alexander Botteram authored and gziolo committed Feb 13, 2018
1 parent 15d5ff2 commit 7006ee1
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 85 deletions.
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 @@ -170,17 +170,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 @@ -240,6 +229,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 @@ -302,7 +298,7 @@ export function isEditedPostPublishable( state ) {
*/
export function isEditedPostSaveable( state ) {
return (
!! getEditedPostTitle( state ) ||
!! getEditedPostAttribute( state, 'title' ) ||
!! getEditedPostExcerpt( state ) ||
!! getEditedPostContent( state )
);
Expand All @@ -324,26 +320,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 @@ -352,7 +328,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,
getCurrentPostSlug,
getPostEdits,
getEditedPostTitle,
getDocumentTitle,
getEditedPostExcerpt,
getEditedPostVisibility,
Expand All @@ -42,6 +40,7 @@ const {
getBlockCount,
getSelectedBlock,
getBlockRootUID,
getEditedPostAttribute,
getMultiSelectedBlockUids,
getMultiSelectedBlocksStartUid,
getMultiSelectedBlocksEndUid,
Expand Down Expand Up @@ -375,13 +374,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 @@ -396,7 +395,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 @@ -470,38 +499,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

0 comments on commit 7006ee1

Please sign in to comment.