|
| 1 | +import { test, expect, Page, Locator } from '@playwright/test'; |
| 2 | +import path from 'node:path'; |
| 3 | + |
| 4 | +async function setup(page: Page, rtl: boolean, direction: string, width: number, sourceRtl?: boolean) { |
| 5 | + await page.setViewportSize({ width: 1500, height: 850 }); |
| 6 | + await page.setContent(`<style> |
| 7 | + body { margin: 0; padding: 30px; } |
| 8 | + main { display: flex; direction: ltr; gap: 40px; margin-top: 140px; } |
| 9 | + .grid-stack { width: 600px; min-height: 400px; background: #eee; } |
| 10 | + .grid-stack-item-content { background: lightblue; } |
| 11 | + #sidebar { position: absolute; left: 30px; top: 30px; width: ${width * 100}px; height: 100px; background: lightblue; } |
| 12 | + </style> |
| 13 | + <div id="sidebar" class="grid-stack-item">Drag me</div> |
| 14 | + <main><div style="direction: ${direction}"><div id="target" class="grid-stack"></div></div> |
| 15 | + ${sourceRtl === undefined ? '' : '<div id="source" class="grid-stack"></div>'}</main>`); |
| 16 | + await page.addStyleTag({ path: path.resolve('dist/gridstack.css') }); |
| 17 | + await page.addScriptTag({ path: path.resolve('dist/gridstack-all.js') }); |
| 18 | + await page.evaluate(({ rtl, width, sourceRtl }) => { |
| 19 | + // GridStack emits a CustomEvent named dragstart, not an HTML5 DragEvent. |
| 20 | + // Keep Playwright's native-drag detector from waiting for a nonexistent native drag. |
| 21 | + window.addEventListener('dragstart', event => { |
| 22 | + if (event instanceof CustomEvent) event.stopImmediatePropagation(); |
| 23 | + }, true); |
| 24 | + const w = window as any; |
| 25 | + const options = { column: 6, cellHeight: 100, minRow: 4, float: true, margin: 0, animate: false, acceptWidgets: true }; |
| 26 | + w.grid = w.GridStack.init({ ...options, rtl }, '#target'); |
| 27 | + if (sourceRtl === undefined) { |
| 28 | + w.GridStack.setupDragIn('#sidebar', { helper: 'clone' }, [{ id: 'incoming', w: width, h: 1, content: 'Dropped' }]); |
| 29 | + } else { |
| 30 | + w.source = w.GridStack.init({ ...options, rtl: sourceRtl }, '#source'); |
| 31 | + w.source.load([{ id: 'incoming', x: 1, y: 0, w: width, h: 1, content: 'Transfer' }]); |
| 32 | + } |
| 33 | + }, { rtl, width, sourceRtl }); |
| 34 | +} |
| 35 | + |
| 36 | +async function grab(page: Page, item: Locator, offset: number) { |
| 37 | + const box = (await item.boundingBox())!; |
| 38 | + await page.mouse.move(box.x + offset, box.y + 40); |
| 39 | + await page.mouse.down(); |
| 40 | + await page.mouse.move(box.x + offset + 15, box.y + 45, { steps: 5 }); |
| 41 | +} |
| 42 | + |
| 43 | +async function expectPosition(item: Locator, x: number, y: number, logicalX: number, width: number) { |
| 44 | + await expect(item).toHaveAttribute('gs-x', String(logicalX)); |
| 45 | + await expect.poll(async () => (await item.boundingBox())!.x).toBeCloseTo(x, 0); |
| 46 | + const box = (await item.boundingBox())!; |
| 47 | + expect(box.y).toBeCloseTo(y, 0); |
| 48 | + expect(box.width).toBeCloseTo(width * 100, 0); |
| 49 | +} |
| 50 | + |
| 51 | +// The pointer is grabbed 30px from the physical left edge, not the center. |
| 52 | +// x=0 is the rightmost cell in an RTL grid, regardless of the parent's direction. |
| 53 | +for (const rtl of [false, true]) { |
| 54 | + for (const direction of ['ltr', 'rtl']) { |
| 55 | + for (const width of [1, 2]) { |
| 56 | + test(`sidebar drop: rtl=${rtl}, parent=${direction}, width=${width}`, async ({ page }) => { |
| 57 | + await setup(page, rtl, direction, width); |
| 58 | + const target = (await page.locator('#target').boundingBox())!; |
| 59 | + await grab(page, page.locator('#sidebar'), 30); |
| 60 | + await page.mouse.move(target.x + 130, target.y + 140, { steps: 20 }); |
| 61 | + const placeholder = page.locator('#target .grid-stack-placeholder'); |
| 62 | + await expectPosition(placeholder, target.x + 100, target.y + 100, rtl ? (width === 1 ? 4 : 3) : 1, width); |
| 63 | + await page.mouse.move(target.x + 330, target.y + 140, { steps: 20 }); |
| 64 | + const finalX = rtl ? (width === 1 ? 2 : 1) : 3; |
| 65 | + await expectPosition(placeholder, target.x + 300, target.y + 100, finalX, width); |
| 66 | + await page.mouse.up(); |
| 67 | + await expect(placeholder).toHaveCount(0); |
| 68 | + await expectPosition(page.locator('#target [gs-id="incoming"]'), target.x + 300, target.y + 100, finalX, width); |
| 69 | + expect(await page.evaluate(() => (window as any).grid.save(false).map(n => ({ ...n, w: n.w ?? 1, h: n.h ?? 1 })))).toEqual([ |
| 70 | + expect.objectContaining({ id: 'incoming', x: finalX, y: 1, w: width, h: 1 }) |
| 71 | + ]); |
| 72 | + }); |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +for (const [sourceRtl, rtl] of [[false, true], [true, false], [true, true]]) { |
| 78 | + test(`cross-grid drop: rtl=${sourceRtl} -> ${rtl}`, async ({ page }) => { |
| 79 | + await setup(page, rtl, 'ltr', 2, sourceRtl); |
| 80 | + const target = (await page.locator('#target').boundingBox())!; |
| 81 | + await grab(page, page.locator('#source [gs-id="incoming"]'), 30); |
| 82 | + await page.mouse.move(target.x + 130, target.y + 140, { steps: 30 }); |
| 83 | + const placeholder = page.locator('#target .grid-stack-placeholder'); |
| 84 | + await expectPosition(placeholder, target.x + 100, target.y + 100, rtl ? 3 : 1, 2); |
| 85 | + await page.mouse.move(target.x + 330, target.y + 140, { steps: 20 }); |
| 86 | + const finalX = rtl ? 1 : 3; |
| 87 | + await expectPosition(placeholder, target.x + 300, target.y + 100, finalX, 2); |
| 88 | + await page.mouse.up(); |
| 89 | + await expectPosition(page.locator('#target [gs-id="incoming"]'), target.x + 300, target.y + 100, finalX, 2); |
| 90 | + expect(await page.evaluate(() => (window as any).source.save(false))).toEqual([]); |
| 91 | + expect(await page.evaluate(() => (window as any).grid.save(false).map(n => ({ ...n, w: n.w ?? 1, h: n.h ?? 1 })))).toEqual([ |
| 92 | + expect.objectContaining({ id: 'incoming', x: finalX, y: 1, w: 2, h: 1 }) |
| 93 | + ]); |
| 94 | + }); |
| 95 | +} |
| 96 | + |
| 97 | +for (const rtl of [false, true]) { |
| 98 | + test(`internal drag keeps its coordinate convention: rtl=${rtl}`, async ({ page }) => { |
| 99 | + await setup(page, rtl, 'ltr', 2); |
| 100 | + await page.evaluate(() => (window as any).grid.load([{ id: 'internal', x: 1, y: 0, w: 2, h: 1 }])); |
| 101 | + const target = (await page.locator('#target').boundingBox())!; |
| 102 | + await grab(page, page.locator('#target [gs-id="internal"]'), 30); |
| 103 | + await page.mouse.move(target.x + 230, target.y + 140, { steps: 20 }); |
| 104 | + const placeholder = page.locator('#target .grid-stack-placeholder'); |
| 105 | + await expectPosition(placeholder, target.x + 200, target.y + 100, 2, 2); |
| 106 | + await page.mouse.up(); |
| 107 | + await expectPosition(page.locator('#target [gs-id="internal"]'), target.x + 200, target.y + 100, 2, 2); |
| 108 | + expect(await page.evaluate(() => (window as any).grid.save(false).map(n => ({ ...n, w: n.w ?? 1, h: n.h ?? 1 })))).toEqual([ |
| 109 | + expect.objectContaining({ id: 'internal', x: 2, y: 1, w: 2, h: 1 }) |
| 110 | + ]); |
| 111 | + }); |
| 112 | +} |
0 commit comments