-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
120 lines (114 loc) · 3.47 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* WordPress dependencies
*/
import { useId } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import {
DropdownMenu,
ToolbarGroup,
ToolbarItem,
__experimentalText as Text,
} from '@wordpress/components';
import { store as blocksStore } from '@wordpress/blocks';
import { useSelect } from '@wordpress/data';
import { copy } from '@wordpress/icons';
/**
* Internal dependencies
*/
import { store as blockEditorStore } from '../../store';
import BlockIcon from '../block-icon';
import useBlockDisplayTitle from '../block-title/use-block-display-title';
/**
* This component is currently only for pattern overrides, which is a WP-only feature.
* Ideally, this should be moved to the `patterns` package once ready.
* @param {Object} props The component props.
* @param {Array} props.clientIds The client IDs of the selected blocks.
*/
export default function PatternOverridesToolbarIndicator( { clientIds } ) {
const isSingleBlockSelected = clientIds.length === 1;
const { icon, firstBlockName } = useSelect(
( select ) => {
const { getBlockAttributes, getBlockNamesByClientId } =
select( blockEditorStore );
const { getBlockType, getActiveBlockVariation } =
select( blocksStore );
const blockTypeNames = getBlockNamesByClientId( clientIds );
const _firstBlockTypeName = blockTypeNames[ 0 ];
const firstBlockType = getBlockType( _firstBlockTypeName );
let _icon;
if ( isSingleBlockSelected ) {
const match = getActiveBlockVariation(
_firstBlockTypeName,
getBlockAttributes( clientIds[ 0 ] )
);
// Take into account active block variations.
_icon = match?.icon || firstBlockType.icon;
} else {
const isSelectionOfSameType =
new Set( blockTypeNames ).size === 1;
// When selection consists of blocks of multiple types, display an
// appropriate icon to communicate the non-uniformity.
_icon = isSelectionOfSameType ? firstBlockType.icon : copy;
}
return {
icon: _icon,
firstBlockName: getBlockAttributes( clientIds[ 0 ] ).metadata
.name,
};
},
[ clientIds, isSingleBlockSelected ]
);
const firstBlockTitle = useBlockDisplayTitle( {
clientId: clientIds[ 0 ],
maximumLength: 35,
} );
const blockDescription = isSingleBlockSelected
? sprintf(
/* translators: %1s: The block type's name; %2s: The block's user-provided name (the same as the override name). */
__( 'This %1$s is editable using the "%2$s" override.' ),
firstBlockTitle.toLowerCase(),
firstBlockName
)
: __( 'These blocks are editable using overrides.' );
const descriptionId = useId();
return (
<ToolbarGroup>
<ToolbarItem>
{ ( toggleProps ) => (
<DropdownMenu
className="block-editor-pattern-overrides-toolbar-indicator"
label={ firstBlockTitle }
popoverProps={ {
placement: 'bottom-start',
className:
'block-editor-pattern-overrides-toolbar-indicator__popover',
} }
icon={
<>
<BlockIcon
icon={ icon }
className="block-editor-pattern-overrides-toolbar-indicator-icon"
showColors
/>
</>
}
toggleProps={ {
describedBy: blockDescription,
...toggleProps,
} }
menuProps={ {
orientation: 'both',
'aria-describedby': descriptionId,
} }
>
{ () => (
<Text id={ descriptionId }>
{ blockDescription }
</Text>
) }
</DropdownMenu>
) }
</ToolbarItem>
</ToolbarGroup>
);
}