tables: add option to pass column size when creating a table

- add optionto the XmlComponent to `delete`/skip elements when exporting to xml
This commit is contained in:
Igor Bulovski
2018-04-26 14:16:02 +02:00
parent e67f5f80e1
commit dc136daeab
5 changed files with 63 additions and 15 deletions

View File

@ -51,6 +51,7 @@ describe("External styles factory", () => {
expect(importedStyle.root.length).to.equal(5); expect(importedStyle.root.length).to.equal(5);
expect(importedStyle.root[1]).to.eql({ expect(importedStyle.root[1]).to.eql({
deleted: false,
root: [], root: [],
rootKey: "w:docDefaults", rootKey: "w:docDefaults",
}); });
@ -59,6 +60,7 @@ describe("External styles factory", () => {
"w:defLockedState": "1", "w:defLockedState": "1",
"w:defUIPriority": "99", "w:defUIPriority": "99",
}, },
deleted: false,
root: [], root: [],
rootKey: "w:latentStyles", rootKey: "w:latentStyles",
}); });
@ -74,15 +76,18 @@ describe("External styles factory", () => {
"w:styleId": "Normal", "w:styleId": "Normal",
"w:type": "paragraph", "w:type": "paragraph",
}, },
deleted: false,
root: [ root: [
{ {
_attr: { _attr: {
"w:val": "Normal", "w:val": "Normal",
}, },
deleted: false,
root: [], root: [],
rootKey: "w:name", rootKey: "w:name",
}, },
{ {
deleted: false,
root: [], root: [],
rootKey: "w:qFormat", rootKey: "w:qFormat",
}, },
@ -95,11 +100,13 @@ describe("External styles factory", () => {
"w:styleId": "Heading1", "w:styleId": "Heading1",
"w:type": "paragraph", "w:type": "paragraph",
}, },
deleted: false,
root: [ root: [
{ {
_attr: { _attr: {
"w:val": "heading 1", "w:val": "heading 1",
}, },
deleted: false,
root: [], root: [],
rootKey: "w:name", rootKey: "w:name",
}, },
@ -107,20 +114,25 @@ describe("External styles factory", () => {
_attr: { _attr: {
"w:val": "Normal", "w:val": "Normal",
}, },
deleted: false,
root: [], root: [],
rootKey: "w:basedOn", rootKey: "w:basedOn",
}, },
{ {
deleted: false,
root: [ root: [
{ {
deleted: false,
root: [], root: [],
rootKey: "w:keepNext", rootKey: "w:keepNext",
}, },
{ {
deleted: false,
root: [], root: [],
rootKey: "w:keepLines", rootKey: "w:keepLines",
}, },
{ {
deleted: false,
root: [ root: [
{ {
_attr: { _attr: {
@ -129,6 +141,7 @@ describe("External styles factory", () => {
"w:sz": "4", "w:sz": "4",
"w:val": "single", "w:val": "single",
}, },
deleted: false,
root: [], root: [],
rootKey: "w:bottom", rootKey: "w:bottom",
}, },

View File

@ -8,11 +8,14 @@ export class Table extends XmlComponent {
private readonly rows: TableRow[]; private readonly rows: TableRow[];
private readonly grid: TableGrid; private readonly grid: TableGrid;
constructor(rows: number, cols: number) { constructor(rows: number, cols: number, colSizes?: number[]) {
super("w:tbl"); super("w:tbl");
this.properties = new TableProperties(); this.properties = new TableProperties();
this.root.push(this.properties); this.root.push(this.properties);
if (colSizes && colSizes.length > 0) {
this.grid = new TableGrid(colSizes);
} else {
const gridCols: number[] = []; const gridCols: number[] = [];
for (let i = 0; i < cols; i++) { for (let i = 0; i < cols; i++) {
/* /*
@ -28,6 +31,8 @@ export class Table extends XmlComponent {
gridCols.push(1); gridCols.push(1);
} }
this.grid = new TableGrid(gridCols); this.grid = new TableGrid(gridCols);
}
this.root.push(this.grid); this.root.push(this.grid);
this.rows = []; this.rows = [];
@ -111,6 +116,10 @@ export class TableCell extends XmlComponent {
this.addContent(para); this.addContent(para);
return para; return para;
} }
get cellProperties() {
return this.properties;
}
} }
export class TableCellProperties extends XmlComponent { export class TableCellProperties extends XmlComponent {

View File

@ -2,10 +2,15 @@ import { IXmlableObject } from "./xmlable-object";
export abstract class BaseXmlComponent { export abstract class BaseXmlComponent {
protected rootKey: string; protected rootKey: string;
protected deleted: boolean = false;
constructor(rootKey: string) { constructor(rootKey: string) {
this.rootKey = rootKey; this.rootKey = rootKey;
} }
public abstract prepForXml(): IXmlableObject; public abstract prepForXml(): IXmlableObject;
get isDeleted() {
return this.deleted;
}
} }

View File

@ -18,4 +18,15 @@ describe("XmlComponent", () => {
assert.equal(newJson.rootKey, "w:test"); assert.equal(newJson.rootKey, "w:test");
}); });
}); });
describe("#prepForXml()", () => {
it("should skip deleted elements", () => {
const child = new TestComponent("w:test1");
child.delete();
xmlComponent.addChildElement(child);
const xml = xmlComponent.prepForXml();
assert.equal(xml['w:test'].length, 0);
});
});
}); });

View File

@ -12,6 +12,12 @@ export abstract class XmlComponent extends BaseXmlComponent {
public prepForXml(): IXmlableObject { public prepForXml(): IXmlableObject {
const children = this.root const children = this.root
.filter(c => {
if (c instanceof BaseXmlComponent) {
return !c.isDeleted;
}
return true;
})
.map((comp) => { .map((comp) => {
if (comp instanceof BaseXmlComponent) { if (comp instanceof BaseXmlComponent) {
return comp.prepForXml(); return comp.prepForXml();
@ -27,4 +33,8 @@ export abstract class XmlComponent extends BaseXmlComponent {
public addChildElement(child: XmlComponent | string) { public addChildElement(child: XmlComponent | string) {
this.root.push(child); this.root.push(child);
} }
public delete() {
this.deleted = true;
}
} }