Clean up API

This commit is contained in:
Dolan
2018-08-07 01:25:28 +01:00
parent ca8f331eec
commit 12c8cb93f6
21 changed files with 95 additions and 49 deletions

View File

@ -12,8 +12,8 @@ var footer = doc.createFooter();
footer.createParagraph("Footer on another page");
doc.addSection({
headerId: header.Header.referenceId,
footerId: footer.Footer.referenceId,
headerId: header.Header.ReferenceId,
footerId: footer.Footer.ReferenceId,
pageNumberStart: 1,
pageNumberFormatType: docx.PageNumberFormat.DECIMAL,
});
@ -21,8 +21,8 @@ doc.addSection({
doc.createParagraph("hello");
doc.addSection({
headerId: header.Header.referenceId,
footerId: footer.Footer.referenceId,
headerId: header.Header.ReferenceId,
footerId: footer.Footer.ReferenceId,
pageNumberStart: 1,
pageNumberFormatType: docx.PageNumberFormat.DECIMAL,
orientation: docx.PageOrientation.LANDSCAPE,

View File

@ -6,7 +6,7 @@ const table = doc.createTable(4, 4);
table
.getCell(2, 2)
.addContent(new docx.Paragraph("Hello"))
.cellProperties.borders.addTopBorder(docx.BorderStyle.DASH_DOT_STROKED, 3, "red")
.CellProperties.Borders.addTopBorder(docx.BorderStyle.DASH_DOT_STROKED, 3, "red")
.addBottomBorder(docx.BorderStyle.DOUBLE, 3, "blue")
.addStartBorder(docx.BorderStyle.DOT_DOT_DASH, 3, "green")
.addEndBorder(docx.BorderStyle.DOT_DOT_DASH, 3, "#ff8000");

View File

@ -15,12 +15,12 @@ const image5 = docx.Media.addImage(doc, "./demo/images/pizza.gif");
const imageBase64Data = `iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAMAAAD04JH5AAACzVBMVEUAAAAAAAAAAAAAAAA/AD8zMzMqKiokJCQfHx8cHBwZGRkuFxcqFSonJyckJCQiIiIfHx8eHh4cHBwoGhomGSYkJCQhISEfHx8eHh4nHR0lHBwkGyQjIyMiIiIgICAfHx8mHh4lHh4kHR0jHCMiGyIhISEgICAfHx8lHx8kHh4jHR0hHCEhISEgICAlHx8kHx8jHh4jHh4iHSIhHCEhISElICAkHx8jHx8jHh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vFAHiXdIJiXxyCK7KJ5FHoMZGK9xBcwyg2QpdlVMxEUM2iyIMuXXZQNF+HswxMsSAAJRQjoE//eoqDCXBSTO6f1xd+O0iyNRY6jaWi1ALNYCocZROj4JdEikroVkjFk9DcStXxpdfCD2MoXodu4RUU9ptxxmXssOfxnvDVcxRTod9FxyhqLoAqis5aPhwTDp9spRgEH2Q6KLbYoKqlaKTm6Isp0C/sJMnjFvhiERXPQvUNRe9p29lhR04CdBpC8Sl8YiuncIxEuzUUg4Dkgj+paVozygY9plPMh28SaymO9kabAopREGF3vt9MzeFFl8G7lRSZ8FFGK8XX4VA8QjEd7XrM3M0OXz8YCy+qKBLgq3wqnofiTorF0Ax56Rg1J1elW+BBAsVe+My6iYq7IK6keBdOIseV2qn5Pb8f3MqkWAXf9ThM8c8lAOIotuFsF875lRrH5klRcG0+xcPwQ1oLxfeRAP4heQTnGL78X2rqlw2DK59SXAV/zKaiGMAuko5InCt68mcOan5+ohf+z1pP8lQY/GHZQMV4YD3FpXDp4qerqbF/lBWBswyi+AL+ia+maLgcRRQj4IYlY/UpauqKBsPJAxQF8NM1TRQ/RudSPAD34rK3scOuR8/HGcspxsJfOVS8NZbiGXiUtPgINU3v3WFDmx8pEuG3EiqKKVbCC1vm2iZqap5LAtCtleQf8F9sFYWDohzeJczYyQ4V2bEZFGsQgJRGqqqhS2phHTWn9lDkIhBTqWqxQZ+IsRvtdHY9AvI2VX2hW68nfqGmuQsCEl3JdjfCF8OW1bPdtwhQ0gm2mQzfRE3a7KCYj0BNZJs8+Kxf/r6WtTEI2FIqlsMfFgRB5A6KUnSe/vUkX0AnuvUIt8SjM1m6wWQymUwmk8lkMgXRf5vi8rLQxtUhAAAAAElFTkSuQmCC`
const image6 = docx.Media.addImageFromBuffer(doc, Buffer.from(imageBase64Data, 'base64'), 100, 100);
doc.insertImage(image);
doc.insertImage(image2);
doc.insertImage(image3);
doc.insertImage(image4);
doc.insertImage(image5);
doc.insertImage(image6);
doc.addImage(image);
doc.addImage(image2);
doc.addImage(image3);
doc.addImage(image4);
doc.addImage(image5);
doc.addImage(image6);
var exporter = new docx.LocalPacker(doc);
exporter.pack("My Document");

View File

@ -12,6 +12,51 @@
* Follow the `TSLint` rules
## Add vs Create
This is just a guideline, and the rules can sometimes be broken.
* Use `create` if the method `new`'s up an element inside:
```js
public createParagraph() {
const paragraph = new Paragraph();
this.root.push(paragraph);
}
```
* Use `add` if you add the element into the method as a parameter:
```js
public createParagraph(paragraph: Paragraph) {
this.root.push(paragraph);
}
```
## Getters and Setters
Getters and Setters are done with a capital letter like so:
```js
public get Level() {
}
```
There is no performance advantage by doing this. It means we don't need to prefix all private variables with the ugly `_`:
**Do not:**
```js
private get _level: string;
```
**Do**
```js
private get level: string;
```
## Testing
Please write a test of every file you make and suffix it with `.spec.ts`.

View File

@ -37,8 +37,8 @@ Also all the supported section properties are implemented according to: http://o
// Add new section with another header and footer
doc.addSection({
headerId: header.Header.referenceId,
footerId: footer.Footer.referenceId,
headerId: header.Header.ReferenceId,
footerId: footer.Footer.ReferenceId,
pageNumberStart: 1,
pageNumberFormatType: docx.PageNumberFormat.DECIMAL,
});

View File

@ -97,7 +97,7 @@ export class Compiler {
name: "_rels/.rels",
});
for (const data of this.file.Media.array) {
for (const data of this.file.Media.Array) {
this.archive.append(data.stream, {
name: `word/media/${data.fileName}`,
});

View File

@ -98,12 +98,12 @@ export class File {
sectionPropertiesOptions = {
footerType: FooterReferenceType.DEFAULT,
headerType: HeaderReferenceType.DEFAULT,
headerId: header.Header.referenceId,
footerId: footer.Footer.referenceId,
headerId: header.Header.ReferenceId,
footerId: footer.Footer.ReferenceId,
};
} else {
sectionPropertiesOptions.headerId = header.Header.referenceId;
sectionPropertiesOptions.footerId = footer.Footer.referenceId;
sectionPropertiesOptions.headerId = header.Header.ReferenceId;
sectionPropertiesOptions.footerId = footer.Footer.ReferenceId;
}
this.document = new Document(sectionPropertiesOptions);
}
@ -131,7 +131,7 @@ export class File {
return image;
}
public insertImage(image: Image): File {
public addImage(image: Image): File {
this.document.addParagraph(image);
return this;
}
@ -181,7 +181,7 @@ export class File {
const header = new HeaderWrapper(this.media, this.currentRelationshipId++);
this.headerWrapper.push(header);
this.docRelationships.createRelationship(
header.Header.referenceId,
header.Header.ReferenceId,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/header",
`header${this.headerWrapper.length}.xml`,
);
@ -193,7 +193,7 @@ export class File {
const footer = new FooterWrapper(this.media, this.currentRelationshipId++);
this.footerWrapper.push(footer);
this.docRelationships.createRelationship(
footer.Footer.referenceId,
footer.Footer.ReferenceId,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer",
`footer${this.footerWrapper.length}.xml`,
);
@ -207,7 +207,7 @@ export class File {
this.document.Body.DefaultSection.addChildElement(
new HeaderReference({
headerType: HeaderReferenceType.FIRST,
headerId: headerWrapper.Header.referenceId,
headerId: headerWrapper.Header.ReferenceId,
}),
);
@ -251,7 +251,7 @@ export class File {
}
public HeaderByRefNumber(refId: number): HeaderWrapper {
const entry = this.headerWrapper.find((h) => h.Header.referenceId === refId);
const entry = this.headerWrapper.find((h) => h.Header.ReferenceId === refId);
if (entry) {
return entry;
}
@ -267,7 +267,7 @@ export class File {
}
public FooterByRefNumber(refId: number): FooterWrapper {
const entry = this.footerWrapper.find((h) => h.Footer.referenceId === refId);
const entry = this.footerWrapper.find((h) => h.Footer.ReferenceId === refId);
if (entry) {
return entry;
}

View File

@ -43,10 +43,10 @@ export class FooterWrapper {
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
`media/${mediaData.fileName}`,
);
this.insertImage(new Image(mediaData));
this.addImage(new Image(mediaData));
}
public insertImage(image: Image): FooterWrapper {
public addImage(image: Image): FooterWrapper {
this.footer.addParagraph(image);
return this;
}

View File

@ -31,7 +31,7 @@ export class Footer extends XmlComponent {
);
}
public get referenceId(): number {
public get ReferenceId(): number {
return this.refId;
}

View File

@ -43,10 +43,10 @@ export class HeaderWrapper {
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
`media/${mediaData.fileName}`,
);
this.insertImage(new Image(mediaData));
this.addImage(new Image(mediaData));
}
public insertImage(image: Image): HeaderWrapper {
public addImage(image: Image): HeaderWrapper {
this.header.addParagraph(image);
return this;
}

View File

@ -31,7 +31,7 @@ export class Header extends XmlComponent {
);
}
public get referenceId(): number {
public get ReferenceId(): number {
return this.refId;
}

View File

@ -120,7 +120,7 @@ export class Media {
return imageData;
}
public get array(): IMediaData[] {
public get Array(): IMediaData[] {
const array = new Array<IMediaData>();
this.map.forEach((data) => {

View File

@ -56,7 +56,7 @@ export class LevelOverride extends XmlComponent {
}
}
public get level(): LevelForOverride {
public get Level(): LevelForOverride {
let lvl: LevelForOverride;
if (!this.lvl) {
lvl = new LevelForOverride(this.levelNum);

View File

@ -387,9 +387,9 @@ describe("concrete numbering", () => {
});
});
it("sets the lvl element if overrideLevel.level is accessed", () => {
it("sets the lvl element if overrideLevel.Level is accessed", () => {
const ol = concreteNumbering.overrideLevel(1);
expect(ol.level).to.be.instanceof(LevelForOverride);
expect(ol.Level).to.be.instanceof(LevelForOverride);
const tree = new Formatter().format(concreteNumbering);
expect(tree["w:num"]).to.include({
"w:lvlOverride": [

View File

@ -22,9 +22,9 @@ describe("TableProperties", () => {
});
});
describe("#fixedWidthLayout", () => {
describe("#setFixedWidthLayout", () => {
it("sets the table to fixed width layout", () => {
const tp = new TableProperties().fixedWidthLayout();
const tp = new TableProperties().setFixedWidthLayout();
const tree = new Formatter().format(tp);
expect(tree).to.deep.equal({
"w:tblPr": [{ "w:tblLayout": [{ _attr: { "w:type": "fixed" } }] }],

View File

@ -12,7 +12,7 @@ export class TableProperties extends XmlComponent {
return this;
}
public fixedWidthLayout(): TableProperties {
public setFixedWidthLayout(): TableProperties {
this.root.push(new TableLayout("fixed"));
return this;
}

View File

@ -186,9 +186,9 @@ describe("Table", () => {
});
});
describe("#fixedWidthLayout", () => {
describe("#setFixedWidthLayout", () => {
it("sets the table to fixed width layout", () => {
const table = new Table(2, 2).fixedWidthLayout();
const table = new Table(2, 2).setFixedWidthLayout();
const tree = new Formatter().format(table);
expect(tree)
.to.have.property("w:tbl")

View File

@ -72,8 +72,8 @@ export class Table extends XmlComponent {
return this;
}
public fixedWidthLayout(): Table {
this.properties.fixedWidthLayout();
public setFixedWidthLayout(): Table {
this.properties.setFixedWidthLayout();
return this;
}
}
@ -94,7 +94,7 @@ export class TableRow extends XmlComponent {
public addGridSpan(ix: number, cellSpan: number): TableCell {
const remainCell = this.cells[ix];
remainCell.cellProperties.addGridSpan(cellSpan);
remainCell.CellProperties.addGridSpan(cellSpan);
this.cells.splice(ix + 1, cellSpan - 1);
this.root.splice(ix + 2, cellSpan - 1);
@ -138,7 +138,7 @@ export class TableCell extends XmlComponent {
return para;
}
public get cellProperties(): TableCellProperties {
public get CellProperties(): TableCellProperties {
return this.properties;
}
}
@ -151,7 +151,7 @@ export class TableCellProperties extends XmlComponent {
this.root.push(this.cellBorder);
}
public get borders(): TableCellBorders {
public get Borders(): TableCellBorders {
return this.cellBorder;
}
@ -167,8 +167,8 @@ export class TableCellProperties extends XmlComponent {
return this;
}
public setVerticalAlign(vAlignType: VerticalAlign): TableCellProperties {
this.root.push(new VAlign(vAlignType));
public setVerticalAlign(type: VerticalAlign): TableCellProperties {
this.root.push(new VAlign(type));
return this;
}

View File

@ -10,7 +10,7 @@ export abstract class BaseXmlComponent {
public abstract prepForXml(): IXmlableObject;
public get isDeleted(): boolean {
public get IsDeleted(): boolean {
return this.deleted;
}
}

View File

@ -14,7 +14,7 @@ export abstract class XmlComponent extends BaseXmlComponent {
const children = this.root
.filter((c) => {
if (c instanceof BaseXmlComponent) {
return !c.isDeleted;
return !c.IsDeleted;
}
return true;
})

View File

@ -37,6 +37,7 @@
],
"no-implicit-dependencies": false,
"no-submodule-imports": false,
"no-null-keyword": true
"no-null-keyword": true,
"return-undefined": true
}
}