diff --git a/packages/block-editor/src/components/rich-text/index.js b/packages/block-editor/src/components/rich-text/index.js index 01753ceda4728e..5f94206c78752f 100644 --- a/packages/block-editor/src/components/rich-text/index.js +++ b/packages/block-editor/src/components/rich-text/index.js @@ -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 @@ -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 ) ) { diff --git a/packages/block-editor/src/hooks/use-bindings-attributes.js b/packages/block-editor/src/hooks/use-bindings-attributes.js index 899faf0a8cbd5d..0e5b6614f07cbf 100644 --- a/packages/block-editor/src/hooks/use-bindings-attributes.js +++ b/packages/block-editor/src/hooks/use-bindings-attributes.js @@ -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'; @@ -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. diff --git a/packages/block-editor/src/store/private-actions.js b/packages/block-editor/src/store/private-actions.js index 606cd01228204f..d402d45657704c 100644 --- a/packages/block-editor/src/store/private-actions.js +++ b/packages/block-editor/src/store/private-actions.js @@ -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. * diff --git a/packages/block-editor/src/store/private-selectors.js b/packages/block-editor/src/store/private-selectors.js index c885e43ba75208..e4885cbbd9e1e1 100644 --- a/packages/block-editor/src/store/private-selectors.js +++ b/packages/block-editor/src/store/private-selectors.js @@ -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 diff --git a/packages/block-editor/src/store/reducer.js b/packages/block-editor/src/store/reducer.js index 33678f64905d69..751a19a1c2a8c2 100644 --- a/packages/block-editor/src/store/reducer.js +++ b/packages/block-editor/src/store/reducer.js @@ -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, @@ -2089,7 +2075,6 @@ const combinedReducers = combineReducers( { blockRemovalRules, openedBlockSettingsMenu, registeredInserterMediaCategories, - blockBindingsSources, } ); function withAutomaticChangeReset( reducer ) { diff --git a/packages/block-library/src/button/edit.js b/packages/block-library/src/button/edit.js index ff90cdd1bf64c0..1a35cea9d8cc7d 100644 --- a/packages/block-library/src/button/edit.js +++ b/packages/block-library/src/button/edit.js @@ -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'; @@ -239,7 +240,7 @@ function ButtonEdit( props ) { } const blockBindingsSource = unlock( - select( blockEditorStore ) + select( blocksStore ) ).getBlockBindingsSource( metadata?.bindings?.url?.source ); return { diff --git a/packages/block-library/src/image/edit.js b/packages/block-library/src/image/edit.js index 86970b588ff03d..e1e221bc9575a0 100644 --- a/packages/block-library/src/image/edit.js +++ b/packages/block-library/src/image/edit.js @@ -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 { @@ -342,7 +343,7 @@ export function ImageEdit( { } const blockBindingsSource = unlock( - select( blockEditorStore ) + select( blocksStore ) ).getBlockBindingsSource( metadata?.bindings?.url?.source ); return { diff --git a/packages/block-library/src/image/image.js b/packages/block-library/src/image/image.js index eecb4da72b9ce6..ea0f82a2e1986b 100644 --- a/packages/block-library/src/image/image.js +++ b/packages/block-library/src/image/image.js @@ -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'; @@ -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, diff --git a/packages/blocks/src/store/private-actions.js b/packages/blocks/src/store/private-actions.js index bc06e231b17222..d609f70b91b55d 100644 --- a/packages/blocks/src/store/private-actions.js +++ b/packages/blocks/src/store/private-actions.js @@ -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, + }; +} diff --git a/packages/blocks/src/store/private-selectors.js b/packages/blocks/src/store/private-selectors.js index f2acf9c1051846..e6eac8be421466 100644 --- a/packages/blocks/src/store/private-selectors.js +++ b/packages/blocks/src/store/private-selectors.js @@ -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 ]; +} diff --git a/packages/blocks/src/store/reducer.js b/packages/blocks/src/store/reducer.js index 91f061871f9e27..f92fb376b530a7 100644 --- a/packages/blocks/src/store/reducer.js +++ b/packages/blocks/src/store/reducer.js @@ -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, @@ -395,4 +409,5 @@ export default combineReducers( { groupingBlockName, categories, collections, + blockBindingsSources, } ); diff --git a/packages/editor/src/bindings/index.js b/packages/editor/src/bindings/index.js index ff0516902e321c..1977f9980b067c 100644 --- a/packages/editor/src/bindings/index.js +++ b/packages/editor/src/bindings/index.js @@ -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 @@ -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 );