1+ /**
2+ * External dependencies
3+ */
4+ import classnames from 'classnames' ;
5+ import TextareaAutosize from 'react-autosize-textarea' ;
6+
17/**
28 * WordPress dependencies
39 */
4- import { __ } from '@wordpress/i18n' ;
5- import { useState } from '@wordpress/element' ;
610import {
711 BlockControls ,
8- PlainText ,
912 transformStyles ,
1013 useBlockProps ,
1114} from '@wordpress/block-editor' ;
1215import {
13- ToolbarButton ,
1416 Disabled ,
1517 SandBox ,
18+ ToolbarButton ,
1619 ToolbarGroup ,
1720} from '@wordpress/components' ;
1821import { useSelect } from '@wordpress/data' ;
22+ import { useState } from '@wordpress/element' ;
23+ import { __ } from '@wordpress/i18n' ;
1924
20- export default function HTMLEdit ( { attributes, setAttributes, isSelected } ) {
21- const [ isPreview , setIsPreview ] = useState ( ) ;
25+ // Default styles used to unset some of the styles
26+ // that might be inherited from the editor style.
27+ const DEFAULT_STYLES = `
28+ html,body,:root {
29+ margin: 0 !important;
30+ padding: 0 !important;
31+ overflow: visible !important;
32+ min-height: auto !important;
33+ }
34+ ` ;
2235
23- const styles = useSelect ( ( select ) => {
24- // Default styles used to unset some of the styles
25- // that might be inherited from the editor style.
26- const defaultStyles = `
27- html,body,:root {
28- margin: 0 !important;
29- padding: 0 !important;
30- overflow: visible !important;
31- min-height: auto !important;
32- }
33- ` ;
36+ export default function HTMLEdit ( { attributes, setAttributes, isSelected } ) {
37+ const [ isPreview , setIsPreview ] = useState ( false ) ;
3438
35- return [
36- defaultStyles ,
39+ const styles = useSelect (
40+ ( select ) => [
41+ DEFAULT_STYLES ,
3742 ...transformStyles (
3843 select ( 'core/block-editor' ) . getSettings ( ) . styles
3944 ) ,
40- ] ;
41- } , [ ] ) ;
45+ ] ,
46+ [ ]
47+ ) ;
4248
4349 function switchToPreview ( ) {
4450 setIsPreview ( true ) ;
@@ -48,55 +54,65 @@ export default function HTMLEdit( { attributes, setAttributes, isSelected } ) {
4854 setIsPreview ( false ) ;
4955 }
5056
57+ const blockProps = useBlockProps ( { className : 'block-library-html' } ) ;
58+
5159 return (
52- < div { ... useBlockProps ( ) } >
60+ < >
5361 < BlockControls >
5462 < ToolbarGroup >
5563 < ToolbarButton
56- className = "components-tab-button"
5764 isPressed = { ! isPreview }
5865 onClick = { switchToHTML }
5966 >
60- < span > HTML</ span >
67+ HTML
6168 </ ToolbarButton >
6269 < ToolbarButton
63- className = "components-tab-button"
6470 isPressed = { isPreview }
6571 onClick = { switchToPreview }
6672 >
67- < span > { __ ( 'Preview' ) } </ span >
73+ { __ ( 'Preview' ) }
6874 </ ToolbarButton >
6975 </ ToolbarGroup >
7076 </ BlockControls >
7177 < Disabled . Consumer >
7278 { ( isDisabled ) =>
7379 isPreview || isDisabled ? (
74- < >
80+ < div { ... blockProps } >
7581 < SandBox
7682 html = { attributes . content }
7783 styles = { styles }
7884 />
79- { /*
80- An overlay is added when the block is not selected in order to register click events.
81- Some browsers do not bubble up the clicks from the sandboxed iframe, which makes it
82- difficult to reselect the block.
83- */ }
85+ { /*
86+ An overlay is added when the block is not selected in order to register click events.
87+ Some browsers do not bubble up the clicks from the sandboxed iframe, which makes it
88+ difficult to reselect the block.
89+ */ }
8490 { ! isSelected && (
85- < div className = "block-library-html__preview-overlay" > </ div >
91+ < div className = "block-library-html__preview-overlay" / >
8692 ) }
87- </ >
93+ </ div >
8894 ) : (
89- < PlainText
90- value = { attributes . content }
91- onChange = { ( content ) =>
92- setAttributes ( { content } )
93- }
94- placeholder = { __ ( 'Write HTML…' ) }
95- aria-label = { __ ( 'HTML' ) }
96- />
95+ < div
96+ { ...blockProps }
97+ className = { classnames (
98+ blockProps . className ,
99+ 'is-html-editor'
100+ ) }
101+ >
102+ < TextareaAutosize
103+ aria-label = { __ ( 'HTML' ) }
104+ value = { attributes . content }
105+ onChange = { ( event ) =>
106+ setAttributes ( {
107+ content : event . target . value ,
108+ } )
109+ }
110+ placeholder = { __ ( 'Write HTML…' ) }
111+ />
112+ </ div >
97113 )
98114 }
99115 </ Disabled . Consumer >
100- </ div >
116+ </ >
101117 ) ;
102118}
0 commit comments