diff --git a/packages/export-docx/src/converters/table-cell.ts b/packages/export-docx/src/converters/table-cell.ts index bc5b5f6..5831d93 100644 --- a/packages/export-docx/src/converters/table-cell.ts +++ b/packages/export-docx/src/converters/table-cell.ts @@ -50,6 +50,14 @@ export function convertTableCell( }, }); } + if (!cell.options.width) { + Object.assign(cell.options, { + width: { + size: 2000, // Default width of about 1.4 inches (2000/1440) + type: "dxa" as const, + }, + }); + } return cell; } diff --git a/packages/export-docx/src/converters/table-header.ts b/packages/export-docx/src/converters/table-header.ts index 68291b3..6d7ab34 100644 --- a/packages/export-docx/src/converters/table-header.ts +++ b/packages/export-docx/src/converters/table-header.ts @@ -42,11 +42,29 @@ export function convertTableHeader( Object.assign(headerCell.options, { rowSpan: node.attrs.rowspan }); } - // Add column width if present + // Add column width if present, otherwise use default width if (node.attrs?.colwidth !== null && node.attrs?.colwidth !== undefined) { + // colwidth is an array in TipTap, take the first value + const width = + Array.isArray(node.attrs.colwidth) && node.attrs.colwidth.length > 0 + ? node.attrs.colwidth[0] + : null; + + if (width) { + Object.assign(headerCell.options, { + width: { + size: width, + type: "dxa" as const, + }, + }); + } + } + + // If no width is set, apply a default minimum width to prevent cells from being too small + if (!headerCell.options.width) { Object.assign(headerCell.options, { width: { - size: node.attrs.colwidth, + size: 2000, // Default width of about 1.4 inches (2000/1440) type: "dxa" as const, }, }); diff --git a/packages/export-docx/src/converters/table.ts b/packages/export-docx/src/converters/table.ts index 720ce14..f8f9174 100644 --- a/packages/export-docx/src/converters/table.ts +++ b/packages/export-docx/src/converters/table.ts @@ -20,6 +20,14 @@ export function convertTable( // Build table options with options const tableOptions: ITableOptions = { rows, + // Set default table width if not specified + width: { + size: options?.run?.width?.size || 100, + type: options?.run?.width?.type || "pct", + }, + // Use fixed layout to ensure cell widths are respected + // This is important when we set specific widths on cells + layout: options?.run?.layout || "fixed", ...options?.run, // Apply table options };