Skip to content

Commit

Permalink
Disable pattern overrides block binding source when editing original …
Browse files Browse the repository at this point in the history
…pattern
  • Loading branch information
talldan committed Jun 25, 2024
1 parent d97e3f7 commit 383b9fa
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 17 deletions.
22 changes: 19 additions & 3 deletions packages/block-editor/src/hooks/use-bindings-attributes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/**
* WordPress dependencies
*/
import { store as blocksStore } from '@wordpress/blocks';
import {
store as blocksStore,
privateApis as blocksPrivateApis,
} from '@wordpress/blocks';
import { createHigherOrderComponent } from '@wordpress/compose';
import { useRegistry, useSelect } from '@wordpress/data';
import { useCallback, useMemo } from '@wordpress/element';
Expand All @@ -12,6 +15,8 @@ import { addFilter } from '@wordpress/hooks';
*/
import { unlock } from '../lock-unlock';

const { isBindingSourceActiveKey } = unlock( blocksPrivateApis );

/** @typedef {import('@wordpress/compose').WPHigherOrderComponent} WPHigherOrderComponent */
/** @typedef {import('@wordpress/blocks').WPBlockSettings} WPBlockSettings */

Expand Down Expand Up @@ -92,14 +97,25 @@ export function canBindAttribute( blockName, attributeName ) {

export const withBlockBindingSupport = createHigherOrderComponent(
( BlockEdit ) => ( props ) => {
const { name, clientId, context } = props;
const registry = useRegistry();
const sources = useSelect( ( select ) =>
const registeredSources = useSelect( ( select ) =>
unlock( select( blocksStore ) ).getAllBlockBindingsSources()
);
const { name, clientId, context } = props;
const sources = Object.fromEntries(
Object.entries( registeredSources ).filter(
( [ , sourceData ] ) =>
sourceData[ isBindingSourceActiveKey ]?.( {
registry,
clientId,
} ) ?? true
)
);
const hasPatternOverridesDefaultBinding =
!! sources[ 'core/pattern-overrides' ] &&
props.attributes.metadata?.bindings?.[ DEFAULT_ATTRIBUTE ]
?.source === 'core/pattern-overrides';

const bindings = useMemo(
() =>
replacePatternOverrideDefaultBindings(
Expand Down
4 changes: 4 additions & 0 deletions packages/blocks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,10 @@ _Returns_

- `Array|string`: A list of blocks or a string, depending on `handlerMode`.

### privateApis

Private @wordpress/blocks APIs.

### rawHandler

Converts an HTML string to known blocks.
Expand Down
1 change: 1 addition & 0 deletions packages/blocks/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
export { store } from './store';
export * from './api';
export * from './deprecated';
export { privateApis } from './private-apis';
13 changes: 13 additions & 0 deletions packages/blocks/src/private-apis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Internal dependencies
*/
import { lock } from './lock-unlock';
import { isBindingSourceActiveKey } from './store/constants';

/**
* Private @wordpress/blocks APIs.
*/
export const privateApis = {};
lock( privateApis, {
isBindingSourceActiveKey,
} );
2 changes: 2 additions & 0 deletions packages/blocks/src/store/constants.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export const STORE_NAME = 'core/blocks';

export const isBindingSourceActiveKey = Symbol( 'isActive' );
8 changes: 1 addition & 7 deletions packages/blocks/src/store/private-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@ export function addUnprocessedBlockType( name, blockType ) {
export function registerBlockBindingsSource( source ) {
return {
type: 'REGISTER_BLOCK_BINDINGS_SOURCE',
sourceName: source.name,
sourceLabel: source.label,
getValue: source.getValue,
setValue: source.setValue,
setValues: source.setValues,
getPlaceholder: source.getPlaceholder,
canUserEditValue: source.canUserEditValue,
source,
};
}
18 changes: 11 additions & 7 deletions packages/blocks/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { __ } from '@wordpress/i18n';
* Internal dependencies
*/
import { omit } from '../api/utils';
import { isBindingSourceActiveKey } from './constants';

/**
* @typedef {Object} WPBlockCategory
Expand Down Expand Up @@ -373,15 +374,18 @@ export function collections( state = {}, action ) {

export function blockBindingsSources( state = {}, action ) {
if ( action.type === 'REGISTER_BLOCK_BINDINGS_SOURCE' ) {
const { source } = action;
return {
...state,
[ action.sourceName ]: {
label: action.sourceLabel,
getValue: action.getValue,
setValue: action.setValue,
setValues: action.setValues,
getPlaceholder: action.getPlaceholder,
canUserEditValue: action.canUserEditValue || ( () => false ),
[ source.name ]: {
label: source.label,
getValue: source.getValue,
setValue: source.setValue,
setValues: source.setValues,
getPlaceholder: source.getPlaceholder,
canUserEditValue: source.canUserEditValue || ( () => false ),
[ isBindingSourceActiveKey ]:
source[ isBindingSourceActiveKey ],
},
};
}
Expand Down
19 changes: 19 additions & 0 deletions packages/editor/src/bindings/pattern-overrides.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,31 @@
*/
import { _x } from '@wordpress/i18n';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { privateApis as blocksPrivateApis } from '@wordpress/blocks';

/**
* Internal dependencies
*/
import { unlock } from '../lock-unlock';

const { isBindingSourceActiveKey } = unlock( blocksPrivateApis );

const CONTENT = 'content';

export default {
name: 'core/pattern-overrides',
label: _x( 'Pattern Overrides', 'block bindings source' ),
[ isBindingSourceActiveKey ]: ( { registry, clientId } ) => {
const { getBlockParentsByBlockName } =
registry.select( blockEditorStore );
const [ patternClientId ] = getBlockParentsByBlockName(
clientId,
'core/block',
true
);

return !! patternClientId;
},
getValue( { registry, clientId, attributeName } ) {
const { getBlockAttributes, getBlockParentsByBlockName } =
registry.select( blockEditorStore );
Expand Down

0 comments on commit 383b9fa

Please sign in to comment.