-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Copy pathindex.js
137 lines (132 loc) · 3.79 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* WordPress dependencies
*/
import { useId } from '@wordpress/element';
import { __, sprintf, _x } 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';
export default function BlockBindingsToolbarIndicator( { clientIds } ) {
const isSingleBlockSelected = clientIds.length === 1;
const { icon, firstBlockName, isConnectedToPatternOverrides } = useSelect(
( select ) => {
const {
getBlockAttributes,
getBlockNamesByClientId,
getBlocksByClientId,
} = 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,
isConnectedToPatternOverrides: getBlocksByClientId(
clientIds
).some( ( block ) =>
Object.values(
block?.attributes?.metadata?.bindings || {}
).some(
( binding ) =>
binding.source === 'core/pattern-overrides'
)
),
};
},
[ clientIds, isSingleBlockSelected ]
);
const firstBlockTitle = useBlockDisplayTitle( {
clientId: clientIds[ 0 ],
maximumLength: 35,
} );
let blockDescription = isSingleBlockSelected
? _x(
'This block is connected.',
'block toolbar button label and description'
)
: _x(
'These blocks are connected.',
'block toolbar button label and description'
);
if ( isConnectedToPatternOverrides && firstBlockName ) {
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-block-bindings-toolbar-indicator"
label={ firstBlockTitle }
popoverProps={ {
placement: 'bottom-start',
className:
'block-editor-block-bindings-toolbar-indicator__popover',
} }
icon={
<>
<BlockIcon
icon={ icon }
className="block-editor-block-bindings-toolbar-indicator-icon"
showColors
/>
</>
}
toggleProps={ {
describedBy: blockDescription,
...toggleProps,
} }
menuProps={ {
orientation: 'both',
'aria-describedby': descriptionId,
} }
>
{ () => (
<Text id={ descriptionId }>
{ blockDescription }
</Text>
) }
</DropdownMenu>
) }
</ToolbarItem>
</ToolbarGroup>
);
}