2022-05-30 14:38:52 +00:00
|
|
|
// Disabling because of `__experimental` property names.
|
|
|
|
/* eslint-disable @typescript-eslint/naming-convention */
|
|
|
|
|
2022-05-19 16:16:46 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { InnerBlocks } from '@wordpress/block-editor';
|
2022-05-30 14:38:52 +00:00
|
|
|
import { registerBlockType } from '@wordpress/blocks';
|
2022-05-19 16:16:46 +00:00
|
|
|
import { getSetting } from '@woocommerce/settings';
|
|
|
|
import { isFeaturePluginBuild } from '@woocommerce/block-settings';
|
2023-03-02 14:26:00 +00:00
|
|
|
import type { FunctionComponent } from 'react';
|
|
|
|
import type { BlockConfiguration } from '@wordpress/blocks';
|
2022-05-19 16:16:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { Edit } from './edit';
|
|
|
|
|
2022-05-30 14:38:52 +00:00
|
|
|
type CSSDirections = 'top' | 'right' | 'bottom' | 'left';
|
|
|
|
|
|
|
|
interface ExtendedBlockSupports {
|
|
|
|
supports: {
|
|
|
|
color?: {
|
|
|
|
background: string;
|
|
|
|
gradients: boolean;
|
|
|
|
link: boolean;
|
|
|
|
text: string;
|
|
|
|
};
|
|
|
|
spacing?: {
|
|
|
|
margin: boolean | CSSDirections[];
|
|
|
|
padding: boolean | CSSDirections[];
|
|
|
|
__experimentalDefaultControls?: {
|
|
|
|
margin?: boolean;
|
|
|
|
padding?: boolean;
|
|
|
|
};
|
|
|
|
__experimentalSkipSerialization?: boolean;
|
|
|
|
};
|
|
|
|
__experimentalBorder?: {
|
|
|
|
color: boolean;
|
|
|
|
radius: boolean;
|
|
|
|
width: boolean;
|
|
|
|
__experimentalSkipSerialization?: boolean;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-05-19 16:16:46 +00:00
|
|
|
export function register(
|
|
|
|
Block: FunctionComponent,
|
|
|
|
example: { attributes: Record< string, unknown > },
|
2022-05-30 14:38:52 +00:00
|
|
|
metadata: BlockConfiguration & ExtendedBlockSupports,
|
2022-05-19 16:16:46 +00:00
|
|
|
settings: Partial< BlockConfiguration >
|
|
|
|
): void {
|
|
|
|
const DEFAULT_SETTINGS = {
|
|
|
|
attributes: {
|
|
|
|
...metadata.attributes,
|
|
|
|
/**
|
|
|
|
* A minimum height for the block.
|
|
|
|
*
|
|
|
|
* Note: if padding is increased, this way the inner content will never
|
|
|
|
* overflow, but instead will resize the container.
|
|
|
|
*
|
|
|
|
* It was decided to change this to make this block more in line with
|
|
|
|
* the “Cover” block.
|
|
|
|
*/
|
|
|
|
minHeight: {
|
|
|
|
type: 'number',
|
|
|
|
default: getSetting( 'default_height', 500 ),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
supports: {
|
|
|
|
...metadata.supports,
|
|
|
|
color: {
|
|
|
|
background: metadata.supports?.color?.background,
|
|
|
|
text: metadata.supports?.color?.text,
|
|
|
|
},
|
|
|
|
spacing: {
|
|
|
|
padding: metadata.supports?.spacing?.padding,
|
|
|
|
...( isFeaturePluginBuild() && {
|
|
|
|
__experimentalDefaultControls: {
|
|
|
|
padding:
|
|
|
|
metadata.supports?.spacing
|
|
|
|
?.__experimentalDefaultControls,
|
|
|
|
},
|
|
|
|
__experimentalSkipSerialization:
|
|
|
|
metadata.supports?.spacing
|
|
|
|
?.__experimentalSkipSerialization,
|
|
|
|
} ),
|
|
|
|
},
|
|
|
|
...( isFeaturePluginBuild() && {
|
|
|
|
__experimentalBorder: metadata?.supports?.__experimentalBorder,
|
|
|
|
} ),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const DEFAULT_EXAMPLE = {
|
|
|
|
attributes: {
|
|
|
|
alt: '',
|
|
|
|
contentAlign: 'center',
|
|
|
|
dimRatio: 50,
|
|
|
|
editMode: false,
|
|
|
|
hasParallax: false,
|
|
|
|
isRepeated: false,
|
|
|
|
height: getSetting( 'default_height', 500 ),
|
|
|
|
mediaSrc: '',
|
|
|
|
overlayColor: '#000000',
|
|
|
|
showDesc: true,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
registerBlockType( metadata, {
|
|
|
|
...DEFAULT_SETTINGS,
|
|
|
|
example: {
|
|
|
|
...DEFAULT_EXAMPLE,
|
2023-01-27 13:19:40 +00:00
|
|
|
...example,
|
2022-05-19 16:16:46 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Renders and manages the block.
|
|
|
|
*
|
|
|
|
* @param {Object} props Props to pass to block.
|
|
|
|
*/
|
|
|
|
edit: Edit( Block ),
|
|
|
|
/**
|
|
|
|
* Block content is rendered in PHP, not via save function.
|
|
|
|
*/
|
|
|
|
save: () => <InnerBlocks.Content />,
|
|
|
|
...settings,
|
|
|
|
} );
|
|
|
|
}
|