Skip to content

Commit bae645e

Browse files
scruffianramonjd
andauthored
Patterns: Change the icon and label for pattern section to use the pattern icon and name (#73203)
Co-authored-by: scruffian <scruffian@git.wordpress.org> Co-authored-by: ramonjd <ramonopoly@git.wordpress.org>
1 parent 86052a8 commit bae645e

File tree

3 files changed

+81
-36
lines changed

3 files changed

+81
-36
lines changed

packages/block-editor/src/components/block-toolbar/block-toolbar-icon.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import { ToolbarButton } from '@wordpress/components';
55
import { __ } from '@wordpress/i18n';
66
import { useSelect } from '@wordpress/data';
7-
import { copy } from '@wordpress/icons';
7+
import { copy, symbol } from '@wordpress/icons';
88
import { getBlockType, store as blocksStore } from '@wordpress/blocks';
99
import { store as preferencesStore } from '@wordpress/preferences';
1010

@@ -24,7 +24,6 @@ function getBlockIconVariant( { select, clientIds } ) {
2424
getBlockName,
2525
getBlockAttributes,
2626
getBlockParentsByBlockName,
27-
isSectionBlock,
2827
canRemoveBlocks,
2928
getTemplateLock,
3029
getBlockEditingMode,
@@ -40,8 +39,8 @@ function getBlockIconVariant( { select, clientIds } ) {
4039
const blockName = isSingleBlock && getBlockName( clientIds[ 0 ] );
4140
const hasBlockStyles =
4241
isSingleBlock && !! getBlockStyles( blockName )?.length;
43-
const isSectionInSelection = clientIds.some( ( id ) =>
44-
isSectionBlock( id )
42+
const hasPatternNameInSelection = clientIds.some(
43+
( id ) => !! getBlockAttributes( id )?.metadata?.patternName
4544
);
4645
const hasPatternOverrides = clientIds.every( ( clientId ) =>
4746
hasPatternOverridesDefaultBinding(
@@ -59,7 +58,7 @@ function getBlockIconVariant( { select, clientIds } ) {
5958
getBlockEditingMode( clientIds[ 0 ] ) === 'default';
6059
const _hideTransformsForSections =
6160
window?.__experimentalContentOnlyPatternInsertion &&
62-
isSectionInSelection;
61+
hasPatternNameInSelection;
6362
const _showBlockSwitcher =
6463
! _hideTransformsForSections &&
6564
isDefaultEditingMode &&
@@ -81,19 +80,24 @@ function getBlockIcon( { select, clientIds } ) {
8180
const { getBlockName, getBlockAttributes } = unlock(
8281
select( blockEditorStore )
8382
);
84-
const { getActiveBlockVariation } = select( blocksStore );
8583

8684
const _isSingleBlock = clientIds.length === 1;
8785
const firstClientId = clientIds[ 0 ];
86+
const blockAttributes = getBlockAttributes( firstClientId );
87+
if (
88+
_isSingleBlock &&
89+
blockAttributes?.metadata?.patternName &&
90+
window?.__experimentalContentOnlyPatternInsertion
91+
) {
92+
return symbol;
93+
}
8894

8995
const blockName = getBlockName( firstClientId );
9096
const blockType = getBlockType( blockName );
9197

9298
if ( _isSingleBlock ) {
93-
const match = getActiveBlockVariation(
94-
blockName,
95-
getBlockAttributes( firstClientId )
96-
);
99+
const { getActiveBlockVariation } = select( blocksStore );
100+
const match = getActiveBlockVariation( blockName, blockAttributes );
97101
return match?.icon || blockType?.icon;
98102
}
99103

packages/block-editor/src/components/list-view/block-select-button.js

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ import {
1212
privateApis as componentsPrivateApis,
1313
} from '@wordpress/components';
1414
import { forwardRef } from '@wordpress/element';
15-
import { Icon, lockSmall as lock, pinSmall, unseen } from '@wordpress/icons';
15+
import {
16+
Icon,
17+
lockSmall as lock,
18+
pinSmall,
19+
unseen,
20+
symbol,
21+
} from '@wordpress/icons';
1622
import { SPACE, ENTER } from '@wordpress/keycodes';
1723
import { useSelect } from '@wordpress/data';
1824
import { hasBlockSupport } from '@wordpress/blocks';
@@ -55,28 +61,35 @@ function ListViewBlockSelectButton(
5561
context: 'list-view',
5662
} );
5763
const { isLocked } = useBlockLock( clientId );
58-
const { canToggleBlockVisibility, isBlockHidden, isContentOnly } =
59-
useSelect(
60-
( select ) => {
61-
const { getBlockName } = select( blockEditorStore );
62-
const { isBlockHidden: _isBlockHidden } = unlock(
63-
select( blockEditorStore )
64-
);
65-
return {
66-
canToggleBlockVisibility: hasBlockSupport(
67-
getBlockName( clientId ),
68-
'blockVisibility',
69-
true
70-
),
71-
isBlockHidden: _isBlockHidden( clientId ),
72-
isContentOnly:
73-
select( blockEditorStore ).getBlockEditingMode(
74-
clientId
75-
) === 'contentOnly',
76-
};
77-
},
78-
[ clientId ]
79-
);
64+
const {
65+
canToggleBlockVisibility,
66+
isBlockHidden,
67+
isContentOnly,
68+
hasPatternName,
69+
} = useSelect(
70+
( select ) => {
71+
const { getBlockName, getBlockAttributes } =
72+
select( blockEditorStore );
73+
const { isBlockHidden: _isBlockHidden } = unlock(
74+
select( blockEditorStore )
75+
);
76+
const blockAttributes = getBlockAttributes( clientId );
77+
return {
78+
canToggleBlockVisibility: hasBlockSupport(
79+
getBlockName( clientId ),
80+
'blockVisibility',
81+
true
82+
),
83+
isBlockHidden: _isBlockHidden( clientId ),
84+
isContentOnly:
85+
select( blockEditorStore ).getBlockEditingMode(
86+
clientId
87+
) === 'contentOnly',
88+
hasPatternName: !! blockAttributes?.metadata?.patternName,
89+
};
90+
},
91+
[ clientId ]
92+
);
8093
const shouldShowLockIcon = isLocked && ! isContentOnly;
8194
const shouldShowBlockVisibilityIcon =
8295
canToggleBlockVisibility && isBlockHidden;
@@ -123,7 +136,7 @@ function ListViewBlockSelectButton(
123136
>
124137
<ListViewExpander onClick={ onToggleExpanded } />
125138
<BlockIcon
126-
icon={ blockInformation?.icon }
139+
icon={ hasPatternName ? symbol : blockInformation?.icon }
127140
showColors
128141
context="list-view"
129142
/>

packages/block-editor/src/components/use-block-display-information/index.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
__experimentalGetBlockLabel as getBlockLabel,
1010
} from '@wordpress/blocks';
1111
import { __ } from '@wordpress/i18n';
12+
import { symbol } from '@wordpress/icons';
1213

1314
/**
1415
* Internal dependencies
@@ -71,8 +72,11 @@ export default function useBlockDisplayInformation( clientId ) {
7172
if ( ! clientId ) {
7273
return null;
7374
}
74-
const { getBlockName, getBlockAttributes } =
75-
select( blockEditorStore );
75+
const {
76+
getBlockName,
77+
getBlockAttributes,
78+
__experimentalGetParsedPattern,
79+
} = select( blockEditorStore );
7680
const { getBlockType, getActiveBlockVariation } =
7781
select( blocksStore );
7882
const blockName = getBlockName( clientId );
@@ -81,6 +85,30 @@ export default function useBlockDisplayInformation( clientId ) {
8185
return null;
8286
}
8387
const attributes = getBlockAttributes( clientId );
88+
89+
// Check if this block is a pattern
90+
const patternName = attributes?.metadata?.patternName;
91+
92+
if (
93+
patternName &&
94+
window?.__experimentalContentOnlyPatternInsertion
95+
) {
96+
const pattern = __experimentalGetParsedPattern( patternName );
97+
98+
const positionLabel = getPositionTypeLabel( attributes );
99+
return {
100+
isSynced: false,
101+
title: __( 'Pattern' ),
102+
icon: symbol,
103+
description:
104+
pattern?.description || __( 'A block pattern.' ),
105+
anchor: attributes?.anchor,
106+
positionLabel,
107+
positionType: attributes?.style?.position?.type,
108+
name: pattern?.title || attributes?.metadata?.name,
109+
};
110+
}
111+
84112
const match = getActiveBlockVariation( blockName, attributes );
85113
const isSynced =
86114
isReusableBlock( blockType ) || isTemplatePart( blockType );

0 commit comments

Comments
 (0)