Skip to content
Closed
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
4 changes: 2 additions & 2 deletions demo/nested.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ <h1>Nested grids demo</h1>
<a class="btn btn-primary" onClick="addNewWidget('.sub2')" href="#">Add Widget Grid2</a>
<a class="btn btn-primary" onClick="addNested()" href="#">Add Nested Grid</a>
<!-- add .grid-stack-item for acceptWidgets:true -->
<div class="sidebar-item grid-stack-item">Drag nested</div>
<div class="sidebar-item grid-stack-item">Outside Drag</div>
</div>
<br />
<div>
Expand Down Expand Up @@ -65,7 +65,7 @@ <h1>Nested grids demo</h1>
resizable: { handles: 'se,e,s,sw,w'},
// subGridOpts, // options for all subgrids, but defaults to column='auto' now so no need.
children: [
{x:0, y:0, content: 'regular item'},
{x:0, y:0, w: 2, h: 2, content: `<div style="width: 90%" class="sidebar-item grid-stack-item">Drag new widget</div>`},
{x:1, y:0, w:4, h:4, sizeToContent: true, content: '<div>nested grid sizeToContent:true with some header content</div>', subGridOpts: {children: sub1, id:'sub1_grid', class: 'sub1'}},
{x:5, y:0, w:3, h:4, subGridOpts: {children: sub2, id:'sub2_grid', class: 'sub2'}},
]
Expand Down
48 changes: 48 additions & 0 deletions e2e/drag-in-from-grid-item.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { test, expect } from '@playwright/test';
import path from 'node:path';

// a setupDragIn() source living inside a grid item starts its drag with the cursor already over the
// parent grid, which never got a mouseenter so it could not be dropped on until leaving & re-entering
test('drag-in source inside a grid item can drop directly on its parent grid', async ({ page }) => {
await page.setViewportSize({ width: 1200, height: 850 });
await page.setContent(`<style>
body { padding: 30px; }
.grid-stack { width: 800px; background: #eee; }
.grid-stack-item-content { background: lightblue; }
</style><div id="main" class="grid-stack"></div>`);
await page.addStyleTag({ path: path.resolve('dist/gridstack.css') });
await page.addScriptTag({ path: path.resolve('dist/gridstack-all.js') });
await page.evaluate(() => {
const w = window as any;
w.GridStack.renderCB = (el: HTMLElement, n: any) => { if (n.content) el.innerHTML = n.content; };
w.grid = w.GridStack.init({
cellHeight: 60, minRow: 6, acceptWidgets: true, animate: false,
children: [
{ id: 'src', x: 0, y: 0, w: 3, h: 2, content: '<div class="sidebar-item grid-stack-item">Drag new widget</div>' },
{ id: 'sub', x: 4, y: 0, w: 4, h: 3, subGridOpts: { children: [{ content: 'a' }] } },
],
}, '#main');
w.GridStack.setupDragIn('.sidebar-item', undefined, [{ w: 2, h: 1, content: 'dropped' }]);
w.drops = 0;
w.grid.on('dropped', () => w.drops++);
});

// synthetic events: the cursor never leaves the main grid, so no (real) mouseenter is involved
const src = (await page.locator('.sidebar-item').boundingBox())!;
const main = (await page.locator('#main').boundingBox())!;
const fire = (type: string, x: number, y: number) => page.evaluate(({ type, x, y }) => {
const target = type === 'mousedown' ? document.querySelector('.sidebar-item')! : document;
target.dispatchEvent(new MouseEvent(type, { bubbles: true, cancelable: true, button: 0, clientX: x, clientY: y }));
}, { type, x, y });
let x = src.x + 10, y = src.y + 10;
await fire('mousedown', x, y);
const endX = main.x + main.width - 100, endY = main.y + main.height - 60;
for (let i = 1; i <= 10; i++) {
await fire('mousemove', x + (endX - x) * i / 10, y + (endY - y) * i / 10);
}
await expect(page.locator('#main > .grid-stack-placeholder')).toHaveCount(1);
await fire('mouseup', endX, endY);

expect(await page.evaluate(() => (window as any).drops)).toBe(1);
expect(await page.evaluate(() => (window as any).grid.engine.nodes.length)).toBe(3);
});
18 changes: 18 additions & 0 deletions src/dd-draggable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,9 @@ export class DDDraggable extends DDBaseImplement implements HTMLElementExtendOpt
this.option.start(ev, this.ui());
}
this.triggerEvent('dragstart', ev);
if (!grid) {
this._enterStartingDroppable(e);
}
this._checkAutoScroll(); // the very move that started the drag may already be at/past an edge
// now track keyboard events to cancel or rotate
document.addEventListener('keydown', this._keyEvent);
Expand All @@ -348,6 +351,21 @@ export class DDDraggable extends DDBaseImplement implements HTMLElementExtendOpt
return true;
}

/**
* @internal a drag-in item that lives inside a droppable (eg. in a grid item content) starts with the
* cursor already over it, so no mouseenter ever fires until we leave & re-enter. Send the enter ourself
* to the innermost accepting droppable we're in.
*/
protected _enterStartingDroppable(e: MouseEvent): void {
let p = this.el.parentElement as DDElementHost | null;
while (p && !DDManager.dropElement) {
let drop = p.ddElement?.ddDroppable;
console.log("drop333", drop);
if (drop && !drop.disabled && !DDManager.dropElement) drop._mouseEnter(e);
p = p.parentElement as DDElementHost | null;
}
}

/** @internal call when the mouse gets released to drop the item at current location */
protected _mouseUp(e: MouseEvent): void {
this._stopScrolling();
Expand Down
2 changes: 1 addition & 1 deletion src/dd-droppable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class DDDroppable extends DDBaseImplement implements HTMLElementExtendOpt
}

/** @internal called when the cursor enters our area - prepare for a possible drop and track leaving */
protected _mouseEnter(e: MouseEvent): void {
public _mouseEnter(e: MouseEvent): void {
// console.log(`${count++} Enter ${this.el.id}`); // TEST
if (!DDManager.dragElement) return;
// During touch drag operations, ignore real browser-generated mouseenter events (isTrusted:true) vs our simulated ones (isTrusted:false).
Expand Down