Remove create table helper function
This commit is contained in:
@ -2,7 +2,6 @@ import { assert, expect } from "chai";
|
||||
|
||||
import { Formatter } from "export/formatter";
|
||||
|
||||
import { Table } from "../table";
|
||||
import { Document } from "./document";
|
||||
|
||||
describe("Document", () => {
|
||||
@ -29,40 +28,4 @@ describe("Document", () => {
|
||||
expect(body[0]).to.have.property("w:sectPr");
|
||||
});
|
||||
});
|
||||
|
||||
describe("#createTable", () => {
|
||||
it("should create a new table and append it to body", () => {
|
||||
const table = document.createTable({
|
||||
rows: 2,
|
||||
columns: 3,
|
||||
});
|
||||
expect(table).to.be.an.instanceof(Table);
|
||||
const body = new Formatter().format(document)["w:document"][1]["w:body"];
|
||||
expect(body)
|
||||
.to.be.an("array")
|
||||
.which.has.length.at.least(1);
|
||||
expect(body[0]).to.have.property("w:tbl");
|
||||
});
|
||||
|
||||
it("should create a table with the correct dimensions", () => {
|
||||
document.createTable({
|
||||
rows: 2,
|
||||
columns: 3,
|
||||
});
|
||||
const body = new Formatter().format(document)["w:document"][1]["w:body"];
|
||||
expect(body)
|
||||
.to.be.an("array")
|
||||
.which.has.length.at.least(1);
|
||||
expect(body[0])
|
||||
.to.have.property("w:tbl")
|
||||
.which.includes({
|
||||
"w:tblGrid": [
|
||||
{ "w:gridCol": { _attr: { "w:w": 100 } } },
|
||||
{ "w:gridCol": { _attr: { "w:w": 100 } } },
|
||||
{ "w:gridCol": { _attr: { "w:w": 100 } } },
|
||||
],
|
||||
});
|
||||
expect(body[0]["w:tbl"].filter((x) => x["w:tr"])).to.have.length(2);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -1,7 +1,7 @@
|
||||
// http://officeopenxml.com/WPdocument.php
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
import { Paragraph } from "../paragraph";
|
||||
import { ITableOptions, Table } from "../table";
|
||||
import { Table } from "../table";
|
||||
import { TableOfContents } from "../table-of-contents";
|
||||
import { Body } from "./body";
|
||||
import { SectionPropertiesOptions } from "./body/section-properties";
|
||||
@ -52,12 +52,6 @@ export class Document extends XmlComponent {
|
||||
return this;
|
||||
}
|
||||
|
||||
public createTable(options: ITableOptions): Table {
|
||||
const table = new Table(options);
|
||||
this.addTable(table);
|
||||
return table;
|
||||
}
|
||||
|
||||
public get Body(): Body {
|
||||
return this.body;
|
||||
}
|
||||
|
@ -104,19 +104,6 @@ describe("File", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("#createTable", () => {
|
||||
it("should call the underlying document's createTable", () => {
|
||||
const wrapper = new File();
|
||||
const spy = sinon.spy(wrapper.Document, "createTable");
|
||||
wrapper.createTable({
|
||||
rows: 1,
|
||||
columns: 1,
|
||||
});
|
||||
|
||||
expect(spy.called).to.equal(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("#addTableOfContents", () => {
|
||||
it("should call the underlying document's addTableOfContents", () => {
|
||||
const wrapper = new File();
|
||||
|
@ -24,7 +24,7 @@ import { Settings } from "./settings";
|
||||
import { Styles } from "./styles";
|
||||
import { ExternalStylesFactory } from "./styles/external-styles-factory";
|
||||
import { DefaultStylesFactory } from "./styles/factory";
|
||||
import { ITableOptions, Table } from "./table";
|
||||
import { Table } from "./table";
|
||||
import { TableOfContents } from "./table-of-contents";
|
||||
|
||||
export class File {
|
||||
@ -127,10 +127,6 @@ export class File {
|
||||
return this;
|
||||
}
|
||||
|
||||
public createTable(options: ITableOptions): Table {
|
||||
return this.document.createTable(options);
|
||||
}
|
||||
|
||||
public addImage(image: Image): File {
|
||||
this.document.addParagraph(image.Paragraph);
|
||||
return this;
|
||||
|
@ -32,16 +32,6 @@ describe("FooterWrapper", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("#createTable", () => {
|
||||
it("should call the underlying footer's createTable", () => {
|
||||
const wrapper = new FooterWrapper(new Media(), 1);
|
||||
const spy = sinon.spy(wrapper.Footer, "createTable");
|
||||
wrapper.createTable(1, 1);
|
||||
|
||||
expect(spy.called).to.equal(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("#addImage", () => {
|
||||
it("should call the underlying footer's addImage", () => {
|
||||
const file = new FooterWrapper(new Media(), 1);
|
||||
@ -56,12 +46,11 @@ describe("FooterWrapper", () => {
|
||||
describe("#createImage", () => {
|
||||
it("should call the underlying footer's createImage", () => {
|
||||
const file = new FooterWrapper(new Media(), 1);
|
||||
const spy = sinon.spy(file.Media, "addMedia");
|
||||
const fileSpy = sinon.spy(file, "addImage");
|
||||
const spy = sinon.spy(Media, "addImage");
|
||||
file.createImage("");
|
||||
|
||||
expect(spy.called).to.equal(true);
|
||||
expect(fileSpy.called).to.equal(true);
|
||||
spy.restore();
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -1,9 +1,10 @@
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
|
||||
import { FooterReferenceType } from "./document";
|
||||
import { IDrawingOptions } from "./drawing";
|
||||
import { Footer } from "./footer/footer";
|
||||
import { Image, Media } from "./media";
|
||||
import { ImageParagraph, Paragraph } from "./paragraph";
|
||||
import { Paragraph } from "./paragraph";
|
||||
import { Relationships } from "./relationships";
|
||||
import { Table } from "./table";
|
||||
|
||||
@ -29,22 +30,26 @@ export class FooterWrapper {
|
||||
this.footer.addTable(table);
|
||||
}
|
||||
|
||||
public createTable(rows: number, cols: number): Table {
|
||||
return this.footer.createTable(rows, cols);
|
||||
public addImage(image: Image): FooterWrapper {
|
||||
this.footer.addParagraph(image.Paragraph);
|
||||
return this;
|
||||
}
|
||||
|
||||
public addChildElement(childElement: XmlComponent): void {
|
||||
this.footer.addChildElement(childElement);
|
||||
}
|
||||
|
||||
public createImage(image: Buffer | string | Uint8Array | ArrayBuffer, width?: number, height?: number): void {
|
||||
const mediaData = this.media.addMedia(image, width, height);
|
||||
this.addImage(new Image(new ImageParagraph(mediaData)));
|
||||
}
|
||||
public createImage(
|
||||
buffer: Buffer | string | Uint8Array | ArrayBuffer,
|
||||
width?: number,
|
||||
height?: number,
|
||||
drawingOptions?: IDrawingOptions,
|
||||
): Paragraph {
|
||||
const image = Media.addImage(this, buffer, width, height, drawingOptions);
|
||||
const paragraph = new Paragraph(image);
|
||||
this.addParagraph(paragraph);
|
||||
|
||||
public addImage(image: Image): FooterWrapper {
|
||||
this.footer.addParagraph(image.Paragraph);
|
||||
return this;
|
||||
return paragraph;
|
||||
}
|
||||
|
||||
public get Footer(): Footer {
|
||||
|
@ -45,13 +45,4 @@ export class Footer extends InitializableXmlComponent {
|
||||
public addTable(table: Table): void {
|
||||
this.root.push(table);
|
||||
}
|
||||
|
||||
public createTable(rows: number, cols: number): Table {
|
||||
const table = new Table({
|
||||
rows: rows,
|
||||
columns: cols,
|
||||
});
|
||||
this.addTable(table);
|
||||
return table;
|
||||
}
|
||||
}
|
||||
|
@ -32,16 +32,6 @@ describe("HeaderWrapper", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("#createTable", () => {
|
||||
it("should call the underlying header's createTable", () => {
|
||||
const wrapper = new HeaderWrapper(new Media(), 1);
|
||||
const spy = sinon.spy(wrapper.Header, "createTable");
|
||||
wrapper.createTable(1, 1);
|
||||
|
||||
expect(spy.called).to.equal(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("#addImage", () => {
|
||||
it("should call the underlying header's addImage", () => {
|
||||
const file = new HeaderWrapper(new Media(), 1);
|
||||
@ -56,12 +46,11 @@ describe("HeaderWrapper", () => {
|
||||
describe("#createImage", () => {
|
||||
it("should call the underlying header's createImage", () => {
|
||||
const file = new HeaderWrapper(new Media(), 1);
|
||||
const spy = sinon.spy(file.Media, "addMedia");
|
||||
const fileSpy = sinon.spy(file, "addImage");
|
||||
const spy = sinon.spy(Media, "addImage");
|
||||
file.createImage("");
|
||||
|
||||
expect(spy.called).to.equal(true);
|
||||
expect(fileSpy.called).to.equal(true);
|
||||
spy.restore();
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -1,9 +1,10 @@
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
|
||||
import { HeaderReferenceType } from "./document";
|
||||
import { IDrawingOptions } from "./drawing";
|
||||
import { Header } from "./header/header";
|
||||
import { Image, Media } from "./media";
|
||||
import { ImageParagraph, Paragraph } from "./paragraph";
|
||||
import { Paragraph } from "./paragraph";
|
||||
import { Relationships } from "./relationships";
|
||||
import { Table } from "./table";
|
||||
|
||||
@ -29,22 +30,26 @@ export class HeaderWrapper {
|
||||
this.header.addTable(table);
|
||||
}
|
||||
|
||||
public createTable(rows: number, cols: number): Table {
|
||||
return this.header.createTable(rows, cols);
|
||||
public addImage(image: Image): HeaderWrapper {
|
||||
this.header.addParagraph(image.Paragraph);
|
||||
return this;
|
||||
}
|
||||
|
||||
public addChildElement(childElement: XmlComponent | string): void {
|
||||
this.header.addChildElement(childElement);
|
||||
}
|
||||
|
||||
public createImage(image: Buffer | string | Uint8Array | ArrayBuffer, width?: number, height?: number): void {
|
||||
const mediaData = this.media.addMedia(image, width, height);
|
||||
this.addImage(new Image(new ImageParagraph(mediaData)));
|
||||
}
|
||||
public createImage(
|
||||
buffer: Buffer | string | Uint8Array | ArrayBuffer,
|
||||
width?: number,
|
||||
height?: number,
|
||||
drawingOptions?: IDrawingOptions,
|
||||
): Paragraph {
|
||||
const image = Media.addImage(this, buffer, width, height, drawingOptions);
|
||||
const paragraph = new Paragraph(image);
|
||||
this.addParagraph(paragraph);
|
||||
|
||||
public addImage(image: Image): HeaderWrapper {
|
||||
this.header.addParagraph(image.Paragraph);
|
||||
return this;
|
||||
return paragraph;
|
||||
}
|
||||
|
||||
public get Header(): Header {
|
||||
|
@ -56,13 +56,4 @@ export class Header extends InitializableXmlComponent {
|
||||
public addTable(table: Table): void {
|
||||
this.root.push(table);
|
||||
}
|
||||
|
||||
public createTable(rows: number, cols: number): Table {
|
||||
const table = new Table({
|
||||
rows: rows,
|
||||
columns: cols,
|
||||
});
|
||||
this.addTable(table);
|
||||
return table;
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +1,14 @@
|
||||
import { IDrawingOptions } from "../drawing";
|
||||
import { File } from "../file";
|
||||
import { FooterWrapper } from "../footer-wrapper";
|
||||
import { HeaderWrapper } from "../header-wrapper";
|
||||
import { PictureRun } from "../paragraph";
|
||||
import { IMediaData } from "./data";
|
||||
// import { Image } from "./image";
|
||||
|
||||
export class Media {
|
||||
public static addImage(
|
||||
file: File,
|
||||
file: File | HeaderWrapper | FooterWrapper,
|
||||
buffer: Buffer | string | Uint8Array | ArrayBuffer,
|
||||
width?: number,
|
||||
height?: number,
|
||||
|
@ -1,243 +0,0 @@
|
||||
// http://officeopenxml.com/WPparagraph.php
|
||||
import { FootnoteReferenceRun } from "file/footnotes/footnote/run/reference-run";
|
||||
import { Image } from "file/media";
|
||||
import { Num } from "file/numbering/num";
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
|
||||
import { Alignment } from "./formatting/alignment";
|
||||
import { Bidirectional } from "./formatting/bidirectional";
|
||||
<<<<<<< HEAD
|
||||
import { Border, ThematicBreak } from "./formatting/border";
|
||||
import { Indent } from "./formatting/indent";
|
||||
=======
|
||||
import { ThematicBreak } from "./formatting/border";
|
||||
import { IIndentAttributesProperties, Indent } from "./formatting/indent";
|
||||
>>>>>>> a53818754a1c76b9930ee2ecc642570170fa3c06
|
||||
import { KeepLines, KeepNext } from "./formatting/keep";
|
||||
import { PageBreak, PageBreakBefore } from "./formatting/page-break";
|
||||
import { ISpacingProperties, Spacing } from "./formatting/spacing";
|
||||
import { Style } from "./formatting/style";
|
||||
import { CenterTabStop, LeftTabStop, MaxRightTabStop, RightTabStop } from "./formatting/tab-stop";
|
||||
import { NumberProperties } from "./formatting/unordered-list";
|
||||
import { Bookmark, Hyperlink } from "./links";
|
||||
import { ParagraphProperties } from "./properties";
|
||||
import { PictureRun, Run, TextRun } from "./run";
|
||||
|
||||
export class Paragraph extends XmlComponent {
|
||||
private readonly properties: ParagraphProperties;
|
||||
|
||||
constructor(text?: string) {
|
||||
super("w:p");
|
||||
this.properties = new ParagraphProperties();
|
||||
this.root.push(this.properties);
|
||||
if (text !== undefined) {
|
||||
this.root.push(new TextRun(text));
|
||||
}
|
||||
}
|
||||
|
||||
public get Borders(): Border {
|
||||
return this.properties.paragraphBorder;
|
||||
}
|
||||
|
||||
public createBorder(): Paragraph {
|
||||
this.properties.createBorder();
|
||||
return this;
|
||||
}
|
||||
|
||||
public addRun(run: Run): Paragraph {
|
||||
this.root.push(run);
|
||||
return this;
|
||||
}
|
||||
|
||||
public addHyperLink(hyperlink: Hyperlink): Paragraph {
|
||||
this.root.push(hyperlink);
|
||||
return this;
|
||||
}
|
||||
|
||||
public addBookmark(bookmark: Bookmark): Paragraph {
|
||||
// Bookmarks by spec have three components, a start, text, and end
|
||||
this.root.push(bookmark.start);
|
||||
this.root.push(bookmark.text);
|
||||
this.root.push(bookmark.end);
|
||||
return this;
|
||||
}
|
||||
|
||||
public createTextRun(text: string): TextRun {
|
||||
const run = new TextRun(text);
|
||||
this.addRun(run);
|
||||
return run;
|
||||
}
|
||||
|
||||
public addImage(image: Image): PictureRun {
|
||||
const run = image.Run;
|
||||
this.addRun(run);
|
||||
|
||||
return run;
|
||||
}
|
||||
|
||||
public heading1(): Paragraph {
|
||||
this.properties.push(new Style("Heading1"));
|
||||
return this;
|
||||
}
|
||||
|
||||
public heading2(): Paragraph {
|
||||
this.properties.push(new Style("Heading2"));
|
||||
return this;
|
||||
}
|
||||
|
||||
public heading3(): Paragraph {
|
||||
this.properties.push(new Style("Heading3"));
|
||||
return this;
|
||||
}
|
||||
|
||||
public heading4(): Paragraph {
|
||||
this.properties.push(new Style("Heading4"));
|
||||
return this;
|
||||
}
|
||||
|
||||
public heading5(): Paragraph {
|
||||
this.properties.push(new Style("Heading5"));
|
||||
return this;
|
||||
}
|
||||
|
||||
public heading6(): Paragraph {
|
||||
this.properties.push(new Style("Heading6"));
|
||||
return this;
|
||||
}
|
||||
|
||||
public title(): Paragraph {
|
||||
this.properties.push(new Style("Title"));
|
||||
return this;
|
||||
}
|
||||
|
||||
public center(): Paragraph {
|
||||
this.properties.push(new Alignment("center"));
|
||||
return this;
|
||||
}
|
||||
|
||||
public left(): Paragraph {
|
||||
this.properties.push(new Alignment("left"));
|
||||
return this;
|
||||
}
|
||||
|
||||
public right(): Paragraph {
|
||||
this.properties.push(new Alignment("right"));
|
||||
return this;
|
||||
}
|
||||
|
||||
public start(): Paragraph {
|
||||
this.properties.push(new Alignment("start"));
|
||||
return this;
|
||||
}
|
||||
|
||||
public end(): Paragraph {
|
||||
this.properties.push(new Alignment("end"));
|
||||
return this;
|
||||
}
|
||||
|
||||
public distribute(): Paragraph {
|
||||
this.properties.push(new Alignment("distribute"));
|
||||
return this;
|
||||
}
|
||||
|
||||
public justified(): Paragraph {
|
||||
this.properties.push(new Alignment("both"));
|
||||
return this;
|
||||
}
|
||||
|
||||
public thematicBreak(): Paragraph {
|
||||
this.properties.push(new ThematicBreak());
|
||||
return this;
|
||||
}
|
||||
|
||||
public pageBreak(): Paragraph {
|
||||
this.root.push(new PageBreak());
|
||||
return this;
|
||||
}
|
||||
|
||||
public pageBreakBefore(): Paragraph {
|
||||
this.properties.push(new PageBreakBefore());
|
||||
return this;
|
||||
}
|
||||
|
||||
public maxRightTabStop(): Paragraph {
|
||||
this.properties.push(new MaxRightTabStop());
|
||||
return this;
|
||||
}
|
||||
|
||||
public leftTabStop(position: number): Paragraph {
|
||||
this.properties.push(new LeftTabStop(position));
|
||||
return this;
|
||||
}
|
||||
|
||||
public rightTabStop(position: number): Paragraph {
|
||||
this.properties.push(new RightTabStop(position));
|
||||
return this;
|
||||
}
|
||||
|
||||
public centerTabStop(position: number): Paragraph {
|
||||
this.properties.push(new CenterTabStop(position));
|
||||
return this;
|
||||
}
|
||||
|
||||
public bullet(indentLevel: number = 0): Paragraph {
|
||||
this.properties.push(new Style("ListParagraph"));
|
||||
this.properties.push(new NumberProperties(1, indentLevel));
|
||||
return this;
|
||||
}
|
||||
|
||||
public setNumbering(numbering: Num, indentLevel: number): Paragraph {
|
||||
this.properties.push(new Style("ListParagraph"));
|
||||
this.properties.push(new NumberProperties(numbering.id, indentLevel));
|
||||
return this;
|
||||
}
|
||||
|
||||
public setCustomNumbering(numberId: number, indentLevel: number): Paragraph {
|
||||
this.properties.push(new NumberProperties(numberId, indentLevel));
|
||||
return this;
|
||||
}
|
||||
|
||||
public style(styleId: string): Paragraph {
|
||||
this.properties.push(new Style(styleId));
|
||||
return this;
|
||||
}
|
||||
|
||||
public indent(attrs: IIndentAttributesProperties): Paragraph {
|
||||
this.properties.push(new Indent(attrs));
|
||||
return this;
|
||||
}
|
||||
|
||||
public spacing(params: ISpacingProperties): Paragraph {
|
||||
this.properties.push(new Spacing(params));
|
||||
return this;
|
||||
}
|
||||
|
||||
public keepNext(): Paragraph {
|
||||
this.properties.push(new KeepNext());
|
||||
return this;
|
||||
}
|
||||
|
||||
public keepLines(): Paragraph {
|
||||
this.properties.push(new KeepLines());
|
||||
return this;
|
||||
}
|
||||
|
||||
public referenceFootnote(id: number): Paragraph {
|
||||
this.root.push(new FootnoteReferenceRun(id));
|
||||
return this;
|
||||
}
|
||||
|
||||
public addRunToFront(run: Run): Paragraph {
|
||||
this.root.splice(1, 0, run);
|
||||
return this;
|
||||
}
|
||||
|
||||
public bidirectional(): Paragraph {
|
||||
this.properties.push(new Bidirectional());
|
||||
return this;
|
||||
}
|
||||
|
||||
public get Properties(): ParagraphProperties {
|
||||
return this.properties;
|
||||
}
|
||||
}
|
@ -8,6 +8,10 @@ import { ITableCellMarginOptions } from "./cell-margin/table-cell-margins";
|
||||
import { TableCellBorders, VerticalAlign, VMergeType } from "./table-cell-components";
|
||||
import { TableCellProperties } from "./table-cell-properties";
|
||||
|
||||
export interface ITableCellOptions {
|
||||
readonly shading?: ITableShadingAttributesProperties;
|
||||
}
|
||||
|
||||
export class TableCell extends XmlComponent {
|
||||
private readonly properties: TableCellProperties;
|
||||
|
||||
|
Reference in New Issue
Block a user