Fix styling and linting

This commit is contained in:
Dolan Miu
2018-05-06 03:19:36 +01:00
parent 79b5b3a1f6
commit 573dd753a7
14 changed files with 112 additions and 81 deletions

View File

@ -11,7 +11,44 @@ export class Media {
this.map = new Map<string, IMediaData>();
}
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<IMediaData>();

View File

@ -154,7 +154,6 @@ describe("External styles factory", () => {
],
rootKey: "w:style",
});
});
});
});

View File

@ -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)

View File

@ -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,

View File

@ -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);
}
}

View File

@ -1,2 +1,2 @@
export * from "./table";
export * from './table-cell';
export * from "./table-cell";

View File

@ -169,12 +169,12 @@ describe("TableCellWidth", () => {
expect(tree).to.deep.equal({
"w:tcW": [
{
"_attr": {
_attr: {
"w:type": "dxa",
"w:w": 100
}
}
]
"w:w": 100,
},
},
],
});
});
});

View File

@ -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<ICellBorder> {
}
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;
}
}

View File

@ -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;
}
}

View File

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

View File

@ -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,
};
}
}

View File

@ -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";

View File

@ -26,7 +26,7 @@ describe("XmlComponent", () => {
xmlComponent.addChildElement(child);
const xml = xmlComponent.prepForXml();
assert.equal(xml['w:test'].length, 0);
assert.equal(xml["w:test"].length, 0);
});
});
});

View File

@ -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;
}
}