@@ -8,6 +8,7 @@ import { isEqual } from 'lodash';
8
8
*/
9
9
import { __ } from '@wordpress/i18n' ;
10
10
import { Component , findDOMNode } from '@wordpress/element' ;
11
+ import { Panel , PanelBody } from '@wordpress/components' ;
11
12
12
13
// @TODO add error handling.
13
14
class MetaboxIframe extends Component {
@@ -17,13 +18,15 @@ class MetaboxIframe extends Component {
17
18
this . state = {
18
19
width : 0 ,
19
20
height : 0 ,
21
+ isOpen : false ,
20
22
} ;
21
23
22
24
this . formData = [ ] ;
23
25
this . originalFormData = [ ] ;
24
26
this . form = null ;
25
27
this . hasLoaded = false ;
26
28
29
+ this . toggle = this . toggle . bind ( this ) ;
27
30
this . checkMessageForResize = this . checkMessageForResize . bind ( this ) ;
28
31
this . handleDoubleBuffering = this . handleDoubleBuffering . bind ( this ) ;
29
32
this . handleMetaboxReload = this . handleMetaboxReload . bind ( this ) ;
@@ -32,6 +35,12 @@ class MetaboxIframe extends Component {
32
35
this . observeChanges = this . observeChanges . bind ( this ) ;
33
36
}
34
37
38
+ toggle ( ) {
39
+ this . setState ( {
40
+ isOpen : ! this . state . isOpen ,
41
+ } ) ;
42
+ }
43
+
35
44
isFrameAccessible ( ) {
36
45
try {
37
46
return ! ! this . node . contentDocument . body ;
@@ -51,13 +60,17 @@ class MetaboxIframe extends Component {
51
60
}
52
61
53
62
componentDidMount ( ) {
54
- /**
55
- * Sets up an event listener for resizing. The resizing occurs inside
56
- * the iframe, see gutenberg/assets/js/metabox.js
57
- */
58
- window . addEventListener ( 'message' , this . checkMessageForResize , false ) ;
59
- this . node . style . display = 'none' ;
60
- this . node . addEventListener ( 'load' , this . observeChanges ) ;
63
+ if ( this . isFrameAccessible ( ) ) {
64
+ /**
65
+ * Sets up an event listener for resizing. The resizing occurs inside
66
+ * the iframe, see gutenberg/assets/js/metabox.js
67
+ */
68
+ window . addEventListener ( 'message' , this . checkMessageForResize , false ) ;
69
+
70
+ // Initially set node to not display anything so that when it loads, we can see it.
71
+ this . node . style . display = 'none' ;
72
+ this . node . addEventListener ( 'load' , this . observeChanges ) ;
73
+ }
61
74
}
62
75
63
76
componentWillReceiveProps ( nextProps ) {
@@ -81,6 +94,20 @@ class MetaboxIframe extends Component {
81
94
}
82
95
}
83
96
97
+ componentDidUpdate ( ) {
98
+ if ( this . isFrameAccessible ( ) ) {
99
+ /**
100
+ * Sets up an event listener for resizing. The resizing occurs inside
101
+ * the iframe, see gutenberg/assets/js/metabox.js
102
+ */
103
+ window . addEventListener ( 'message' , this . checkMessageForResize , false ) ;
104
+
105
+ // Initially set node to not display anything so that when it loads, we can see it.
106
+ //this.node.style.display = 'none';
107
+ this . node . addEventListener ( 'load' , this . observeChanges ) ;
108
+ }
109
+ }
110
+
84
111
handleDoubleBuffering ( ) {
85
112
const iframe = findDOMNode ( this . node ) ;
86
113
const cloneIframe = findDOMNode ( this . clonedNode ) ;
@@ -114,19 +141,21 @@ class MetaboxIframe extends Component {
114
141
}
115
142
116
143
componentWillUnmount ( ) {
117
- const iframe = findDOMNode ( this . node ) ;
118
- iframe . removeEventListener ( 'message' , this . checkMessageForResize ) ;
144
+ if ( this . isFrameAccessible ( ) ) {
145
+ const iframe = findDOMNode ( this . node ) ;
146
+ iframe . removeEventListener ( 'message' , this . checkMessageForResize ) ;
119
147
120
- if ( this . dirtyObserver ) {
121
- this . dirtyObserver . disconnect ( ) ;
122
- }
148
+ if ( this . dirtyObserver ) {
149
+ this . dirtyObserver . disconnect ( ) ;
150
+ }
123
151
124
- if ( this . form !== null ) {
125
- this . form . removeEventListener ( 'input' , this . checkMetaboxState ) ;
126
- this . form . removeEventListener ( 'change' , this . checkMetaboxState ) ;
127
- }
152
+ if ( this . form !== null ) {
153
+ this . form . removeEventListener ( 'input' , this . checkMetaboxState ) ;
154
+ this . form . removeEventListener ( 'change' , this . checkMetaboxState ) ;
155
+ }
128
156
129
- this . node . removeEventListener ( 'load' , this . observeChanges ) ;
157
+ this . node . removeEventListener ( 'load' , this . observeChanges ) ;
158
+ }
130
159
}
131
160
132
161
observeChanges ( ) {
@@ -157,7 +186,7 @@ class MetaboxIframe extends Component {
157
186
}
158
187
159
188
getFormData ( node ) {
160
- if ( ! this . isFrameAccessible ) {
189
+ if ( ! this . isFrameAccessible ( ) ) {
161
190
return ;
162
191
}
163
192
@@ -169,19 +198,21 @@ class MetaboxIframe extends Component {
169
198
}
170
199
171
200
checkMetaboxState ( ) {
172
- const entries = this . getFormData ( this . node ) ;
201
+ if ( this . props . isUpdating !== true ) {
202
+ const entries = this . getFormData ( this . node ) ;
173
203
174
- if ( ! isEqual ( this . originalFormData , entries ) ) {
175
- if ( this . props . isDirty === false ) {
176
- this . props . changedMetaboxState ( this . props . location , true ) ;
177
- }
204
+ if ( ! isEqual ( this . originalFormData , entries ) ) {
205
+ if ( this . props . isDirty === false ) {
206
+ this . props . changedMetaboxState ( this . props . location , true ) ;
207
+ }
178
208
179
- return ;
180
- }
209
+ return ;
210
+ }
181
211
182
- // If the data is the same as the original and we have metabox marked as dirty.
183
- if ( this . props . isDirty === true ) {
184
- this . props . changedMetaboxState ( this . props . location , false ) ;
212
+ // If the data is the same as the original and we have metabox marked as dirty.
213
+ if ( this . props . isDirty === true ) {
214
+ this . props . changedMetaboxState ( this . props . location , false ) ;
215
+ }
185
216
}
186
217
}
187
218
@@ -231,20 +262,28 @@ class MetaboxIframe extends Component {
231
262
232
263
render ( ) {
233
264
const { location, className, id } = this . props ;
265
+ const { isOpen } = this . state ;
234
266
235
267
return (
236
- < div id = "iframe-container" className = { className } >
237
- < iframe
238
- ref = { ( node ) => {
239
- this . node = node ;
240
- } }
268
+ < Panel className = "editor-meta-boxes" >
269
+ < PanelBody
241
270
title = { __ ( 'Extended Settings' ) }
242
- key = "metabox"
243
- id = { id }
244
- src = { `${ window . _wpMetaboxUrl } &metabox=${ location } ` }
245
- width = { Math . ceil ( this . state . width ) }
246
- height = { Math . ceil ( this . state . height ) } />
247
- </ div >
271
+ opened = { isOpen }
272
+ onToggle = { this . toggle } >
273
+ < div id = "iframe-container" className = { className } >
274
+ < iframe
275
+ ref = { ( node ) => {
276
+ this . node = node ;
277
+ } }
278
+ title = { __ ( 'Extended Settings' ) }
279
+ key = "metabox"
280
+ id = { id }
281
+ src = { `${ window . _wpMetaboxUrl } &metabox=${ location } ` }
282
+ width = { Math . ceil ( this . state . width ) }
283
+ height = { Math . ceil ( this . state . height ) } />
284
+ </ div >
285
+ </ PanelBody >
286
+ </ Panel >
248
287
) ;
249
288
}
250
289
}
0 commit comments