@wordpress/block-library
Version:
Block library for the WordPress editor.
79 lines (68 loc) • 1.69 kB
JavaScript
/**
* WordPress dependencies
*/
import { page, addSubmenu } from '@wordpress/icons';
import { _x, __ } from '@wordpress/i18n';
import { privateApis as blocksPrivateApis } from '@wordpress/blocks';
/**
* Internal dependencies
*/
import initBlock from '../utils/init-block';
import metadata from './block.json';
import edit from './edit';
import save from './save';
import transforms from './transforms';
import { unlock } from '../lock-unlock';
const { fieldsKey, formKey } = unlock( blocksPrivateApis );
const { name } = metadata;
export { metadata, name };
export const settings = {
icon: ( { context } ) => {
if ( context === 'list-view' ) {
return page;
}
return addSubmenu;
},
__experimentalLabel( attributes, { context } ) {
const { label } = attributes;
const customName = attributes?.metadata?.name;
// In the list view, use the block's menu label as the label.
// If the menu label is empty, fall back to the default label.
if ( context === 'list-view' && ( customName || label ) ) {
return attributes?.metadata?.name || label;
}
return label;
},
edit,
example: {
attributes: {
label: _x( 'About', 'Example link text for Navigation Submenu' ),
type: 'page',
},
},
save,
transforms,
};
if ( window.__experimentalContentOnlyInspectorFields ) {
settings[ fieldsKey ] = [
{
id: 'label',
label: __( 'Label' ),
type: 'richtext',
},
{
id: 'link',
label: __( 'Link' ),
type: 'link',
mapping: {
href: 'url',
rel: 'rel',
// TODO - opens in new tab? id?
},
},
];
settings[ formKey ] = {
fields: [ 'label' ],
};
}
export const init = () => initBlock( { name, metadata, settings } );