-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathedit.js
89 lines (81 loc) · 2.06 KB
/
edit.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
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { useContext, useState } from '@wordpress/element';
import {
BlockControls,
PlainText,
useBlockProps,
store as blockEditorStore,
} from '@wordpress/block-editor';
import {
ToolbarButton,
Disabled,
ToolbarGroup,
VisuallyHidden,
} from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import { useInstanceId } from '@wordpress/compose';
/**
* Internal dependencies
*/
import Preview from './preview';
export default function HTMLEdit( { attributes, setAttributes, isSelected } ) {
const [ isPreview, setIsPreview ] = useState();
const isDisabled = useContext( Disabled.Context );
const instanceId = useInstanceId( HTMLEdit, 'html-edit-desc' );
const isPreviewMode = useSelect( ( select ) => {
return select( blockEditorStore ).getSettings().isPreviewMode;
}, [] );
function switchToPreview() {
setIsPreview( true );
}
function switchToHTML() {
setIsPreview( false );
}
const blockProps = useBlockProps( {
className: 'block-library-html__edit',
'aria-describedby': isPreview ? instanceId : undefined,
} );
return (
<div { ...blockProps }>
<BlockControls>
<ToolbarGroup>
<ToolbarButton
isPressed={ ! isPreview }
onClick={ switchToHTML }
>
HTML
</ToolbarButton>
<ToolbarButton
isPressed={ isPreview }
onClick={ switchToPreview }
>
{ __( 'Preview' ) }
</ToolbarButton>
</ToolbarGroup>
</BlockControls>
{ isPreview || isPreviewMode || isDisabled ? (
<>
<Preview
content={ attributes.content }
isSelected={ isSelected }
/>
<VisuallyHidden id={ instanceId }>
{ __(
'HTML preview is not yet fully accessible. Please switch screen reader to virtualized mode to navigate the below iFrame.'
) }
</VisuallyHidden>
</>
) : (
<PlainText
value={ attributes.content }
onChange={ ( content ) => setAttributes( { content } ) }
placeholder={ __( 'Write HTML…' ) }
aria-label={ __( 'HTML' ) }
/>
) }
</div>
);
}