Skip to content

Commit

Permalink
Block bindings: fix Enter on disabled rich text (#59320)
Browse files Browse the repository at this point in the history
Co-authored-by: ellatrix <[email protected]>
Co-authored-by: gziolo <[email protected]>
Co-authored-by: artemiomorales <[email protected]>
Co-authored-by: colorful-tones <[email protected]>
Co-authored-by: afercia <[email protected]>
  • Loading branch information
6 people authored Feb 24, 2024
1 parent b91beb0 commit 5be7d52
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 45 deletions.
1 change: 1 addition & 0 deletions packages/block-editor/src/components/block-edit/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { createContext, useContext } from '@wordpress/element';
export const mayDisplayControlsKey = Symbol( 'mayDisplayControls' );
export const mayDisplayParentControlsKey = Symbol( 'mayDisplayParentControls' );
export const blockEditingModeKey = Symbol( 'blockEditingMode' );
export const blockBindingsKey = Symbol( 'blockBindings' );

export const DEFAULT_BLOCK_EDIT_CONTEXT = {
name: '',
Expand Down
6 changes: 5 additions & 1 deletion packages/block-editor/src/components/block-edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
mayDisplayControlsKey,
mayDisplayParentControlsKey,
blockEditingModeKey,
blockBindingsKey,
} from './context';

/**
Expand Down Expand Up @@ -41,7 +42,8 @@ export default function BlockEdit( {
attributes = {},
__unstableLayoutClassNames,
} = props;
const { layout = null } = attributes;
const { layout = null, metadata = {} } = attributes;
const { bindings } = metadata;
const layoutSupport =
hasBlockSupport( name, 'layout', false ) ||
hasBlockSupport( name, '__experimentalLayout', false );
Expand All @@ -62,6 +64,7 @@ export default function BlockEdit( {
[ mayDisplayControlsKey ]: mayDisplayControls,
[ mayDisplayParentControlsKey ]: mayDisplayParentControls,
[ blockEditingModeKey ]: blockEditingMode,
[ blockBindingsKey ]: bindings,
} ),
[
name,
Expand All @@ -73,6 +76,7 @@ export default function BlockEdit( {
mayDisplayControls,
mayDisplayParentControls,
blockEditingMode,
bindings,
]
) }
>
Expand Down
94 changes: 50 additions & 44 deletions packages/block-editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { getBlockType, store as blocksStore } from '@wordpress/blocks';
*/
import { useBlockEditorAutocompleteProps } from '../autocomplete';
import { useBlockEditContext } from '../block-edit';
import { blockBindingsKey } from '../block-edit/context';
import FormatToolbarContainer from './format-toolbar-container';
import { store as blockEditorStore } from '../../store';
import { useUndoAutomaticChange } from './use-undo-automatic-change';
Expand Down Expand Up @@ -117,24 +118,20 @@ export function RichTextWrapper(
props = removeNativeProps( props );

const anchorRef = useRef();
const {
clientId,
isSelected: isBlockSelected,
name: blockName,
} = useBlockEditContext();
const context = useBlockEditContext();
const { clientId, isSelected: isBlockSelected, name: blockName } = context;
const blockBindings = context[ blockBindingsKey ];
const selector = ( select ) => {
// Avoid subscribing to the block editor store if the block is not
// selected.
if ( ! isBlockSelected ) {
return { isSelected: false };
}

const { getSelectionStart, getSelectionEnd, getBlockAttributes } =
const { getSelectionStart, getSelectionEnd } =
select( blockEditorStore );
const selectionStart = getSelectionStart();
const selectionEnd = getSelectionEnd();
const blockBindings =
getBlockAttributes( clientId )?.metadata?.bindings;

let isSelected;

Expand All @@ -147,48 +144,57 @@ export function RichTextWrapper(
isSelected = selectionStart.clientId === clientId;
}

// Disable Rich Text editing if block bindings specify that.
let disableBoundBlocks = false;
if ( blockBindings && blockName in BLOCK_BINDINGS_ALLOWED_BLOCKS ) {
const blockTypeAttributes = getBlockType( blockName ).attributes;
const { getBlockBindingsSource } = unlock( select( blocksStore ) );
for ( const [ attribute, args ] of Object.entries(
blockBindings
) ) {
if (
blockTypeAttributes?.[ attribute ]?.source !== 'rich-text'
) {
break;
}

// If the source is not defined, or if its value of `lockAttributesEditing` is `true`, disable it.
const blockBindingsSource = getBlockBindingsSource(
args.source
);
if (
! blockBindingsSource ||
blockBindingsSource.lockAttributesEditing
) {
disableBoundBlocks = true;
break;
}
}
}

return {
selectionStart: isSelected ? selectionStart.offset : undefined,
selectionEnd: isSelected ? selectionEnd.offset : undefined,
isSelected,
disableBoundBlocks,
};
};
const { selectionStart, selectionEnd, isSelected, disableBoundBlocks } =
useSelect( selector, [
clientId,
identifier,
originalIsSelected,
isBlockSelected,
] );
const { selectionStart, selectionEnd, isSelected } = useSelect( selector, [
clientId,
identifier,
originalIsSelected,
isBlockSelected,
] );

const disableBoundBlocks = useSelect(
( select ) => {
// Disable Rich Text editing if block bindings specify that.
let _disableBoundBlocks = false;
if ( blockBindings && blockName in BLOCK_BINDINGS_ALLOWED_BLOCKS ) {
const blockTypeAttributes =
getBlockType( blockName ).attributes;
const { getBlockBindingsSource } = unlock(
select( blocksStore )
);
for ( const [ attribute, args ] of Object.entries(
blockBindings
) ) {
if (
blockTypeAttributes?.[ attribute ]?.source !==
'rich-text'
) {
break;
}

// If the source is not defined, or if its value of `lockAttributesEditing` is `true`, disable it.
const blockBindingsSource = getBlockBindingsSource(
args.source
);
if (
! blockBindingsSource ||
blockBindingsSource.lockAttributesEditing
) {
_disableBoundBlocks = true;
break;
}
}
}

return _disableBoundBlocks;
},
[ blockBindings, blockName ]
);

const shouldDisableEditing = disableEditing || disableBoundBlocks;

Expand Down
4 changes: 4 additions & 0 deletions packages/block-editor/src/components/rich-text/use-enter.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export function useEnter( props ) {
propsRef.current = props;
return useRefEffect( ( element ) => {
function onKeyDown( event ) {
if ( event.target.contentEditable !== 'true' ) {
return;
}

if ( event.defaultPrevented ) {
return;
}
Expand Down

0 comments on commit 5be7d52

Please sign in to comment.