Skip to content

Commit

Permalink
Add inert attribute to disabled blocks that have only disabled descen…
Browse files Browse the repository at this point in the history
…dants (WordPress#51079)

* Add inert attribute to disabled blocks that have only disabled descendants

* Fix typo

---------

Co-authored-by: ramon <[email protected]>
  • Loading branch information
2 people authored and artemiomorales committed Jun 2, 2023
1 parent 1ea4cd4 commit e0cd239
Show file tree
Hide file tree
Showing 3 changed files with 235 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { useBlockRefProvider } from './use-block-refs';
import { useIntersectionObserver } from './use-intersection-observer';
import { store as blockEditorStore } from '../../../store';
import useBlockOverlayActive from '../../block-content-overlay';
import { unlock } from '../../../lock-unlock';

/**
* If the block count exceeds the threshold, we disable the reordering animation
Expand Down Expand Up @@ -75,6 +76,7 @@ export function useBlockProps( props = {}, { __unstableIsHtml } = {} ) {
isPartOfSelection,
adjustScrolling,
enableAnimation,
isSubtreeDisabled,
} = useSelect(
( select ) => {
const {
Expand All @@ -88,7 +90,8 @@ export function useBlockProps( props = {}, { __unstableIsHtml } = {} ) {
isBlockMultiSelected,
isAncestorMultiSelected,
isFirstMultiSelectedBlock,
} = select( blockEditorStore );
isBlockSubtreeDisabled,
} = unlock( select( blockEditorStore ) );
const { getActiveBlockVariation } = select( blocksStore );
const isSelected = isBlockSelected( clientId );
const isPartOfMultiSelection =
Expand All @@ -111,6 +114,7 @@ export function useBlockProps( props = {}, { __unstableIsHtml } = {} ) {
enableAnimation:
! isTyping() &&
getGlobalBlockCount() <= BLOCK_ANIMATION_THRESHOLD,
isSubtreeDisabled: isBlockSubtreeDisabled( clientId ),
};
},
[ clientId ]
Expand Down Expand Up @@ -158,6 +162,7 @@ export function useBlockProps( props = {}, { __unstableIsHtml } = {} ) {
'data-block': clientId,
'data-type': name,
'data-title': blockTitle,
inert: isSubtreeDisabled ? 'true' : undefined,
className: classnames(
// The wp-block className is important for editor styles.
classnames( 'block-editor-block-list__block', {
Expand Down
33 changes: 31 additions & 2 deletions packages/block-editor/src/store/private-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
getBlockRootClientId,
getTemplateLock,
getBlockName,
getBlockOrder,
} from './selectors';

/**
Expand Down Expand Up @@ -74,7 +75,7 @@ export function getLastInsertedBlocksClientIds( state ) {
export const getBlockEditingMode = createRegistrySelector(
( select ) =>
( state, clientId = '' ) => {
const explicitEditingMode = getExplcitBlockEditingMode(
const explicitEditingMode = getExplicitBlockEditingMode(
state,
clientId
);
Expand All @@ -101,7 +102,7 @@ export const getBlockEditingMode = createRegistrySelector(
}
);

const getExplcitBlockEditingMode = createSelector(
const getExplicitBlockEditingMode = createSelector(
( state, clientId = '' ) => {
while (
! state.blockEditingModes.has( clientId ) &&
Expand All @@ -113,3 +114,31 @@ const getExplcitBlockEditingMode = createSelector(
},
( state ) => [ state.blockEditingModes, state.blocks.parents ]
);

/**
* Returns true if the block with the given client ID and all of its descendants
* have an editing mode of 'disabled', or false otherwise.
*
* @param {Object} state Global application state.
* @param {string} clientId The block client ID.
*
* @return {boolean} Whether the block and its descendants are disabled.
*/
export const isBlockSubtreeDisabled = createSelector(
( state, clientId ) => {
const isChildSubtreeDisabled = ( childClientId ) => {
const mode = state.blockEditingModes.get( childClientId );
return (
( mode === undefined || mode === 'disabled' ) &&
getBlockOrder( state, childClientId ).every(
isChildSubtreeDisabled
)
);
};
return (
getExplicitBlockEditingMode( state, clientId ) === 'disabled' &&
getBlockOrder( state, clientId ).every( isChildSubtreeDisabled )
);
},
( state ) => [ state.blockEditingModes, state.blocks.parents ]
);
Loading

0 comments on commit e0cd239

Please sign in to comment.