Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abstract block bindings attributes locking #66638

Draft
wants to merge 5 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion packages/block-editor/src/components/block-edit/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ import {
getBlockDefaultClassName,
hasBlockSupport,
getBlockType,
getBlockBindingsSource,
} from '@wordpress/blocks';
import { useSelect } from '@wordpress/data';
import { useContext, useMemo } from '@wordpress/element';
/**
* Internal dependencies
*/
import { InspectorControls, BlockControls } from '../../components';

/**
* Internal dependencies
Expand All @@ -29,6 +35,41 @@ import BlockContext from '../block-context';
*/
const DEFAULT_BLOCK_CONTEXT = {};

const AttributeWrapper = ( { control, ...props } ) => {
const { context, isSelected, attributes } = props;
const { metadata } = attributes;
const { key } = control;
const isDisabled = useSelect(
( select ) => {
if ( ! isSelected ) {
return {};
}

const blockBindingsSource = getBlockBindingsSource(
metadata?.bindings?.[ key ]?.source
);

return (
!! metadata?.bindings?.[ key ] &&
! blockBindingsSource?.canUserEditValue?.( {
select,
context,
args: metadata?.bindings?.[ key ]?.args,
} )
);
},
[ context, isSelected, key, metadata?.bindings ]
);
const Wrapper =
control.type === 'toolbar' ? BlockControls : InspectorControls;

return (
<Wrapper group={ control.group } key={ control.key }>
<control.Control isDisabled={ isDisabled } { ...props } />
</Wrapper>
);
};

const Edit = ( props ) => {
const { name } = props;
const blockType = getBlockType( name );
Expand All @@ -37,12 +78,22 @@ const Edit = ( props ) => {
return null;
}

const controls = [];

if ( blockType.attributeControls?.length > 0 ) {
for ( const control of blockType.attributeControls ) {
controls.push(
<AttributeWrapper control={ control } { ...props } />
);
}
}

// `edit` and `save` are functions or components describing the markup
// with which a block is displayed. If `blockType` is valid, assign
// them preferentially as the render value for the block.
const Component = blockType.edit || blockType.save;

return <Component { ...props } />;
return [ <Component { ...props } key="content" />, controls ];
};

const EditWithFilters = withFilters( 'editor.BlockEdit' )( Edit );
Expand Down
202 changes: 3 additions & 199 deletions packages/block-library/src/button/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,32 @@ import clsx from 'clsx';
/**
* Internal dependencies
*/
import { NEW_TAB_TARGET, NOFOLLOW_REL } from './constants';
import { getUpdatedLinkAttributes } from './get-updated-link-attributes';
import removeAnchorTag from '../utils/remove-anchor-tag';

/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { useEffect, useState, useRef, useMemo } from '@wordpress/element';
import { useEffect, useState, useRef } from '@wordpress/element';
import {
Button,
ButtonGroup,
PanelBody,
TextControl,
ToolbarButton,
Popover,
} from '@wordpress/components';
import {
AlignmentControl,
BlockControls,
InspectorControls,
RichText,
useBlockProps,
__experimentalUseBorderProps as useBorderProps,
__experimentalUseColorProps as useColorProps,
__experimentalGetSpacingClassesAndStyles as useSpacingProps,
__experimentalGetShadowClassesAndStyles as useShadowProps,
__experimentalLinkControl as LinkControl,
__experimentalGetElementClassName,
store as blockEditorStore,
useBlockEditingMode,
} from '@wordpress/block-editor';
import { displayShortcut, isKeyboardEvent, ENTER } from '@wordpress/keycodes';
import { link, linkOff } from '@wordpress/icons';
import { isKeyboardEvent, ENTER } from '@wordpress/keycodes';
import {
createBlock,
cloneBlock,
getDefaultBlockName,
getBlockBindingsSource,
} from '@wordpress/blocks';
import { useMergeRefs, useRefEffect } from '@wordpress/compose';
import { useSelect, useDispatch } from '@wordpress/data';

const LINK_SETTINGS = [
...LinkControl.DEFAULT_LINK_SETTINGS,
{
id: 'nofollow',
title: __( 'Mark as nofollow' ),
},
];

function useEnter( props ) {
const { replaceBlocks, selectionChange } = useDispatch( blockEditorStore );
const { getBlock, getBlockRootClientId, getBlockIndex } =
Expand Down Expand Up @@ -113,39 +88,6 @@ function useEnter( props ) {
}, [] );
}

function WidthPanel( { selectedWidth, setAttributes } ) {
function handleChange( newWidth ) {
// Check if we are toggling the width off
const width = selectedWidth === newWidth ? undefined : newWidth;

// Update attributes.
setAttributes( { width } );
}

return (
<PanelBody title={ __( 'Settings' ) }>
<ButtonGroup aria-label={ __( 'Button width' ) }>
{ [ 25, 50, 75, 100 ].map( ( widthValue ) => {
return (
<Button
key={ widthValue }
size="small"
variant={
widthValue === selectedWidth
? 'primary'
: undefined
}
onClick={ () => handleChange( widthValue ) }
>
{ widthValue }%
</Button>
);
} ) }
</ButtonGroup>
</PanelBody>
);
}

function ButtonEdit( props ) {
const {
attributes,
Expand All @@ -155,22 +97,8 @@ function ButtonEdit( props ) {
onReplace,
mergeBlocks,
clientId,
context,
} = props;
const {
tagName,
textAlign,
linkTarget,
placeholder,
rel,
style,
text,
url,
width,
metadata,
} = attributes;

const TagName = tagName || 'a';
const { textAlign, placeholder, style, text, width } = attributes;

function onKeyDown( event ) {
if ( isKeyboardEvent.primary( event, 'k' ) ) {
Expand All @@ -195,13 +123,7 @@ function ButtonEdit( props ) {
ref: useMergeRefs( [ setPopoverAnchor, ref ] ),
onKeyDown,
} );
const blockEditingMode = useBlockEditingMode();

const [ isEditingURL, setIsEditingURL ] = useState( false );
const isURLSet = !! url;
const opensInNewTab = linkTarget === NEW_TAB_TARGET;
const nofollow = !! rel?.includes( NOFOLLOW_REL );
const isLinkTag = 'a' === TagName;

function startEditing( event ) {
event.preventDefault();
Expand All @@ -223,39 +145,9 @@ function ButtonEdit( props ) {
}
}, [ isSelected ] );

// Memoize link value to avoid overriding the LinkControl's internal state.
// This is a temporary fix. See https://github.com/WordPress/gutenberg/issues/51256.
const linkValue = useMemo(
() => ( { url, opensInNewTab, nofollow } ),
[ url, opensInNewTab, nofollow ]
);

const useEnterRef = useEnter( { content: text, clientId } );
const mergedRef = useMergeRefs( [ useEnterRef, richTextRef ] );

const { lockUrlControls = false } = useSelect(
( select ) => {
if ( ! isSelected ) {
return {};
}

const blockBindingsSource = getBlockBindingsSource(
metadata?.bindings?.url?.source
);

return {
lockUrlControls:
!! metadata?.bindings?.url &&
! blockBindingsSource?.canUserEditValue?.( {
select,
context,
args: metadata?.bindings?.url?.args,
} ),
};
},
[ context, isSelected, metadata?.bindings?.url ]
);

return (
<>
<div
Expand Down Expand Up @@ -301,94 +193,6 @@ function ButtonEdit( props ) {
identifier="text"
/>
</div>
<BlockControls group="block">
{ blockEditingMode === 'default' && (
<AlignmentControl
value={ textAlign }
onChange={ ( nextAlign ) => {
setAttributes( { textAlign: nextAlign } );
} }
/>
) }
{ ! isURLSet && isLinkTag && ! lockUrlControls && (
<ToolbarButton
name="link"
icon={ link }
title={ __( 'Link' ) }
shortcut={ displayShortcut.primary( 'k' ) }
onClick={ startEditing }
/>
) }
{ isURLSet && isLinkTag && ! lockUrlControls && (
<ToolbarButton
name="link"
icon={ linkOff }
title={ __( 'Unlink' ) }
shortcut={ displayShortcut.primaryShift( 'k' ) }
onClick={ unlink }
isActive
/>
) }
</BlockControls>
{ isLinkTag &&
isSelected &&
( isEditingURL || isURLSet ) &&
! lockUrlControls && (
<Popover
placement="bottom"
onClose={ () => {
setIsEditingURL( false );
richTextRef.current?.focus();
} }
anchor={ popoverAnchor }
focusOnMount={ isEditingURL ? 'firstElement' : false }
__unstableSlotName="__unstable-block-tools-after"
shift
>
<LinkControl
value={ linkValue }
onChange={ ( {
url: newURL,
opensInNewTab: newOpensInNewTab,
nofollow: newNofollow,
} ) =>
setAttributes(
getUpdatedLinkAttributes( {
rel,
url: newURL,
opensInNewTab: newOpensInNewTab,
nofollow: newNofollow,
} )
)
}
onRemove={ () => {
unlink();
richTextRef.current?.focus();
} }
forceIsEditingLink={ isEditingURL }
settings={ LINK_SETTINGS }
/>
</Popover>
) }
<InspectorControls>
<WidthPanel
selectedWidth={ width }
setAttributes={ setAttributes }
/>
</InspectorControls>
<InspectorControls group="advanced">
{ isLinkTag && (
<TextControl
__next40pxDefaultSize
__nextHasNoMarginBottom
label={ __( 'Link rel' ) }
value={ rel || '' }
onChange={ ( newRel ) =>
setAttributes( { rel: newRel } )
}
/>
) }
</InspectorControls>
</>
);
}
Expand Down
Loading
Loading