Skip to content

Commit 3bcf007

Browse files
authored
Merge pull request #1530 from hbcarlos/develop
Resizing issue
2 parents e46df91 + 2ec824c commit 3bcf007

File tree

3 files changed

+57
-10
lines changed

3 files changed

+57
-10
lines changed

src/gridstack-dd.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,8 @@ GridStack.prototype._prepareDragDropByNode = function(node: GridStackNode): Grid
415415
if (node._lastTriedX === x && node._lastTriedY === y) return;
416416
} else if (event.type === 'resize') {
417417
if (x < 0) return;
418+
// Scrolling page if needed
419+
Utils.updateScrollResize(event as MouseEvent, el, cellHeight);
418420
w = Math.round(ui.size.width / cellWidth);
419421
h = Math.round(ui.size.height / cellHeight);
420422
if (w === node.w && h === node.h) return;

src/h5/dd-resizable.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import { DDResizableHandle } from './dd-resizable-handle';
99
import { DDBaseImplement, HTMLElementExtendOpt } from './dd-base-impl';
1010
import { DDUtils } from './dd-utils';
11+
import { Utils } from '../utils';
1112
import { DDUIData, Rect, Size } from '../types';
1213

1314
// TODO: merge with DDDragOpt
@@ -37,6 +38,12 @@ export class DDResizable extends DDBaseImplement implements HTMLElementExtendOpt
3738
/** @internal */
3839
private temporalRect: Rect;
3940
/** @internal */
41+
private scrollY: number;
42+
/** @internal */
43+
private scrolled: number;
44+
/** @internal */
45+
private scrollEl: HTMLElement;
46+
/** @internal */
4047
private startEvent: MouseEvent;
4148
/** @internal value saved in the same order as _originStyleProp[] */
4249
private elOriginStyleVal: string[];
@@ -149,6 +156,8 @@ export class DDResizable extends DDBaseImplement implements HTMLElementExtendOpt
149156
/** @internal */
150157
private _resizeStart(event: MouseEvent): DDResizable {
151158
this.originalRect = this.el.getBoundingClientRect();
159+
this.scrollEl = Utils.getScrollParent(this.el);
160+
this.scrollY = this.scrollEl.scrollTop;
152161
this.startEvent = event;
153162
this._setupHelper();
154163
this._applyChange();
@@ -163,6 +172,7 @@ export class DDResizable extends DDBaseImplement implements HTMLElementExtendOpt
163172

164173
/** @internal */
165174
private _resizing(event: MouseEvent, dir: string): DDResizable {
175+
this.scrolled = this.scrollEl.scrollTop - this.scrollY;
166176
this.temporalRect = this._getChange(event, dir);
167177
this._applyChange();
168178
const ev = DDUtils.initEvent<MouseEvent>(event, { type: 'resize', target: this.el });
@@ -185,6 +195,8 @@ export class DDResizable extends DDBaseImplement implements HTMLElementExtendOpt
185195
delete this.startEvent;
186196
delete this.originalRect;
187197
delete this.temporalRect;
198+
delete this.scrollY;
199+
delete this.scrolled;
188200
return this;
189201
}
190202

@@ -215,10 +227,11 @@ export class DDResizable extends DDBaseImplement implements HTMLElementExtendOpt
215227
const oEvent = this.startEvent;
216228
const newRect = { // Note: originalRect is a complex object, not a simple Rect, so copy out.
217229
width: this.originalRect.width,
218-
height: this.originalRect.height,
230+
height: this.originalRect.height + this.scrolled,
219231
left: this.originalRect.left,
220-
top: this.originalRect.top
232+
top: this.originalRect.top - this.scrolled
221233
};
234+
222235
const offsetH = event.clientX - oEvent.clientX;
223236
const offsetV = event.clientY - oEvent.clientY;
224237

@@ -290,7 +303,13 @@ export class DDResizable extends DDBaseImplement implements HTMLElementExtendOpt
290303
private _ui = (): DDUIData => {
291304
const containmentEl = this.el.parentElement;
292305
const containmentRect = containmentEl.getBoundingClientRect();
293-
const rect = this.temporalRect || this.originalRect;
306+
const newRect = { // Note: originalRect is a complex object, not a simple Rect, so copy out.
307+
width: this.originalRect.width,
308+
height: this.originalRect.height + this.scrolled,
309+
left: this.originalRect.left,
310+
top: this.originalRect.top - this.scrolled
311+
};
312+
const rect = this.temporalRect || newRect;
294313
return {
295314
position: {
296315
left: rect.left - containmentRect.left,

src/utils.ts

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -280,15 +280,15 @@ export class Utils {
280280

281281
/** @internal */
282282
static getScrollParent(el: HTMLElement): HTMLElement {
283-
let returnEl;
284-
if (el === null) {
285-
returnEl = null;
286-
} else if (el.scrollHeight > el.clientHeight) {
287-
returnEl = el;
283+
if (el === null) return document.documentElement;
284+
const style = getComputedStyle(el);
285+
const overflowRegex = /(auto|scroll)/;
286+
287+
if (overflowRegex.test(style.overflow + style.overflowY)) {
288+
return el;
288289
} else {
289-
returnEl = this.getScrollParent(el.parentElement);
290+
return this.getScrollParent(el.parentElement);
290291
}
291-
return returnEl;
292292
}
293293

294294
/** @internal */
@@ -327,5 +327,31 @@ export class Utils {
327327
}
328328
}
329329
}
330+
331+
/**
332+
* @internal
333+
*
334+
* Function used to scroll the page.
335+
*
336+
* @param event `MouseEvent` that triggers the resize
337+
* @param el `HTMLElement` that's being resized
338+
* @param distance Distance to scroll
339+
*/
340+
static updateScrollResize(event: MouseEvent, el: HTMLElement, distance: number): void {
341+
const scrollEl = this.getScrollParent(el);
342+
const height = scrollEl.clientHeight;
343+
344+
const top = event.clientY < distance;
345+
const bottom = event.clientY > height - distance;
346+
347+
if (top) {
348+
// This also can be done with a timeout to keep scrolling while the mouse is
349+
// in the scrolling zone. (will have smoother behavior)
350+
scrollEl.scrollBy({ behavior: 'smooth', top: event.clientY - distance});
351+
352+
} else if (bottom) {
353+
scrollEl.scrollBy({ behavior: 'smooth', top: distance - (height - event.clientY)});
354+
}
355+
}
330356
}
331357

0 commit comments

Comments
 (0)