-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
unknown-converter.js
57 lines (51 loc) · 1.47 KB
/
unknown-converter.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
/**
* External dependencies
*/
import { get } from 'lodash';
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { IconButton, withAPIData } from '@wordpress/components';
import { getUnknownTypeHandlerName, rawHandler, serialize } from '@wordpress/blocks';
import { compose } from '@wordpress/element';
import { withSelect, withDispatch } from '@wordpress/data';
export function UnknownConverter( { block, onReplace, small, user, role } ) {
if ( ! block || getUnknownTypeHandlerName() !== block.name ) {
return null;
}
const label = __( 'Convert to blocks' );
const convertToBlocks = () => {
onReplace( block.uid, rawHandler( {
HTML: serialize( block ),
mode: 'BLOCKS',
canUserUseUnfilteredHTML: get( user, [ 'data', 'capabilities', 'unfiltered_html' ], false ),
} ) );
};
return (
<IconButton
className="editor-block-settings-menu__control"
onClick={ convertToBlocks }
icon="screenoptions"
label={ small ? label : undefined }
role={ role }
>
{ ! small && label }
</IconButton>
);
}
export default compose(
withSelect( ( select, { uid } ) => {
const { getBlock, getCurrentPostType } = select( 'core/editor' );
return {
block: getBlock( uid ),
postType: getCurrentPostType(),
};
} ),
withDispatch( ( dispatch ) => ( {
onReplace: dispatch( 'core/editor' ).replaceBlocks,
} ) ),
withAPIData( ( { postType } ) => ( {
user: `/wp/v2/users/me?post_type=${ postType }&context=edit`,
} ) ),
)( UnknownConverter );