-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
153 lines (142 loc) · 3.89 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
/**
* External dependencies
*/
import { connect } from 'react-redux';
import { every, uniq, get, reduce, find } from 'lodash';
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { Dropdown, Dashicon, IconButton, Toolbar, NavigableMenu, withContext } from '@wordpress/components';
import { getBlockType, getBlockTypes, switchToBlockType, BlockIcon } from '@wordpress/blocks';
import { compose } from '@wordpress/element';
import { keycodes } from '@wordpress/utils';
/**
* Internal dependencies
*/
import './style.scss';
import { replaceBlocks } from '../../actions';
import { getBlock } from '../../selectors';
/**
* Module Constants
*/
const { DOWN } = keycodes;
function BlockSwitcher( { blocks, onTransform, isLocked } ) {
if ( ! blocks || ! blocks[ 0 ] || isLocked ) {
return null;
}
const isMultiBlock = blocks.length > 1;
const sourceBlockName = blocks[ 0 ].name;
if ( isMultiBlock && ! every( blocks, ( block ) => ( block.name === sourceBlockName ) ) ) {
return null;
}
const blockType = getBlockType( sourceBlockName );
const blocksToBeTransformedFrom = reduce( getBlockTypes(), ( memo, type ) => {
const transformFrom = get( type, 'transforms.from', [] );
const transformation = find(
transformFrom,
t => t.type === 'block' && t.blocks.indexOf( sourceBlockName ) !== -1 &&
( ! isMultiBlock || t.isMultiBlock )
);
return transformation ? memo.concat( [ type.name ] ) : memo;
}, [] );
const blocksToBeTransformedTo = get( blockType, 'transforms.to', [] )
.reduce(
( memo, transformation ) =>
memo.concat( ! isMultiBlock || transformation.isMultiBlock ? transformation.blocks : [] ),
[]
);
const allowedBlocks = uniq( blocksToBeTransformedFrom.concat( blocksToBeTransformedTo ) )
.reduce( ( memo, name ) => {
const type = getBlockType( name );
return !! type ? memo.concat( type ) : memo;
}, [] );
if ( ! allowedBlocks.length ) {
return null;
}
return (
<Dropdown
className="editor-block-switcher"
contentClassName="editor-block-switcher__popover"
renderToggle={ ( { onToggle, isOpen } ) => {
const openOnArrowDown = ( event ) => {
if ( ! isOpen && event.keyCode === DOWN ) {
event.preventDefault();
event.stopPropagation();
onToggle();
}
};
return (
<Toolbar>
<IconButton
className="editor-block-switcher__toggle"
icon={ <BlockIcon icon={ blockType.icon } /> }
onClick={ onToggle }
aria-haspopup="true"
aria-expanded={ isOpen }
label={ __( 'Change block type' ) }
onKeyDown={ openOnArrowDown }
>
<Dashicon icon="arrow-down" />
</IconButton>
</Toolbar>
);
} }
renderContent={ ( { onClose } ) => (
<div>
<span
className="editor-block-switcher__menu-title"
>
{ __( 'Transform into:' ) }
</span>
<NavigableMenu
role="menu"
aria-label={ __( 'Block types' ) }
>
{ allowedBlocks.map( ( { name, title, icon } ) => (
<IconButton
key={ name }
onClick={ () => {
onTransform( blocks, name );
onClose();
} }
className="editor-block-switcher__menu-item"
icon={ (
<span className="editor-block-switcher__block-icon">
<BlockIcon icon={ icon } />
</span>
) }
role="menuitem"
>
{ title }
</IconButton>
) ) }
</NavigableMenu>
</div>
) }
/>
);
}
export default compose(
connect(
( state, ownProps ) => {
return {
blocks: ownProps.uids.map( ( uid ) => getBlock( state, uid ) ),
};
},
( dispatch, ownProps ) => ( {
onTransform( blocks, name ) {
dispatch( replaceBlocks(
ownProps.uids,
switchToBlockType( blocks, name )
) );
},
} )
),
withContext( 'editor' )( ( settings ) => {
const { templateLock } = settings;
return {
isLocked: !! templateLock,
};
} ),
)( BlockSwitcher );