@@ -26,6 +26,7 @@ import { getBlockType, store as blocksStore } from '@wordpress/blocks';
2626 */
2727import { useBlockEditorAutocompleteProps } from '../autocomplete' ;
2828import { useBlockEditContext } from '../block-edit' ;
29+ import { blockBindingsKey } from '../block-edit/context' ;
2930import FormatToolbarContainer from './format-toolbar-container' ;
3031import { store as blockEditorStore } from '../../store' ;
3132import { 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
0 commit comments