-
Notifications
You must be signed in to change notification settings - Fork 191
HAR-9454 - handle table colspan and colgroup #464
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
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 |
|---|---|---|
|
|
@@ -118,6 +118,7 @@ export function handleTableCellNode( | |
| columnWidth = null, | ||
| styleTag, | ||
| params, | ||
| cellIndex, | ||
| ) { | ||
| const { docx, nodeListHandler } = params; | ||
| const tcPr = node.elements.find((el) => el.name === 'w:tcPr'); | ||
|
|
@@ -159,10 +160,33 @@ export function handleTableCellNode( | |
| if (width) { | ||
| attributes['colwidth'] = [width]; | ||
| attributes['widthUnit'] = 'px'; | ||
|
|
||
| const defaultColWidths = gridColumnWidths; | ||
| const hasDefaultColWidths = gridColumnWidths && gridColumnWidths.length > 0; | ||
| const colspanNum = parseInt(colspan, 10); | ||
|
|
||
| if (colspanNum && colspanNum > 1 && hasDefaultColWidths) { | ||
| let colwidth = []; | ||
|
|
||
| for (let i = 0; i < colspanNum; i++) { | ||
| let colwidthValue = defaultColWidths[cellIndex + i]; | ||
| let defaultColwidth = 100; | ||
|
|
||
| if (typeof colwidthValue !== 'undefined') { | ||
| colwidth.push(colwidthValue); | ||
| } else { | ||
| colwidth.push(defaultColwidth); | ||
| } | ||
| } | ||
|
|
||
| if (colwidth.length) { | ||
| attributes['colwidth'] = [...colwidth]; | ||
|
Contributor
Author
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. If the column has
Contributor
Author
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. Or |
||
| } | ||
| } | ||
| } | ||
|
|
||
| if (widthType) attributes['widthType'] = widthType; | ||
| if (colspan) attributes['colspan'] = Number(colspan); | ||
| if (colspan) attributes['colspan'] = parseInt(colspan, 10); | ||
| if (background) attributes['background'] = background; | ||
| if (verticalAlign) attributes['verticalAlign'] = verticalAlign; | ||
| if (fontSize) attributes['fontSize'] = fontSize; | ||
|
|
@@ -397,7 +421,7 @@ export function handleTableRowNode(node, table, rowBorders, styleTag, params) { | |
| const content = | ||
| cellNodes?.map((n, index) => { | ||
| const colWidth = cellNodes.length > 1 ? gridColumnWidths[index] : null; | ||
| return handleTableCellNode(n, node, table, borders, colWidth, styleTag, params); | ||
| return handleTableCellNode(n, node, table, borders, colWidth, styleTag, params, index); | ||
| }) || []; | ||
|
|
||
| const newNode = { | ||
|
|
@@ -448,6 +472,6 @@ const getGridColumnWidths = (tableNode) => { | |
| tblGrid?.elements?.flatMap((el) => { | ||
| if (el.name !== 'w:gridCol') return []; | ||
| return twipsToPixels(el.attributes['w:w']); | ||
| }) || {} | ||
| }) || [] | ||
| ); | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import { getExportedResult } from './export-helpers/index.js'; | ||
| import { twipsToPixels } from '../../core/super-converter/helpers.js'; | ||
|
|
||
| describe('test table export', async () => { | ||
| const fileName = 'table-merged-cells.docx'; | ||
| const result = await getExportedResult(fileName); | ||
|
|
||
| const body = {}; | ||
|
|
||
| beforeEach(() => { | ||
| Object.assign(body, result.elements?.find((el) => el.name === 'w:body')); | ||
| }); | ||
|
|
||
| it('correctly gets w:tblGrid', () => { | ||
| const tableGrid = body.elements[0].elements[0].elements; | ||
|
|
||
| const gridCol1 = twipsToPixels(tableGrid[0].attributes['w:w']); | ||
| const gridCol2 = twipsToPixels(tableGrid[1].attributes['w:w']); | ||
| const gridCol3 = twipsToPixels(tableGrid[2].attributes['w:w']); | ||
|
|
||
| expect(gridCol1).toBe(94); | ||
| expect(gridCol2).toBe(331); | ||
| expect(gridCol3).toBe(176); | ||
| }); | ||
| }); |
Large diffs are not rendered by default.
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.
It is important to save
w:gridColelement for the table, it was not saved before. Now we generate the same values that were for the table node in the editor.