@@ -18,9 +18,9 @@ function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
1818 var _wp$components = wp . components ,
1919 PanelBody = _wp$components . PanelBody ,
2020 ToggleControl = _wp$components . ToggleControl ,
21+ TextareaControl = _wp$components . TextareaControl ,
2122 Button = _wp$components . Button ,
22- Notice = _wp$components . Notice ,
23- FormFileUpload = _wp$components . FormFileUpload ;
23+ Notice = _wp$components . Notice ;
2424
2525 /**
2626 * Add custom SVG animation controls to GenerateBlocks Shape block.
@@ -45,14 +45,6 @@ function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
4545 _useState4 = _slicedToArray ( _useState3 , 2 ) ,
4646 jsonPreview = _useState4 [ 0 ] ,
4747 setJsonPreview = _useState4 [ 1 ] ;
48- var _useState5 = useState ( '' ) ,
49- _useState6 = _slicedToArray ( _useState5 , 2 ) ,
50- fileName = _useState6 [ 0 ] ,
51- setFileName = _useState6 [ 1 ] ;
52- var _useState7 = useState ( 0 ) ,
53- _useState8 = _slicedToArray ( _useState7 , 2 ) ,
54- fileInputKey = _useState8 [ 0 ] ,
55- setFileInputKey = _useState8 [ 1 ] ;
5648
5749 // Detect if JSON is Lottie format.
5850 var isLottieJson = function isLottieJson ( parsed ) {
@@ -100,46 +92,19 @@ function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
10092 }
10193 } ;
10294
103- // Handle file upload.
104- var handleFileUpload = function handleFileUpload ( event ) {
105- var file = event . target . files [ 0 ] ;
106- if ( ! file ) {
107- return ;
108- }
109-
110- // Check if it's a JSON file.
111- if ( ! file . name . endsWith ( '.json' ) ) {
112- setJsonError ( __ ( 'Please select a JSON file' , 'frontblocks' ) ) ;
113- setFileInputKey ( function ( prev ) { return prev + 1 ; } ) ;
114- return ;
95+ // Handle JSON change.
96+ var handleJsonChange = function handleJsonChange ( value ) {
97+ setAttributes ( {
98+ frblCustomSvgAnimationJson : value
99+ } ) ;
100+ if ( value . trim ( ) ) {
101+ validateJson ( value ) ;
102+ } else {
103+ setJsonError ( '' ) ;
104+ setJsonPreview ( null ) ;
115105 }
116-
117- setFileName ( file . name ) ;
118-
119- // Read file content.
120- var reader = new FileReader ( ) ;
121- reader . onload = function ( e ) {
122- var content = e . target . result ;
123- setAttributes ( { frblCustomSvgAnimationJson : content } ) ;
124- validateJson ( content ) ;
125- setFileInputKey ( function ( prev ) { return prev + 1 ; } ) ;
126- } ;
127- reader . onerror = function ( ) {
128- setJsonError ( __ ( 'Error reading file' , 'frontblocks' ) ) ;
129- setFileInputKey ( function ( prev ) { return prev + 1 ; } ) ;
130- } ;
131- reader . readAsText ( file ) ;
132- } ;
133-
134- // Clear imported JSON.
135- var handleClear = function handleClear ( ) {
136- setAttributes ( { frblCustomSvgAnimationJson : '' } ) ;
137- setJsonError ( '' ) ;
138- setJsonPreview ( null ) ;
139- setFileName ( '' ) ;
140- setFileInputKey ( function ( prev ) { return prev + 1 ; } ) ;
141106 } ;
142-
107+
143108 // Example JSON template.
144109 var exampleJson = JSON . stringify ( {
145110 "svg" : "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 24 24\" width=\"48\" height=\"48\" fill=\"currentColor\"><path d=\"M12 2L2 7v10c0 5.55 3.84 10.74 9 12 5.16-1.26 9-6.45 9-12V7l-10-5z\"/></svg>" ,
@@ -152,20 +117,6 @@ function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
152117 "trigger" : "load"
153118 }
154119 } , null , 2 ) ;
155-
156- // Download example JSON.
157- var handleDownloadExample = function handleDownloadExample ( ) {
158- var blob = new Blob ( [ exampleJson ] , { type : 'application/json' } ) ;
159- var url = URL . createObjectURL ( blob ) ;
160- var a = document . createElement ( 'a' ) ;
161- a . href = url ;
162- a . download = 'example-animation.json' ;
163- document . body . appendChild ( a ) ;
164- a . click ( ) ;
165- document . body . removeChild ( a ) ;
166- URL . revokeObjectURL ( url ) ;
167- } ;
168-
169120 return wp . element . createElement ( Fragment , { } , wp . element . createElement ( BlockEdit , props ) , wp . element . createElement ( InspectorControls , { } , wp . element . createElement ( PanelBody , {
170121 title : __ ( 'FrontBlocks Custom SVG Animation' , 'frontblocks' ) ,
171122 initialOpen : false
@@ -181,36 +132,16 @@ function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
181132 } ) , frblCustomSvgAnimationEnabled && wp . element . createElement ( Fragment , { } , wp . element . createElement ( 'p' , {
182133 style : {
183134 fontSize : '12px' ,
184- marginBottom : '12px ' ,
135+ marginBottom : '8px ' ,
185136 color : '#757575'
186137 }
187- } , __ ( 'Import a JSON file with your animation configuration:' , 'frontblocks' ) ) , wp . element . createElement ( FormFileUpload , {
188- key : fileInputKey ,
189- accept : '.json' ,
190- onChange : handleFileUpload ,
191- render : function ( ref ) {
192- return wp . element . createElement ( Button , {
193- isSecondary : true ,
194- onClick : ref . openFileDialog ,
195- style : { marginBottom : '8px' , width : '100%' }
196- } , fileName ? __ ( 'Change JSON file' , 'frontblocks' ) : __ ( 'Import JSON file' , 'frontblocks' ) ) ;
197- }
198- } ) , fileName && wp . element . createElement ( 'div' , {
199- style : {
200- display : 'flex' ,
201- alignItems : 'center' ,
202- justifyContent : 'space-between' ,
203- marginBottom : '12px' ,
204- padding : '8px' ,
205- background : '#f6f7f7' ,
206- borderRadius : '4px' ,
207- fontSize : '12px'
208- }
209- } , wp . element . createElement ( 'span' , { } , '📄 ' + fileName ) , wp . element . createElement ( Button , {
210- isSmall : true ,
211- isDestructive : true ,
212- onClick : handleClear
213- } , __ ( 'Clear' , 'frontblocks' ) ) ) , jsonError && wp . element . createElement ( Notice , {
138+ } , __ ( 'Paste your JSON configuration below:' , 'frontblocks' ) ) , wp . element . createElement ( TextareaControl , {
139+ label : __ ( 'JSON Configuration' , 'frontblocks' ) ,
140+ value : frblCustomSvgAnimationJson ,
141+ onChange : handleJsonChange ,
142+ rows : 10 ,
143+ help : __ ( 'JSON with svg and animation properties' , 'frontblocks' )
144+ } ) , jsonError && wp . element . createElement ( Notice , {
214145 status : 'error' ,
215146 isDismissible : false
216147 } , jsonError ) , jsonPreview && wp . element . createElement ( Notice , {
@@ -227,7 +158,7 @@ function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
227158 fontWeight : '600' ,
228159 marginBottom : '8px'
229160 }
230- } , __ ( '📋 Download example JSON' , 'frontblocks' ) ) , wp . element . createElement ( 'pre' , {
161+ } , __ ( '📋 Show example JSON' , 'frontblocks' ) ) , wp . element . createElement ( 'pre' , {
231162 style : {
232163 background : '#f6f7f7' ,
233164 padding : '12px' ,
@@ -237,23 +168,13 @@ function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
237168 maxHeight : '300px'
238169 }
239170 } , exampleJson ) , wp . element . createElement ( Button , {
240- isSecondary : true ,
241- isSmall : true ,
242- onClick : handleDownloadExample ,
243- style : {
244- marginTop : '8px' ,
245- marginRight : '8px'
246- }
247- } , __ ( 'Download example' , 'frontblocks' ) ) , wp . element . createElement ( Button , {
248171 isSecondary : true ,
249172 isSmall : true ,
250173 onClick : function onClick ( ) {
251174 setAttributes ( {
252175 frblCustomSvgAnimationJson : exampleJson
253176 } ) ;
254- setFileName ( 'example-animation.json' ) ;
255177 validateJson ( exampleJson ) ;
256- setFileInputKey ( function ( prev ) { return prev + 1 ; } ) ;
257178 } ,
258179 style : {
259180 marginTop : '8px'
0 commit comments