From e9ab901757a9f519d0d65c005acc494a4579bb71 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Tue, 15 Jun 2021 15:47:47 +0100 Subject: [PATCH] Fix Post Content block regression (#32693) --- .../block-library/src/post-content/edit.js | 12 ++--- .../block-library/src/post-excerpt/edit.js | 7 +-- packages/block-library/src/post-title/edit.js | 7 +-- packages/block-library/src/utils/hooks.js | 12 ++--- packages/core-data/README.md | 1 - packages/core-data/src/entities.js | 1 + packages/core-data/src/resolvers.js | 22 ++++---- packages/core-data/src/selectors.js | 17 +++--- packages/core-data/src/test/selectors.js | 52 +++++-------------- .../specs/performance/site-editor.test.js | 3 ++ 10 files changed, 48 insertions(+), 86 deletions(-) diff --git a/packages/block-library/src/post-content/edit.js b/packages/block-library/src/post-content/edit.js index 4d1ae859d6d5f5..9283b120057b36 100644 --- a/packages/block-library/src/post-content/edit.js +++ b/packages/block-library/src/post-content/edit.js @@ -38,7 +38,8 @@ function ReadOnlyContent( { userCanEdit, postType, postId } ) { ); } -function EditableContent( { layout, postType, postId } ) { +function EditableContent( { layout, context = {} } ) { + const { postType, postId } = context; const themeSupportsLayout = useSelect( ( select ) => { const { getSettings } = select( blockEditorStore ); return getSettings()?.supportsLayout; @@ -65,6 +66,7 @@ function EditableContent( { layout, postType, postId } ) { postType, { id: postId } ); + const props = useInnerBlocksProps( useBlockProps( { className: 'entry-content' } ), { @@ -80,13 +82,9 @@ function EditableContent( { layout, postType, postId } ) { function Content( props ) { const { context: { queryId, postType, postId } = {} } = props; const isDescendentOfQueryLoop = !! queryId; - const userCanEdit = useCanEditEntity( - 'root', - 'postType', - postType, - postId - ); + const userCanEdit = useCanEditEntity( 'postType', postType, postId ); const isEditable = userCanEdit && ! isDescendentOfQueryLoop; + return isEditable ? ( ) : ( diff --git a/packages/block-library/src/post-excerpt/edit.js b/packages/block-library/src/post-excerpt/edit.js index 0d7648903495ea..0df53eb1879235 100644 --- a/packages/block-library/src/post-excerpt/edit.js +++ b/packages/block-library/src/post-excerpt/edit.js @@ -52,12 +52,7 @@ export default function PostExcerptEditor( { context: { postId, postType, queryId }, } ) { const isDescendentOfQueryLoop = !! queryId; - const userCanEdit = useCanEditEntity( - 'root', - 'postType', - postType, - postId - ); + const userCanEdit = useCanEditEntity( 'postType', postType, postId ); const isEditable = userCanEdit && ! isDescendentOfQueryLoop; const [ rawExcerpt, diff --git a/packages/block-library/src/post-title/edit.js b/packages/block-library/src/post-title/edit.js index 77f5dbbd64f2d4..259f12b19632bb 100644 --- a/packages/block-library/src/post-title/edit.js +++ b/packages/block-library/src/post-title/edit.js @@ -31,12 +31,7 @@ export default function PostTitleEdit( { } ) { const TagName = 0 === level ? 'p' : 'h' + level; const isDescendentOfQueryLoop = !! queryId; - const userCanEdit = useCanEditEntity( - 'root', - 'postType', - postType, - postId - ); + const userCanEdit = useCanEditEntity( 'postType', postType, postId ); const [ rawTitle = '', setTitle, fullTitle ] = useEntityProp( 'postType', postType, diff --git a/packages/block-library/src/utils/hooks.js b/packages/block-library/src/utils/hooks.js index 1a71c4b0e3f7be..4b64c529299467 100644 --- a/packages/block-library/src/utils/hooks.js +++ b/packages/block-library/src/utils/hooks.js @@ -9,19 +9,13 @@ import { store as coreStore } from '@wordpress/core-data'; * * @param {string} kind Entity kind. * @param {string} name Entity name. - * @param {number} key Record's key. * @param {string} recordId Record's id. */ -export function useCanEditEntity( kind, name, key, recordId ) { +export function useCanEditEntity( kind, name, recordId ) { return useSelect( ( select ) => - select( coreStore ).canUserEditEntityRecord( - kind, - name, - key, - recordId - ), - [ kind, name, key, recordId ] + select( coreStore ).canUserEditEntityRecord( kind, name, recordId ), + [ kind, name, recordId ] ); } diff --git a/packages/core-data/README.md b/packages/core-data/README.md index 2e8e58fee25952..3c5d3ff07a6b9a 100644 --- a/packages/core-data/README.md +++ b/packages/core-data/README.md @@ -282,7 +282,6 @@ _Parameters_ - _state_ `Object`: Data state. - _kind_ `string`: Entity kind. - _name_ `string`: Entity name. -- _key_ `number`: Record's key. - _recordId_ `string`: Record's id. _Returns_ diff --git a/packages/core-data/src/entities.js b/packages/core-data/src/entities.js index 741e6696e3ed0e..ccae001bc0ed62 100644 --- a/packages/core-data/src/entities.js +++ b/packages/core-data/src/entities.js @@ -189,6 +189,7 @@ function* loadPostTypeEntities() { record?.title || ( isTemplate ? startCase( record.slug ) : String( record.id ) ), __unstablePrePersist: isTemplate ? undefined : prePersistPostType, + __unstable_rest_base: postType.rest_base, }; } ); } diff --git a/packages/core-data/src/resolvers.js b/packages/core-data/src/resolvers.js index be0f9b9bd2cf1e..a0f31c87ef1841 100644 --- a/packages/core-data/src/resolvers.js +++ b/packages/core-data/src/resolvers.js @@ -347,20 +347,18 @@ export function* canUser( action, resource, id ) { * Checks whether the current user can perform the given action on the given * REST resource. * - * @param {string} kind Entity kind. - * @param {string} name Entity name. - * @param {number} key Record's key. + * @param {string} kind Entity kind. + * @param {string} name Entity name. * @param {string} recordId Record's id. */ -export function* canUserEditEntityRecord( kind, name, key, recordId ) { - const entity = yield controls.select( - coreStoreName, - 'getEntityRecord', - kind, - name, - key - ); - const resource = entity?.rest_base || ''; +export function* canUserEditEntityRecord( kind, name, recordId ) { + const entities = yield getKindEntities( kind ); + const entity = find( entities, { kind, name } ); + if ( ! entity ) { + return; + } + + const resource = entity.__unstable_rest_base; yield canUser( 'update', resource, recordId ); } diff --git a/packages/core-data/src/selectors.js b/packages/core-data/src/selectors.js index 67672e6a267bed..c4e03392cce2c4 100644 --- a/packages/core-data/src/selectors.js +++ b/packages/core-data/src/selectors.js @@ -646,17 +646,20 @@ export function canUser( state, action, resource, id ) { * * https://developer.wordpress.org/rest-api/reference/ * - * @param {Object} state Data state. - * @param {string} kind Entity kind. - * @param {string} name Entity name. - * @param {number} key Record's key. + * @param {Object} state Data state. + * @param {string} kind Entity kind. + * @param {string} name Entity name. * @param {string} recordId Record's id. * @return {boolean|undefined} Whether or not the user can edit, * or `undefined` if the OPTIONS request is still being made. */ -export function canUserEditEntityRecord( state, kind, name, key, recordId ) { - const entity = getEntityRecord( state, kind, name, key ); - const resource = entity?.rest_base || ''; +export function canUserEditEntityRecord( state, kind, name, recordId ) { + const entity = getEntity( state, kind, name ); + if ( ! entity ) { + return false; + } + const resource = entity.__unstable_rest_base; + return canUser( state, 'update', resource, recordId ); } diff --git a/packages/core-data/src/test/selectors.js b/packages/core-data/src/test/selectors.js index afc8448d39362b..cce4293885f32d 100644 --- a/packages/core-data/src/test/selectors.js +++ b/packages/core-data/src/test/selectors.js @@ -446,63 +446,39 @@ describe( 'canUser', () => { } ); describe( 'canUserEditEntityRecord', () => { - it( 'returns undefined by default', () => { + it( 'returns false by default', () => { const state = deepFreeze( { userPermissions: {}, entities: { data: {} }, } ); - expect( - canUserEditEntityRecord( state, 'root', 'postType', 'post' ) - ).toBe( undefined ); + expect( canUserEditEntityRecord( state, 'postType', 'post' ) ).toBe( + false + ); } ); it( 'returns whether the user can edit', () => { const state = deepFreeze( { userPermissions: { 'create/posts': false, - 'update/posts': true, + 'update/posts/1': true, }, entities: { - data: { - root: { - postType: { - queriedData: { - items: { - post: { slug: 'post', rest_base: 'posts' }, - }, - itemIsComplete: { - post: true, - }, - queries: {}, - }, - }, + config: [ + { + kind: 'postType', + name: 'post', + __unstable_rest_base: 'posts', }, - }, - }, - } ); - expect( - canUserEditEntityRecord( state, 'root', 'postType', 'post' ) - ).toBe( true ); - } ); - - it( 'returns whether whether the user can edit a given resource', () => { - const state = deepFreeze( { - userPermissions: { - 'create/posts': false, - 'update/pages': false, - 'update/pages/2010': true, - 'update/posts/2010': false, - }, - entities: { + ], data: { root: { postType: { queriedData: { items: { - page: { slug: 'page', rest_base: 'pages' }, + post: { slug: 'post', __unstable: 'posts' }, }, itemIsComplete: { - page: true, + post: true, }, queries: {}, }, @@ -512,7 +488,7 @@ describe( 'canUserEditEntityRecord', () => { }, } ); expect( - canUserEditEntityRecord( state, 'root', 'postType', 'page', 2010 ) + canUserEditEntityRecord( state, 'postType', 'post', '1' ) ).toBe( true ); } ); } ); diff --git a/packages/e2e-tests/specs/performance/site-editor.test.js b/packages/e2e-tests/specs/performance/site-editor.test.js index b13c9c6f8e1db3..21451db3961879 100644 --- a/packages/e2e-tests/specs/performance/site-editor.test.js +++ b/packages/e2e-tests/specs/performance/site-editor.test.js @@ -88,6 +88,9 @@ describe( 'Site Editor Performance', () => { } // Measuring typing performance inside the post content. + await canvas().waitForSelector( + '[data-type="core/post-content"] [data-type="core/paragraph"]' + ); await canvas().click( '[data-type="core/post-content"] [data-type="core/paragraph"]' );