Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Framework: Deprecate uid in favor of clientId #7990

Merged
merged 1 commit into from
Jul 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions blocks/api/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,14 @@ export function createBlock( name, blockAttributes = {}, innerBlocks = [] ) {
return result;
}, {} );

// Blocks are stored with a unique ID, the assigned type name,
// the block attributes, and their inner blocks.
const clientId = uuid();

// Blocks are stored with a unique ID, the assigned type name, the block
// attributes, and their inner blocks.
return {
uid: uuid(),
clientId,
// TODO: Remove from block interface in 3.5 "UID" deprecation.
uid: clientId,
name,
isValid: true,
attributes,
Expand All @@ -74,8 +78,12 @@ export function createBlock( name, blockAttributes = {}, innerBlocks = [] ) {
* @return {Object} A cloned block.
*/
export function cloneBlock( block, mergeAttributes = {}, newInnerBlocks ) {
const clientId = uuid();

return {
...block,
clientId,
// TODO: Remove from block interface in 3.5 "UID" deprecation.
uid: uuid(),
attributes: {
...block.attributes,
Expand Down Expand Up @@ -365,8 +373,8 @@ export function switchToBlockType( blocks, name ) {
const transformedBlock = {
...result,
// The first transformed block whose type matches the "destination"
// type gets to keep the existing UID of the first block.
uid: index === firstSwitchedBlock ? firstBlock.uid : result.uid,
// type gets to keep the existing client ID of the first block.
clientId: index === firstSwitchedBlock ? firstBlock.clientId : result.clientId,
};

/**
Expand Down
42 changes: 28 additions & 14 deletions blocks/api/raw-handling/test/shortcode-converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ describe( 'segmentHTMLToShortcodeBlock', () => {
const expectedBlock = createBlock( 'core/shortcode', {
text: '[foo bar="apple"]',
} );
// uuid will always be random.
expectedBlock.uid = transformed[ 1 ].uid;
// clientId will always be random.
expectedBlock.clientId = transformed[ 1 ].clientId;
// TODO: Remove in 3.5 "UID" deprecation.
expectedBlock.uid = expectedBlock.clientId;
expect( transformed[ 1 ] ).toEqual( expectedBlock );
expect( transformed[ 2 ] ).toBe( `

Expand All @@ -45,16 +47,20 @@ describe( 'segmentHTMLToShortcodeBlock', () => {
const firstExpectedBlock = createBlock( 'core/shortcode', {
text: '[foo one]',
} );
// uid will always be random.
firstExpectedBlock.uid = transformed[ 1 ].uid;
// clientId will always be random.
firstExpectedBlock.clientId = transformed[ 1 ].clientId;
// TODO: Remove in 3.5 "UID" deprecation.
firstExpectedBlock.uid = firstExpectedBlock.clientId;
expect( transformed[ 1 ] ).toEqual( firstExpectedBlock );
expect( transformed[ 2 ] ).toEqual( `</p>
<p>` );
const secondExpectedBlock = createBlock( 'core/shortcode', {
text: '[foo two]',
} );
// uid will always be random.
secondExpectedBlock.uid = transformed[ 3 ].uid;
// clientId will always be random.
secondExpectedBlock.clientId = transformed[ 3 ].clientId;
// TODO: Remove in 3.5 "UID" deprecation.
secondExpectedBlock.uid = secondExpectedBlock.clientId;
expect( transformed[ 3 ] ).toEqual( secondExpectedBlock );
expect( transformed[ 4 ] ).toEqual( '</p>' );
expect( transformed ).toHaveLength( 5 );
Expand All @@ -71,32 +77,40 @@ describe( 'segmentHTMLToShortcodeBlock', () => {
const firstExpectedBlock = createBlock( 'core/shortcode', {
text: '[foo one]',
} );
// uid will always be random.
firstExpectedBlock.uid = transformed[ 1 ].uid;
// clientId will always be random.
firstExpectedBlock.clientId = transformed[ 1 ].clientId;
// TODO: Remove in 3.5 "UID" deprecation.
firstExpectedBlock.uid = firstExpectedBlock.clientId;
expect( transformed[ 1 ] ).toEqual( firstExpectedBlock );
expect( transformed[ 2 ] ).toEqual( `</p>
<p>` );
const secondExpectedBlock = createBlock( 'core/shortcode', {
text: '[foo two]',
} );
// uid will always be random.
secondExpectedBlock.uid = transformed[ 3 ].uid;
// clientId will always be random.
secondExpectedBlock.clientId = transformed[ 3 ].clientId;
// TODO: Remove in 3.5 "UID" deprecation.
secondExpectedBlock.uid = secondExpectedBlock.clientId;
expect( transformed[ 3 ] ).toEqual( secondExpectedBlock );
expect( transformed[ 4 ] ).toEqual( `</p>
<p>` );
const thirdExpectedBlock = createBlock( 'core/shortcode', {
text: '[foo three]',
} );
// uid will always be random.
thirdExpectedBlock.uid = transformed[ 5 ].uid;
// clientId will always be random.
thirdExpectedBlock.clientId = transformed[ 5 ].clientId;
// TODO: Remove in 3.5 "UID" deprecation.
thirdExpectedBlock.uid = thirdExpectedBlock.clientId;
expect( transformed[ 5 ] ).toEqual( thirdExpectedBlock );
expect( transformed[ 6 ] ).toEqual( `</p>
<p>` );
const fourthExpectedBlock = createBlock( 'core/shortcode', {
text: '[foo four]',
} );
// uid will always be random.
fourthExpectedBlock.uid = transformed[ 7 ].uid;
// clientId will always be random.
fourthExpectedBlock.clientId = transformed[ 7 ].clientId;
// TODO: Remove in 3.5 "UID" deprecation.
fourthExpectedBlock.uid = fourthExpectedBlock.clientId;
expect( transformed[ 7 ] ).toEqual( fourthExpectedBlock );
expect( transformed[ 8 ] ).toEqual( '</p>' );
expect( transformed ).toHaveLength( 9 );
Expand Down
22 changes: 11 additions & 11 deletions blocks/api/test/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe( 'block factory', () => {
expect( block.isValid ).toBe( true );
expect( block.innerBlocks ).toHaveLength( 1 );
expect( block.innerBlocks[ 0 ].name ).toBe( 'core/test-block' );
expect( typeof block.uid ).toBe( 'string' );
expect( typeof block.clientId ).toBe( 'string' );
} );
} );

Expand Down Expand Up @@ -114,8 +114,8 @@ describe( 'block factory', () => {
isDifferent: true,
} );
expect( clonedBlock.innerBlocks ).toHaveLength( 1 );
expect( typeof clonedBlock.uid ).toBe( 'string' );
expect( clonedBlock.uid ).not.toBe( block.uid );
expect( typeof clonedBlock.clientId ).toBe( 'string' );
expect( clonedBlock.clientId ).not.toBe( block.clientId );
} );

it( 'should replace inner blocks of the existing block', () => {
Expand Down Expand Up @@ -181,10 +181,10 @@ describe( 'block factory', () => {
const clonedBlock = cloneBlock( block );

expect( clonedBlock.innerBlocks ).toHaveLength( 2 );
expect( clonedBlock.innerBlocks[ 0 ].uid ).not.toBe( block.innerBlocks[ 0 ].uid );
expect( clonedBlock.innerBlocks[ 0 ].clientId ).not.toBe( block.innerBlocks[ 0 ].clientId );
expect( clonedBlock.innerBlocks[ 0 ].attributes ).not.toBe( block.innerBlocks[ 0 ].attributes );
expect( clonedBlock.innerBlocks[ 0 ].attributes ).toEqual( block.innerBlocks[ 0 ].attributes );
expect( clonedBlock.innerBlocks[ 1 ].uid ).not.toBe( block.innerBlocks[ 1 ].uid );
expect( clonedBlock.innerBlocks[ 1 ].clientId ).not.toBe( block.innerBlocks[ 1 ].clientId );
expect( clonedBlock.innerBlocks[ 1 ].attributes ).not.toBe( block.innerBlocks[ 1 ].attributes );
expect( clonedBlock.innerBlocks[ 1 ].attributes ).toEqual( block.innerBlocks[ 1 ].attributes );
} );
Expand Down Expand Up @@ -731,7 +731,7 @@ describe( 'block factory', () => {
const transformedBlocks = switchToBlockType( block, 'core/updated-text-block' );

expect( transformedBlocks ).toHaveLength( 1 );
expect( transformedBlocks[ 0 ] ).toHaveProperty( 'uid' );
expect( transformedBlocks[ 0 ] ).toHaveProperty( 'clientId' );
expect( transformedBlocks[ 0 ].name ).toBe( 'core/updated-text-block' );
expect( transformedBlocks[ 0 ].isValid ).toBe( true );
expect( transformedBlocks[ 0 ].attributes ).toEqual( {
Expand Down Expand Up @@ -770,7 +770,7 @@ describe( 'block factory', () => {
const transformedBlocks = switchToBlockType( block, 'core/updated-text-block' );

expect( transformedBlocks ).toHaveLength( 1 );
expect( transformedBlocks[ 0 ] ).toHaveProperty( 'uid' );
expect( transformedBlocks[ 0 ] ).toHaveProperty( 'clientId' );
expect( transformedBlocks[ 0 ].name ).toBe( 'core/updated-text-block' );
expect( transformedBlocks[ 0 ].isValid ).toBe( true );
expect( transformedBlocks[ 0 ].attributes ).toEqual( {
Expand Down Expand Up @@ -1028,15 +1028,15 @@ describe( 'block factory', () => {
// transformed block whose type matches the "destination" type gets
// to keep the existing block's UID.
expect( transformedBlocks ).toHaveLength( 2 );
expect( transformedBlocks[ 0 ] ).toHaveProperty( 'uid' );
expect( transformedBlocks[ 0 ].uid ).not.toBe( block.uid );
expect( transformedBlocks[ 0 ] ).toHaveProperty( 'clientId' );
expect( transformedBlocks[ 0 ].clientId ).not.toBe( block.clientId );
expect( transformedBlocks[ 0 ].name ).toBe( 'core/text-block' );
expect( transformedBlocks[ 0 ].isValid ).toBe( true );
expect( transformedBlocks[ 0 ].attributes ).toEqual( {
value: 'chicken ribs',
} );
expect( transformedBlocks[ 1 ].uid ).toBe( block.uid );
expect( transformedBlocks[ 1 ] ).toHaveProperty( 'uid' );
expect( transformedBlocks[ 1 ].clientId ).toBe( block.clientId );
expect( transformedBlocks[ 1 ] ).toHaveProperty( 'clientId' );
expect( transformedBlocks[ 1 ].name ).toBe( 'core/updated-text-block' );
expect( transformedBlocks[ 1 ].isValid ).toBe( true );
expect( transformedBlocks[ 1 ].attributes ).toEqual( {
Expand Down
4 changes: 2 additions & 2 deletions blocks/api/test/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ describe( 'block parser', () => {
url: 'http://google.com',
chicken: 'ribs & \'wings\'',
} );
expect( typeof parsed[ 0 ].uid ).toBe( 'string' );
expect( typeof parsed[ 0 ].clientId ).toBe( 'string' );
} );

it( 'should parse empty post content', () => {
Expand Down Expand Up @@ -625,7 +625,7 @@ describe( 'block parser', () => {
expect( parsed[ 0 ].attributes ).toEqual( {
content: 'Ribs',
} );
expect( typeof parsed[ 0 ].uid ).toBe( 'string' );
expect( typeof parsed[ 0 ].clientId ).toBe( 'string' );
} );

it( 'should add the core namespace to un-namespaced blocks', () => {
Expand Down
6 changes: 3 additions & 3 deletions core-blocks/block/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class SharedBlockEdit extends Component {
onUpdateTitle( title );
}

updateAttributes( block.uid, changedAttributes );
updateAttributes( block.clientId, changedAttributes );
onSave();

this.stopEditing();
Expand All @@ -112,7 +112,7 @@ class SharedBlockEdit extends Component {
<BlockEdit
{ ...this.props }
isSelected={ isEditing && isSelected }
id={ block.uid }
clientId={ block.clientId }
name={ block.name }
attributes={ { ...block.attributes, ...changedAttributes } }
setAttributes={ isEditing ? this.setAttributes : noop }
Expand Down Expand Up @@ -158,7 +158,7 @@ export default compose( [
sharedBlock,
isFetching: isFetchingSharedBlock( ref ),
isSaving: isSavingSharedBlock( ref ),
block: sharedBlock ? getBlock( sharedBlock.uid ) : null,
block: sharedBlock ? getBlock( sharedBlock.clientId ) : null,
};
} ),
withDispatch( ( dispatch, ownProps ) => {
Expand Down
2 changes: 1 addition & 1 deletion core-blocks/gallery/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export const settings = {
} );
editorMediaUpload( {
filesList: files,
onFileChange: ( images ) => onChange( block.uid, { images } ),
onFileChange: ( images ) => onChange( block.clientId, { images } ),
allowedType: 'image',
} );
return block;
Expand Down
2 changes: 1 addition & 1 deletion core-blocks/test/fixtures/core-embed__animoto.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"uid": "_uid_0",
"clientId": "_clientId_0",
"name": "core-embed/animoto",
"isValid": true,
"attributes": {
Expand Down
2 changes: 1 addition & 1 deletion core-blocks/test/fixtures/core-embed__cloudup.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"uid": "_uid_0",
"clientId": "_clientId_0",
"name": "core-embed/cloudup",
"isValid": true,
"attributes": {
Expand Down
2 changes: 1 addition & 1 deletion core-blocks/test/fixtures/core-embed__collegehumor.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"uid": "_uid_0",
"clientId": "_clientId_0",
"name": "core-embed/collegehumor",
"isValid": true,
"attributes": {
Expand Down
2 changes: 1 addition & 1 deletion core-blocks/test/fixtures/core-embed__dailymotion.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"uid": "_uid_0",
"clientId": "_clientId_0",
"name": "core-embed/dailymotion",
"isValid": true,
"attributes": {
Expand Down
2 changes: 1 addition & 1 deletion core-blocks/test/fixtures/core-embed__facebook.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"uid": "_uid_0",
"clientId": "_clientId_0",
"name": "core-embed/facebook",
"isValid": true,
"attributes": {
Expand Down
2 changes: 1 addition & 1 deletion core-blocks/test/fixtures/core-embed__flickr.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"uid": "_uid_0",
"clientId": "_clientId_0",
"name": "core-embed/flickr",
"isValid": true,
"attributes": {
Expand Down
2 changes: 1 addition & 1 deletion core-blocks/test/fixtures/core-embed__funnyordie.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"uid": "_uid_0",
"clientId": "_clientId_0",
"name": "core-embed/funnyordie",
"isValid": true,
"attributes": {
Expand Down
2 changes: 1 addition & 1 deletion core-blocks/test/fixtures/core-embed__hulu.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"uid": "_uid_0",
"clientId": "_clientId_0",
"name": "core-embed/hulu",
"isValid": true,
"attributes": {
Expand Down
2 changes: 1 addition & 1 deletion core-blocks/test/fixtures/core-embed__imgur.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"uid": "_uid_0",
"clientId": "_clientId_0",
"name": "core-embed/imgur",
"isValid": true,
"attributes": {
Expand Down
2 changes: 1 addition & 1 deletion core-blocks/test/fixtures/core-embed__instagram.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"uid": "_uid_0",
"clientId": "_clientId_0",
"name": "core-embed/instagram",
"isValid": true,
"attributes": {
Expand Down
2 changes: 1 addition & 1 deletion core-blocks/test/fixtures/core-embed__issuu.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"uid": "_uid_0",
"clientId": "_clientId_0",
"name": "core-embed/issuu",
"isValid": true,
"attributes": {
Expand Down
2 changes: 1 addition & 1 deletion core-blocks/test/fixtures/core-embed__kickstarter.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"uid": "_uid_0",
"clientId": "_clientId_0",
"name": "core-embed/kickstarter",
"isValid": true,
"attributes": {
Expand Down
2 changes: 1 addition & 1 deletion core-blocks/test/fixtures/core-embed__meetup-com.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"uid": "_uid_0",
"clientId": "_clientId_0",
"name": "core-embed/meetup-com",
"isValid": true,
"attributes": {
Expand Down
2 changes: 1 addition & 1 deletion core-blocks/test/fixtures/core-embed__mixcloud.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"uid": "_uid_0",
"clientId": "_clientId_0",
"name": "core-embed/mixcloud",
"isValid": true,
"attributes": {
Expand Down
2 changes: 1 addition & 1 deletion core-blocks/test/fixtures/core-embed__photobucket.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"uid": "_uid_0",
"clientId": "_clientId_0",
"name": "core-embed/photobucket",
"isValid": true,
"attributes": {
Expand Down
2 changes: 1 addition & 1 deletion core-blocks/test/fixtures/core-embed__polldaddy.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"uid": "_uid_0",
"clientId": "_clientId_0",
"name": "core-embed/polldaddy",
"isValid": true,
"attributes": {
Expand Down
2 changes: 1 addition & 1 deletion core-blocks/test/fixtures/core-embed__reddit.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"uid": "_uid_0",
"clientId": "_clientId_0",
"name": "core-embed/reddit",
"isValid": true,
"attributes": {
Expand Down
2 changes: 1 addition & 1 deletion core-blocks/test/fixtures/core-embed__reverbnation.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"uid": "_uid_0",
"clientId": "_clientId_0",
"name": "core-embed/reverbnation",
"isValid": true,
"attributes": {
Expand Down
Loading