diff --git a/src/file/media/media.ts b/src/file/media/media.ts index 7f5c960a55..ee4ca9ae15 100644 --- a/src/file/media/media.ts +++ b/src/file/media/media.ts @@ -11,7 +11,44 @@ export class Media { this.map = new Map(); } - private createMedia(key: string, relationshipsCount, dimensions, data: fs.ReadStream | Buffer, filePath?: string, ) { + public getMedia(key: string): IMediaData { + const data = this.map.get(key); + + if (data === undefined) { + throw new Error(`Cannot find image with the key ${key}`); + } + + return data; + } + + public addMedia(filePath: string, relationshipsCount: number): IMediaData { + const key = path.basename(filePath); + const dimensions = sizeOf(filePath); + return this.createMedia(key, relationshipsCount, dimensions, fs.createReadStream(filePath), filePath); + } + + public addMediaWithData(fileName: string, data: Buffer, relationshipsCount: number, width?: number, height?: number): IMediaData { + const key = fileName; + let dimensions; + if (width && height) { + dimensions = { + width: width, + height: height, + }; + } else { + dimensions = sizeOf(data); + } + + return this.createMedia(key, relationshipsCount, dimensions, data); + } + + private createMedia( + key: string, + relationshipsCount: number, + dimensions: { width: number; height: number }, + data: fs.ReadStream | Buffer, + filePath?: string, + ): IMediaData { const imageData = { referenceId: this.map.size + relationshipsCount + 1, stream: data, @@ -32,36 +69,6 @@ export class Media { return imageData; } - public getMedia(key: string): IMediaData { - const data = this.map.get(key); - - if (data === undefined) { - throw new Error(`Cannot find image with the key ${key}`); - } - - return data; - } - - public addMedia(filePath: string, relationshipsCount: number): IMediaData { - const key = path.basename(filePath); - const dimensions = sizeOf(filePath); - return this.createMedia(key, relationshipsCount, dimensions, fs.createReadStream(filePath), filePath); - } - - public addMediaWithData(fileName: string, data: Buffer, relationshipsCount: number, width?, height?): IMediaData { - const key = fileName; - let dimensions; - if (width && height) { - dimensions = { - width: width, - height: height - } - } else { - dimensions = sizeOf(data); - } - - return this.createMedia(key, relationshipsCount, dimensions, data); - } public get array(): IMediaData[] { const array = new Array(); diff --git a/src/file/styles/external-styles-factory.spec.ts b/src/file/styles/external-styles-factory.spec.ts index 0957c25d9f..861a6f05d8 100644 --- a/src/file/styles/external-styles-factory.spec.ts +++ b/src/file/styles/external-styles-factory.spec.ts @@ -154,7 +154,6 @@ describe("External styles factory", () => { ], rootKey: "w:style", }); - }); }); }); diff --git a/src/file/styles/external-styles-factory.ts b/src/file/styles/external-styles-factory.ts index 703f017922..904461dcda 100644 --- a/src/file/styles/external-styles-factory.ts +++ b/src/file/styles/external-styles-factory.ts @@ -1,6 +1,7 @@ -import { Styles } from "./"; import * as fastXmlParser from "fast-xml-parser"; -import { ImportedXmlComponent, ImportedRootElementAttributes } from "./../../file/xml-components"; + +import { Styles } from "./"; +import { ImportedRootElementAttributes, ImportedXmlComponent } from "./../../file/xml-components"; const parseOptions = { ignoreAttributes: false, @@ -51,7 +52,8 @@ export class ExternalStylesFactory { return importedStyle; } - convertElement(elementName: string, element: any): ImportedXmlComponent { + // tslint:disable-next-line:no-any + public convertElement(elementName: string, element: any): ImportedXmlComponent { const xmlElement = new ImportedXmlComponent(elementName, element._attr); if (typeof element === "object") { Object.keys(element) diff --git a/src/file/styles/factory.ts b/src/file/styles/factory.ts index d7016cb0a5..93533e792f 100644 --- a/src/file/styles/factory.ts +++ b/src/file/styles/factory.ts @@ -1,7 +1,6 @@ -import { Color, Italics, Size } from "../paragraph/run/formatting"; - -import { Styles } from "./"; import { DocumentAttributes } from "../document/document-attributes"; +import { Color, Italics, Size } from "../paragraph/run/formatting"; +import { Styles } from "./"; import { Heading1Style, diff --git a/src/file/styles/index.ts b/src/file/styles/index.ts index 5b0bd1b063..30f81c0012 100644 --- a/src/file/styles/index.ts +++ b/src/file/styles/index.ts @@ -1,12 +1,12 @@ -import { XmlComponent, BaseXmlComponent } from "file/xml-components"; +import { BaseXmlComponent, XmlComponent } from "file/xml-components"; import { DocumentDefaults } from "./defaults"; import { ParagraphStyle } from "./style"; export class Styles extends XmlComponent { - constructor(_initialStyles?: BaseXmlComponent) { + constructor(initialStyles?: BaseXmlComponent) { super("w:styles"); - if (_initialStyles) { - this.root.push(_initialStyles); + if (initialStyles) { + this.root.push(initialStyles); } } diff --git a/src/file/table/index.ts b/src/file/table/index.ts index ef3d91a47b..dfba175857 100644 --- a/src/file/table/index.ts +++ b/src/file/table/index.ts @@ -1,2 +1,2 @@ export * from "./table"; -export * from './table-cell'; \ No newline at end of file +export * from "./table-cell"; diff --git a/src/file/table/table-cell.spec.ts b/src/file/table/table-cell.spec.ts index 596150d6d5..01c81848bb 100644 --- a/src/file/table/table-cell.spec.ts +++ b/src/file/table/table-cell.spec.ts @@ -169,13 +169,13 @@ describe("TableCellWidth", () => { expect(tree).to.deep.equal({ "w:tcW": [ { - "_attr": { + _attr: { "w:type": "dxa", - "w:w": 100 - } - } - ] + "w:w": 100, + }, + }, + ], }); }); }); -}); \ No newline at end of file +}); diff --git a/src/file/table/table-cell.ts b/src/file/table/table-cell.ts index 99282f1125..5e64cc04b6 100644 --- a/src/file/table/table-cell.ts +++ b/src/file/table/table-cell.ts @@ -1,4 +1,4 @@ -import { XmlComponent, XmlAttributeComponent, IXmlableObject } from "file/xml-components"; +import { IXmlableObject, XmlAttributeComponent, XmlComponent } from "file/xml-components"; export enum BorderStyle { SINGLE = "single", @@ -41,13 +41,15 @@ class CellBorderAttributes extends XmlAttributeComponent { } class BaseTableCellBorder extends XmlComponent { - setProperties(style: BorderStyle, size: number, color: string) { - let attrs = new CellBorderAttributes({ + public setProperties(style: BorderStyle, size: number, color: string): BaseTableCellBorder { + const attrs = new CellBorderAttributes({ style: style, size: size, color: color, }); this.root.push(attrs); + + return this; } } @@ -60,28 +62,36 @@ export class TableCellBorders extends XmlComponent { return this.root.length > 0 ? super.prepForXml() : ""; } - addTopBorder(style: BorderStyle, size: number, color: string) { + public addTopBorder(style: BorderStyle, size: number, color: string): TableCellBorders { const top = new BaseTableCellBorder("w:top"); top.setProperties(style, size, color); this.root.push(top); + + return this; } - addStartBorder(style: BorderStyle, size: number, color: string) { + public addStartBorder(style: BorderStyle, size: number, color: string): TableCellBorders { const start = new BaseTableCellBorder("w:start"); start.setProperties(style, size, color); this.root.push(start); + + return this; } - addBottomBorder(style: BorderStyle, size: number, color: string) { + public addBottomBorder(style: BorderStyle, size: number, color: string): TableCellBorders { const bottom = new BaseTableCellBorder("w:bottom"); bottom.setProperties(style, size, color); this.root.push(bottom); + + return this; } - addEndBorder(style: BorderStyle, size: number, color: string) { + public addEndBorder(style: BorderStyle, size: number, color: string): TableCellBorders { const end = new BaseTableCellBorder("w:end"); end.setProperties(style, size, color); this.root.push(end); + + return this; } } diff --git a/src/file/table/table.ts b/src/file/table/table.ts index 3e180e9dfb..2d19086baa 100644 --- a/src/file/table/table.ts +++ b/src/file/table/table.ts @@ -1,8 +1,8 @@ +import { GridSpan, TableCellBorders, TableCellWidth, VAlign, VerticalAlign, VMerge, VMergeType, WidthType } from "file/table/table-cell"; import { IXmlableObject, XmlComponent } from "file/xml-components"; import { Paragraph } from "../paragraph"; import { TableGrid } from "./grid"; import { TableProperties, WidthTypes } from "./properties"; -import { TableCellBorders, GridSpan, VMerge, VMergeType, VerticalAlign, VAlign, TableCellWidth, WidthType } from "file/table/table-cell"; export class Table extends XmlComponent { private readonly properties: TableProperties; @@ -119,7 +119,7 @@ export class TableCell extends XmlComponent { return para; } - get cellProperties() { + get cellProperties(): TableCellProperties { return this.properties; } } @@ -132,22 +132,31 @@ export class TableCellProperties extends XmlComponent { this.root.push(this.cellBorder); } - get borders() { + get borders(): TableCellBorders { return this.cellBorder; } - addGridSpan(cellSpan: number) { + + public addGridSpan(cellSpan: number): TableCellProperties { this.root.push(new GridSpan(cellSpan)); + + return this; } - addVerticalMerge(type: VMergeType) { + public addVerticalMerge(type: VMergeType): TableCellProperties { this.root.push(new VMerge(type)); + + return this; } - setVerticalAlign(vAlignType: VerticalAlign) { + public setVerticalAlign(vAlignType: VerticalAlign): TableCellProperties { this.root.push(new VAlign(vAlignType)); + + return this; } - setWidth(width: string | number, type: WidthType) { + public setWidth(width: string | number, type: WidthType): TableCellProperties { this.root.push(new TableCellWidth(width, type)); + + return this; } } diff --git a/src/file/xml-components/base.ts b/src/file/xml-components/base.ts index f6382f5e7e..0dd5333765 100644 --- a/src/file/xml-components/base.ts +++ b/src/file/xml-components/base.ts @@ -10,7 +10,7 @@ export abstract class BaseXmlComponent { public abstract prepForXml(): IXmlableObject; - get isDeleted() { + public get isDeleted(): boolean { return this.deleted; } } diff --git a/src/file/xml-components/imported-xml-component.ts b/src/file/xml-components/imported-xml-component.ts index 51af733c2e..0a6c0e8c9d 100644 --- a/src/file/xml-components/imported-xml-component.ts +++ b/src/file/xml-components/imported-xml-component.ts @@ -1,15 +1,17 @@ -import { XmlComponent, IXmlableObject } from "."; +// tslint:disable:no-any +import { IXmlableObject, XmlComponent } from "./"; /** * Represents imported xml component from xml file. */ export class ImportedXmlComponent extends XmlComponent { - private _attr: any; + private attr: any; - constructor(rootKey: string, _attr?: any) { + constructor(rootKey: string, attr?: any) { super(rootKey); - if (_attr) { - this._attr = _attr; + + if (attr) { + this.attr = attr; } } @@ -39,18 +41,18 @@ export class ImportedXmlComponent extends XmlComponent { * ] * } */ - prepForXml(): IXmlableObject { + public prepForXml(): IXmlableObject { const result = super.prepForXml(); - if (!!this._attr) { + if (!!this.attr) { if (!Array.isArray(result[this.rootKey])) { result[this.rootKey] = [result[this.rootKey]]; } - result[this.rootKey].unshift({ _attr: this._attr }); + result[this.rootKey].unshift({ _attr: this.attr }); } return result; } - push(xmlComponent: XmlComponent) { + public push(xmlComponent: XmlComponent): void { this.root.push(xmlComponent); } } @@ -59,13 +61,13 @@ export class ImportedXmlComponent extends XmlComponent { * Used for the attributes of root element that is being imported. */ export class ImportedRootElementAttributes extends XmlComponent { - constructor(private _attr: any) { + constructor(private attr: any) { super(""); } public prepForXml(): IXmlableObject { return { - _attr: this._attr, + _attr: this.attr, }; } } diff --git a/src/file/xml-components/index.ts b/src/file/xml-components/index.ts index 85e7e383f7..917933869e 100644 --- a/src/file/xml-components/index.ts +++ b/src/file/xml-components/index.ts @@ -1,5 +1,5 @@ export * from "./xml-component"; export * from "./attributes"; export * from "./default-attributes"; -export * from './imported-xml-component'; +export * from "./imported-xml-component"; export * from "./xmlable-object"; diff --git a/src/file/xml-components/xml-component.spec.ts b/src/file/xml-components/xml-component.spec.ts index 25bf442bc9..8b4f983388 100644 --- a/src/file/xml-components/xml-component.spec.ts +++ b/src/file/xml-components/xml-component.spec.ts @@ -24,9 +24,9 @@ describe("XmlComponent", () => { const child = new TestComponent("w:test1"); child.delete(); xmlComponent.addChildElement(child); - + const xml = xmlComponent.prepForXml(); - assert.equal(xml['w:test'].length, 0); + assert.equal(xml["w:test"].length, 0); }); }); }); diff --git a/src/file/xml-components/xml-component.ts b/src/file/xml-components/xml-component.ts index b41bb01267..fa7709a043 100644 --- a/src/file/xml-components/xml-component.ts +++ b/src/file/xml-components/xml-component.ts @@ -12,7 +12,7 @@ export abstract class XmlComponent extends BaseXmlComponent { public prepForXml(): IXmlableObject { const children = this.root - .filter(c => { + .filter((c) => { if (c instanceof BaseXmlComponent) { return !c.isDeleted; } @@ -30,11 +30,14 @@ export abstract class XmlComponent extends BaseXmlComponent { }; } - public addChildElement(child: XmlComponent | string) { + // TODO: Unused method + public addChildElement(child: XmlComponent | string): XmlComponent { this.root.push(child); + + return this; } - public delete() { + public delete(): void { this.deleted = true; } }