-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Plugin: Implement block registering API (#289)
* Remove namespace from PHP `register_block` stub * Plugin: Implement block registering API * Add .nvmrc * Add babel-plugin-transform-runtime Right now, this is to polyfill `Object.values` (used in `getBlocks`). Node.js and older browsers lack support for this function. * Add Mocha unit tests
- Loading branch information
1 parent
d1feebe
commit ffd1cef
Showing
12 changed files
with
205 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,5 +5,13 @@ | |
"modules": false | ||
} | ||
} ] | ||
] | ||
], | ||
"plugins": [ | ||
"transform-runtime" | ||
], | ||
"env": { | ||
"test": { | ||
"presets": [ "latest" ] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
modules/*/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules | ||
build | ||
*.log | ||
yarn.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
v6.10.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import chai from 'chai'; | ||
import dirtyChai from 'dirty-chai'; | ||
|
||
chai.use( dirtyChai ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,66 @@ | ||
export { default as Editable } from './components/editable'; | ||
|
||
/** | ||
* Block settings keyed by block slug. | ||
* | ||
* @var {Object} blocks | ||
*/ | ||
const blocks = {}; | ||
|
||
/** | ||
* Registers a block. | ||
* | ||
* @param {string} namespace Block grouping unique to package or plugin | ||
* @param {string} block Block name | ||
* @param {Object} settings Block settings | ||
* @param {string} slug Block slug | ||
* @param {Object} settings Block settings | ||
*/ | ||
export function registerBlock( namespace, block, settings ) { | ||
export function registerBlock( slug, settings ) { | ||
if ( typeof slug !== 'string' ) { | ||
throw new Error( | ||
'Block slugs must be strings.' | ||
); | ||
} | ||
if ( ! /^[a-z0-9-]+\/[a-z0-9-]+$/.test( slug ) ) { | ||
throw new Error( | ||
'Block slugs must contain a namespace prefix. Example: my-plugin/my-custom-block' | ||
); | ||
} | ||
if ( blocks[ slug ] ) { | ||
throw new Error( | ||
'Block "' + slug + '" is already registered.' | ||
); | ||
} | ||
blocks[ slug ] = Object.assign( { slug }, settings ); | ||
} | ||
|
||
/** | ||
* Unregisters a block. | ||
* | ||
* @param {string} slug Block slug | ||
*/ | ||
export function unregisterBlock( slug ) { | ||
if ( ! blocks[ slug ] ) { | ||
throw new Error( | ||
'Block "' + slug + '" is not registered.' | ||
); | ||
} | ||
delete blocks[ slug ]; | ||
} | ||
|
||
/** | ||
* Returns settings associated with a block. | ||
* | ||
* @param {string} namespace Block grouping unique to package or plugin | ||
* @param {string} block Block name | ||
* @return {?Object} Block settings | ||
* @param {string} slug Block slug | ||
* @return {?Object} Block settings | ||
*/ | ||
export function getBlockSettings( namespace, block ) { | ||
|
||
export function getBlockSettings( slug ) { | ||
return blocks[ slug ]; | ||
} | ||
|
||
/** | ||
* Returns all registered blocks. | ||
* | ||
* @return {Object} Block settings keyed by block name | ||
* @return {Array} Block settings | ||
*/ | ||
export function getBlocks() { | ||
|
||
return Object.values( blocks ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { expect } from 'chai'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import * as blocks from '../'; | ||
|
||
describe( 'blocks API', () => { | ||
// Reset block state before each test. | ||
beforeEach( () => { | ||
blocks.getBlocks().forEach( block => { | ||
blocks.unregisterBlock( block.slug ); | ||
} ); | ||
} ); | ||
|
||
describe( 'registerBlock', () => { | ||
it( 'should reject numbers', () => { | ||
expect( | ||
() => blocks.registerBlock( 999 ) | ||
).to.throw( 'Block slugs must be strings.' ); | ||
} ); | ||
|
||
it( 'should reject blocks without a namespace', () => { | ||
expect( | ||
() => blocks.registerBlock( 'doing-it-wrong' ) | ||
).to.throw( /^Block slugs must contain a namespace prefix/ ); | ||
} ); | ||
|
||
it( 'should reject blocks with invalid characters', () => { | ||
expect( | ||
() => blocks.registerBlock( 'still/_doing_it_wrong' ) | ||
).to.throw( /^Block slugs must contain a namespace prefix/ ); | ||
} ); | ||
|
||
it( 'should accept valid block names', () => { | ||
expect( | ||
() => blocks.registerBlock( 'my-plugin/fancy-block-4' ) | ||
).not.to.throw(); | ||
} ); | ||
|
||
it( 'should prohibit registering the same block twice', () => { | ||
blocks.registerBlock( 'core/test-block' ); | ||
expect( | ||
() => blocks.registerBlock( 'core/test-block' ) | ||
).to.throw( 'Block "core/test-block" is already registered.' ); | ||
} ); | ||
|
||
it( 'should store a copy of block settings', () => { | ||
const blockSettings = { settingName: 'settingValue' }; | ||
blocks.registerBlock( 'core/test-block-with-settings', blockSettings ); | ||
blockSettings.mutated = true; | ||
expect( blocks.getBlockSettings( 'core/test-block-with-settings' ) ).to.eql( { | ||
slug: 'core/test-block-with-settings', | ||
settingName: 'settingValue', | ||
} ); | ||
} ); | ||
} ); | ||
|
||
describe( 'unregisterBlock', () => { | ||
it( 'should fail if a block is not registered', () => { | ||
expect( | ||
() => blocks.unregisterBlock( 'core/test-block' ) | ||
).to.throw( 'Block "core/test-block" is not registered.' ); | ||
} ); | ||
|
||
it( 'should unregister existing blocks', () => { | ||
blocks.registerBlock( 'core/test-block' ); | ||
expect( blocks.getBlocks() ).to.eql( [ | ||
{ slug: 'core/test-block' }, | ||
] ); | ||
blocks.unregisterBlock( 'core/test-block' ); | ||
expect( blocks.getBlocks() ).to.eql( [] ); | ||
} ); | ||
} ); | ||
|
||
describe( 'getBlockSettings', () => { | ||
it( 'should return { slug } for blocks with no settings', () => { | ||
blocks.registerBlock( 'core/test-block' ); | ||
expect( blocks.getBlockSettings( 'core/test-block' ) ).to.eql( { | ||
slug: 'core/test-block', | ||
} ); | ||
} ); | ||
|
||
it( 'should return all block settings', () => { | ||
const blockSettings = { settingName: 'settingValue' }; | ||
blocks.registerBlock( 'core/test-block-with-settings', blockSettings ); | ||
expect( blocks.getBlockSettings( 'core/test-block-with-settings' ) ).to.eql( { | ||
slug: 'core/test-block-with-settings', | ||
settingName: 'settingValue', | ||
} ); | ||
} ); | ||
} ); | ||
|
||
describe( 'getBlocks', () => { | ||
it( 'should return an empty array at first', () => { | ||
expect( blocks.getBlocks() ).to.eql( [] ); | ||
} ); | ||
|
||
it( 'should return all registered blocks', () => { | ||
blocks.registerBlock( 'core/test-block' ); | ||
const blockSettings = { settingName: 'settingValue' }; | ||
blocks.registerBlock( 'core/test-block-with-settings', blockSettings ); | ||
expect( blocks.getBlocks() ).to.eql( [ | ||
{ slug: 'core/test-block' }, | ||
{ slug: 'core/test-block-with-settings', settingName: 'settingValue' }, | ||
] ); | ||
} ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters