Skip to content

Commit

Permalink
Simplify blockName attribute.
Browse files Browse the repository at this point in the history
  • Loading branch information
mtias authored and youknowriad committed Jun 1, 2017
1 parent f620983 commit 11ac2a2
Show file tree
Hide file tree
Showing 29 changed files with 130 additions and 130 deletions.
26 changes: 13 additions & 13 deletions blocks/api/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import { getBlockType } from './registration';
/**
* Returns a block object given its type and attributes.
*
* @param {String} blockName Block name
* @param {String} name Block name
* @param {Object} attributes Block attributes
* @return {Object} Block object
*/
export function createBlock( blockName, attributes = {} ) {
export function createBlock( name, attributes = {} ) {
// Get the type definition associated with a registered block.
const blockType = getBlockType( blockName );
const blockType = getBlockType( name );

// Do we need this? What purpose does it have?
let defaultAttributes;
Expand All @@ -30,7 +30,7 @@ export function createBlock( blockName, attributes = {} ) {
// and the block attributes.
return {
uid: uuid(),
blockName,
name,
attributes: {
...defaultAttributes,
...attributes,
Expand All @@ -42,19 +42,19 @@ export function createBlock( blockName, attributes = {} ) {
* Switch a block into one or more blocks of the new block type.
*
* @param {Object} block Block object
* @param {string} blockName Block name
* @param {string} name Block name
* @return {Array} Block object
*/
export function switchToBlockType( block, blockName ) {
export function switchToBlockType( block, name ) {
// Find the right transformation by giving priority to the "to"
// transformation.
const destinationType = getBlockType( blockName );
const sourceType = getBlockType( block.blockName );
const destinationType = getBlockType( name );
const sourceType = getBlockType( block.name );
const transformationsFrom = get( destinationType, 'transforms.from', [] );
const transformationsTo = get( sourceType, 'transforms.to', [] );
const transformation =
find( transformationsTo, t => t.blocks.indexOf( blockName ) !== -1 ) ||
find( transformationsFrom, t => t.blocks.indexOf( block.blockName ) !== -1 );
find( transformationsTo, t => t.blocks.indexOf( name ) !== -1 ) ||
find( transformationsFrom, t => t.blocks.indexOf( block.name ) !== -1 );

// Stop if there is no valid transformation. (How did we get here?)
if ( ! transformation ) {
Expand All @@ -75,11 +75,11 @@ export function switchToBlockType( block, blockName ) {

// Ensure that every block object returned by the transformation has a
// valid block type.
if ( transformationResults.some( ( result ) => ! getBlockType( result.blockName ) ) ) {
if ( transformationResults.some( ( result ) => ! getBlockType( result.name ) ) ) {
return null;
}

const firstSwitchedBlock = findIndex( transformationResults, ( result ) => result.blockName === blockName );
const firstSwitchedBlock = findIndex( transformationResults, ( result ) => result.name === name );

// Ensure that at least one block object returned by the transformation has
// the expected "destination" block type.
Expand All @@ -92,7 +92,7 @@ export function switchToBlockType( block, blockName ) {
// The first transformed block whose type matches the "destination"
// type gets to keep the existing block's UID.
uid: index === firstSwitchedBlock ? block.uid : result.uid,
blockName: result.blockName,
name: result.name,
attributes: result.attributes,
};
} );
Expand Down
2 changes: 1 addition & 1 deletion blocks/api/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export function getCommentAttributes( realAttributes, expectedAttributes ) {
*/
export default function serialize( blocks ) {
return blocks.reduce( ( memo, block ) => {
const blockName = block.blockName;
const blockName = block.name;
const blockType = getBlockType( blockName );
const saveContent = getSaveContent( blockType.save, block.attributes );
const beautifyOptions = {
Expand Down
30 changes: 15 additions & 15 deletions blocks/api/test/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe( 'block factory', () => {
align: 'left',
} );

expect( block.blockName ).to.eql( 'core/test-block' );
expect( block.name ).to.eql( 'core/test-block' );
expect( block.attributes ).to.eql( {
includesDefault: true,
align: 'left',
Expand All @@ -55,7 +55,7 @@ describe( 'block factory', () => {

const block = {
uid: 1,
blockName: 'core/text-block',
name: 'core/text-block',
attributes: {
value: 'ribs',
},
Expand All @@ -65,7 +65,7 @@ describe( 'block factory', () => {

expect( updatedBlock ).to.eql( [ {
uid: 1,
blockName: 'core/updated-text-block',
name: 'core/updated-text-block',
attributes: {
value: 'chicken ribs',
},
Expand All @@ -89,7 +89,7 @@ describe( 'block factory', () => {

const block = {
uid: 1,
blockName: 'core/text-block',
name: 'core/text-block',
attributes: {
value: 'ribs',
},
Expand All @@ -99,7 +99,7 @@ describe( 'block factory', () => {

expect( updatedBlock ).to.eql( [ {
uid: 1,
blockName: 'core/updated-text-block',
name: 'core/updated-text-block',
attributes: {
value: 'chicken ribs',
},
Expand All @@ -112,7 +112,7 @@ describe( 'block factory', () => {

const block = {
uid: 1,
blockName: 'core/text-block',
name: 'core/text-block',
attributes: {
value: 'ribs',
},
Expand All @@ -136,7 +136,7 @@ describe( 'block factory', () => {

const block = {
uid: 1,
blockName: 'core/text-block',
name: 'core/text-block',
attributes: {
value: 'ribs',
},
Expand All @@ -160,7 +160,7 @@ describe( 'block factory', () => {

const block = {
uid: 1,
blockName: 'core/text-block',
name: 'core/text-block',
attributes: {
value: 'ribs',
},
Expand Down Expand Up @@ -190,7 +190,7 @@ describe( 'block factory', () => {

const block = {
uid: 1,
blockName: 'core/text-block',
name: 'core/text-block',
attributes: {
value: 'ribs',
},
Expand Down Expand Up @@ -225,7 +225,7 @@ describe( 'block factory', () => {

const block = {
uid: 1,
blockName: 'core/text-block',
name: 'core/text-block',
attributes: {
value: 'ribs',
},
Expand Down Expand Up @@ -253,7 +253,7 @@ describe( 'block factory', () => {

const block = {
uid: 1,
blockName: 'core/text-block',
name: 'core/text-block',
attributes: {
value: 'ribs',
},
Expand Down Expand Up @@ -286,7 +286,7 @@ describe( 'block factory', () => {

const block = {
uid: 1,
blockName: 'core/text-block',
name: 'core/text-block',
attributes: {
value: 'ribs',
},
Expand Down Expand Up @@ -319,7 +319,7 @@ describe( 'block factory', () => {

const block = {
uid: 1,
blockName: 'core/text-block',
name: 'core/text-block',
attributes: {
value: 'ribs',
},
Expand All @@ -337,13 +337,13 @@ describe( 'block factory', () => {

expect( updatedBlock ).to.eql( [ {
uid: 2,
blockName: 'core/text-block',
name: 'core/text-block',
attributes: {
value: 'chicken ribs',
},
}, {
uid: 1,
blockName: 'core/updated-text-block',
name: 'core/updated-text-block',
attributes: {
value: 'smoked ribs',
},
Expand Down
16 changes: 8 additions & 8 deletions blocks/api/test/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ describe( 'block parser', () => {
'content',
{ attr: 'value' }
);
expect( block.blockName ).to.eql( 'core/test-block' );
expect( block.name ).to.eql( 'core/test-block' );
expect( block.attributes ).to.eql( { attr: 'value' } );
} );

it( 'should create the requested block with no attributes if it exists', () => {
registerBlockType( 'core/test-block', {} );

const block = createBlockWithFallback( 'core/test-block', 'content' );
expect( block.blockName ).to.eql( 'core/test-block' );
expect( block.name ).to.eql( 'core/test-block' );
expect( block.attributes ).to.eql( {} );
} );

Expand All @@ -121,7 +121,7 @@ describe( 'block parser', () => {
'content',
{ attr: 'value' }
);
expect( block.blockName ).to.eql( 'core/unknown-block' );
expect( block.name ).to.eql( 'core/unknown-block' );
expect( block.attributes ).to.eql( { attr: 'value' } );
} );

Expand All @@ -130,7 +130,7 @@ describe( 'block parser', () => {
setUnknownTypeHandler( 'core/unknown-block' );

const block = createBlockWithFallback( null, 'content' );
expect( block.blockName ).to.eql( 'core/unknown-block' );
expect( block.name ).to.eql( 'core/unknown-block' );
expect( block.attributes ).to.eql( {} );
} );

Expand Down Expand Up @@ -162,7 +162,7 @@ describe( 'block parser', () => {
);

expect( parsed ).to.have.lengthOf( 1 );
expect( parsed[ 0 ].blockName ).to.equal( 'core/test-block' );
expect( parsed[ 0 ].name ).to.equal( 'core/test-block' );
expect( parsed[ 0 ].attributes ).to.eql( {
content: 'Brisket',
smoked: 'yes',
Expand Down Expand Up @@ -195,7 +195,7 @@ describe( 'block parser', () => {
);

expect( parsed ).to.have.lengthOf( 1 );
expect( parsed[ 0 ].blockName ).to.equal( 'core/test-block' );
expect( parsed[ 0 ].name ).to.equal( 'core/test-block' );
expect( parsed[ 0 ].attributes ).to.eql( {
content: 'Ribs & Chicken',
} );
Expand All @@ -215,7 +215,7 @@ describe( 'block parser', () => {
);

expect( parsed ).to.have.lengthOf( 3 );
expect( parsed.map( ( { blockName } ) => blockName ) ).to.eql( [
expect( parsed.map( ( { name } ) => name ) ).to.eql( [
'core/test-block',
'core/unknown-block',
'core/unknown-block',
Expand Down Expand Up @@ -244,7 +244,7 @@ describe( 'block parser', () => {
);

expect( parsed ).to.have.lengthOf( 5 );
expect( parsed.map( ( { blockName } ) => blockName ) ).to.eql( [
expect( parsed.map( ( { name } ) => name ) ).to.eql( [
'core/unknown-block',
'core/test-block',
'core/unknown-block',
Expand Down
4 changes: 2 additions & 2 deletions blocks/api/test/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { getBlockTypes, registerBlockType, unregisterBlockType } from '../regist
describe( 'block serializer', () => {
afterEach( () => {
getBlockTypes().forEach( block => {
unregisterBlockType( block.slug );
unregisterBlockType( block.name );
} );
} );

Expand Down Expand Up @@ -102,7 +102,7 @@ describe( 'block serializer', () => {
registerBlockType( 'core/test-block', blockType );
const blockList = [
{
blockName: 'core/test-block',
name: 'core/test-block',
attributes: {
content: 'Ribs & Chicken',
align: 'left',
Expand Down
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core-button-center.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"uid": "_uid_0",
"blockName": "core/button",
"name": "core/button",
"attributes": {
"align": "center",
"url": "https://github.com/WordPress/gutenberg",
Expand Down
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core-code.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"uid": "_uid_0",
"blockName": "core/code",
"name": "core/code",
"attributes": {
"content": "export default function MyButton() {\n\treturn <Button>Click Me!</Button>;\n}"
}
Expand Down
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core-embed-youtube-caption.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"uid": "_uid_0",
"blockName": "core/embed",
"name": "core/embed",
"attributes": {
"url": "//www.youtube.com/embed/Nl6U7UotA-M",
"caption": [
Expand Down
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core-freeform.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"uid": "_uid_0",
"blockName": "core/freeform",
"name": "core/freeform",
"attributes": {
"html": "Testing freeform block with some<div class=\"wp-some-class\">HTML <span style=\"color: red;\">content</span></div>"
}
Expand Down
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core-heading-h2-em.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"uid": "_uid_0",
"blockName": "core/heading",
"name": "core/heading",
"attributes": {
"content": [
"The ",
Expand Down
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core-heading-h2.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"uid": "_uid_0",
"blockName": "core/heading",
"name": "core/heading",
"attributes": {
"content": [
"A picture is worth a thousand words, or so the saying goes"
Expand Down
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core-image-center-caption.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"uid": "_uid_0",
"blockName": "core/image",
"name": "core/image",
"attributes": {
"align": "center",
"url": "https://cldup.com/YLYhpou2oq.jpg",
Expand Down
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core-image.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"uid": "_uid_0",
"blockName": "core/image",
"name": "core/image",
"attributes": {
"url": "https://cldup.com/uuUqE_dXzy.jpg",
"caption": []
Expand Down
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core-list-ul.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"uid": "_uid_0",
"blockName": "core/list",
"name": "core/list",
"attributes": {
"nodeName": "UL",
"values": [
Expand Down
2 changes: 1 addition & 1 deletion blocks/test/fixtures/core-preformatted.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"uid": "_uid_0",
"blockName": "core/preformatted",
"name": "core/preformatted",
"attributes": {
"content": [
"Some ",
Expand Down
Loading

0 comments on commit 11ac2a2

Please sign in to comment.