Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ https://github.com/ProseMirror/prosemirror-tables/blob/master/demo/index.html

.ProseMirror .tableWrapper {
--table-border-width: 1px;
--offset: 1px;
--offset: 2px;

overflow-x: auto;
scrollbar-width: thin;
Expand Down
31 changes: 24 additions & 7 deletions packages/super-editor/src/core/super-converter/exporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import { TrackDeleteMarkName, TrackInsertMarkName, TrackFormatMarkName } from '@
import { carbonCopy } from '../utilities/carbonCopy.js';
import { baseBulletList, baseOrderedListDef } from './v2/exporter/helpers/base-list.definitions.js';
import { translateCommentNode } from './v2/exporter/commentsExporter.js';
import { createColGroup } from '@extensions/table/tableHelpers/createColGroup.js';


/**
* @typedef {Object} ExportParams
Expand Down Expand Up @@ -808,7 +810,7 @@ function translateTable(params) {
params.node = preProcessVerticalMergeCells(params.node, params);
const elements = translateChildNodes(params);
const tableProperties = generateTableProperties(params.node);
const gridProperties = generateTableGrid(params.node);
const gridProperties = generateTableGrid(params.node, params);

elements.unshift(tableProperties);
elements.unshift(gridProperties);
Expand Down Expand Up @@ -992,17 +994,32 @@ function generateTableBorders(node) {
* @param {SchemaNode} node
* @returns {XmlReadyNode} The table grid properties node
*/
function generateTableGrid(node) {
const { gridColumnWidths } = node.attrs;

function generateTableGrid(node, params) {
const { editorSchema } = params;

let colgroup = [];

try {
const pmNode = editorSchema.nodeFromJSON(node);
const cellMinWidth = 25;
const { colgroupValues } = createColGroup(
pmNode,
cellMinWidth,
);

colgroup = colgroupValues;
} catch(err) {
colgroup = [];
}

const elements = [];
gridColumnWidths?.forEach((width) => {
colgroup?.forEach((width) => {
elements.push({
name: 'w:gridCol',

Copy link
Copy Markdown
Contributor Author

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:gridCol element for the table, it was not saved before. Now we generate the same values ​​that were for the table node in the editor.

attributes: { 'w:w': inchesToTwips(width) },
attributes: { 'w:w': pixelsToTwips(width) },
});
});

return {
name: 'w:tblGrid',
elements,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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];

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the column has colspan greater than 1, then it is important that colwidth attribute has this form [200, 400] (not [600] for colspan 2).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or [200,300,300] for colspan 3 (not [800]).

}
}
}

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;
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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
Expand Up @@ -5,6 +5,7 @@ export const createColGroup = (node, cellMinWidth, overrideCol, overrideValue) =
let fixedWidth = true;

const cols = [];
const colsValues = [];
const row = node.firstChild;

if (!row) return {};
Expand All @@ -18,16 +19,19 @@ export const createColGroup = (node, cellMinWidth, overrideCol, overrideValue) =
if (!hasWidth) fixedWidth = false;
const [prop, value] = getColStyleDeclaration(cellMinWidth, hasWidth);
cols.push(['col', { style: `${prop}: ${value}` }]);
colsValues.push(parseInt(value, 10));
}
}

const tableWidth = fixedWidth ? `${totalWidth}px` : '';
const tableMinWidth = fixedWidth ? '' : `${totalWidth}px`;
const colgroup = ['colgroup', {}, ...cols];
const colgroupValues = [...colsValues];

return {
colgroup,
tableWidth,
tableMinWidth,
tableMinWidth,
colgroupValues,
};
};
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export const getExportedResult = async (name, comments = []) => {
const commentDefinitions = processedComments.map((c, index) => getCommentDefinition(c, index));

const [result, params] = exportSchemaToJson({
editorSchema: editor.schema,
node: schema,
bodyNode,
relationships: [],
Expand Down
25 changes: 25 additions & 0 deletions packages/super-editor/src/tests/export/tableExporter.test.js
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);
});
});
34 changes: 34 additions & 0 deletions packages/super-editor/src/tests/import/tableImporter.test.js

Large diffs are not rendered by default.