@@ -41,7 +41,18 @@ class ResizeGrid extends Component {
4141 const { totalColumns, layoutGrid } = this . props ;
4242 const start = layoutGrid . getStart ( this . state . resizingColumn ) ;
4343 const span = layoutGrid . getSpan ( this . state . resizingColumn ) ;
44- const nearest = Math . min ( totalColumns , Math . max ( 0 , findNearest ( this . containerRef . current , this . getMouseX ( mouse ) , direction , totalColumns ) ) ) ;
44+ const nearest = Math . min (
45+ totalColumns ,
46+ Math . max (
47+ 0 ,
48+ findNearest (
49+ this . containerRef . current ,
50+ this . getMouseX ( mouse ) ,
51+ direction ,
52+ totalColumns
53+ )
54+ )
55+ ) ;
4556
4657 if ( direction === 'left' ) {
4758 if ( nearest === start ) {
@@ -85,7 +96,11 @@ class ResizeGrid extends Component {
8596 const { width } = this . state ;
8697 const handleWidth = optionalWidth > 0 ? optionalWidth : width ;
8798
88- return offset - this . containerRef . current . getBoundingClientRect ( ) . left - ( ( handleWidth ) / 2 ) ;
99+ return (
100+ offset -
101+ this . containerRef . current . getBoundingClientRect ( ) . left -
102+ handleWidth / 2
103+ ) ;
89104 }
90105
91106 getAdjustedTop ( offset ) {
@@ -114,15 +129,24 @@ class ResizeGrid extends Component {
114129 return pos ;
115130 }
116131
117- onMouseDown = ev => {
132+ onMouseDown = ( ev ) => {
118133 const { target } = ev ;
119134
120135 // This is a bit of hardcoded DOM searching - we check if the current click is on a resize handle and then find the column associated with that
121136 // There may be a better way.
122- if ( ( ev . button === 0 || ev . touches ) && ( target . dataset . resizeRight || target . dataset . resizeLeft ) ) {
137+ if (
138+ ( ev . button === 0 || ev . touches ) &&
139+ ( target . dataset . resizeRight || target . dataset . resizeLeft )
140+ ) {
123141 this . block = target . closest ( '.wp-block' ) ;
124142
125- const { height, right, left, top } = this . block . getBoundingClientRect ( ) ;
143+ // Get the document that contains the target element. We need to use target.ownerDocument
144+ // because the Gutenberg editor is iframed, so some events are prevented from bubbling to the document.
145+ // https://github.com/WordPress/gutenberg/blob/trunk/packages/block-editor/src/components/iframe/index.js#L79
146+ this . targetDocument = target . ownerDocument ;
147+
148+ const { height, right, left, top } =
149+ this . block . getBoundingClientRect ( ) ;
126150 const { width } = target . getBoundingClientRect ( ) ;
127151 const pos = this . getChildPosition ( this . block ) ;
128152 const isLeft = target . dataset . resizeLeft ;
@@ -134,24 +158,39 @@ class ResizeGrid extends Component {
134158 width,
135159 top : this . getAdjustedTop ( top ) ,
136160 direction : isLeft ? 'left' : 'right' ,
137- max : isLeft ? this . getAdjustedOffset ( right , width ) : this . getAdjustedOffset ( left , width ) ,
161+ max : isLeft
162+ ? this . getAdjustedOffset ( right , width )
163+ : this . getAdjustedOffset ( left , width ) ,
138164 } ) ;
139165
140166 if ( ev . button === 0 ) {
141- document . addEventListener ( 'mousemove' , this . onMouseMove ) ;
142- document . addEventListener ( 'mouseup' , this . onMouseUp ) ;
167+ // Add listeners to the target document instead of global document
168+ this . targetDocument . addEventListener (
169+ 'mousemove' ,
170+ this . onMouseMove
171+ ) ;
172+ this . targetDocument . addEventListener (
173+ 'mouseup' ,
174+ this . onMouseUp
175+ ) ;
143176
144177 ev . preventDefault ( ) ;
145178 } else {
146- document . addEventListener ( 'touchmove' , this . onMouseMove ) ;
147- document . addEventListener ( 'touchend' , this . onMouseUp ) ;
179+ this . targetDocument . addEventListener (
180+ 'touchmove' ,
181+ this . onMouseMove
182+ ) ;
183+ this . targetDocument . addEventListener (
184+ 'touchend' ,
185+ this . onMouseUp
186+ ) ;
148187 }
149188
150189 ev . stopPropagation ( ) ;
151190 }
152- }
191+ } ;
153192
154- onMouseMove = ev => {
193+ onMouseMove = ( ev ) => {
155194 ev . stopPropagation ( ) ;
156195
157196 if ( ev . touches === undefined ) {
@@ -161,7 +200,9 @@ class ResizeGrid extends Component {
161200 const { height } = this . block . getBoundingClientRect ( ) ;
162201
163202 this . setState ( {
164- xPos : this . getRestrictedOffset ( this . getAdjustedOffset ( this . getMouseX ( ev ) ) ) ,
203+ xPos : this . getRestrictedOffset (
204+ this . getAdjustedOffset ( this . getMouseX ( ev ) )
205+ ) ,
165206 height,
166207 } ) ;
167208
@@ -170,28 +211,49 @@ class ResizeGrid extends Component {
170211 if ( adjustment ) {
171212 this . props . onResize ( this . state . resizingColumn , adjustment ) ;
172213 }
173- }
214+ } ;
174215
175- onMouseUp = ev => {
216+ onMouseUp = ( ev ) => {
176217 this . setState ( { resizingColumn : - 1 } ) ;
177218
178- document . removeEventListener ( 'mousemove' , this . onMouseMove ) ;
179- document . removeEventListener ( 'mouseup' , this . onMouseUp ) ;
180- document . removeEventListener ( 'touchmove' , this . onMouseMove ) ;
181- document . removeEventListener ( 'touchend' , this . onMouseUp ) ;
182- }
219+ this . targetDocument . removeEventListener (
220+ 'mousemove' ,
221+ this . onMouseMove
222+ ) ;
223+ this . targetDocument . removeEventListener ( 'mouseup' , this . onMouseUp ) ;
224+ this . targetDocument . removeEventListener (
225+ 'touchmove' ,
226+ this . onMouseMove
227+ ) ;
228+ this . targetDocument . removeEventListener ( 'touchend' , this . onMouseUp ) ;
229+ } ;
183230
184231 render ( ) {
185232 const { className, children, isSelected } = this . props ;
186233 const { resizingColumn, xPos, height } = this . state ;
187234 const classes = classnames (
188235 className ,
189- resizingColumn !== - 1 ? 'wp-block-jetpack-layout-grid__resizing' : null ,
236+ resizingColumn !== - 1
237+ ? 'wp-block-jetpack-layout-grid__resizing'
238+ : null
190239 ) ;
191240
192241 return (
193- < div className = { classes } onMouseDown = { this . onMouseDown } onTouchStart = { this . onMouseDown } ref = { this . containerRef } >
194- { resizingColumn !== - 1 && < ResizeHandle direction = { this . state . direction } height = { height } xPos = { xPos } top = { this . state . top } isSelected = { isSelected } /> }
242+ < div
243+ className = { classes }
244+ onMouseDown = { this . onMouseDown }
245+ onTouchStart = { this . onMouseDown }
246+ ref = { this . containerRef }
247+ >
248+ { resizingColumn !== - 1 && (
249+ < ResizeHandle
250+ direction = { this . state . direction }
251+ height = { height }
252+ xPos = { xPos }
253+ top = { this . state . top }
254+ isSelected = { isSelected }
255+ />
256+ ) }
195257 { children }
196258 </ div >
197259 ) ;
0 commit comments