Skip to content

Commit

Permalink
fix: handled table width
Browse files Browse the repository at this point in the history
  • Loading branch information
privateOmega committed May 28, 2020
1 parent 164c0f5 commit 237ddfd
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/helpers/xml-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,10 +370,8 @@ const buildTableCell = (vNode) => {
// eslint-disable-next-line no-plusplus
for (let index = 0; index < vNode.children.length; index++) {
const childVNode = vNode.children[index];
if (isVText(childVNode)) {
const paragraphFragment = buildParagraph(childVNode);
tableCellFragment.import(paragraphFragment);
}
const paragraphFragment = buildParagraph(childVNode);
tableCellFragment.import(paragraphFragment);
}
} else {
// TODO: Figure out why building with buildParagraph() isn't working
Expand Down Expand Up @@ -424,7 +422,7 @@ const buildTableGrid = (vNode, attributes) => {
}).ele('@w', 'tblGrid');
if (vNode.children && Array.isArray(vNode.children) && vNode.children.length) {
const gridColumns = vNode.children.filter((childVNode) => childVNode.tagName === 'col');
const gridWidth = attributes.width / gridColumns.length;
const gridWidth = attributes.maximumWidth / gridColumns.length;
// eslint-disable-next-line no-plusplus
for (let index = 0; index < gridColumns.length; index++) {
const tableGridColFragment = buildTableGridCol(gridWidth);
Expand Down Expand Up @@ -459,12 +457,25 @@ const buildTableBorders = () => {
return tableBordersFragment;
};

const buildTableProperties = (styles) => {
const buildTableWidth = (width) => {
const tableWidthFragment = fragment({
namespaceAlias: { w: namespaces.w },
})
.ele('@w', 'tblW')
.att('@w', 'type', 'dxa')
.att('@w', 'w', String(width))
.up();

return tableWidthFragment;
};

const buildTableProperties = (attributes) => {
const tablePropertiesFragment = fragment({
namespaceAlias: { w: namespaces.w },
}).ele('@w', 'tblPr');
// TODO: Add styles within it

const tableWidthFragment = buildTableWidth(attributes.maximumWidth);
tablePropertiesFragment.import(tableWidthFragment);
const tableBordersFragment = buildTableBorders();
tablePropertiesFragment.import(tableBordersFragment);

Expand All @@ -477,7 +488,7 @@ const buildTable = (vNode, attributes) => {
const tableFragment = fragment({
namespaceAlias: { w: namespaces.w },
}).ele('@w', 'tbl');
const tablePropertiesFragment = buildTableProperties();
const tablePropertiesFragment = buildTableProperties(attributes);
tableFragment.import(tablePropertiesFragment);
if (vNode.children && Array.isArray(vNode.children) && vNode.children.length) {
// eslint-disable-next-line no-plusplus
Expand Down

0 comments on commit 237ddfd

Please sign in to comment.