Skip to content

Commit

Permalink
Block library: Try to use Babel plugins to inline block.json metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Mar 21, 2019
1 parent 429558a commit 9a0ad89
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 34 deletions.
1 change: 1 addition & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ module.exports = function( api ) {

return {
presets: [ '@wordpress/babel-preset-default' ],
plugins: [ 'babel-plugin-inline-json-import' ],
};
};
24 changes: 24 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"@wordpress/npm-package-json-lint-config": "file:packages/npm-package-json-lint-config",
"@wordpress/postcss-themes": "file:packages/postcss-themes",
"@wordpress/scripts": "file:packages/scripts",
"babel-plugin-inline-json-import": "0.3.2",
"benchmark": "2.1.4",
"browserslist": "4.4.1",
"chalk": "2.4.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ _This package assumes that your code will run in an **ES2015+** environment. If

### registerCoreBlocks

[src/index.js#L70-L132](src/index.js#L70-L132)
[src/index.js#L71-L136](src/index.js#L71-L136)

Function to register core blocks provided by the block editor.

Expand Down
6 changes: 5 additions & 1 deletion packages/block-library/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
setDefaultBlockName,
setFreeformContentHandlerName,
setUnregisteredTypeHandlerName,
unstable__setBlockMetadata, // eslint-disable-line camelcase
} from '@wordpress/blocks';

/**
Expand Down Expand Up @@ -120,7 +121,10 @@ export const registerCoreBlocks = () => {
if ( ! block ) {
return;
}
const { name, settings } = block;
const { metadata, settings, name = metadata.name } = block;
if ( metadata ) {
unstable__setBlockMetadata( metadata ); // eslint-disable-line camelcase
}
registerBlockType( name, settings );
} );

Expand Down
29 changes: 29 additions & 0 deletions packages/block-library/src/text-columns/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "core/text-columns",
"icon": "columns",
"category": "layout",
"attributes": {
"content": {
"type": "array",
"source": "query",
"selector": "p",
"query": {
"children": {
"type": "string",
"source": "html"
}
},
"default": [ { }, { } ]
},
"columns": {
"type": "number",
"default": 2
},
"width": {
"type": "string"
}
},
"supports": {
"inserter": false
}
}
38 changes: 6 additions & 32 deletions packages/block-library/src/text-columns/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,44 +18,18 @@ import {
} from '@wordpress/block-editor';
import deprecated from '@wordpress/deprecated';

export const name = 'core/text-columns';
/**
* Internal dependencies
*/
import metadata from './block.json';

export const settings = {
// Disable insertion as this block is deprecated and ultimately replaced by the Columns block.
supports: {
inserter: false,
},
export { metadata };

export const settings = {
title: __( 'Text Columns (deprecated)' ),

description: __( 'This block is deprecated. Please use the Columns block instead.' ),

icon: 'columns',

category: 'layout',

attributes: {
content: {
type: 'array',
source: 'query',
selector: 'p',
query: {
children: {
type: 'string',
source: 'html',
},
},
default: [ {}, {} ],
},
columns: {
type: 'number',
default: 2,
},
width: {
type: 'string',
},
},

transforms: {
to: [
{
Expand Down
1 change: 1 addition & 0 deletions packages/blocks/src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export {
hasChildBlocks,
hasChildBlocksWithInserterSupport,
unstable__bootstrapServerSideBlockDefinitions, // eslint-disable-line camelcase
unstable__setBlockMetadata, // eslint-disable-line camelcase
registerBlockStyle,
unregisterBlockStyle,
} from './registration';
Expand Down
9 changes: 9 additions & 0 deletions packages/blocks/src/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ export function unstable__bootstrapServerSideBlockDefinitions( definitions ) { /
serverSideBlockDefinitions = definitions;
}

/**
* Sets the block's metadata.
*
* @param {Object} definitions Server-side block definitions
*/
export function unstable__setBlockMetadata( { name, ...settings } ) { // eslint-disable-line camelcase
serverSideBlockDefinitions[ name ] = settings;
}

/**
* Registers a new block provided a unique name and an object defining its
* behavior. Once registered, the block is made available as an option to any
Expand Down

0 comments on commit 9a0ad89

Please sign in to comment.