From 72676908d38bb7fe74701573f8a35ef400340570 Mon Sep 17 00:00:00 2001 From: Alexander Botteram Date: Mon, 12 Feb 2018 11:41:43 +0100 Subject: [PATCH 1/9] Exposed getEditedPostAttribute and removed unnecessary selectors --- .../sidebar/featured-image/index.js | 2 +- .../sidebar/page-attributes/index.js | 2 +- editor/components/document-outline/index.js | 4 +- editor/components/post-title/index.js | 4 +- editor/store/index.js | 8 +--- editor/store/selectors.js | 37 ++----------------- 6 files changed, 11 insertions(+), 46 deletions(-) diff --git a/edit-post/components/sidebar/featured-image/index.js b/edit-post/components/sidebar/featured-image/index.js index 448c1eb9619b88..97e3f7f33391c9 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' ), } ) ); 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/store/index.js b/editor/store/index.js index fd9bbd9f226542..1f6b3cdd3d427b 100644 --- a/editor/store/index.js +++ b/editor/store/index.js @@ -9,11 +9,9 @@ import { registerReducer, registerSelectors, withRehydratation, loadAndPersist } import reducer from './reducer'; import applyMiddlewares from './middlewares'; import { - getCurrentPostType, + getEditedPostAttribute, getEditedPostContent, - getEditedPostTitle, getSelectedBlockCount, - getCurrentPostSlug, } from './selectors'; /** @@ -28,11 +26,9 @@ const store = applyMiddlewares( loadAndPersist( store, reducer, 'preferences', STORAGE_KEY ); registerSelectors( MODULE_KEY, { - getCurrentPostType, + getEditedPostAttribute, getEditedPostContent, - getEditedPostTitle, getSelectedBlockCount, - getCurrentPostSlug, } ); export default store; diff --git a/editor/store/selectors.js b/editor/store/selectors.js index 3362efb5b0a326..145cbdd9fe0a0c 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. @@ -301,9 +290,9 @@ export function isEditedPostPublishable( state ) { */ export function isEditedPostSaveable( state ) { return ( - !! getEditedPostTitle( state ) || + !! getEditedPostAttribute( state, 'title' ) || !! getEditedPostExcerpt( state ) || - !! getEditedPostContent( state ) + !! getEditedPostAttribute( state, 'content' ) ); } @@ -323,26 +312,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 +320,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)' ); From 70f5b912be299026227ce66459fa6a5b9e77c757 Mon Sep 17 00:00:00 2001 From: Alexander Botteram Date: Mon, 12 Feb 2018 11:55:08 +0100 Subject: [PATCH 2/9] Updated tests --- editor/store/selectors.js | 2 +- editor/store/test/selectors.js | 71 ++++++++++++++++------------------ 2 files changed, 35 insertions(+), 38 deletions(-) diff --git a/editor/store/selectors.js b/editor/store/selectors.js index 145cbdd9fe0a0c..92dc98b884bbda 100644 --- a/editor/store/selectors.js +++ b/editor/store/selectors.js @@ -292,7 +292,7 @@ export function isEditedPostSaveable( state ) { return ( !! getEditedPostAttribute( state, 'title' ) || !! getEditedPostExcerpt( state ) || - !! getEditedPostAttribute( state, 'content' ) + !! getEditedPostContent( state ) ); } 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', () => { From c2c559693f0f27d69afe5d9515eaaff35e881c06 Mon Sep 17 00:00:00 2001 From: Alexander Botteram Date: Mon, 12 Feb 2018 14:35:30 +0100 Subject: [PATCH 3/9] Removed getEditedPostContent from public api --- editor/store/index.js | 2 -- editor/store/selectors.js | 7 +++++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/editor/store/index.js b/editor/store/index.js index 1f6b3cdd3d427b..efadc97b4311a6 100644 --- a/editor/store/index.js +++ b/editor/store/index.js @@ -10,7 +10,6 @@ import reducer from './reducer'; import applyMiddlewares from './middlewares'; import { getEditedPostAttribute, - getEditedPostContent, getSelectedBlockCount, } from './selectors'; @@ -27,7 +26,6 @@ loadAndPersist( store, reducer, 'preferences', STORAGE_KEY ); registerSelectors( MODULE_KEY, { getEditedPostAttribute, - getEditedPostContent, getSelectedBlockCount, } ); diff --git a/editor/store/selectors.js b/editor/store/selectors.js index 92dc98b884bbda..71d2420c23bf48 100644 --- a/editor/store/selectors.js +++ b/editor/store/selectors.js @@ -228,6 +228,13 @@ export function getPostEdits( state ) { */ export function getEditedPostAttribute( state, attributeName ) { const edits = getPostEdits( state ); + + // Special cases + switch ( attributeName ) { + case 'content': + return getEditedPostContent(); + } + return edits[ attributeName ] === undefined ? state.currentPost[ attributeName ] : edits[ attributeName ]; From d407b60d55a41011a62a58a7bb0d92bf44728f2c Mon Sep 17 00:00:00 2001 From: Alexander Botteram Date: Mon, 12 Feb 2018 15:17:35 +0100 Subject: [PATCH 4/9] Fixed mistake in featured-image --- edit-post/components/sidebar/featured-image/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/edit-post/components/sidebar/featured-image/index.js b/edit-post/components/sidebar/featured-image/index.js index 97e3f7f33391c9..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', 'getEditedPostAttribute' ), + postTypeSlug: select( 'core/editor', 'getEditedPostAttribute', 'type' ), } ) ); const applyConnect = connect( From a81ad0162b7131e90dbd2e48f932dab12bc1411d Mon Sep 17 00:00:00 2001 From: Alexander Botteram Date: Mon, 12 Feb 2018 15:24:48 +0100 Subject: [PATCH 5/9] Implemented getEditedPostAttributes --- editor/store/index.js | 2 ++ editor/store/selectors.js | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/editor/store/index.js b/editor/store/index.js index efadc97b4311a6..1ec30886c82345 100644 --- a/editor/store/index.js +++ b/editor/store/index.js @@ -10,6 +10,7 @@ import reducer from './reducer'; import applyMiddlewares from './middlewares'; import { getEditedPostAttribute, + getEditedPostAttributes, getSelectedBlockCount, } from './selectors'; @@ -26,6 +27,7 @@ loadAndPersist( store, reducer, 'preferences', STORAGE_KEY ); registerSelectors( MODULE_KEY, { getEditedPostAttribute, + getEditedPostAttributes, getSelectedBlockCount, } ); diff --git a/editor/store/selectors.js b/editor/store/selectors.js index 84e4f0f16e515b..a2f3ed2b832c92 100644 --- a/editor/store/selectors.js +++ b/editor/store/selectors.js @@ -240,6 +240,24 @@ export function getEditedPostAttribute( state, attributeName ) { edits[ attributeName ]; } +/** + * Returns a single attribute of the post being edited, preferring the unsaved + * edit if one exists, but falling back to the attribute for the last known + * saved state of the post. + * + * @param {Object} state Global application state. + * @param {string[]} attributeNames Post attribute name. + * + * @return {Object} Post attributes mapped to object by name. + */ +export function getEditedPostAttributes( state, attributeNames ) { + const attributes = {}; + attributeNames.map( attributeName => { + attributes[ attributeName ] = getEditedPostAttribute( state, attributeName ); + } ); + return attributes; +} + /** * Returns the current visibility of the post being edited, preferring the * unsaved value if different than the saved post. The return value is one of From bc57afe72411057d92083d7ad27b4ff320478c74 Mon Sep 17 00:00:00 2001 From: Alexander Botteram Date: Mon, 12 Feb 2018 16:05:04 +0100 Subject: [PATCH 6/9] Implemented getEditedPostAttributes selector --- editor/store/selectors.js | 7 ++++-- editor/store/test/selectors.js | 42 ++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/editor/store/selectors.js b/editor/store/selectors.js index a2f3ed2b832c92..afce158f9cb60c 100644 --- a/editor/store/selectors.js +++ b/editor/store/selectors.js @@ -252,8 +252,11 @@ export function getEditedPostAttribute( state, attributeName ) { */ export function getEditedPostAttributes( state, attributeNames ) { const attributes = {}; - attributeNames.map( attributeName => { - attributes[ attributeName ] = getEditedPostAttribute( state, attributeName ); + map( attributeNames, attributeName => { + const value = getEditedPostAttribute( state, attributeName ); + if ( value ) { + attributes[ attributeName ] = getEditedPostAttribute( state, attributeName ); + } } ); return attributes; } diff --git a/editor/store/test/selectors.js b/editor/store/test/selectors.js index d8f1a54aadd8bf..2b3c43610950b4 100644 --- a/editor/store/test/selectors.js +++ b/editor/store/test/selectors.js @@ -41,6 +41,7 @@ const { getSelectedBlock, getBlockRootUID, getEditedPostAttribute, + getEditedPostAttributes, getMultiSelectedBlockUids, getMultiSelectedBlocksStartUid, getMultiSelectedBlocksEndUid, @@ -428,6 +429,47 @@ describe( 'selectors', () => { } ); } ); + describe( 'getEditedPostAttributes', () => { + it( 'should return the current post\'s slug and type if no edits have been made', () => { + const state = { + currentPost: { + slug: 'post slug', + type: 'post', + }, + }; + + const expected = { + slug: 'post slug', + type: 'post', + }; + + expect( getEditedPostAttributes( state, [ 'slug', 'type' ] ) ).toEqual( expected ); + } ); + + it( 'should return the current post\'s slug and type if only the slug is edited', () => { + const state = { + currentPost: { + slug: 'old slug', + type: 'post', + }, + editor: { + present: { + edits: { + slug: 'new slug', + }, + }, + }, + }; + + const expected = { + slug: 'new slug', + type: 'post', + }; + + expect( getEditedPostAttributes( state, [ 'slug', 'type' ] ) ).toEqual( expected ); + } ); + } ); + describe( 'getCurrentPostLastRevisionId', () => { it( 'should return null if the post has not yet been saved', () => { const state = { From 1f17b5b9053dbf53d4c1bcd3588c616142f57304 Mon Sep 17 00:00:00 2001 From: Alexander Botteram Date: Mon, 12 Feb 2018 16:23:21 +0100 Subject: [PATCH 7/9] Fixed error when opening editor menu --- data/index.js | 4 ---- editor/hooks/copy-content/index.js | 2 +- editor/store/selectors.js | 2 +- 3 files changed, 2 insertions(+), 6 deletions(-) diff --git a/data/index.js b/data/index.js index 676f5d7eaef4d9..89de303b67c298 100644 --- a/data/index.js +++ b/data/index.js @@ -100,10 +100,6 @@ export const query = ( mapSelectorsToProps ) => ( WrappedComponent ) => { }; return connectWithStore( ( state, ownProps ) => { - const select = ( key, selectorName, ...args ) => { - return selectors[ key ][ selectorName ]( state[ key ], ...args ); - }; - return mapSelectorsToProps( select, ownProps ); } ); }; 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/selectors.js b/editor/store/selectors.js index afce158f9cb60c..abbe6fed7738f3 100644 --- a/editor/store/selectors.js +++ b/editor/store/selectors.js @@ -232,7 +232,7 @@ export function getEditedPostAttribute( state, attributeName ) { // Special cases switch ( attributeName ) { case 'content': - return getEditedPostContent(); + return getEditedPostContent( state ); } return edits[ attributeName ] === undefined ? From bf8c6b68b534f8d298f7b2e0470ab711aba7d8e0 Mon Sep 17 00:00:00 2001 From: Alexander Botteram Date: Tue, 13 Feb 2018 09:47:11 +0100 Subject: [PATCH 8/9] Removed getEditedPostSttributes selector --- editor/store/index.js | 2 -- editor/store/selectors.js | 21 ----------------- editor/store/test/selectors.js | 42 ---------------------------------- 3 files changed, 65 deletions(-) diff --git a/editor/store/index.js b/editor/store/index.js index 1ec30886c82345..efadc97b4311a6 100644 --- a/editor/store/index.js +++ b/editor/store/index.js @@ -10,7 +10,6 @@ import reducer from './reducer'; import applyMiddlewares from './middlewares'; import { getEditedPostAttribute, - getEditedPostAttributes, getSelectedBlockCount, } from './selectors'; @@ -27,7 +26,6 @@ loadAndPersist( store, reducer, 'preferences', STORAGE_KEY ); registerSelectors( MODULE_KEY, { getEditedPostAttribute, - getEditedPostAttributes, getSelectedBlockCount, } ); diff --git a/editor/store/selectors.js b/editor/store/selectors.js index abbe6fed7738f3..d198b8e39c5724 100644 --- a/editor/store/selectors.js +++ b/editor/store/selectors.js @@ -240,27 +240,6 @@ export function getEditedPostAttribute( state, attributeName ) { edits[ attributeName ]; } -/** - * Returns a single attribute of the post being edited, preferring the unsaved - * edit if one exists, but falling back to the attribute for the last known - * saved state of the post. - * - * @param {Object} state Global application state. - * @param {string[]} attributeNames Post attribute name. - * - * @return {Object} Post attributes mapped to object by name. - */ -export function getEditedPostAttributes( state, attributeNames ) { - const attributes = {}; - map( attributeNames, attributeName => { - const value = getEditedPostAttribute( state, attributeName ); - if ( value ) { - attributes[ attributeName ] = getEditedPostAttribute( state, attributeName ); - } - } ); - return attributes; -} - /** * Returns the current visibility of the post being edited, preferring the * unsaved value if different than the saved post. The return value is one of diff --git a/editor/store/test/selectors.js b/editor/store/test/selectors.js index 2b3c43610950b4..d8f1a54aadd8bf 100644 --- a/editor/store/test/selectors.js +++ b/editor/store/test/selectors.js @@ -41,7 +41,6 @@ const { getSelectedBlock, getBlockRootUID, getEditedPostAttribute, - getEditedPostAttributes, getMultiSelectedBlockUids, getMultiSelectedBlocksStartUid, getMultiSelectedBlocksEndUid, @@ -429,47 +428,6 @@ describe( 'selectors', () => { } ); } ); - describe( 'getEditedPostAttributes', () => { - it( 'should return the current post\'s slug and type if no edits have been made', () => { - const state = { - currentPost: { - slug: 'post slug', - type: 'post', - }, - }; - - const expected = { - slug: 'post slug', - type: 'post', - }; - - expect( getEditedPostAttributes( state, [ 'slug', 'type' ] ) ).toEqual( expected ); - } ); - - it( 'should return the current post\'s slug and type if only the slug is edited', () => { - const state = { - currentPost: { - slug: 'old slug', - type: 'post', - }, - editor: { - present: { - edits: { - slug: 'new slug', - }, - }, - }, - }; - - const expected = { - slug: 'new slug', - type: 'post', - }; - - expect( getEditedPostAttributes( state, [ 'slug', 'type' ] ) ).toEqual( expected ); - } ); - } ); - describe( 'getCurrentPostLastRevisionId', () => { it( 'should return null if the post has not yet been saved', () => { const state = { From 7227f606fdc4e31c30c2ba2f90f66fc55504cbbc Mon Sep 17 00:00:00 2001 From: Alexander Botteram Date: Tue, 13 Feb 2018 14:32:53 +0100 Subject: [PATCH 9/9] Reverted changes in data/index.js --- data/index.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/data/index.js b/data/index.js index 89de303b67c298..676f5d7eaef4d9 100644 --- a/data/index.js +++ b/data/index.js @@ -100,6 +100,10 @@ export const query = ( mapSelectorsToProps ) => ( WrappedComponent ) => { }; return connectWithStore( ( state, ownProps ) => { + const select = ( key, selectorName, ...args ) => { + return selectors[ key ][ selectorName ]( state[ key ], ...args ); + }; + return mapSelectorsToProps( select, ownProps ); } ); };