Skip to content

Commit ae25e5a

Browse files
ellatrixgzioloartemiomoralescolorful-tonesafercia
authored andcommitted
Block bindings: fix Enter on disabled rich text (#59320)
Co-authored-by: ellatrix <ellatrix@git.wordpress.org> Co-authored-by: gziolo <gziolo@git.wordpress.org> Co-authored-by: artemiomorales <artemiosans@git.wordpress.org> Co-authored-by: colorful-tones <colorful-tones@git.wordpress.org> Co-authored-by: afercia <afercia@git.wordpress.org>
1 parent afff734 commit ae25e5a

4 files changed

Lines changed: 60 additions & 45 deletions

File tree

packages/block-editor/src/components/block-edit/context.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { createContext, useContext } from '@wordpress/element';
66
export const mayDisplayControlsKey = Symbol( 'mayDisplayControls' );
77
export const mayDisplayParentControlsKey = Symbol( 'mayDisplayParentControls' );
88
export const blockEditingModeKey = Symbol( 'blockEditingMode' );
9+
export const blockBindingsKey = Symbol( 'blockBindings' );
910

1011
export const DEFAULT_BLOCK_EDIT_CONTEXT = {
1112
name: '',

packages/block-editor/src/components/block-edit/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
mayDisplayControlsKey,
1515
mayDisplayParentControlsKey,
1616
blockEditingModeKey,
17+
blockBindingsKey,
1718
} from './context';
1819

1920
/**
@@ -41,7 +42,8 @@ export default function BlockEdit( {
4142
attributes = {},
4243
__unstableLayoutClassNames,
4344
} = props;
44-
const { layout = null } = attributes;
45+
const { layout = null, metadata = {} } = attributes;
46+
const { bindings } = metadata;
4547
const layoutSupport =
4648
hasBlockSupport( name, 'layout', false ) ||
4749
hasBlockSupport( name, '__experimentalLayout', false );
@@ -62,6 +64,7 @@ export default function BlockEdit( {
6264
[ mayDisplayControlsKey ]: mayDisplayControls,
6365
[ mayDisplayParentControlsKey ]: mayDisplayParentControls,
6466
[ blockEditingModeKey ]: blockEditingMode,
67+
[ blockBindingsKey ]: bindings,
6568
} ),
6669
[
6770
name,
@@ -73,6 +76,7 @@ export default function BlockEdit( {
7376
mayDisplayControls,
7477
mayDisplayParentControls,
7578
blockEditingMode,
79+
bindings,
7680
]
7781
) }
7882
>

packages/block-editor/src/components/rich-text/index.js

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { getBlockType, store as blocksStore } from '@wordpress/blocks';
2626
*/
2727
import { useBlockEditorAutocompleteProps } from '../autocomplete';
2828
import { useBlockEditContext } from '../block-edit';
29+
import { blockBindingsKey } from '../block-edit/context';
2930
import FormatToolbarContainer from './format-toolbar-container';
3031
import { store as blockEditorStore } from '../../store';
3132
import { useUndoAutomaticChange } from './use-undo-automatic-change';
@@ -117,24 +118,20 @@ export function RichTextWrapper(
117118
props = removeNativeProps( props );
118119

119120
const anchorRef = useRef();
120-
const {
121-
clientId,
122-
isSelected: isBlockSelected,
123-
name: blockName,
124-
} = useBlockEditContext();
121+
const context = useBlockEditContext();
122+
const { clientId, isSelected: isBlockSelected, name: blockName } = context;
123+
const blockBindings = context[ blockBindingsKey ];
125124
const selector = ( select ) => {
126125
// Avoid subscribing to the block editor store if the block is not
127126
// selected.
128127
if ( ! isBlockSelected ) {
129128
return { isSelected: false };
130129
}
131130

132-
const { getSelectionStart, getSelectionEnd, getBlockAttributes } =
131+
const { getSelectionStart, getSelectionEnd } =
133132
select( blockEditorStore );
134133
const selectionStart = getSelectionStart();
135134
const selectionEnd = getSelectionEnd();
136-
const blockBindings =
137-
getBlockAttributes( clientId )?.metadata?.bindings;
138135

139136
let isSelected;
140137

@@ -147,48 +144,57 @@ export function RichTextWrapper(
147144
isSelected = selectionStart.clientId === clientId;
148145
}
149146

150-
// Disable Rich Text editing if block bindings specify that.
151-
let disableBoundBlocks = false;
152-
if ( blockBindings && blockName in BLOCK_BINDINGS_ALLOWED_BLOCKS ) {
153-
const blockTypeAttributes = getBlockType( blockName ).attributes;
154-
const { getBlockBindingsSource } = unlock( select( blocksStore ) );
155-
for ( const [ attribute, args ] of Object.entries(
156-
blockBindings
157-
) ) {
158-
if (
159-
blockTypeAttributes?.[ attribute ]?.source !== 'rich-text'
160-
) {
161-
break;
162-
}
163-
164-
// If the source is not defined, or if its value of `lockAttributesEditing` is `true`, disable it.
165-
const blockBindingsSource = getBlockBindingsSource(
166-
args.source
167-
);
168-
if (
169-
! blockBindingsSource ||
170-
blockBindingsSource.lockAttributesEditing
171-
) {
172-
disableBoundBlocks = true;
173-
break;
174-
}
175-
}
176-
}
177-
178147
return {
179148
selectionStart: isSelected ? selectionStart.offset : undefined,
180149
selectionEnd: isSelected ? selectionEnd.offset : undefined,
181150
isSelected,
182-
disableBoundBlocks,
183151
};
184152
};
185-
const { selectionStart, selectionEnd, isSelected, disableBoundBlocks } =
186-
useSelect( selector, [
187-
clientId,
188-
identifier,
189-
originalIsSelected,
190-
isBlockSelected,
191-
] );
153+
const { selectionStart, selectionEnd, isSelected } = useSelect( selector, [
154+
clientId,
155+
identifier,
156+
originalIsSelected,
157+
isBlockSelected,
158+
] );
159+
160+
const disableBoundBlocks = useSelect(
161+
( select ) => {
162+
// Disable Rich Text editing if block bindings specify that.
163+
let _disableBoundBlocks = false;
164+
if ( blockBindings && blockName in BLOCK_BINDINGS_ALLOWED_BLOCKS ) {
165+
const blockTypeAttributes =
166+
getBlockType( blockName ).attributes;
167+
const { getBlockBindingsSource } = unlock(
168+
select( blocksStore )
169+
);
170+
for ( const [ attribute, args ] of Object.entries(
171+
blockBindings
172+
) ) {
173+
if (
174+
blockTypeAttributes?.[ attribute ]?.source !==
175+
'rich-text'
176+
) {
177+
break;
178+
}
179+
180+
// If the source is not defined, or if its value of `lockAttributesEditing` is `true`, disable it.
181+
const blockBindingsSource = getBlockBindingsSource(
182+
args.source
183+
);
184+
if (
185+
! blockBindingsSource ||
186+
blockBindingsSource.lockAttributesEditing
187+
) {
188+
_disableBoundBlocks = true;
189+
break;
190+
}
191+
}
192+
}
193+
194+
return _disableBoundBlocks;
195+
},
196+
[ blockBindings, blockName ]
197+
);
192198

193199
const shouldDisableEditing = disableEditing || disableBoundBlocks;
194200

packages/block-editor/src/components/rich-text/use-enter.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ export function useEnter( props ) {
2121
propsRef.current = props;
2222
return useRefEffect( ( element ) => {
2323
function onKeyDown( event ) {
24+
if ( event.target.contentEditable !== 'true' ) {
25+
return;
26+
}
27+
2428
if ( event.defaultPrevented ) {
2529
return;
2630
}

0 commit comments

Comments
 (0)