@wordpress/block-library
Version:
Block library for the WordPress editor.
92 lines (84 loc) • 1.82 kB
JavaScript
import { addFilter } from '@wordpress/hooks';
import { __ } from '@wordpress/i18n';
import initBlock from '../utils/init-block';
import edit from './edit';
import metadata from './block.json';
import save from './save';
import variations from './variations';
import deprecated from './deprecated';
import { icon } from './icons';
import {
formSubmissionNotificationSuccess,
formSubmissionNotificationError,
} from './utils';
const { name } = metadata;
export { metadata, name };
const TEMPLATE = [
formSubmissionNotificationSuccess,
formSubmissionNotificationError,
[
'core/form-input',
{
type: 'text',
label: __( 'Name' ),
required: true,
},
],
[
'core/form-input',
{
type: 'email',
label: __( 'Email' ),
required: true,
},
],
[
'core/form-input',
{
type: 'textarea',
label: __( 'Comment' ),
required: true,
},
],
[ 'core/form-submit-button', {} ],
];
export const settings = {
icon,
template: TEMPLATE,
edit,
save,
deprecated,
variations,
example: {},
};
export const init = () => {
// Prevent adding forms inside forms.
const DISALLOWED_PARENTS = [ 'core/form' ];
addFilter(
'blockEditor.__unstableCanInsertBlockType',
'core/block-library/preventInsertingFormIntoAnotherForm',
(
canInsert,
blockType,
rootClientId,
{ getBlock, getBlockParentsByBlockName }
) => {
if ( blockType.name !== 'core/form' ) {
return canInsert;
}
for ( const disallowedParentType of DISALLOWED_PARENTS ) {
const hasDisallowedParent =
getBlock( rootClientId )?.name === disallowedParentType ||
getBlockParentsByBlockName(
rootClientId,
disallowedParentType
).length;
if ( hasDisallowedParent ) {
return false;
}
}
return true;
}
);
return initBlock( { name, metadata, settings } );
};