2021-09-03 13:25:09 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { registerBlockComponent } from '@woocommerce/blocks-registry';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import {
|
|
|
|
assertBlockName,
|
2021-09-07 16:01:14 +00:00
|
|
|
assertBlockParent,
|
2021-09-03 13:25:09 +00:00
|
|
|
assertOption,
|
|
|
|
assertBlockComponent,
|
|
|
|
} from './utils';
|
|
|
|
import { registeredBlocks } from './registered-blocks';
|
2023-03-22 07:23:52 +00:00
|
|
|
import type { CheckoutBlockOptions } from './types';
|
2021-09-03 13:25:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Main API for registering a new checkout block within areas.
|
|
|
|
*/
|
|
|
|
export const registerCheckoutBlock = (
|
|
|
|
options: CheckoutBlockOptions
|
|
|
|
): void => {
|
2021-09-07 16:01:14 +00:00
|
|
|
assertOption( options, 'metadata', 'object' );
|
|
|
|
assertBlockName( options.metadata.name );
|
|
|
|
assertBlockParent( options.metadata.parent );
|
2021-09-03 13:25:09 +00:00
|
|
|
assertBlockComponent( options, 'component' );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This ensures the frontend component for the checkout block is available.
|
|
|
|
*/
|
|
|
|
registerBlockComponent( {
|
2021-09-07 16:01:14 +00:00
|
|
|
blockName: options.metadata.name as string,
|
2021-09-03 13:25:09 +00:00
|
|
|
component: options.component,
|
|
|
|
} );
|
|
|
|
|
2023-03-22 07:23:52 +00:00
|
|
|
// Infer the `force` value from whether the block is locked or not. But
|
|
|
|
// allow overriding it on block registration.
|
|
|
|
const force =
|
|
|
|
typeof options.force === 'boolean'
|
|
|
|
? options.force
|
|
|
|
: Boolean( options.metadata?.attributes?.lock?.default?.remove );
|
|
|
|
|
2021-09-03 13:25:09 +00:00
|
|
|
/**
|
2021-09-07 16:01:14 +00:00
|
|
|
* Store block metadata for later lookup.
|
2021-09-03 13:25:09 +00:00
|
|
|
*/
|
2021-09-07 16:01:14 +00:00
|
|
|
registeredBlocks[ options.metadata.name ] = {
|
|
|
|
blockName: options.metadata.name,
|
|
|
|
metadata: options.metadata,
|
|
|
|
component: options.component,
|
2023-03-22 07:23:52 +00:00
|
|
|
force,
|
2021-09-07 16:01:14 +00:00
|
|
|
};
|
2021-09-03 13:25:09 +00:00
|
|
|
};
|