-
Notifications
You must be signed in to change notification settings - Fork 132
feat: adaptive insert table column width #1533
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
Merged
harbournick
merged 5 commits into
main
from
caio/sd-1148-inserttable-with-adaptive-column-widths-word-parity
Feb 20, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
32adc53
feat: adaptive insert table column width
caio-pizzol 0e9e0fe
Merge branch 'main' into caio/sd-1148-inserttable-with-adaptive-colum…
harbournick c31047e
fix: add columnWidths to TableConfig JSDoc typedef for published types
harbournick e193e7f
test(behavior): add behavior test for adaptive column insertion
harbournick 3fbb276
chore: fix resize test
harbournick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 5 additions & 2 deletions
7
packages/super-editor/src/extensions/table/tableHelpers/createCell.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,10 @@ | ||
| // @ts-check | ||
| export const createCell = (cellType, cellContent = null) => { | ||
| export const createCell = (cellType, cellContent = null, attrs = null) => { | ||
| if (cellContent) { | ||
| return cellType.createChecked(null, cellContent); | ||
| return cellType.createChecked(attrs, cellContent); | ||
| } | ||
| if (attrs) { | ||
| return cellType.createAndFill(attrs); | ||
| } | ||
| return cellType.createAndFill(); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
tests/behavior/tests/tables/insert-table-column-widths.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| import { test, expect } from '../../fixtures/superdoc.js'; | ||
| import type { Page } from '@playwright/test'; | ||
|
|
||
| test.use({ config: { toolbar: 'none' } }); | ||
|
|
||
| /** Read colwidth arrays from every cell in the first table found in the document. */ | ||
| async function getCellColwidths(page: Page): Promise<(number[] | null)[]> { | ||
| return page.evaluate(() => { | ||
| const doc = (window as any).editor.state.doc; | ||
| const widths: (number[] | null)[] = []; | ||
| let inTable = false; | ||
|
|
||
| doc.descendants((node: any) => { | ||
| if (node.type.name === 'table') { | ||
| inTable = true; | ||
| return true; // descend into this table | ||
| } | ||
| if (inTable && (node.type.name === 'tableCell' || node.type.name === 'tableHeader')) { | ||
| widths.push(node.attrs.colwidth ?? null); | ||
| } | ||
| }); | ||
|
|
||
| return widths; | ||
| }); | ||
| } | ||
|
|
||
| test.describe('insertTable column widths', () => { | ||
| test('auto-calculates equal column widths from page dimensions', async ({ superdoc }) => { | ||
| await superdoc.executeCommand('insertTable', { rows: 3, cols: 3 }); | ||
| await superdoc.waitForStable(); | ||
|
|
||
| await superdoc.assertTableExists(3, 3); | ||
|
|
||
| const colwidths = await getCellColwidths(superdoc.page); | ||
|
|
||
| // Every cell should have a colwidth set (not null) | ||
| for (const cw of colwidths) { | ||
| expect(cw).not.toBeNull(); | ||
| expect(cw).toHaveLength(1); | ||
| } | ||
|
|
||
| // All columns should have the same width (equal division of page width) | ||
| const uniqueWidths = new Set(colwidths.map((cw) => cw![0])); | ||
| expect(uniqueWidths.size).toBe(1); | ||
|
|
||
| // The auto-calculated width should be a reasonable page-derived value (not the 100px default) | ||
| const autoWidth = colwidths[0]![0]; | ||
| expect(autoWidth).toBeGreaterThan(100); | ||
| }); | ||
|
|
||
| test('uses explicit columnWidths when provided', async ({ superdoc }) => { | ||
| const explicitWidths = [200, 100, 200]; | ||
|
|
||
| await superdoc.executeCommand('insertTable', { | ||
| rows: 2, | ||
| cols: 3, | ||
| columnWidths: explicitWidths, | ||
| }); | ||
| await superdoc.waitForStable(); | ||
|
|
||
| await superdoc.assertTableExists(2, 3); | ||
|
|
||
| const colwidths = await getCellColwidths(superdoc.page); | ||
|
|
||
| // 2 rows x 3 cols = 6 cells | ||
| expect(colwidths).toHaveLength(6); | ||
|
|
||
| // Each cell's colwidth should match the explicit width for its column | ||
| for (let row = 0; row < 2; row++) { | ||
| for (let col = 0; col < 3; col++) { | ||
| const cellIndex = row * 3 + col; | ||
| expect(colwidths[cellIndex]).toEqual([explicitWidths[col]]); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| test('auto widths differ from explicit widths', async ({ superdoc }) => { | ||
| // Insert table with auto widths | ||
| await superdoc.executeCommand('insertTable', { rows: 2, cols: 3 }); | ||
| await superdoc.waitForStable(); | ||
|
|
||
| const autoColwidths = await getCellColwidths(superdoc.page); | ||
| const autoWidth = autoColwidths[0]![0]; | ||
|
|
||
| // Clear the editor and insert with explicit widths | ||
| await superdoc.selectAll(); | ||
| await superdoc.press('Backspace'); | ||
| await superdoc.waitForStable(); | ||
|
|
||
| await superdoc.executeCommand('insertTable', { | ||
| rows: 2, | ||
| cols: 3, | ||
| columnWidths: [200, 100, 200], | ||
| }); | ||
| await superdoc.waitForStable(); | ||
|
|
||
| const explicitColwidths = await getCellColwidths(superdoc.page); | ||
|
|
||
| // Auto-calculated widths should all be equal | ||
| expect(autoColwidths[0]![0]).toBe(autoColwidths[1]![0]); | ||
| expect(autoColwidths[1]![0]).toBe(autoColwidths[2]![0]); | ||
|
|
||
| // Explicit widths should not all be equal | ||
| expect(explicitColwidths[0]![0]).not.toBe(explicitColwidths[1]![0]); | ||
|
|
||
| // The auto width should differ from any of the explicit widths | ||
| expect(autoWidth).not.toBe(200); | ||
| expect(autoWidth).not.toBe(100); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.