169 lines
10 KiB
JavaScript
169 lines
10 KiB
JavaScript
// Table drag handles: hover and focus grips, previews, row and column moves, undo, cancellation, keyboard moves, and auto-scroll.
|
|
import assert from "node:assert/strict";
|
|
import path from "node:path";
|
|
import { chooseView, mockTauri, outputDir, runSmoke, smokeUrl } from "../_harness/index.mjs";
|
|
|
|
const source = '# Drag tables\n\n| Name | Value | Notes |\n| :--- | ---: | :---: |\n| Alpha | 10 | **Bold**<br>second line |\n| Beta | 20 | Two |\n| Gamma | 30 | Three |\n| Delta | 40 | Four |\n\nAfter.\n';
|
|
const long = '# Long table\n\n| Item | Value |\n| --- | --- |\n' + Array.from({ length: 30 }, (_, i) => `| Item ${i + 1} | ${i + 1} |`).join('\n') + '\n\nAfter.\n';
|
|
|
|
await runSmoke("tables/drag", async ({ page }) => {
|
|
const output = await outputDir("tables/drag");
|
|
await mockTauri(page, {
|
|
state: { files: { 'drag.md': source, 'long.md': long } },
|
|
commands: test => {
|
|
const workspace = test.workspace({ root: "C:\\smoke-workspace", files: test.state.files, writeEvents: true });
|
|
test.files = workspace.files;
|
|
return workspace;
|
|
},
|
|
});
|
|
async function quickOpen(name) {
|
|
await page.keyboard.press("Control+p");
|
|
await page.getByRole("textbox", { name: "Find a file" }).fill(name);
|
|
await page.getByRole("dialog").getByRole("button").filter({ hasText: name }).first().click();
|
|
await chooseView(page, "Source");
|
|
await page.locator(".source-editor .cm-content").waitFor();
|
|
}
|
|
const table = page.locator('.live-writer table');
|
|
const rows = () => table.locator('tbody tr td:first-child').allTextContents();
|
|
const headers = () => table.locator('th').allTextContents();
|
|
async function grip(axis, index) {
|
|
const handle = page.locator(`.table-drag-${axis}[data-index="${index}"]`);
|
|
await handle.scrollIntoViewIfNeeded();
|
|
const rect = await handle.boundingBox();
|
|
await page.mouse.move(rect.x + rect.width / 2, rect.y + rect.height / 2);
|
|
await page.mouse.down();
|
|
}
|
|
async function target(axis, index, after = false) {
|
|
const rect = await (axis === 'row' ? table.locator('tr').nth(index) : table.locator('th').nth(index)).boundingBox();
|
|
await page.mouse.move(axis === 'row' ? rect.x + 10 : rect.x + (after ? rect.width - 2 : 2), axis === 'row' ? rect.y + (after ? rect.height - 2 : 2) : rect.y + 10, { steps: 8 });
|
|
}
|
|
async function save() {
|
|
await page.keyboard.press('Control+s');
|
|
await page.waitForFunction(() => !document.querySelector('[aria-label^="Save changes"]'));
|
|
return page.evaluate(() => window.__TEST__.files.get('drag.md'));
|
|
}
|
|
|
|
await page.goto(smokeUrl);
|
|
await page.getByText('smoke-workspace', { exact: true }).waitFor();
|
|
await quickOpen('drag.md');
|
|
await chooseView(page, "Write");
|
|
await table.waitFor();
|
|
assert.equal(await page.locator('.table-drag-row[data-index="0"]').count(), 0);
|
|
await page.mouse.move(5, 5);
|
|
await page.waitForFunction(() => [...document.querySelectorAll('.table-drag-handle')].every(el => getComputedStyle(el).opacity === '0'));
|
|
const firstGrip = page.locator('.table-drag-row[data-index="1"]');
|
|
await firstGrip.hover();
|
|
await page.waitForFunction(() => getComputedStyle(document.querySelector('.table-drag-row[data-index="1"]')).opacity === '1');
|
|
assert.equal(await page.locator('.table-drag-row[data-index="2"]').evaluate(el => getComputedStyle(el).opacity), '0');
|
|
await page.mouse.move(5, 5);
|
|
await page.waitForFunction(() => getComputedStyle(document.querySelector('.table-drag-row[data-index="1"]')).opacity === '0');
|
|
await grip('row', 1);
|
|
assert.equal(await page.locator('.table-drag-preview').count(), 0);
|
|
await target('row', 2, true);
|
|
const preview = page.locator('.table-drag-preview');
|
|
await preview.waitFor();
|
|
assert.match(await preview.innerText(), /Alpha/);
|
|
assert.equal(await preview.getAttribute('aria-hidden'), 'true');
|
|
assert.equal(await preview.evaluate(el => getComputedStyle(el).pointerEvents), 'none');
|
|
const initialPreview = await preview.boundingBox();
|
|
await target('row', 4, true);
|
|
assert.ok((await preview.boundingBox()).y > initialPreview.y + 20);
|
|
assert.equal(await preview.locator('.table-drag-preview-label').innerText(), 'Row 2 → 5');
|
|
assert.equal(await table.locator('.is-cell-selected').count(), 3);
|
|
await page.locator('.table-drop-indicator:visible').waitFor();
|
|
await page.screenshot({ path: path.join(output, 'row.png') });
|
|
await page.mouse.up();
|
|
assert.equal(await preview.count(), 0);
|
|
await page.mouse.move(5, 5);
|
|
await page.waitForFunction(() => [...document.querySelectorAll('.table-drag-handle')].every(el => getComputedStyle(el).opacity === '0'));
|
|
assert.deepEqual(await rows(), ['Beta', 'Gamma', 'Delta', 'Alpha']);
|
|
await save();
|
|
await page.waitForFunction(() => window.__TEST__.files.get('drag.md').includes('| Delta | 40 | Four |\n| Alpha'));
|
|
await page.keyboard.press('Control+z');
|
|
assert.deepEqual(await rows(), ['Alpha', 'Beta', 'Gamma', 'Delta']);
|
|
await page.keyboard.press('Control+y');
|
|
assert.deepEqual(await rows(), ['Beta', 'Gamma', 'Delta', 'Alpha']);
|
|
await grip('row', 4); await target('row', 1); await page.mouse.up();
|
|
assert.deepEqual(await rows(), ['Alpha', 'Beta', 'Gamma', 'Delta']);
|
|
await grip('column', 0); await target('column', 2, true);
|
|
assert.deepEqual(await preview.locator('tr').allTextContents(), ['Name', 'Alpha', 'Beta', 'Gamma', 'Delta']);
|
|
await page.locator('.table-drop-indicator:visible').waitFor();
|
|
await page.screenshot({ path: path.join(output, 'column.png') });
|
|
await page.mouse.up();
|
|
assert.deepEqual(await headers(), ['Value', 'Notes', 'Name']);
|
|
await save();
|
|
await page.waitForFunction(() => window.__TEST__.files.get('drag.md').includes('| ---: | :---: | :--- |'));
|
|
await grip('column', 2); await target('column', 0); await page.mouse.up();
|
|
assert.deepEqual(await headers(), ['Name', 'Value', 'Notes']);
|
|
// Cancel, out-of-table drop, and click without a drag must not change source.
|
|
await grip('row', 1); await target('row', 4, true); await page.keyboard.press('Escape'); await page.mouse.up();
|
|
assert.equal(await preview.count(), 0);
|
|
assert.deepEqual(await rows(), ['Alpha', 'Beta', 'Gamma', 'Delta']);
|
|
await grip('row', 1); await page.mouse.move(5, 5, { steps: 5 }); await page.mouse.up();
|
|
assert.deepEqual(await rows(), ['Alpha', 'Beta', 'Gamma', 'Delta']);
|
|
await grip('column', 0); await page.mouse.up();
|
|
assert.equal(await preview.count(), 0);
|
|
assert.deepEqual(await headers(), ['Name', 'Value', 'Notes']);
|
|
// The focused grip offers the same operation to keyboard users.
|
|
// Tab into the handle, rather than leaving mouse focus on it.
|
|
await page.locator('.table-add-row').focus();
|
|
await page.keyboard.press('Tab');
|
|
assert.equal(await page.evaluate(() => document.activeElement?.getAttribute('aria-label')), 'Move column 1');
|
|
await page.waitForFunction(() => getComputedStyle(document.querySelector('.table-drag-column[data-index="0"]')).opacity === '1');
|
|
await page.keyboard.press('ArrowRight');
|
|
assert.deepEqual(await headers(), ['Value', 'Name', 'Notes']);
|
|
assert.equal(await page.evaluate(() => document.activeElement?.getAttribute('aria-label')), 'Move column 2');
|
|
await page.keyboard.press('ArrowLeft');
|
|
assert.deepEqual(await headers(), ['Name', 'Value', 'Notes']);
|
|
// Editing a cell before dragging must commit its latest contents.
|
|
await table.locator('tbody tr').first().locator('td').first().click();
|
|
await page.keyboard.press('Control+End'); await page.keyboard.type(' edited');
|
|
await grip('row', 1); await target('row', 4, true); await page.mouse.up();
|
|
assert.deepEqual(await rows(), ['Beta', 'Gamma', 'Delta', 'Alpha edited']);
|
|
await page.keyboard.press('Control+z');
|
|
assert.deepEqual(await rows(), ['Alpha edited', 'Beta', 'Gamma', 'Delta']);
|
|
// Keep column handles aligned when a narrow table viewport scrolls.
|
|
// At 760 pixels the side panes give way. The test opens both again, so the table pane stays narrow.
|
|
await page.setViewportSize({ width: 760, height: 700 });
|
|
await page.locator('aside.left').waitFor({ state: 'detached' });
|
|
await page.keyboard.press('Control+Backslash');
|
|
await page.locator('aside.left').waitFor();
|
|
await page.getByRole('button', { name: 'Show outline', exact: true }).click();
|
|
await page.locator('aside.right').waitFor();
|
|
await table.locator('th').first().scrollIntoViewIfNeeded();
|
|
await grip('column', 0);
|
|
const viewport = await page.locator('.writer-table .md-table-scroll').boundingBox();
|
|
await page.mouse.move(viewport.x + viewport.width - 2, viewport.y + 15, { steps: 8 });
|
|
await page.waitForFunction(() => { const el = document.querySelector('.writer-table .md-table-scroll'); return el.scrollLeft > 0; });
|
|
await page.waitForFunction(() => { const el = document.querySelector('.writer-table .md-table-scroll'); return el.scrollLeft + el.clientWidth >= el.scrollWidth - 2; });
|
|
await target('column', 2, true); await page.mouse.up();
|
|
assert.deepEqual(await headers(), ['Value', 'Notes', 'Name']);
|
|
await page.screenshot({ path: path.join(output, 'narrow.png') });
|
|
// Long tables scroll the document while the pointer stays at its lower edge.
|
|
await page.setViewportSize({ width: 1440, height: 700 });
|
|
await quickOpen('long.md');
|
|
await chooseView(page, "Write");
|
|
await table.waitFor();
|
|
await grip('row', 1);
|
|
const scroller = await page.locator('.live-writer .cm-scroller').boundingBox();
|
|
const first = await table.locator('tr').nth(1).boundingBox();
|
|
await page.mouse.move(first.x + 20, scroller.y + scroller.height - 5, { steps: 8 });
|
|
await page.waitForFunction(() => {
|
|
const last = document.querySelector('.live-writer table tbody tr:last-child').getBoundingClientRect();
|
|
const viewport = document.querySelector('.live-writer .cm-scroller').getBoundingClientRect();
|
|
return last.bottom < viewport.bottom - 20;
|
|
});
|
|
await target('row', 30, true);
|
|
await page.mouse.up();
|
|
assert.equal((await rows()).at(-1), 'Item 1');
|
|
assert.equal((await rows())[0], 'Item 2');
|
|
await page.keyboard.press('Control+z');
|
|
assert.equal((await rows())[0], 'Item 1');
|
|
await grip('row', 2); await target('row', 3, true);
|
|
await page.locator('.table-drag-row[data-index="2"]').dispatchEvent('pointercancel');
|
|
await page.mouse.up();
|
|
assert.equal((await rows())[1], 'Item 2');
|
|
assert.equal(await page.locator('.is-table-dragging').count(), 0);
|
|
assert.equal(await preview.count(), 0);
|
|
});
|