Skip to content

Commit

Permalink
Render generic fallback block for unknown block type
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Mar 27, 2017
1 parent 4ac6ebe commit c358ca0
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 133 deletions.
26 changes: 25 additions & 1 deletion blocks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as query from 'hpq';

export { query };
export { default as Editable } from './components/editable';
export { default as parse } from './parser';
export { parse } from './post.pegjs';

/**
* Block settings keyed by block slug.
Expand Down Expand Up @@ -79,6 +79,30 @@ export function getBlockSettings( slug ) {
return blocks[ slug ];
}

/**
* Returns the block attributes of a registered block node given its settings.
*
* @param {Object} blockNode Parsed block node
* @param {Object} blockSettings Block settings
* @return {Object} Block state, or undefined if type unknown
*/
export function getBlockAttributes( blockNode, blockSettings ) {
const { rawContent } = blockNode;

// Merge attributes from parse with block implementation
let { attrs } = blockNode;

// Block attributes by function
if ( 'function' === typeof blockSettings.attributes ) {
return { ...attrs, ...blockSettings.attributes( rawContent ) };
}

// Block attributes by hpq
if ( blockSettings.attributes ) {
return { ...attrs, ...query.parse( rawContent, blockSettings.attributes ) };
}
}

/**
* Returns all registered blocks.
*
Expand Down
52 changes: 0 additions & 52 deletions blocks/parser/index.js

This file was deleted.

79 changes: 0 additions & 79 deletions blocks/parser/test/index.js

This file was deleted.

File renamed without changes.
57 changes: 57 additions & 0 deletions blocks/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import sinon from 'sinon';
import * as blocks from '../';

describe( 'blocks', () => {
const { text } = blocks.query;

// Reset block state before each test.
beforeEach( () => {
blocks.getBlocks().forEach( block => {
Expand Down Expand Up @@ -104,6 +106,61 @@ describe( 'blocks', () => {
} );
} );

describe( 'getBlockAttributes()', () => {
it( 'should merge attributes from function implementation', () => {
const blockSettings = {
attributes: function( rawContent ) {
return {
content: rawContent + ' & Chicken'
};
}
};

const blockNode = {
blockType: 'core/test-block',
attrs: {
align: 'left'
},
rawContent: 'Ribs'
};

expect( blocks.getBlockAttributes( blockNode, blockSettings ) ).to.eql( {
align: 'left',
content: 'Ribs & Chicken'
} );
} );

it( 'should merge attributes from query object implementation', () => {
const blockSettings = {
attributes: {
emphasis: text( 'strong' )
}
};

const blockNode = {
blockType: 'core/test-block',
attrs: {},
rawContent: '<span>Ribs <strong>& Chicken</strong></span>'
};

expect( blocks.getBlockAttributes( blockNode, blockSettings ) ).to.eql( {
emphasis: '& Chicken'
} );
} );
} );

describe( 'parse()', () => {
it( 'should parse the post content properly', () => {
const postContent = '<!-- wp:core/test-block -->Ribs<!-- /wp:core/test-block -->';

expect( blocks.parse( postContent ) ).to.eql( [ {
blockType: 'core/test-block',
attrs: {},
rawContent: 'Ribs'
} ] );
} );
} );

describe( 'getBlocks()', () => {
it( 'should return an empty array at first', () => {
expect( blocks.getBlocks() ).to.eql( [] );
Expand Down
1 change: 1 addition & 0 deletions editor/assets/stylesheets/main.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@import '../../inserter/style';
@import '../../blocks/generic-block/style';

body.toplevel_page_gutenberg {
background: #fff;
Expand Down
26 changes: 26 additions & 0 deletions editor/blocks/generic-block/_style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
.generic-block {
position: relative;
}

.generic-block:before {
content: '';
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
background-color: rgba( 64, 64, 64, 0.4 );
}

.generic-block:after {
content: 'Unknown Block';
position: absolute;
top: 50%;
right: 0;
left: 0;
text-align: center;
margin-top: -12px;
font-size: 24px;
line-height: 1;
color: #fff;
}
20 changes: 20 additions & 0 deletions editor/blocks/generic-block/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const { html } = wp.blocks.query;

wp.blocks.registerBlock( 'wp/generic', {
attributes: {
html: html()
},

edit( { attributes } ) {
// TODO: Not `dangerouslySetInnerHTML`
return (
<div
dangerouslySetInnerHTML={ { __html: attributes.html } }
className="generic-block" />
);
},

save( { attributes } ) {
return attributes.html;
}
} );
1 change: 1 addition & 0 deletions editor/blocks/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
import './generic-block';
import './text-block';
6 changes: 5 additions & 1 deletion editor/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ const Editor = ( { state: { blocks, inserter }, toggleInserter } ) => {
<div contentEditable>
{ blocks.map( ( block, index ) => {
let settings = wp.blocks.getBlockSettings( block.blockType );
if ( ! settings ) {
settings = wp.blocks.getBlockSettings( 'wp/generic' );
}

if ( ! settings ) {
return;
}

return (
<settings.edit
key={ index }
attributes={ block.attributes }
attributes={ wp.blocks.getBlockAttributes( block, settings ) }
onChange={ () => {} } />
);
} ) }
Expand Down

0 comments on commit c358ca0

Please sign in to comment.