| 1 |
import {BlockConfiguration} from '@wordpress/blocks'; |
| 2 |
|
| 3 |
/** |
| 4 |
* @since 3.0.0 |
| 5 |
*/ |
| 6 |
export interface Block { |
| 7 |
name: string; |
| 8 |
settings: BlockConfiguration; |
| 9 |
} |
| 10 |
|
| 11 |
/** |
| 12 |
* @since 3.0.0 |
| 13 |
*/ |
| 14 |
interface Registrar { |
| 15 |
register(name: string, settings: BlockConfiguration): void; |
| 16 |
|
| 17 |
getAll(): Block[]; |
| 18 |
|
| 19 |
get(blockName: string): Block | undefined; |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* @since 3.0.0 |
| 24 |
*/ |
| 25 |
export default class BlockRegistrar implements Registrar { |
| 26 |
/** |
| 27 |
* @since 3.0.0 |
| 28 |
*/ |
| 29 |
private blocks: Block[] = []; |
| 30 |
|
| 31 |
/** |
| 32 |
* @since 3.0.0 |
| 33 |
*/ |
| 34 |
public get(blockName: string): Block | undefined { |
| 35 |
return this.blocks.find(({name}) => name === blockName); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* @since 3.0.0 |
| 40 |
*/ |
| 41 |
public getAll(): Block[] { |
| 42 |
return this.blocks; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* @since 3.0.0 |
| 47 |
*/ |
| 48 |
public register(name, settings: BlockConfiguration): void { |
| 49 |
if (this.get(name)) { |
| 50 |
throw new Error(`Block "${name}" is already registered.`); |
| 51 |
} |
| 52 |
|
| 53 |
this.blocks.push({name, settings}); |
| 54 |
} |
| 55 |
} |
| 56 |
|