Skip to content

Commit

Permalink
Add isRawAttribute to entity config (#34388)
Browse files Browse the repository at this point in the history
* Add rawBlockMarkupFields

* Add unit tests for getRawEntityRecord

* Rename rawBlockMarkupFields to rawAttributes

* Use POST_RAW_ATTRIBUTES instead of an inline list

* Rename rawBlockMarkupFields to isRawAttribute

* Add isRawAttribute selector

* Make isRawAttribute a utility function instead of a selector

* Remove isRawAttribute from saveEntityRecord – the logic is deceivingly similar, but is about including specific attributes regardless of their "raw-ness"

* Add rawAttributes to mobile postTypeEntities

* Lint

* Lint

* Lint
  • Loading branch information
adamziel authored Sep 3, 2021
1 parent c1b800a commit 4dafc47
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 9 deletions.
4 changes: 4 additions & 0 deletions packages/core-data/src/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import { STORE_NAME } from './name';

export const DEFAULT_ENTITY_KEY = 'id';

const POST_RAW_ATTRIBUTES = [ 'title', 'excerpt', 'content' ];

export const defaultEntities = [
{
label: __( 'Base' ),
Expand All @@ -41,6 +43,7 @@ export const defaultEntities = [
key: 'slug',
baseURL: '/wp/v2/types',
baseURLParams: { context: 'edit' },
rawAttributes: POST_RAW_ATTRIBUTES,
},
{
name: 'media',
Expand Down Expand Up @@ -184,6 +187,7 @@ function* loadPostTypeEntities() {
selection: true,
},
mergedEdits: { meta: true },
rawAttributes: POST_RAW_ATTRIBUTES,
getTitle: ( record ) =>
record?.title?.rendered ||
record?.title ||
Expand Down
22 changes: 13 additions & 9 deletions packages/core-data/src/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import deprecated from '@wordpress/deprecated';
import { STORE_NAME } from './name';
import { getQueriedItems } from './queried-data';
import { DEFAULT_ENTITY_KEY } from './entities';
import { getNormalizedCommaSeparable } from './utils';
import { getNormalizedCommaSeparable, isRawAttribute } from './utils';

/**
* Shared reference to an empty array for cases where it is important to avoid
Expand Down Expand Up @@ -205,14 +205,18 @@ export const getRawEntityRecord = createSelector(
return (
record &&
Object.keys( record ).reduce( ( accumulator, _key ) => {
// Because edits are the "raw" attribute values,
// we return those from record selectors to make rendering,
// comparisons, and joins with edits easier.
accumulator[ _key ] = get(
record[ _key ],
'raw',
record[ _key ]
);
if ( isRawAttribute( getEntity( state, kind, name ), _key ) ) {
// Because edits are the "raw" attribute values,
// we return those from record selectors to make rendering,
// comparisons, and joins with edits easier.
accumulator[ _key ] = get(
record[ _key ],
'raw',
record[ _key ]
);
} else {
accumulator[ _key ] = record[ _key ];
}
return accumulator;
}, {} )
);
Expand Down
71 changes: 71 additions & 0 deletions packages/core-data/src/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
__experimentalGetEntityRecordNoResolver,
hasEntityRecords,
getEntityRecords,
getRawEntityRecord,
__experimentalGetDirtyEntityRecords,
__experimentalGetEntitiesBeingSaved,
getEntityRecordNonTransientEdits,
Expand Down Expand Up @@ -204,6 +205,76 @@ describe( 'hasEntityRecords', () => {
} );
} );

describe( 'getRawEntityRecord', () => {
const data = {
someKind: {
someName: {
queriedData: {
items: {
default: {
post: {
title: {
raw: { html: '<h1>post</h1>' },
rendered:
'<div id="post"><h1>rendered post</h1></div>',
},
},
},
},
itemIsComplete: {
default: {
post: true,
},
},
queries: {},
},
},
},
};
it( 'should preserve the structure of `raw` field by default', () => {
const state = deepFreeze( {
entities: {
config: [
{
kind: 'someKind',
name: 'someName',
},
],
data: { ...data },
},
} );
expect(
getRawEntityRecord( state, 'someKind', 'someName', 'post' )
).toEqual( {
title: {
raw: { html: '<h1>post</h1>' },
rendered: '<div id="post"><h1>rendered post</h1></div>',
},
} );
} );
it( 'should flatten the structure of `raw` field for entities configured with rawAttributes', () => {
const state = deepFreeze( {
entities: {
config: [
{
kind: 'someKind',
name: 'someName',
rawAttributes: [ 'title' ],
},
],
data: { ...data },
},
} );
expect(
getRawEntityRecord( state, 'someKind', 'someName', 'post' )
).toEqual( {
title: {
html: '<h1>post</h1>',
},
} );
} );
} );

describe( 'getEntityRecords', () => {
it( 'should return null by default', () => {
const state = deepFreeze( {
Expand Down
1 change: 1 addition & 0 deletions packages/core-data/src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export { default as ifNotResolved } from './if-not-resolved';
export { default as onSubKey } from './on-sub-key';
export { default as replaceAction } from './replace-action';
export { default as withWeakMapCache } from './with-weak-map-cache';
export { default as isRawAttribute } from './is-raw-attribute';
11 changes: 11 additions & 0 deletions packages/core-data/src/utils/is-raw-attribute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Checks whether the attribute is a "raw" attribute or not.
*
* @param {Object} entity Entity data.
* @param {string} attribute Attribute name.
*
* @return {boolean} Is the attribute raw
*/
export default function isRawAttribute( entity, attribute ) {
return ( entity.rawAttributes || [] ).includes( attribute );
}
22 changes: 22 additions & 0 deletions packages/core-data/src/utils/test/is-raw-attribute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Internal dependencies
*/
import { isRawAttribute } from '../';

describe( 'isRawAttribute', () => {
it( 'should correctly assess that the attribute is not raw', () => {
const entity = {
kind: 'someKind',
name: 'someName',
};
expect( isRawAttribute( entity, 'title' ) ).toBe( false );
} );
it( 'should correctly assess that the attribute is raw', () => {
const entity = {
kind: 'someKind',
name: 'someName',
rawAttributes: [ 'title' ],
};
expect( isRawAttribute( entity, 'title' ) ).toBe( true );
} );
} );
1 change: 1 addition & 0 deletions packages/editor/src/components/provider/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const postTypeEntities = [
mergedEdits: {
meta: true,
},
rawAttributes: [ 'title', 'excerpt', 'content' ],
} ) );
import { EditorHelpTopics } from '@wordpress/editor';

Expand Down

0 comments on commit 4dafc47

Please sign in to comment.