Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/super-editor/src/core/super-converter/exporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ export function exportSchemaToJson(params) {
table: wTblNodeTranslator,
tableRow: wTrNodeTranslator,
tableCell: wTcNodeTranslator,
tableHeader: wTcNodeTranslator,
bookmarkStart: wBookmarkStartTranslator,
bookmarkEnd: wBookmarkEndTranslator,
fieldAnnotation: wSdtNodeTranslator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,18 @@ export const fillPlaceholderColumns = ({
*/
export const isPlaceholderCell = (cell) => {
if (!cell) return false;
if (cell.attrs?.__placeholder) return true;
if (cell.attrs?.__placeholder) {
return true;
}

const widths = cell.attrs?.colwidth;
if (Array.isArray(widths) && widths.length > 0) {
const hasMeaningfulWidth = widths.some(
(value) => typeof value === 'number' && Number.isFinite(value) && Math.abs(value) > 1,
);
if (!hasMeaningfulWidth) return true;
if (!hasMeaningfulWidth) {
return true;
}
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ const decode = (params, decodedAttrs) => {
const { node } = params;

const cells = node.content || [];

let leadingPlaceholders = 0;
while (leadingPlaceholders < cells.length && isPlaceholderCell(cells[leadingPlaceholders])) {
leadingPlaceholders += 1;
Expand Down
49 changes: 48 additions & 1 deletion packages/super-editor/src/tests/export/exporter-utils.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest';
import { isLineBreakOnlyRun, processOutputMarks } from '@converter/exporter.js';
import { isLineBreakOnlyRun, processOutputMarks, exportSchemaToJson } from '@converter/exporter.js';

describe('isLineBreakOnlyRun', () => {
it('returns true for a run containing only line break nodes', () => {
Expand Down Expand Up @@ -45,3 +45,50 @@ describe('processOutputMarks', () => {
]);
});
});

describe('exportSchemaToJson', () => {
it('routes tableHeader nodes to the table cell translator (SD-1709)', () => {
const tableHeaderNode = {
type: 'tableHeader',
attrs: {
colspan: 1,
rowspan: 1,
colwidth: [100],
},
content: [
{
type: 'paragraph',
content: [],
},
],
};

const result = exportSchemaToJson({ node: tableHeaderNode });

// tableHeader should be exported as w:tc (same as tableCell)
expect(result).not.toBeNull();
expect(result.name).toBe('w:tc');
});

it('routes tableCell nodes to the table cell translator', () => {
const tableCellNode = {
type: 'tableCell',
attrs: {
colspan: 1,
rowspan: 1,
colwidth: [100],
},
content: [
{
type: 'paragraph',
content: [],
},
],
};

const result = exportSchemaToJson({ node: tableCellNode });

expect(result).not.toBeNull();
expect(result.name).toBe('w:tc');
});
});
131 changes: 130 additions & 1 deletion packages/super-editor/src/tests/export/tableExporter.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getExportedResult } from './export-helpers/index.js';
import { getExportedResult, getExportedResultWithDocContent } from './export-helpers/index.js';
import { twipsToPixels } from '../../core/super-converter/helpers.js';

describe('test table export', async () => {
Expand All @@ -25,3 +25,132 @@ describe('test table export', async () => {
expect(gridCol3).toBeCloseTo(176.133, 3);
});
});

describe('tableHeader export', () => {
it('exports tables with tableHeader nodes to valid OOXML structure', async () => {
const tableWithHeaders = {
type: 'table',
attrs: {
grid: [{ col: 1500 }, { col: 1500 }],
tableProperties: {},
},
content: [
{
type: 'tableRow',
attrs: {},
content: [
{
type: 'tableHeader',
attrs: { colspan: 1, rowspan: 1, colwidth: [100] },
content: [
{
type: 'paragraph',
attrs: {},
content: [{ type: 'run', content: [{ type: 'text', text: 'Header 1' }] }],
},
],
},
{
type: 'tableHeader',
attrs: { colspan: 1, rowspan: 1, colwidth: [100] },
content: [
{
type: 'paragraph',
attrs: {},
content: [{ type: 'run', content: [{ type: 'text', text: 'Header 2' }] }],
},
],
},
],
},
{
type: 'tableRow',
attrs: {},
content: [
{
type: 'tableCell',
attrs: { colspan: 1, rowspan: 1, colwidth: [100] },
content: [
{
type: 'paragraph',
attrs: {},
content: [{ type: 'run', content: [{ type: 'text', text: 'Cell 1' }] }],
},
],
},
{
type: 'tableCell',
attrs: { colspan: 1, rowspan: 1, colwidth: [100] },
content: [
{
type: 'paragraph',
attrs: {},
content: [{ type: 'run', content: [{ type: 'text', text: 'Cell 2' }] }],
},
],
},
],
},
],
};

const result = await getExportedResultWithDocContent([tableWithHeaders]);

const body = result.elements.find((el) => el.name === 'w:body');
expect(body).toBeDefined();

const tbl = body.elements.find((el) => el.name === 'w:tbl');
expect(tbl).toBeDefined();

const tblGrid = tbl.elements.find((el) => el.name === 'w:tblGrid');
expect(tblGrid).toBeDefined();
const gridCols = tblGrid.elements.filter((el) => el.name === 'w:gridCol');
expect(gridCols.length).toBeGreaterThan(0);

const rows = tbl.elements.filter((el) => el.name === 'w:tr');
expect(rows.length).toBe(2);

const firstRowCells = rows[0].elements.filter((el) => el.name === 'w:tc');
expect(firstRowCells.length).toBe(2);

const secondRowCells = rows[1].elements.filter((el) => el.name === 'w:tc');
expect(secondRowCells.length).toBe(2);
});

it('exports mixed tableHeader and tableCell in same row', async () => {
const tableWithMixedCells = {
type: 'table',
attrs: {
grid: [{ col: 1500 }, { col: 1500 }],
tableProperties: {},
},
content: [
{
type: 'tableRow',
attrs: {},
content: [
{
type: 'tableHeader',
attrs: { colspan: 1, rowspan: 1, colwidth: [100] },
content: [{ type: 'paragraph', attrs: {}, content: [] }],
},
{
type: 'tableCell',
attrs: { colspan: 1, rowspan: 1, colwidth: [100] },
content: [{ type: 'paragraph', attrs: {}, content: [] }],
},
],
},
],
};

const result = await getExportedResultWithDocContent([tableWithMixedCells]);

const body = result.elements.find((el) => el.name === 'w:body');
const tbl = body.elements.find((el) => el.name === 'w:tbl');
const rows = tbl.elements.filter((el) => el.name === 'w:tr');
const cells = rows[0].elements.filter((el) => el.name === 'w:tc');

expect(cells.length).toBe(2);
});
});
Loading