Skip to content

Commit

Permalink
Fix query loop with block bindings not working in the editor as expec…
Browse files Browse the repository at this point in the history
…ted (#59283)

* Move actions and selectors to blocks package

* Update use bindings attributes hook to use blocksStore

* Update blocks using getBlockBindingsSource

* Update register sources dependency

Co-authored-by: SantosGuillamot <santosguillamot@git.wordpress.org>
Co-authored-by: gziolo <gziolo@git.wordpress.org>
Co-authored-by: artemiomorales <artemiosans@git.wordpress.org>
  • Loading branch information
4 people authored Feb 23, 2024
1 parent 599f336 commit a691bdd
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 52 deletions.
6 changes: 2 additions & 4 deletions packages/block-editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
removeFormat,
} from '@wordpress/rich-text';
import { Popover } from '@wordpress/components';
import { getBlockType } from '@wordpress/blocks';
import { getBlockType, store as blocksStore } from '@wordpress/blocks';

/**
* Internal dependencies
Expand Down Expand Up @@ -151,9 +151,7 @@ export function RichTextWrapper(
let disableBoundBlocks = false;
if ( blockBindings && blockName in BLOCK_BINDINGS_ALLOWED_BLOCKS ) {
const blockTypeAttributes = getBlockType( blockName ).attributes;
const { getBlockBindingsSource } = unlock(
select( blockEditorStore )
);
const { getBlockBindingsSource } = unlock( select( blocksStore ) );
for ( const [ attribute, args ] of Object.entries(
blockBindings
) ) {
Expand Down
12 changes: 5 additions & 7 deletions packages/block-editor/src/hooks/use-bindings-attributes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { getBlockType } from '@wordpress/blocks';
import { getBlockType, store as blocksStore } from '@wordpress/blocks';
import { createHigherOrderComponent } from '@wordpress/compose';
import { useSelect } from '@wordpress/data';
import { addFilter } from '@wordpress/hooks';
Expand Down Expand Up @@ -33,18 +33,16 @@ const createEditFunctionWithBindingsAttribute = () =>
createHigherOrderComponent(
( BlockEdit ) => ( props ) => {
const { clientId, name: blockName } = useBlockEditContext();
const { getBlockBindingsSource } = unlock(
useSelect( blockEditorStore )
);
const blockBindingsSources = unlock(
useSelect( blocksStore )
).getAllBlockBindingsSources();
const { getBlockAttributes } = useSelect( blockEditorStore );

const updatedAttributes = getBlockAttributes( clientId );
if ( updatedAttributes?.metadata?.bindings ) {
Object.entries( updatedAttributes.metadata.bindings ).forEach(
( [ attributeName, settings ] ) => {
const source = getBlockBindingsSource(
settings.source
);
const source = blockBindingsSources[ settings.source ];

if ( source && source.useSource ) {
// Second argument (`updateMetaValue`) will be used to update the value in the future.
Expand Down
10 changes: 0 additions & 10 deletions packages/block-editor/src/store/private-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,16 +355,6 @@ export function stopEditingAsBlocks( clientId ) {
};
}

export function registerBlockBindingsSource( source ) {
return {
type: 'REGISTER_BLOCK_BINDINGS_SOURCE',
sourceName: source.name,
sourceLabel: source.label,
useSource: source.useSource,
lockAttributesEditing: source.lockAttributesEditing,
};
}

/**
* Returns an action object used in signalling that the user has begun to drag.
*
Expand Down
8 changes: 0 additions & 8 deletions packages/block-editor/src/store/private-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,14 +341,6 @@ export function getLastFocus( state ) {
return state.lastFocus;
}

export function getAllBlockBindingsSources( state ) {
return state.blockBindingsSources;
}

export function getBlockBindingsSource( state, sourceName ) {
return state.blockBindingsSources[ sourceName ];
}

/**
* Returns true if the user is dragging anything, or false otherwise. It is possible for a
* user to be dragging data from outside of the editor, so this selector is separate from
Expand Down
15 changes: 0 additions & 15 deletions packages/block-editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2044,20 +2044,6 @@ export function lastFocus( state = false, action ) {
return state;
}

function blockBindingsSources( state = {}, action ) {
if ( action.type === 'REGISTER_BLOCK_BINDINGS_SOURCE' ) {
return {
...state,
[ action.sourceName ]: {
label: action.sourceLabel,
useSource: action.useSource,
lockAttributesEditing: action.lockAttributesEditing ?? true,
},
};
}
return state;
}

const combinedReducers = combineReducers( {
blocks,
isDragging,
Expand Down Expand Up @@ -2089,7 +2075,6 @@ const combinedReducers = combineReducers( {
blockRemovalRules,
openedBlockSettingsMenu,
registeredInserterMediaCategories,
blockBindingsSources,
} );

function withAutomaticChangeReset( reducer ) {
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/button/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
createBlock,
cloneBlock,
getDefaultBlockName,
store as blocksStore,
} from '@wordpress/blocks';
import { useMergeRefs, useRefEffect } from '@wordpress/compose';
import { useSelect, useDispatch } from '@wordpress/data';
Expand Down Expand Up @@ -239,7 +240,7 @@ function ButtonEdit( props ) {
}

const blockBindingsSource = unlock(
select( blockEditorStore )
select( blocksStore )
).getBlockBindingsSource( metadata?.bindings?.url?.source );

return {
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import classnames from 'classnames';
* WordPress dependencies
*/
import { getBlobByURL, isBlobURL, revokeBlobURL } from '@wordpress/blob';
import { store as blocksStore } from '@wordpress/blocks';
import { Placeholder } from '@wordpress/components';
import { useDispatch, useSelect } from '@wordpress/data';
import {
Expand Down Expand Up @@ -342,7 +343,7 @@ export function ImageEdit( {
}

const blockBindingsSource = unlock(
select( blockEditorStore )
select( blocksStore )
).getBlockBindingsSource( metadata?.bindings?.url?.source );

return {
Expand Down
9 changes: 5 additions & 4 deletions packages/block-library/src/image/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { useEffect, useMemo, useState, useRef } from '@wordpress/element';
import { __, _x, sprintf, isRTL } from '@wordpress/i18n';
import { DOWN } from '@wordpress/keycodes';
import { getFilename } from '@wordpress/url';
import { switchToBlockType } from '@wordpress/blocks';
import { switchToBlockType, store as blocksStore } from '@wordpress/blocks';
import { crop, overlayText, upload } from '@wordpress/icons';
import { store as noticesStore } from '@wordpress/notices';
import { store as coreStore } from '@wordpress/core-data';
Expand Down Expand Up @@ -417,9 +417,10 @@ export default function Image( {
if ( ! isSingleSelected ) {
return {};
}

const { getBlockBindingsSource, getBlockParentsByBlockName } =
unlock( select( blockEditorStore ) );
const { getBlockBindingsSource } = unlock( select( blocksStore ) );
const { getBlockParentsByBlockName } = unlock(
select( blockEditorStore )
);
const {
url: urlBinding,
alt: altBinding,
Expand Down
15 changes: 15 additions & 0 deletions packages/blocks/src/store/private-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,18 @@ export function addUnprocessedBlockType( name, blockType ) {
dispatch.addBlockTypes( processedBlockType );
};
}

/**
* Register new block bindings source.
*
* @param {string} source Name of the source to register.
*/
export function registerBlockBindingsSource( source ) {
return {
type: 'REGISTER_BLOCK_BINDINGS_SOURCE',
sourceName: source.name,
sourceLabel: source.label,
useSource: source.useSource,
lockAttributesEditing: source.lockAttributesEditing,
};
}
23 changes: 23 additions & 0 deletions packages/blocks/src/store/private-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,26 @@ export function getBootstrappedBlockType( state, name ) {
export function getUnprocessedBlockTypes( state ) {
return state.unprocessedBlockTypes;
}

/**
* Returns all the block bindings sources registered.
*
* @param {Object} state Data state.
*
* @return {Object} All the registered sources and their properties.
*/
export function getAllBlockBindingsSources( state ) {
return state.blockBindingsSources;
}

/**
* Returns a specific block bindings source.
*
* @param {Object} state Data state.
* @param {string} sourceName Name of the source to get.
*
* @return {Object} The specific block binding source and its properties.
*/
export function getBlockBindingsSource( state, sourceName ) {
return state.blockBindingsSources[ sourceName ];
}
15 changes: 15 additions & 0 deletions packages/blocks/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,20 @@ export function collections( state = {}, action ) {
return state;
}

export function blockBindingsSources( state = {}, action ) {
if ( action.type === 'REGISTER_BLOCK_BINDINGS_SOURCE' ) {
return {
...state,
[ action.sourceName ]: {
label: action.sourceLabel,
useSource: action.useSource,
lockAttributesEditing: action.lockAttributesEditing ?? true,
},
};
}
return state;
}

export default combineReducers( {
bootstrappedBlockTypes,
unprocessedBlockTypes,
Expand All @@ -395,4 +409,5 @@ export default combineReducers( {
groupingBlockName,
categories,
collections,
blockBindingsSources,
} );
4 changes: 2 additions & 2 deletions packages/editor/src/bindings/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { store as blockEditorStore } from '@wordpress/block-editor';
import { store as blocksStore } from '@wordpress/blocks';
import { dispatch } from '@wordpress/data';
/**
* Internal dependencies
Expand All @@ -10,6 +10,6 @@ import { unlock } from '../lock-unlock';
import patternOverrides from './pattern-overrides';
import postMeta from './post-meta';

const { registerBlockBindingsSource } = unlock( dispatch( blockEditorStore ) );
const { registerBlockBindingsSource } = unlock( dispatch( blocksStore ) );
registerBlockBindingsSource( patternOverrides );
registerBlockBindingsSource( postMeta );

1 comment on commit a691bdd

@github-actions
Copy link

@github-actions github-actions bot commented on a691bdd Feb 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in a691bdd.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/8016494861
📝 Reported issues:

Please sign in to comment.