-
Notifications
You must be signed in to change notification settings - Fork 49
Fix table context menu actions and dialog-backed cell splitting #243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import { test, expect } from '@playwright/test'; | ||
| import { EditorPage } from '../helpers/editor-page'; | ||
|
|
||
| async function fillTableWithCoordinates(editor: EditorPage, rows: number, cols: number) { | ||
| for (let row = 0; row < rows; row++) { | ||
| for (let col = 0; col < cols; col++) { | ||
| await editor.clickTableCell(0, row, col); | ||
| await editor.page.keyboard.type(`${row + 1}${col + 1}`); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| async function getTableMatrix(editor: EditorPage) { | ||
| return await editor.page.evaluate(() => { | ||
| const table = document.querySelector('.ProseMirror table'); | ||
| if (!table) return null; | ||
|
|
||
| return Array.from(table.querySelectorAll('tr')).map((row) => | ||
| Array.from(row.querySelectorAll('td, th')).map((cell) => (cell.textContent || '').trim()) | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| test.describe('Table Add Column Regression', () => { | ||
| let editor: EditorPage; | ||
|
|
||
| test.beforeEach(async ({ page }) => { | ||
| editor = new EditorPage(page); | ||
| await editor.goto(); | ||
| await editor.waitForReady(); | ||
| await editor.focus(); | ||
| }); | ||
|
|
||
| test('adding a row and then a column from the middle cell preserves a 4x4 matrix', async () => { | ||
| await editor.insertTable(3, 3); | ||
| await fillTableWithCoordinates(editor, 3, 3); | ||
|
|
||
| await editor.clickTableCell(0, 1, 1); | ||
| await editor.addRowBelow(); | ||
| await editor.page.waitForTimeout(400); | ||
|
|
||
| await editor.clickTableCell(0, 1, 1); | ||
| await editor.addColumnRight(); | ||
| await editor.page.waitForTimeout(600); | ||
|
|
||
| const matrix = await getTableMatrix(editor); | ||
| expect(matrix).toEqual([ | ||
| ['11', '12', '', '13'], | ||
| ['21', '22', '', '23'], | ||
| ['', '', '', ''], | ||
| ['31', '32', '', '33'], | ||
| ]); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import { test, expect } from '@playwright/test'; | ||
| import { EditorPage } from '../helpers/editor-page'; | ||
|
|
||
| test.describe('Table Context Menu', () => { | ||
| let editor: EditorPage; | ||
|
|
||
| test.beforeEach(async ({ page }) => { | ||
| editor = new EditorPage(page); | ||
| await editor.goto(); | ||
| await editor.waitForReady(); | ||
| await editor.focus(); | ||
| }); | ||
|
|
||
| test('right-click table menu shows merge and split actions and can insert a row', async ({ | ||
| page, | ||
| }) => { | ||
| await editor.loadDocxFile('fixtures/with-tables.docx'); | ||
| await editor.rightClickTableCell(0, 0, 0); | ||
|
|
||
| const menu = page.locator('[role="menu"]'); | ||
| await expect(menu).toHaveCount(1); | ||
| await expect( | ||
| menu.locator('[role="menuitem"]').filter({ hasText: /^Merge cells$/ }) | ||
| ).toHaveCount(1); | ||
| await expect(menu.locator('[role="menuitem"]').filter({ hasText: /^Split cell$/ })).toHaveCount( | ||
| 1 | ||
| ); | ||
|
|
||
| await menu | ||
| .locator('[role="menuitem"]') | ||
| .filter({ hasText: /^Insert row below$/ }) | ||
| .click(); | ||
| await page.waitForTimeout(300); | ||
|
|
||
| const dimensions = await editor.getTableDimensions(0); | ||
| expect(dimensions.rows).toBe(4); | ||
| expect(dimensions.cols).toBeGreaterThan(0); | ||
| }); | ||
|
|
||
| test('right-click split cell applies a one-by-two split', async ({ page }) => { | ||
| await editor.loadDocxFile('fixtures/with-tables.docx'); | ||
| await editor.rightClickTableCell(0, 0, 0); | ||
|
|
||
| const menu = page.locator('[role="menu"]'); | ||
| await menu | ||
| .locator('[role="menuitem"]') | ||
| .filter({ hasText: /^Split cell$/ }) | ||
| .click(); | ||
|
|
||
| const dialog = page.getByRole('dialog', { name: 'Split Cell' }); | ||
| await expect(dialog).toBeVisible(); | ||
|
|
||
| const inputs = dialog.locator('input[type="number"]'); | ||
| await inputs.nth(0).fill('1'); | ||
| await inputs.nth(1).fill('2'); | ||
| await dialog.getByRole('button', { name: 'Apply' }).click(); | ||
| await page.waitForTimeout(300); | ||
|
|
||
| const dimensions = await editor.getTableDimensions(0); | ||
| expect(dimensions.cols).toBe(4); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1141,15 +1141,131 @@ function inferTableBorders(rows: TableRow[]): TableBorders | undefined { | |
| return undefined; | ||
| } | ||
|
|
||
| interface PMTableCellAnchor { | ||
| row: number; | ||
| col: number; | ||
| rowspan: number; | ||
| colspan: number; | ||
| cell: TableCell; | ||
| } | ||
|
|
||
| function collectPMTableAnchors( | ||
| node: PMNode, | ||
| documentCounts?: TrackedChangeCounts | ||
| ): { | ||
| anchors: PMTableCellAnchor[]; | ||
| totalCols: number; | ||
| } { | ||
| const occupied: boolean[][] = []; | ||
| const anchors: PMTableCellAnchor[] = []; | ||
| let totalCols = 0; | ||
|
|
||
| for (let rowIndex = 0; rowIndex < node.childCount; rowIndex++) { | ||
| const rowNode = node.child(rowIndex); | ||
| let colIndex = 0; | ||
|
|
||
| rowNode.forEach((cellNode) => { | ||
| if (cellNode.type.name !== 'tableCell' && cellNode.type.name !== 'tableHeader') return; | ||
|
|
||
| while (occupied[rowIndex]?.[colIndex]) colIndex++; | ||
|
|
||
| const rowspan = (cellNode.attrs as TableCellAttrs).rowspan || 1; | ||
| const colspan = (cellNode.attrs as TableCellAttrs).colspan || 1; | ||
|
|
||
| anchors.push({ | ||
| row: rowIndex, | ||
| col: colIndex, | ||
| rowspan, | ||
| colspan, | ||
| cell: convertPMTableCell(cellNode, documentCounts), | ||
| }); | ||
|
|
||
| for (let r = rowIndex; r < rowIndex + rowspan; r++) { | ||
| const rowSlots = occupied[r] ?? []; | ||
| occupied[r] = rowSlots; | ||
| for (let c = colIndex; c < colIndex + colspan; c++) { | ||
| rowSlots[c] = true; | ||
| } | ||
| } | ||
|
|
||
| colIndex += colspan; | ||
| totalCols = Math.max(totalCols, colIndex); | ||
| }); | ||
| } | ||
|
|
||
| return { anchors, totalCols }; | ||
| } | ||
|
|
||
| function convertPMTable(node: PMNode, documentCounts?: TrackedChangeCounts): Table { | ||
| const attrs = node.attrs as TableAttrs; | ||
| const { anchors, totalCols } = collectPMTableAnchors(node, documentCounts); | ||
| const anchorByStart = new Map<string, PMTableCellAnchor>(); | ||
| const anchorByCoveredSlot = new Map<string, PMTableCellAnchor>(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This The old |
||
|
|
||
| for (const anchor of anchors) { | ||
| anchorByStart.set(`${anchor.row}-${anchor.col}`, anchor); | ||
| for (let row = anchor.row; row < anchor.row + anchor.rowspan; row++) { | ||
| for (let col = anchor.col; col < anchor.col + anchor.colspan; col++) { | ||
| anchorByCoveredSlot.set(`${row}-${col}`, anchor); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const rows: TableRow[] = []; | ||
| for (let rowIndex = 0; rowIndex < node.childCount; rowIndex++) { | ||
| const rowNode = node.child(rowIndex); | ||
| const cells: TableCell[] = []; | ||
|
|
||
| for (let colIndex = 0; colIndex < totalCols; ) { | ||
| const anchor = anchorByStart.get(`${rowIndex}-${colIndex}`); | ||
| if (anchor) { | ||
| const formatting = { ...(anchor.cell.formatting ?? {}) }; | ||
| if (anchor.colspan > 1) { | ||
| formatting.gridSpan = anchor.colspan; | ||
| } else { | ||
| delete formatting.gridSpan; | ||
| } | ||
| if (anchor.rowspan > 1) { | ||
| formatting.vMerge = 'restart'; | ||
| } else { | ||
| delete formatting.vMerge; | ||
| } | ||
| cells.push({ | ||
| ...anchor.cell, | ||
| formatting: Object.keys(formatting).length ? formatting : undefined, | ||
| }); | ||
| colIndex += anchor.colspan; | ||
| continue; | ||
| } | ||
|
|
||
| const coveringAnchor = anchorByCoveredSlot.get(`${rowIndex}-${colIndex}`); | ||
| if (!coveringAnchor) { | ||
| colIndex++; | ||
| continue; | ||
| } | ||
|
|
||
| const formatting = { ...(coveringAnchor.cell.formatting ?? {}) }; | ||
| if (coveringAnchor.colspan > 1) { | ||
| formatting.gridSpan = coveringAnchor.colspan; | ||
| } else { | ||
| delete formatting.gridSpan; | ||
| } | ||
| formatting.vMerge = 'continue'; | ||
|
|
||
| node.forEach((rowNode) => { | ||
| if (rowNode.type.name === 'tableRow') { | ||
| rows.push(convertPMTableRow(rowNode, documentCounts)); | ||
| cells.push({ | ||
| ...coveringAnchor.cell, | ||
| content: [], | ||
| formatting, | ||
| }); | ||
| colIndex += coveringAnchor.colspan; | ||
| } | ||
| }); | ||
|
|
||
| rows.push({ | ||
| type: 'tableRow', | ||
| formatting: tableRowAttrsToFormatting(rowNode.attrs as TableRowAttrs), | ||
| cells, | ||
| }); | ||
| } | ||
|
|
||
| const formatting = tableAttrsToFormatting(attrs) || undefined; | ||
| if (!formatting?.borders) { | ||
|
|
@@ -1296,26 +1412,6 @@ function tableAttrsToFormatting(attrs: TableAttrs): TableFormatting | undefined | |
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Convert a ProseMirror table row node to our TableRow type | ||
| */ | ||
| function convertPMTableRow(node: PMNode, documentCounts?: TrackedChangeCounts): TableRow { | ||
| const attrs = node.attrs as TableRowAttrs; | ||
| const cells: TableCell[] = []; | ||
|
|
||
| node.forEach((cellNode) => { | ||
| if (cellNode.type.name === 'tableCell' || cellNode.type.name === 'tableHeader') { | ||
| cells.push(convertPMTableCell(cellNode, documentCounts)); | ||
| } | ||
| }); | ||
|
|
||
| return { | ||
| type: 'tableRow', | ||
| formatting: tableRowAttrsToFormatting(attrs), | ||
| cells, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Convert ProseMirror table row attrs to TableRowFormatting | ||
| */ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the third copy of the same anchor-collection algorithm in this PR (also in
tableSplit.ts:collectTableAnchorsandTableToolbar.tsx:collectDocumentTableAnchors). All three walk the table, build anoccupiedgrid, and produce{ row, col, rowspan, colspan }anchors.The types differ (PMNode vs TableCell) but the grid logic is identical. Worth extracting into a generic helper that takes a row-iterator and a cell-span-getter.