Skip to content

Commit cf30ce8

Browse files
authored
Fix layout grid resize listeners for iframed block editor
Fix layout grid listeners
2 parents 42d4820 + 6534b01 commit cf30ce8

File tree

3 files changed

+89
-24
lines changed

3 files changed

+89
-24
lines changed

blocks/layout-grid/src/grid/resize-grid/index.js

Lines changed: 85 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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
);

bundler/bundles/layout-grid.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"blocks": [
33
"layout-grid"
44
],
5-
"version": "1.8.4",
5+
"version": "1.8.5",
66
"name": "Layout Grid",
77
"description": "Let any blocks align to a global grid",
88
"resource": "jetpack-layout-grid",

bundler/resources/jetpack-layout-grid/readme.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ You can follow development, file an issue, suggest features, and view the source
2424

2525
== Changelog ==
2626

27+
= 1.8.5 - 28th March 2025 =
28+
* Fix drag resize listener for iframed block editor
29+
2730
= 1.8.4 - 11th July 2023 =
2831
* Fix error in editor with icons
2932

0 commit comments

Comments
 (0)