Skip to content

Commit

Permalink
Combine into one selector
Browse files Browse the repository at this point in the history
  • Loading branch information
talldan committed Oct 2, 2024
1 parent bf02476 commit ec59ed1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 50 deletions.
47 changes: 0 additions & 47 deletions packages/block-editor/src/store/private-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { createSelector, createRegistrySelector } from '@wordpress/data';
* Internal dependencies
*/
import {
getBlockAttributes,
getBlockOrder,
getBlockParents,
getBlockEditingMode,
Expand All @@ -18,7 +17,6 @@ import {
getClientIdsWithDescendants,
isNavigationMode,
getBlockRootClientId,
__unstableGetEditorMode,
} from './selectors';
import {
checkAllowListRecursive,
Expand Down Expand Up @@ -687,51 +685,6 @@ export function getInsertionPoint( state ) {
return state.insertionPoint;
}

/**
* Retrieves the editing mode of a pattern block and its children.
*
* @param {Object} state Global application state.
* @param {string} clientId The block client ID.
*
* @return {string} The block editing mode.
*/
export function getPatternBlockEditingMode( state, clientId ) {
const parentPatternCount = getParentPatternCount( state, clientId );

// If there are no parent patterns, use the block's own editing mode.
if ( parentPatternCount === 0 ) {
return state.blockEditingModes.get( clientId );
}

// Disable nested patterns.
if ( parentPatternCount > 1 ) {
return 'disabled';
}

// Make the outer pattern block content only mode.
if (
getBlockName( state, clientId ) === 'core/block' &&
parentPatternCount === 0
) {
return 'contentOnly';
}

// Disable pattern content in zoom-out mode.
const _isZoomOut = __unstableGetEditorMode( state ) === 'zoom-out';
if ( _isZoomOut ) {
return 'disabled';
}

// If the block has a binding of any kind, allow content only editing.
const attributes = getBlockAttributes( state, clientId );
if ( Object.keys( attributes.metadata?.bindings ?? {} )?.length > 0 ) {
return 'contentOnly';
}

// Otherwise, the block is part of the pattern source and should not be editable.
return 'disabled';
}

/**
* Retrieves the number of parent pattern blocks.
*
Expand Down
37 changes: 34 additions & 3 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import {
isSectionBlock,
getParentSectionBlock,
getParentPatternCount,
getPatternBlockEditingMode,
} from './private-selectors';

/**
Expand Down Expand Up @@ -2974,8 +2973,40 @@ export const getBlockEditingMode = createRegistrySelector(
clientId = '';
}

if ( getParentPatternCount( state, clientId ) > 0 ) {
return getPatternBlockEditingMode( state, clientId );
// Handle pattern blocks (core/block) and the content of those blocks.
const parentPatternCount = getParentPatternCount( state, clientId );
if ( parentPatternCount > 0 ) {
// Disable nested patterns.
if ( parentPatternCount > 1 ) {
return 'disabled';
}

// Make the outer pattern block content only mode.
if (
getBlockName( state, clientId ) === 'core/block' &&
parentPatternCount === 0
) {
return 'contentOnly';
}

// Disable pattern content editing in zoom-out mode.
const _isZoomOut =
__unstableGetEditorMode( state ) === 'zoom-out';
if ( _isZoomOut ) {
return 'disabled';
}

// If the block has a binding of any kind, allow content only editing.
const attributes = getBlockAttributes( state, clientId );
if (
Object.keys( attributes.metadata?.bindings ?? {} )?.length >
0
) {
return 'contentOnly';
}

// Otherwise, the block is part of the pattern source and should not be editable.
return 'disabled';
}

// In zoom-out mode, override the behavior set by
Expand Down

0 comments on commit ec59ed1

Please sign in to comment.