Skip to content

Commit

Permalink
Previews: avoid unneeded block selectors (WordPress#60543)
Browse files Browse the repository at this point in the history
Co-authored-by: ellatrix <ellatrix@git.wordpress.org>
Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
  • Loading branch information
3 people authored and cbravobernal committed Apr 9, 2024
1 parent 43427f9 commit 29e050b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 25 deletions.
65 changes: 41 additions & 24 deletions packages/block-editor/src/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -562,38 +562,61 @@ function BlockListBlockProvider( props ) {
hasBlockSupport: _hasBlockSupport,
getActiveBlockVariation,
} = select( blocksStore );
const _isSelected = isBlockSelected( clientId );
const canRemove = canRemoveBlock( clientId, rootClientId );
const canMove = canMoveBlock( clientId, rootClientId );
const attributes = getBlockAttributes( clientId );
const { name: blockName, isValid } = blockWithoutAttributes;
const blockType = getBlockType( blockName );
const {
outlineMode,
supportsLayout,
__unstableIsPreviewMode: isPreviewMode,
} = getSettings();
const hasLightBlockWrapper = blockType?.apiVersion > 1;
const previewContext = {
blockWithoutAttributes,
name: blockName,
attributes,
isValid,
themeSupportsLayout: supportsLayout,
index: getBlockIndex( clientId ),
isReusable: isReusableBlock( blockType ),
className: hasLightBlockWrapper
? attributes.className
: undefined,
defaultClassName: hasLightBlockWrapper
? getBlockDefaultClassName( blockName )
: undefined,
blockTitle: blockType?.title,
};

// When in preview mode, we can avoid a lot of selection and
// editing related selectors.
if ( isPreviewMode ) {
return previewContext;
}

const _isSelected = isBlockSelected( clientId );
const canRemove = canRemoveBlock( clientId, rootClientId );
const canMove = canMoveBlock( clientId, rootClientId );
const match = getActiveBlockVariation( blockName, attributes );
const { outlineMode, supportsLayout } = getSettings();
const isMultiSelected = isBlockMultiSelected( clientId );
const checkDeep = true;
const isAncestorOfSelectedBlock = hasSelectedInnerBlock(
clientId,
checkDeep
);
const typing = isTyping();
const hasLightBlockWrapper = blockType?.apiVersion > 1;
const movingClientId = hasBlockMovingClientId();
const blockEditingMode = getBlockEditingMode( clientId );

return {
...previewContext,
mode: getBlockMode( clientId ),
isSelectionEnabled: isSelectionEnabled(),
isLocked: !! getTemplateLock( rootClientId ),
templateLock: getTemplateLock( clientId ),
canRemove,
canMove,
blockWithoutAttributes,
name: blockName,
attributes,
isValid,
isSelected: _isSelected,
themeSupportsLayout: supportsLayout,
isTemporarilyEditingAsBlocks:
__unstableGetTemporarilyEditingAsBlocks() === clientId,
blockEditingMode,
Expand All @@ -609,7 +632,6 @@ function BlockListBlockProvider( props ) {
'__experimentalExposeControlsToChildren',
false
) && hasSelectedInnerBlock( clientId ),
index: getBlockIndex( clientId ),
blockApiVersion: blockType?.apiVersion || 1,
blockTitle: match?.title || blockType?.title,
isSubtreeDisabled:
Expand All @@ -629,7 +651,6 @@ function BlockListBlockProvider( props ) {
isMultiSelected &&
! __unstableIsFullySelected() &&
! __unstableSelectionHasUnmergeableBlock(),
isReusable: isReusableBlock( blockType ),
isDragging: isBlockBeingDragged( clientId ),
hasChildSelected: isAncestorOfSelectedBlock,
removeOutline: _isSelected && outlineMode && typing,
Expand All @@ -644,28 +665,24 @@ function BlockListBlockProvider( props ) {
hasEditableOutline:
blockEditingMode !== 'disabled' &&
getBlockEditingMode( rootClientId ) === 'disabled',
className: hasLightBlockWrapper
? attributes.className
: undefined,
defaultClassName: hasLightBlockWrapper
? getBlockDefaultClassName( blockName )
: undefined,
};
},
[ clientId, rootClientId ]
);

const {
mode,
isSelectionEnabled,
isLocked,
canRemove,
canMove,
// Fill values that end up as a public API and may not be defined in
// preview mode.
mode = 'visual',
isSelectionEnabled = false,
isLocked = false,
canRemove = false,
canMove = false,
blockWithoutAttributes,
name,
attributes,
isValid,
isSelected,
isSelected = false,
themeSupportsLayout,
isTemporarilyEditingAsBlocks,
blockEditingMode,
Expand Down
17 changes: 16 additions & 1 deletion packages/block-editor/src/components/block-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ export default function BlockList( settings ) {
);
}

const EMPTY_ARRAY = [];
const EMPTY_SET = new Set();

function Items( {
placeholder,
rootClientId,
Expand All @@ -175,6 +178,7 @@ function Items( {
useSelect(
( select ) => {
const {
getSettings,
getBlockOrder,
getSelectedBlockClientId,
getSelectedBlockClientIds,
Expand All @@ -183,9 +187,20 @@ function Items( {
getBlockEditingMode,
__unstableGetEditorMode,
} = select( blockEditorStore );

const _order = getBlockOrder( rootClientId );

if ( getSettings().__unstableIsPreviewMode ) {
return {
order: _order,
selectedBlocks: EMPTY_ARRAY,
visibleBlocks: EMPTY_SET,
};
}

const selectedBlockClientId = getSelectedBlockClientId();
return {
order: getBlockOrder( rootClientId ),
order: _order,
selectedBlocks: getSelectedBlockClientIds(),
visibleBlocks: __unstableGetVisibleBlocks(),
shouldRenderAppender:
Expand Down

0 comments on commit 29e050b

Please sign in to comment.