2
2
* iframe block
3
3
*
4
4
* @handle taro-iframe-block-editor
5
- * @deps wp-i18n, wp-components, wp-blocks, wp-block-editor, wp-server-side-render, wp-compose , wp-data
5
+ * @deps wp-i18n, wp-components, wp-blocks, wp-block-editor, wp-server-side-render, wp-data , wp-element
6
6
*/
7
7
8
8
/* global TaroIframeBlockEditor:false */
9
9
10
10
const { registerBlockType } = wp . blocks ;
11
+ const { useState } = wp . element ;
11
12
const { __ , sprintf } = wp . i18n ;
12
13
const { InspectorControls } = wp . blockEditor ;
13
14
const { PanelBody, ToggleControl, TextControl, TextareaControl, Button } = wp . components ;
14
15
const { serverSideRender : ServerSideRender } = wp ;
15
- const { withState } = wp . compose ;
16
16
const { dispatch } = wp . data ;
17
17
18
18
/**
@@ -41,7 +41,7 @@ const convertHtmlToOptions = ( string ) => {
41
41
// error.
42
42
dispatch ( 'core/notices' ) . createNotice ( 'error' , __ ( 'Sorry, but failed to parse iframe tag.' , 'taro-iframe-block' ) , {
43
43
type : 'snackbar' ,
44
- } ) . then ( res => {
44
+ } ) . then ( ( res ) => {
45
45
setTimeout ( ( ) => {
46
46
dispatch ( 'core/notices' ) . removeNotice ( res . notice . id ) ;
47
47
} , 3000 ) ;
@@ -76,14 +76,13 @@ const convertHtmlToOptions = ( string ) => {
76
76
return false ;
77
77
} ;
78
78
79
- const IframeInserter = withState ( {
80
- html : ''
81
- } ) ( ( { html, setState, onConvert } ) => {
79
+ const IframeInserter = ( { onConvert } ) => {
80
+ const [ html , setHtmlState ] = useState ( '' ) ;
82
81
return (
83
82
< >
84
83
< TextareaControl label = { __ ( 'iframe tag' , 'taro-iframe-block' ) } value = { html }
85
- onChange = { ( newHtml ) => setState ( { html : newHtml } ) }
86
- help = { __ ( 'Paste html tag here and convert into options.' , 'taro-iframe-block' ) }
84
+ onChange = { ( newHtml ) => setHtmlState ( newHtml ) }
85
+ help = { __ ( 'Paste html tag here and convert into options.' , 'taro-iframe-block' ) }
87
86
placeholder = { 'e.g. <iframe src="https://example.com" width="640" height="480" />' } rows = { 4 } />
88
87
< Button onClick = { ( ) => {
89
88
if ( onConvert ) {
@@ -97,7 +96,7 @@ const IframeInserter = withState( {
97
96
</ Button >
98
97
</ >
99
98
) ;
100
- } ) ;
99
+ } ;
101
100
102
101
registerBlockType ( 'taro/iframe-block' , {
103
102
@@ -129,7 +128,7 @@ registerBlockType( 'taro/iframe-block', {
129
128
example : {
130
129
src : 'https://wordpress.org' ,
131
130
width : 320 ,
132
- height : 180
131
+ height : 180 ,
133
132
} ,
134
133
135
134
keywords : [ 'iframe' , __ ( 'Embed' , 'taro-iframe-block' ) ] ,
@@ -139,8 +138,6 @@ registerBlockType( 'taro/iframe-block', {
139
138
description : __ ( 'Add responsive iframe block which keep original aspect ratio.' , 'taro-iframe-block' ) ,
140
139
141
140
edit ( { attributes, setAttributes } ) {
142
-
143
-
144
141
let responsiveHelp ;
145
142
if ( attributes . responsive ) {
146
143
if ( / ^ \d + $ / . test ( attributes . width ) && / ^ \d + $ / . test ( attributes . height ) && 0 < attributes . width * attributes . height ) {
@@ -157,16 +154,16 @@ registerBlockType( 'taro/iframe-block', {
157
154
< >
158
155
< InspectorControls >
159
156
< PanelBody defaultOpen = { true } title = { __ ( 'Display Setting' , 'taro-iframe-block' ) } >
160
- < TextControl type = "url" label = { __ ( 'SRC attribute' , 'taro-iframe-block' ) } value = { attributes . src } onChange = { src => setAttributes ( { src } ) } />
161
- < TextControl label = { __ ( 'Width' , 'taro-iframe-block' ) } value = { attributes . width } onChange = { width => setAttributes ( { width } ) } />
162
- < TextControl label = { __ ( 'Height' , 'taro-iframe-block' ) } value = { attributes . height } onChange = { height => setAttributes ( { height } ) } />
163
- < ToggleControl checked = { attributes . responsive } label = { __ ( 'Responsive' , 'taro-iframe-block' ) } onChange = { responsive => setAttributes ( { responsive } ) }
157
+ < TextControl type = "url" label = { __ ( 'SRC attribute' , 'taro-iframe-block' ) } value = { attributes . src } onChange = { ( src ) => setAttributes ( { src } ) } />
158
+ < TextControl label = { __ ( 'Width' , 'taro-iframe-block' ) } value = { attributes . width } onChange = { ( width ) => setAttributes ( { width } ) } />
159
+ < TextControl label = { __ ( 'Height' , 'taro-iframe-block' ) } value = { attributes . height } onChange = { ( height ) => setAttributes ( { height } ) } />
160
+ < ToggleControl checked = { attributes . responsive } label = { __ ( 'Responsive' , 'taro-iframe-block' ) } onChange = { ( responsive ) => setAttributes ( { responsive } ) }
164
161
help = { responsiveHelp } />
165
- < ToggleControl checked = { attributes . fullscreen } label = { __ ( 'Allow Fullscreen' , 'taro-iframe-block' ) } onChange = { fullscreen => setAttributes ( { fullscreen } ) } />
162
+ < ToggleControl checked = { attributes . fullscreen } label = { __ ( 'Allow Fullscreen' , 'taro-iframe-block' ) } onChange = { ( fullscreen ) => setAttributes ( { fullscreen } ) } />
166
163
< TextControl label = { __ ( 'Other Attributes' , 'taro-iframe-block' ) }
167
164
placeholder = { 'e.g. id="frame" name="my-map"' }
168
165
help = { __ ( 'Add other attribute here.' , 'taro-iframe-block' ) }
169
- value = { attributes . other } onChange = { other => setAttributes ( { other } ) } />
166
+ value = { attributes . other } onChange = { ( other ) => setAttributes ( { other } ) } />
170
167
</ PanelBody >
171
168
< PanelBody defaultOpen = { false } title = { __ ( 'Converter' , 'taro-iframe-block' ) } >
172
169
< IframeInserter onConvert = { ( newAttr ) => setAttributes ( newAttr ) } />
0 commit comments