-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathblock-invalid-warning.js
126 lines (118 loc) · 3.18 KB
/
block-invalid-warning.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
/**
* WordPress dependencies
*/
import { __, _x } from '@wordpress/i18n';
import { Button, Modal } from '@wordpress/components';
import { useState, useCallback, useMemo } from '@wordpress/element';
import { getBlockType, createBlock, rawHandler } from '@wordpress/blocks';
import { compose } from '@wordpress/compose';
import { withDispatch, withSelect } from '@wordpress/data';
/**
* Internal dependencies
*/
import Warning from '../warning';
import BlockCompare from '../block-compare';
export function BlockInvalidWarning( {
convertToHTML,
convertToBlocks,
convertToClassic,
attemptBlockRecovery,
block,
} ) {
const hasHTMLBlock = !! getBlockType( 'core/html' );
const [ compare, setCompare ] = useState( false );
const onCompare = useCallback( () => setCompare( true ), [] );
const onCompareClose = useCallback( () => setCompare( false ), [] );
// We memo the array here to prevent the children components from being updated unexpectedly
const hiddenActions = useMemo(
() =>
[
{
// translators: Button to fix block content
title: _x( 'Resolve', 'imperative verb' ),
onClick: onCompare,
},
hasHTMLBlock && {
title: __( 'Convert to HTML' ),
onClick: convertToHTML,
},
{
title: __( 'Convert to Classic Block' ),
onClick: convertToClassic,
},
].filter( Boolean ),
[ onCompare, convertToHTML, convertToClassic ]
);
return (
<>
<Warning
actions={ [
<Button
key="recover"
onClick={ attemptBlockRecovery }
isPrimary
>
{ __( 'Attempt Block Recovery' ) }
</Button>,
] }
secondaryActions={ hiddenActions }
>
{ __( 'This block contains unexpected or invalid content.' ) }
</Warning>
{ compare && (
<Modal
title={
// translators: Dialog title to fix block content
__( 'Resolve Block' )
}
onRequestClose={ onCompareClose }
className="block-editor-block-compare"
>
<BlockCompare
block={ block }
onKeep={ convertToHTML }
onConvert={ convertToBlocks }
convertor={ blockToBlocks }
convertButtonText={ __( 'Convert to Blocks' ) }
/>
</Modal>
) }
</>
);
}
const blockToClassic = ( block ) =>
createBlock( 'core/freeform', {
content: block.originalContent,
} );
const blockToHTML = ( block ) =>
createBlock( 'core/html', {
content: block.originalContent,
} );
const blockToBlocks = ( block ) =>
rawHandler( {
HTML: block.originalContent,
} );
const recoverBlock = ( { name, attributes, innerBlocks } ) =>
createBlock( name, attributes, innerBlocks );
export default compose( [
withSelect( ( select, { clientId } ) => ( {
block: select( 'core/block-editor' ).getBlock( clientId ),
} ) ),
withDispatch( ( dispatch, { block } ) => {
const { replaceBlock } = dispatch( 'core/block-editor' );
return {
convertToClassic() {
replaceBlock( block.clientId, blockToClassic( block ) );
},
convertToHTML() {
replaceBlock( block.clientId, blockToHTML( block ) );
},
convertToBlocks() {
replaceBlock( block.clientId, blockToBlocks( block ) );
},
attemptBlockRecovery() {
replaceBlock( block.clientId, recoverBlock( block ) );
},
};
} ),
] )( BlockInvalidWarning );