WordPress Core Group Block - markhowellsmead/helpers GitHub Wiki
Adding a link control to the core/group block
JavaScript
/** * External dependencies */importclassnamesfrom'classnames';/** * WordPress dependencies */import{__}from'@wordpress/i18n';import{addFilter}from'@wordpress/hooks';import{BlockControls,__experimentalLinkControlasLinkControl}from'@wordpress/block-editor';import{Button,ToolbarButton,MenuGroup,MenuItem,Popover}from'@wordpress/components';import{link,linkOff,page,Icon}from'@wordpress/icons';import{useState}from'@wordpress/element';/** * Add the attributes needed for linked groups. * * @param {Object} settings */functionaddAttributes(settings){if('core/group'!==settings.name){returnsettings;}// Add the link attributes.constlinkAttributes={href: {type: 'string',},linkDestination: {type: 'string',},linkTarget: {type: 'string',},};constnewSettings={
...settings,attributes: {
...settings.attributes,
...linkAttributes,},};returnnewSettings;}addFilter('blocks.registerBlockType','enable-linked-groups/add-attributes',addAttributes);/** * Filter the BlockEdit object and add linked group inspector controls. * * @todo Fix the issue where the popover remains open when clicking another block. * * @param {Object} BlockEdit */functionaddInspectorControls(BlockEdit){return(props)=>{if(props.name!=='core/group'){return<BlockEdit{...props}/>;}const[isEditingURL,setIsEditingURL]=useState(false);const[popoverAnchor,setPopoverAnchor]=useState(null);const{ attributes, setAttributes }=props;const{ href, linkDestination, linkTarget }=attributes;return(<><BlockEdit{...props}/><BlockControlsgroup="block"><ToolbarButtonref={setPopoverAnchor}name="link"icon={link}title={__('Link','shp-linked-group-block')}onClick={()=>setIsEditingURL(true)}isActive={!!href||linkDestination==='post'||isEditingURL}/>{isEditingURL&&(<Popoveranchor={popoverAnchor}onClose={()=>setIsEditingURL(false)}focusOnMount={true}offset={10}className="enable-linked-groups__link-popover"variant="alternate">{linkDestination!=='post'&&(<LinkControlvalue={{url: href,opensInNewTab: linkTarget==='_blank',}}onChange={({url: newURL='', opensInNewTab })=>{setAttributes({href: newURL,linkDestination: newURL ? 'custom' : undefined,linkTarget: opensInNewTab ? '_blank' : undefined,});}}onRemove={()=>setAttributes({href: undefined,linkDestination: undefined,linkTarget: undefined,})}/>)}{!href&&!linkDestination&&(<divclassName="enable-linked-groups__link-popover-menu"><MenuGroup><MenuItemicon={page}iconPosition="left"info={__('Use when the Group is located in a Query block.','shp-linked-group-block')}onClick={()=>setAttributes({linkDestination: 'post',})}>{__('Link to current post','shp-linked-group-block')}</MenuItem></MenuGroup></div>)}{linkDestination==='post'&&(<divclassName="enable-linked-groups__link-popover-post-selected"><divclassName="enable-linked-groups__link-popover-post-selected-label"><spanclassName="enable-linked-groups__link-popover-post-selected-icon"><Iconicon={page}/></span>{__('Linked to current post','shp-linked-group-block')}</div><Buttonicon={linkOff}label={__('Remove link','shp-linked-group-block')}onClick={()=>setAttributes({linkDestination: undefined,})}/></div>)}</Popover>)}</BlockControls></>);};}addFilter('editor.BlockEdit','enable-linked-groups/add-inspector-controls',addInspectorControls);/** * Add linked group classes in the Editor. * * @param {Object} BlockListBlock */functionaddClasses(BlockListBlock){return(props)=>{const{ name, attributes }=props;if('core/group'!==name){return<BlockListBlock{...props}/>;}constclasses=classnames(props?.className,{'is-linked': attributes?.href||attributes?.linkDestination==='post',});return<BlockListBlock{...props}className={classes}/>;};}addFilter('editor.BlockListBlock','enable-linked-groups/add-classes',addClasses);