Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ Change log
<!-- END doctoc generated TOC please keep comment here to allow auto update -->

## 14.0.1 (TBD)
* fix: [#2728](https://github.com/gridstack/gridstack.js/issues/2728) drag helper drifts off the cursor when the page scrolls under a CSS-transformed containing block
* fix: [#1959](https://github.com/gridstack/gridstack.js/issues/1959) `update()` refuses a move that would land on a locked item
* fix: [#2666](https://github.com/gridstack/gridstack.js/issues/2666) iOS auto-scroll uses visualViewport, and un-bind touch handlers
* fix: [#3418](https://github.com/gridstack/gridstack.js/pull/3418) preserve saved layout after cancelling a cross-grid drag- #3418 - thank you [sameerdeolalikar](https://github.com/sameerdeolalikar)
Expand Down
78 changes: 78 additions & 0 deletions spec/regression-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -884,4 +884,82 @@ describe('regression >', () => {
expect(free.y).toBe(2);
});
});

describe('2728 drag + page scroll under a css transform >', () => {
let host: HTMLElement;
// jsdom has no layout, so the real probe measures 0x0 -> scale Infinity. Feed it a
// believable translate(50,100) scale(0.5) reading like demo/transform.html has.
const SCALED = {xScale: 2, yScale: 2, xOffset: 50, yOffset: 100};

afterEach(() => {
delete DDManager.mouseHandled;
delete DDManager.dragElement;
vi.restoreAllMocks();
document.getElementById('gs-cont')?.remove();
});

/** get into a live dragging state so helper/dragOffset/dragTransform are all set up */
const startDrag = (transform = SCALED) => {
vi.spyOn(Utils, 'getValuesFromTransformedElement').mockReturnValue({...transform});
document.body.insertAdjacentHTML('afterbegin',
'<div id="gs-cont"><div class="scaled"><div class="grid-stack">' +
'<div class="grid-stack-item"><div class="grid-stack-item-content">x</div></div>' +
'</div></div></div>');
host = document.querySelector('.grid-stack-item');
const handle = host.querySelector('.grid-stack-item-content') as HTMLElement;
const dd = DDElement.init(host as GridItemHTMLElement).setupDraggable({handle: '.grid-stack-item-content'}).ddDraggable!;
const down = new MouseEvent('mousedown', {button: 0, bubbles: true, clientX: 100, clientY: 100});
Object.defineProperty(down, 'target', {value: handle});
Object.defineProperty(down, 'currentTarget', {value: handle});
dd['_mouseDown'](down);
dd['_mouseMove'](new MouseEvent('mousemove', {clientX: 120, clientY: 120}));
expect(dd['dragging']).toBe(true);
return dd;
};

it('backs the helper out by however far the containing block scrolled', () => {
const dd = startDrag();
expect(dd['_cbDrift']).toEqual({dx: 0, dy: 0});

const move = new MouseEvent('mousemove', {clientX: 120, clientY: 120});
dd['_dragFollow'](move);
const before = parseFloat(dd['helper']!.style.top);
expect(Number.isFinite(before)).toBe(true);

// page scrolls down 200px: the transformed block - and the fixed probe inside it - slides up 200
vi.mocked(Utils.getValuesFromTransformedElement).mockReturnValue({...SCALED, yOffset: SCALED.yOffset - 200});
document.dispatchEvent(new Event('scroll'));
expect(dd['_cbDrift']).toEqual({dx: 0, dy: -200});

dd['_dragFollow'](move);
const after = parseFloat(dd['helper']!.style.top);
// pushed back down by the scroll, expressed in the block's own unscaled units
expect(after - before).toBeCloseTo(200 * SCALED.yScale, 5);

dd['_mouseUp'](new MouseEvent('mouseup'));
expect(dd['_cbDrift']).toEqual({dx: 0, dy: 0});
});

it('no transform: the fixed probe reads (0,0) at any scroll, so nothing is corrected', () => {
const NONE = {xScale: 1, yScale: 1, xOffset: 0, yOffset: 0};
const dd = startDrag(NONE);
const move = new MouseEvent('mousemove', {clientX: 120, clientY: 120});
dd['_dragFollow'](move);
const before = parseFloat(dd['helper']!.style.top);

document.dispatchEvent(new Event('scroll')); // probe still (0,0) - viewport is the containing block
expect(dd['_cbDrift']).toEqual({dx: 0, dy: 0});
dd['_dragFollow'](move);
expect(parseFloat(dd['helper']!.style.top)).toBe(before);
dd['_mouseUp'](new MouseEvent('mouseup'));
});

it('stops listening for scroll once the drag ends', () => {
const dd = startDrag();
dd['_mouseUp'](new MouseEvent('mouseup'));
vi.mocked(Utils.getValuesFromTransformedElement).mockReturnValue({...SCALED, yOffset: SCALED.yOffset - 500});
document.dispatchEvent(new Event('scroll'));
expect(dd['_cbDrift']).toEqual({dx: 0, dy: 0});
});
});
});
31 changes: 28 additions & 3 deletions src/dd-draggable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ export class DDDraggable extends DDBaseImplement implements HTMLElementExtendOpt
};
/** @internal element we bound the touch handlers to in _mouseDown(), so _mouseUp() can un-bind the same one */
protected _touchTarget?: HTMLElement;
/** @internal how far the helper's containing block has slid since dragstart.
* A transform on an ancestor makes IT - not the viewport - the containing block for our
* position:fixed helper, so the left/top we write are relative to that element, and it scrolls
* away from where we measured it at dragstart, drifting the helper off the cursor (#2728).
* With no transform the probe below always reads (0,0) so this stays zero and nothing changes. */
protected _cbDrift: { dx: number, dy: number } = { dx: 0, dy: 0 };
/** @internal auto-scroll animation variables */
protected _autoScrollAnimId?: number;
protected _autoScrollContainer?: HTMLElement;
Expand Down Expand Up @@ -291,7 +297,10 @@ export class DDDraggable extends DDBaseImplement implements HTMLElementExtendOpt
this.helper = this._createHelper();
this._setupHelperContainmentStyle();
this.dragTransform = Utils.getValuesFromTransformedElement(this.helperContainment);
this._cbDrift = { dx: 0, dy: 0 };
this.dragOffset = this._getDragOffset(e, this.el, this.helperContainment);
// capture=true: scroll doesn't bubble, so this is how we hear about ANY container scrolling
document.addEventListener('scroll', this._updateCBDrift, { capture: true, passive: true });
this._setupHelperStyle(e);

const ev = Utils.initEvent<MouseEvent>(e, { target: this.el, type: 'dragstart' });
Expand Down Expand Up @@ -323,6 +332,8 @@ export class DDDraggable extends DDBaseImplement implements HTMLElementExtendOpt
Utils.pauseIframePointerEvents(false);
delete (this.el.gridstackNode as GridStackNodeRotate)?._origRotate;
document.removeEventListener('keydown', this._keyEvent);
document.removeEventListener('scroll', this._updateCBDrift, true);
this._cbDrift = { dx: 0, dy: 0 };

// reset the drop target if dragging over ourself (already parented, just moving during stop callback below)
if (DDManager.dropElement?.el === this.el.parentElement) {
Expand Down Expand Up @@ -448,20 +459,33 @@ export class DDDraggable extends DDBaseImplement implements HTMLElementExtendOpt
return this;
}

/** @internal re-probe where our containing block now sits and record how far it moved since dragstart,
* then re-place the helper right away rather than waiting for the next mouse move. */
protected _updateCBDrift = (): void => {
if (!this.dragging || !this.helperContainment) return;
const t = Utils.getValuesFromTransformedElement(this.helperContainment);
this._cbDrift = {
dx: t.xOffset - this.dragTransform.xOffset,
dy: t.yOffset - this.dragTransform.yOffset,
};
if (this.lastDrag) this._dragFollow(this.lastDrag);
}

/** @internal updates the top/left position to follow the mouse */
public _dragFollow(e: MouseEvent): void {
const style = this.helper!.style;
const offset = this.dragOffset;
const { dx, dy } = this._cbDrift; // back out however far our containing block has scrolled (#2728)
if (this.option.rtl) {
style.right = ((window.innerWidth - e.clientX) + offset.offsetX) * this.dragTransform.xScale + 'px';
style.right = ((window.innerWidth - e.clientX) + offset.offsetX + dx) * this.dragTransform.xScale + 'px';
if (style.left)
style.left = '';
} else {
style.left = (e.clientX + offset.offsetX) * this.dragTransform.xScale + 'px';
style.left = (e.clientX + offset.offsetX - dx) * this.dragTransform.xScale + 'px';
if (style.right)
style.right = '';
}
style.top = (e.clientY + offset.offsetTop) * this.dragTransform.yScale + 'px';
style.top = (e.clientY + offset.offsetTop - dy) * this.dragTransform.yScale + 'px';
}

/** @internal */
Expand Down Expand Up @@ -550,6 +574,7 @@ export class DDDraggable extends DDBaseImplement implements HTMLElementExtendOpt
if (scrollCont.scrollTop === prevScroll) { this._stopScrolling(); return; }

if (this.dragging && this.lastDrag) {
this._updateCBDrift(); // we just scrolled; the 'scroll' event lands async so re-sync now
this._dragFollow(this.lastDrag);
this._callDrag(this.lastDrag);
}
Expand Down