diff --git a/edit-post/components/sidebar/featured-image/index.js b/edit-post/components/sidebar/featured-image/index.js index 448c1eb9619b88..9263b8614aa3df 100644 --- a/edit-post/components/sidebar/featured-image/index.js +++ b/edit-post/components/sidebar/featured-image/index.js @@ -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( diff --git a/edit-post/components/sidebar/page-attributes/index.js b/edit-post/components/sidebar/page-attributes/index.js index cc5b4266703d9b..d20573e1441888 100644 --- a/edit-post/components/sidebar/page-attributes/index.js +++ b/edit-post/components/sidebar/page-attributes/index.js @@ -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( diff --git a/editor/components/document-outline/index.js b/editor/components/document-outline/index.js index d74cea7037e513..5f8c4561b646d2 100644 --- a/editor/components/document-outline/index.js +++ b/editor/components/document-outline/index.js @@ -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'; /** @@ -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 ), }; }, diff --git a/editor/components/post-title/index.js b/editor/components/post-title/index.js index 8413c7210bc9ce..aad6417c3f2158 100644 --- a/editor/components/post-title/index.js +++ b/editor/components/post-title/index.js @@ -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'; /** @@ -130,7 +130,7 @@ class PostTitle extends Component { const applyConnect = connect( ( state ) => ( { - title: getEditedPostTitle( state ), + title: getEditedPostAttribute( state, 'title' ), } ), { onEnterPress() { diff --git a/editor/hooks/copy-content/index.js b/editor/hooks/copy-content/index.js index 14ba4851508620..7a80c271a7ce99 100644 --- a/editor/hooks/copy-content/index.js +++ b/editor/hooks/copy-content/index.js @@ -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 ); diff --git a/editor/store/index.js b/editor/store/index.js index fd9bbd9f226542..efadc97b4311a6 100644 --- a/editor/store/index.js +++ b/editor/store/index.js @@ -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'; /** @@ -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; diff --git a/editor/store/selectors.js b/editor/store/selectors.js index 4b563f8bd6fcff..d198b8e39c5724 100644 --- a/editor/store/selectors.js +++ b/editor/store/selectors.js @@ -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. @@ -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 ]; @@ -301,7 +297,7 @@ export function isEditedPostPublishable( state ) { */ export function isEditedPostSaveable( state ) { return ( - !! getEditedPostTitle( state ) || + !! getEditedPostAttribute( state, 'title' ) || !! getEditedPostExcerpt( state ) || !! getEditedPostContent( state ) ); @@ -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. * @@ -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)' ); diff --git a/editor/store/test/selectors.js b/editor/store/test/selectors.js index c3aeed433d29a1..d8f1a54aadd8bf 100644 --- a/editor/store/test/selectors.js +++ b/editor/store/test/selectors.js @@ -26,9 +26,7 @@ const { getCurrentPostLastRevisionId, getCurrentPostRevisionsCount, getCurrentPostType, - getCurrentPostSlug, getPostEdits, - getEditedPostTitle, getDocumentTitle, getEditedPostExcerpt, getEditedPostVisibility, @@ -42,6 +40,7 @@ const { getBlockCount, getSelectedBlock, getBlockRootUID, + getEditedPostAttribute, getMultiSelectedBlockUids, getMultiSelectedBlocksStartUid, getMultiSelectedBlocksEndUid, @@ -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', () => { @@ -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' ); } ); } ); @@ -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', () => {