/
index.js
203 lines (189 loc) · 6.11 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
* External dependencies
*/
import { castArray, filter, first, mapKeys, orderBy, uniq, map } from 'lodash';
/**
* WordPress dependencies
*/
import { __, _n, sprintf } from '@wordpress/i18n';
import { Dropdown, IconButton, Toolbar, PanelBody, Path, SVG } from '@wordpress/components';
import { getBlockType, getPossibleBlockTransformations, switchToBlockType, hasChildBlocksWithInserterSupport, cloneBlock } from '@wordpress/blocks';
import { Component } from '@wordpress/element';
import { DOWN } from '@wordpress/keycodes';
import { withSelect, withDispatch } from '@wordpress/data';
import { compose } from '@wordpress/compose';
/**
* Internal dependencies
*/
import BlockIcon from '../block-icon';
import BlockStyles from '../block-styles';
import BlockPreview from '../block-preview';
import BlockPreviewDropdownWrapper from '../block-preview/dropdown-wrapper';
import BlockTypesList from '../block-types-list';
export class BlockSwitcher extends Component {
constructor() {
super( ...arguments );
this.state = {
hoveredClassName: null,
};
this.onHoverClassName = this.onHoverClassName.bind( this );
}
onHoverClassName( className ) {
this.setState( { hoveredClassName: className } );
}
render() {
const { blocks, onTransform, inserterItems, hasBlockStyles } = this.props;
const { hoveredClassName } = this.state;
if ( ! blocks || ! blocks.length ) {
return null;
}
const itemsByName = mapKeys( inserterItems, ( { name } ) => name );
const possibleBlockTransformations = orderBy(
filter(
getPossibleBlockTransformations( blocks ),
( block ) => block && !! itemsByName[ block.name ]
),
( block ) => itemsByName[ block.name ].frecency,
'desc'
);
// When selection consists of blocks of multiple types, display an
// appropriate icon to communicate the non-uniformity.
const isSelectionOfSameType = uniq( map( blocks, 'name' ) ).length === 1;
let icon;
if ( isSelectionOfSameType ) {
const sourceBlockName = blocks[ 0 ].name;
const blockType = getBlockType( sourceBlockName );
icon = blockType.icon;
} else {
icon = 'layout';
}
if ( ! hasBlockStyles && ! possibleBlockTransformations.length ) {
return (
<Toolbar>
<IconButton
disabled
className="editor-block-switcher__no-switcher-icon block-editor-block-switcher__no-switcher-icon"
label={ __( 'Block icon' ) }
icon={ <BlockIcon icon={ icon } showColors /> }
/>
</Toolbar>
);
}
return (
<Dropdown
position="bottom right"
className="editor-block-switcher block-editor-block-switcher"
contentClassName="editor-block-switcher__popover block-editor-block-switcher__popover"
renderToggle={ ( { onToggle, isOpen } ) => {
const openOnArrowDown = ( event ) => {
if ( ! isOpen && event.keyCode === DOWN ) {
event.preventDefault();
event.stopPropagation();
onToggle();
}
};
const label = (
1 === blocks.length ?
__( 'Change block type or style' ) :
sprintf(
_n(
'Change type of %d block',
'Change type of %d blocks',
blocks.length
),
blocks.length
)
);
return (
<Toolbar>
<IconButton
className="editor-block-switcher__toggle block-editor-block-switcher__toggle"
onClick={ onToggle }
aria-haspopup="true"
aria-expanded={ isOpen }
label={ label }
tooltip={ label }
onKeyDown={ openOnArrowDown }
icon={ (
<>
<BlockIcon icon={ icon } showColors />
<SVG className="editor-block-switcher__transform block-editor-block-switcher__transform" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><Path d="M6.5 8.9c.6-.6 1.4-.9 2.2-.9h6.9l-1.3 1.3 1.4 1.4L19.4 7l-3.7-3.7-1.4 1.4L15.6 6H8.7c-1.4 0-2.6.5-3.6 1.5l-2.8 2.8 1.4 1.4 2.8-2.8zm13.8 2.4l-2.8 2.8c-.6.6-1.3.9-2.1.9h-7l1.3-1.3-1.4-1.4L4.6 16l3.7 3.7 1.4-1.4L8.4 17h6.9c1.3 0 2.6-.5 3.5-1.5l2.8-2.8-1.3-1.4z" /></SVG>
</>
) }
/>
</Toolbar>
);
} }
renderContent={ ( { onClose } ) => (
<>
{ hasBlockStyles &&
<PanelBody
title={ __( 'Block Styles' ) }
initialOpen
>
<BlockStyles
clientId={ blocks[ 0 ].clientId }
onSwitch={ onClose }
onHoverClassName={ this.onHoverClassName }
/>
</PanelBody>
}
{ possibleBlockTransformations.length !== 0 &&
<PanelBody
title={ __( 'Transform To:' ) }
initialOpen
>
<BlockTypesList
items={ possibleBlockTransformations.map( ( destinationBlockType ) => ( {
id: destinationBlockType.name,
icon: destinationBlockType.icon,
title: destinationBlockType.title,
hasChildBlocksWithInserterSupport: hasChildBlocksWithInserterSupport( destinationBlockType.name ),
} ) ) }
onSelect={ ( item ) => {
onTransform( blocks, item.id );
onClose();
} }
/>
</PanelBody>
}
{ ( hoveredClassName !== null ) &&
<BlockPreviewDropdownWrapper>
<BlockPreview
blocks={
cloneBlock( blocks[ 0 ], {
className: hoveredClassName,
} )
}
/>
</BlockPreviewDropdownWrapper>
}
</>
) }
/>
);
}
}
export default compose(
withSelect( ( select, { clientIds } ) => {
const { getBlocksByClientId, getBlockRootClientId, getInserterItems } = select( 'core/block-editor' );
const { getBlockStyles } = select( 'core/blocks' );
const rootClientId = getBlockRootClientId( first( castArray( clientIds ) ) );
const blocks = getBlocksByClientId( clientIds );
const firstBlock = blocks && blocks.length === 1 ? blocks[ 0 ] : null;
const styles = firstBlock && getBlockStyles( firstBlock.name );
return {
blocks,
inserterItems: getInserterItems( rootClientId ),
hasBlockStyles: styles && styles.length > 0,
};
} ),
withDispatch( ( dispatch, ownProps ) => ( {
onTransform( blocks, name ) {
dispatch( 'core/block-editor' ).replaceBlocks(
ownProps.clientIds,
switchToBlockType( blocks, name )
);
},
} ) ),
)( BlockSwitcher );