Merge branch 'master' of github.com:dolanmiu/docx into Numbering

# Conflicts:
#	src/file/paragraph/run/run.ts
This commit is contained in:
Forman
2019-08-13 10:40:49 +03:00
156 changed files with 3416 additions and 2960 deletions

View File

@ -11,35 +11,7 @@ describe("Body", () => {
body = new Body();
});
describe("#constructor()", () => {
it("should create default section", () => {
const formatted = new Formatter().format(body)["w:body"][0];
expect(formatted)
.to.have.property("w:sectPr")
.and.to.be.an.instanceof(Array);
expect(formatted["w:sectPr"]).to.have.length(4);
});
});
describe("#addSection", () => {
it("should add section with options", () => {
body.addSection({
width: 10000,
height: 10000,
});
const formatted = new Formatter().format(body)["w:body"];
expect(formatted).to.be.an.instanceof(Array);
const defaultSectionPr = formatted[0]["w:p"][0]["w:pPr"][0]["w:sectPr"];
// check that this is the default section and added first in paragraph
expect(defaultSectionPr[0]).to.deep.equal({ "w:pgSz": { _attr: { "w:h": 16838, "w:w": 11906, "w:orient": "portrait" } } });
// check for new section (since it's the last one, it's direct child of body)
const newSection = formatted[1]["w:sectPr"];
expect(newSection[0]).to.deep.equal({ "w:pgSz": { _attr: { "w:h": 10000, "w:w": 10000, "w:orient": "portrait" } } });
});
it("should add section with default parameters", () => {
body.addSection({
width: 10000,
@ -51,33 +23,7 @@ describe("Body", () => {
expect(tree).to.deep.equal({
"w:body": [
{
"w:p": [
{
"w:pPr": [
{
"w:sectPr": [
{ "w:pgSz": { _attr: { "w:w": 11906, "w:h": 16838, "w:orient": "portrait" } } },
{
"w:pgMar": {
_attr: {
"w:top": 1440,
"w:right": 1440,
"w:bottom": 1440,
"w:left": 1440,
"w:header": 708,
"w:footer": 708,
"w:gutter": 0,
"w:mirrorMargins": false,
},
},
},
{ "w:cols": { _attr: { "w:space": 708, "w:num": 1 } } },
{ "w:docGrid": { _attr: { "w:linePitch": 360 } } },
],
},
],
},
],
"w:p": {},
},
{
"w:sectPr": [
@ -104,21 +50,4 @@ describe("Body", () => {
});
});
});
describe("#getParagraphs", () => {
it("should get no paragraphs", () => {
const paragraphs = body.getParagraphs();
expect(paragraphs).to.be.an.instanceof(Array);
});
});
describe("#DefaultSection", () => {
it("should get section", () => {
const section = body.DefaultSection;
const tree = new Formatter().format(section);
expect(tree["w:sectPr"]).to.be.an.instanceof(Array);
});
});
});

View File

@ -3,15 +3,10 @@ import { Paragraph, ParagraphProperties, TableOfContents } from "../..";
import { SectionProperties, SectionPropertiesOptions } from "./section-properties/section-properties";
export class Body extends XmlComponent {
private readonly defaultSection: SectionProperties;
private readonly sections: SectionProperties[] = [];
constructor(sectionPropertiesOptions?: SectionPropertiesOptions) {
constructor() {
super("w:body");
this.defaultSection = new SectionProperties(sectionPropertiesOptions);
this.sections.push(this.defaultSection);
}
/**
@ -20,21 +15,15 @@ export class Body extends XmlComponent {
* The spec says:
* - section element should be in the last paragraph of the section
* - last section should be direct child of body
* @param section new section
* @param options new section options
*/
public addSection(section: SectionPropertiesOptions | SectionProperties): void {
public addSection(options: SectionPropertiesOptions): void {
const currentSection = this.sections.pop() as SectionProperties;
this.root.push(this.createSectionParagraph(currentSection));
if (section instanceof SectionProperties) {
this.sections.push(section);
} else {
const params = {
...this.defaultSection.Options,
...section,
};
this.sections.push(new SectionProperties(params));
}
this.sections.push(new SectionProperties(options));
}
public prepForXml(): IXmlableObject | undefined {
if (this.sections.length === 1) {
this.root.push(this.sections.pop() as SectionProperties);
@ -47,21 +36,13 @@ export class Body extends XmlComponent {
this.root.push(component);
}
public get DefaultSection(): SectionProperties {
return this.defaultSection;
}
public getTablesOfContents(): TableOfContents[] {
return this.root.filter((child) => child instanceof TableOfContents) as TableOfContents[];
}
public getParagraphs(): Paragraph[] {
return this.root.filter((child) => child instanceof Paragraph) as Paragraph[];
}
private createSectionParagraph(section: SectionProperties): Paragraph {
const paragraph = new Paragraph();
const properties = new ParagraphProperties();
const paragraph = new Paragraph({});
const properties = new ParagraphProperties({});
properties.addChildElement(section);
paragraph.addChildElement(properties);
return paragraph;

View File

@ -1,13 +1,13 @@
import { XmlComponent } from "file/xml-components";
import { HeaderReferenceAttributes, HeaderReferenceType } from "./header-reference-attributes";
export interface IHeaderOptions {
export interface IHeaderReferenceOptions {
readonly headerType?: HeaderReferenceType;
readonly headerId?: number;
}
export class HeaderReference extends XmlComponent {
constructor(options: IHeaderOptions) {
constructor(options: IHeaderReferenceOptions) {
super("w:headerReference");
this.root.push(
new HeaderReferenceAttributes({

View File

@ -1,9 +1,7 @@
import { assert, expect } from "chai";
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { Paragraph } from "../paragraph";
import { Table } from "../table";
import { Document } from "./document";
describe("Document", () => {
@ -15,81 +13,34 @@ describe("Document", () => {
describe("#constructor()", () => {
it("should create valid JSON", () => {
const stringifiedJson = JSON.stringify(document);
const tree = new Formatter().format(document);
try {
JSON.parse(stringifiedJson);
} catch (e) {
assert.isTrue(false);
}
assert.isTrue(true);
});
it("should create default section", () => {
const body = new Formatter().format(document)["w:document"][1]["w:body"];
expect(body[0]).to.have.property("w:sectPr");
});
});
describe("#createParagraph", () => {
it("should create a new paragraph and append it to body", () => {
const para = document.createParagraph();
expect(para).to.be.an.instanceof(Paragraph);
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:p");
});
it("should use the text given to create a run in the paragraph", () => {
const para = document.createParagraph("sample paragraph text");
expect(para).to.be.an.instanceof(Paragraph);
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:p")
.which.includes({
"w:r": [{ "w:t": [{ _attr: { "xml:space": "preserve" } }, "sample paragraph text"] }],
});
});
});
describe("#createTable", () => {
it("should create a new table and append it to body", () => {
const table = document.createTable({
rows: 2,
columns: 3,
expect(tree).to.deep.equal({
"w:document": [
{
_attr: {
"xmlns:wpc": "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas",
"xmlns:mc": "http://schemas.openxmlformats.org/markup-compatibility/2006",
"xmlns:o": "urn:schemas-microsoft-com:office:office",
"xmlns:r": "http://schemas.openxmlformats.org/officeDocument/2006/relationships",
"xmlns:m": "http://schemas.openxmlformats.org/officeDocument/2006/math",
"xmlns:v": "urn:schemas-microsoft-com:vml",
"xmlns:wp14": "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing",
"xmlns:wp": "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing",
"xmlns:w10": "urn:schemas-microsoft-com:office:word",
"xmlns:w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main",
"xmlns:w14": "http://schemas.microsoft.com/office/word/2010/wordml",
"xmlns:w15": "http://schemas.microsoft.com/office/word/2012/wordml",
"xmlns:wpg": "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup",
"xmlns:wpi": "http://schemas.microsoft.com/office/word/2010/wordprocessingInk",
"xmlns:wne": "http://schemas.microsoft.com/office/word/2006/wordml",
"xmlns:wps": "http://schemas.microsoft.com/office/word/2010/wordprocessingShape",
"mc:Ignorable": "w14 w15 wp14",
},
},
{ "w:body": {} },
],
});
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);
});
});
});

View File

@ -1,16 +1,15 @@
// 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";
import { DocumentAttributes } from "./document-attributes";
export class Document extends XmlComponent {
private readonly body: Body;
constructor(sectionPropertiesOptions?: SectionPropertiesOptions) {
constructor() {
super("w:document");
this.root.push(
new DocumentAttributes({
@ -33,37 +32,15 @@ export class Document extends XmlComponent {
Ignorable: "w14 w15 wp14",
}),
);
this.body = new Body(sectionPropertiesOptions);
this.body = new Body();
this.root.push(this.body);
}
public addParagraph(paragraph: Paragraph): Document {
this.body.push(paragraph);
public add(item: Paragraph | Table | TableOfContents): Document {
this.body.push(item);
return this;
}
public addTableOfContents(toc: TableOfContents): Document {
this.body.push(toc);
return this;
}
public createParagraph(text?: string): Paragraph {
const para = new Paragraph(text);
this.addParagraph(para);
return para;
}
public addTable(table: Table): Document {
this.body.push(table);
return this;
}
public createTable(options: ITableOptions): Table {
const table = new Table(options);
this.addTable(table);
return table;
}
public get Body(): Body {
return this.body;
}
@ -71,8 +48,4 @@ export class Document extends XmlComponent {
public getTablesOfContents(): TableOfContents[] {
return this.body.getTablesOfContents();
}
public getParagraphs(): Paragraph[] {
return this.body.getParagraphs();
}
}

View File

@ -4,23 +4,37 @@ import * as sinon from "sinon";
import { Formatter } from "export/formatter";
import { File } from "./file";
import { Footer, Header } from "./header";
import { Paragraph } from "./paragraph";
import { Table } from "./table";
import { TableOfContents } from "./table-of-contents";
describe("File", () => {
describe("#constructor", () => {
it("should create with correct headers and footers by default", () => {
const doc = new File();
doc.addSection({
children: [],
});
const tree = new Formatter().format(doc.Document.Body);
expect(tree["w:body"][1]["w:sectPr"][4]["w:headerReference"]._attr["w:type"]).to.equal("default");
expect(tree["w:body"][1]["w:sectPr"][5]["w:footerReference"]._attr["w:type"]).to.equal("default");
});
it("should create with correct headers and footers", () => {
const doc = new File();
const header = doc.createHeader();
const footer = doc.createFooter();
doc.addSection({
headers: {
default: header,
default: new Header(),
},
footers: {
default: footer,
default: new Footer(),
},
children: [],
});
const tree = new Formatter().format(doc.Document.Body);
@ -31,40 +45,38 @@ describe("File", () => {
it("should create with first headers and footers", () => {
const doc = new File();
const header = doc.createHeader();
const footer = doc.createFooter();
doc.addSection({
headers: {
first: header,
first: new Header(),
},
footers: {
first: footer,
first: new Footer(),
},
children: [],
});
const tree = new Formatter().format(doc.Document.Body);
expect(tree["w:body"][1]["w:sectPr"][4]["w:headerReference"]._attr["w:type"]).to.equal("first");
expect(tree["w:body"][1]["w:sectPr"][5]["w:footerReference"]._attr["w:type"]).to.equal("first");
expect(tree["w:body"][1]["w:sectPr"][5]["w:headerReference"]._attr["w:type"]).to.equal("first");
expect(tree["w:body"][1]["w:sectPr"][7]["w:footerReference"]._attr["w:type"]).to.equal("first");
});
it("should create with correct headers", () => {
const doc = new File();
const header = doc.createHeader();
const footer = doc.createFooter();
doc.addSection({
headers: {
default: header,
first: header,
even: header,
default: new Header(),
first: new Header(),
even: new Header(),
},
footers: {
default: footer,
first: footer,
even: footer,
default: new Footer(),
first: new Footer(),
even: new Footer(),
},
children: [],
});
const tree = new Formatter().format(doc.Document.Body);
@ -79,88 +91,56 @@ describe("File", () => {
});
});
describe("#addParagraph", () => {
it("should call the underlying document's addParagraph", () => {
describe("#addSection", () => {
it("should call the underlying document's add a Paragraph", () => {
const file = new File();
const spy = sinon.spy(file.Document, "addParagraph");
file.addParagraph(new Paragraph());
const spy = sinon.spy(file.Document, "add");
file.addSection({
children: [new Paragraph({})],
});
expect(spy.called).to.equal(true);
});
});
describe("#addTable", () => {
it("should call the underlying document's addTable", () => {
const wrapper = new File();
const spy = sinon.spy(wrapper.Document, "addTable");
wrapper.addTable(
new Table({
rows: 1,
columns: 1,
}),
);
it("should call the underlying document's add when adding a Table", () => {
const file = new File();
const spy = sinon.spy(file.Document, "add");
file.addSection({
children: [
new Table({
rows: 1,
columns: 1,
}),
],
});
expect(spy.called).to.equal(true);
});
});
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,
it("should call the underlying document's add when adding an Image (paragraph)", () => {
const file = new File();
const spy = sinon.spy(file.Document, "add");
// tslint:disable-next-line:no-any
file.addSection({
children: [new Paragraph("")],
});
expect(spy.called).to.equal(true);
});
});
describe("#addTableOfContents", () => {
it("should call the underlying document's addTableOfContents", () => {
const wrapper = new File();
const spy = sinon.spy(wrapper.Document, "addTableOfContents");
// tslint:disable-next-line:no-any
wrapper.addTableOfContents({} as any);
describe("#addSection", () => {
it("should call the underlying document's add", () => {
const file = new File();
const spy = sinon.spy(file.Document, "add");
file.addSection({
children: [new TableOfContents()],
});
expect(spy.called).to.equal(true);
});
});
describe("#createParagraph", () => {
it("should call the underlying document's createParagraph", () => {
const wrapper = new File();
const spy = sinon.spy(wrapper.Document, "createParagraph");
wrapper.createParagraph("test");
expect(spy.called).to.equal(true);
});
});
describe("#addImage", () => {
it("should call the underlying document's addImage", () => {
const wrapper = new File();
const spy = sinon.spy(wrapper.Document, "addParagraph");
// tslint:disable-next-line:no-any
wrapper.addImage({} as any);
expect(spy.called).to.equal(true);
});
});
describe("#createImage", () => {
it("should call the underlying document's createImage", () => {
const wrapper = new File();
const spy = sinon.spy(wrapper.Media, "addMedia");
const wrapperSpy = sinon.spy(wrapper.Document, "addParagraph");
wrapper.createImage("");
expect(spy.called).to.equal(true);
expect(wrapperSpy.called).to.equal(true);
});
});
describe("#createFootnote", () => {
it("should call the underlying document's createFootnote", () => {
const wrapper = new File();

View File

@ -3,19 +3,18 @@ import { ContentTypes } from "./content-types/content-types";
import { CoreProperties, IPropertiesOptions } from "./core-properties";
import { Document } from "./document";
import {
FooterReference,
FooterReferenceType,
HeaderReference,
HeaderReferenceType,
IHeaderFooterGroup,
IPageSizeAttributes,
SectionPropertiesOptions,
} from "./document/body/section-properties";
import { IDrawingOptions } from "./drawing";
import { IPageMarginAttributes } from "./document/body/section-properties/page-margin/page-margin-attributes";
import { IFileProperties } from "./file-properties";
import { FooterWrapper, IDocumentFooter } from "./footer-wrapper";
import { FootNotes } from "./footnotes";
import { Footer, Header } from "./header";
import { HeaderWrapper, IDocumentHeader } from "./header-wrapper";
import { Image, Media } from "./media";
import { Media } from "./media";
import { Numbering } from "./numbering";
import { Bookmark, Hyperlink, Paragraph } from "./paragraph";
import { Relationships } from "./relationships";
@ -24,12 +23,31 @@ 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 interface ISectionOptions {
readonly headers?: {
readonly default?: Header;
readonly first?: Header;
readonly even?: Header;
};
readonly footers?: {
readonly default?: Footer;
readonly first?: Footer;
readonly even?: Footer;
};
readonly size?: IPageSizeAttributes;
readonly margins?: IPageMarginAttributes;
readonly properties?: SectionPropertiesOptions;
readonly children: Array<Paragraph | Table | TableOfContents>;
}
export class File {
// tslint:disable-next-line:readonly-keyword
private currentRelationshipId: number = 1;
// tslint:disable-next-line:readonly-keyword
private styles: Styles;
private readonly document: Document;
private readonly headers: IDocumentHeader[] = [];
@ -43,8 +61,6 @@ export class File {
private readonly settings: Settings;
private readonly contentTypes: ContentTypes;
private readonly appProperties: AppProperties;
// tslint:disable-next-line:readonly-keyword
private styles: Styles;
constructor(
options: IPropertiesOptions = {
@ -52,8 +68,8 @@ export class File {
revision: "1",
lastModifiedBy: "Un-named",
},
sectionPropertiesOptions: SectionPropertiesOptions = {},
fileProperties: IFileProperties = {},
sections: ISectionOptions[] = [],
) {
this.coreProperties = new CoreProperties(options);
this.numbering = new Numbering();
@ -62,6 +78,8 @@ export class File {
this.appProperties = new AppProperties();
this.footNotes = new FootNotes();
this.contentTypes = new ContentTypes();
this.document = new Document();
this.settings = new Settings();
this.media = fileProperties.template && fileProperties.template.media ? fileProperties.template.media : new Media();
@ -90,66 +108,21 @@ export class File {
for (const templateHeader of fileProperties.template.headers) {
this.addHeaderToDocument(templateHeader.header, templateHeader.type);
}
} else {
this.createHeader();
}
if (fileProperties.template && fileProperties.template.footers) {
for (const templateFooter of fileProperties.template.footers) {
this.addFooterToDocument(templateFooter.footer, templateFooter.type);
}
} else {
this.createFooter();
}
const newSectionPropertiesOptions = {
...sectionPropertiesOptions,
headers: this.groupHeaders(this.headers, sectionPropertiesOptions.headers),
footers: this.groupFooters(this.footers, sectionPropertiesOptions.footers),
};
for (const section of sections) {
this.document.Body.addSection(section.properties ? section.properties : {});
this.document = new Document(newSectionPropertiesOptions);
this.settings = new Settings();
}
public addTableOfContents(toc: TableOfContents): File {
this.document.addTableOfContents(toc);
return this;
}
public addParagraph(paragraph: Paragraph): File {
this.document.addParagraph(paragraph);
return this;
}
public createParagraph(text?: string): Paragraph {
return this.document.createParagraph(text);
}
public addTable(table: Table): File {
this.document.addTable(table);
return this;
}
public createTable(options: ITableOptions): Table {
return this.document.createTable(options);
}
public addImage(image: Image): File {
this.document.addParagraph(image.Paragraph);
return this;
}
public createImage(
buffer: Buffer | string | Uint8Array | ArrayBuffer,
width?: number,
height?: number,
drawingOptions?: IDrawingOptions,
): Image {
const image = Media.addImage(this, buffer, width, height, drawingOptions);
this.document.addParagraph(image.Paragraph);
return image;
for (const child of section.children) {
this.document.add(child);
}
}
}
public createHyperlink(link: string, text?: string): Hyperlink {
@ -172,106 +145,71 @@ export class File {
return hyperlink;
}
public createBookmark(name: string, text?: string): Bookmark {
const newText = text === undefined ? name : text;
const bookmark = new Bookmark(name, newText, this.docRelationships.RelationshipCount);
return bookmark;
public createBookmark(name: string, text: string = name): Bookmark {
return new Bookmark(name, text, this.docRelationships.RelationshipCount);
}
public addSection(sectionPropertiesOptions: SectionPropertiesOptions): void {
this.document.Body.addSection(sectionPropertiesOptions);
public addSection({
headers = { default: new Header() },
footers = { default: new Header() },
margins = {},
size = {},
properties,
children,
}: ISectionOptions): void {
this.document.Body.addSection({
...properties,
headers: {
default: headers.default ? this.createHeader(headers.default) : this.createHeader(new Header()),
first: headers.first ? this.createHeader(headers.first) : undefined,
even: headers.even ? this.createHeader(headers.even) : undefined,
},
footers: {
default: footers.default ? this.createFooter(footers.default) : this.createFooter(new Footer()),
first: footers.first ? this.createFooter(footers.first) : undefined,
even: footers.even ? this.createFooter(footers.even) : undefined,
},
...margins,
...size,
});
for (const child of children) {
this.document.add(child);
}
}
public createFootnote(paragraph: Paragraph): void {
this.footNotes.createFootNote(paragraph);
}
public createHeader(): HeaderWrapper {
const header = new HeaderWrapper(this.media, this.currentRelationshipId++);
this.addHeaderToDocument(header);
return header;
}
public createFooter(): FooterWrapper {
const footer = new FooterWrapper(this.media, this.currentRelationshipId++);
this.addFooterToDocument(footer);
return footer;
}
public createFirstPageHeader(): HeaderWrapper {
const headerWrapper = this.createHeader();
this.document.Body.DefaultSection.addChildElement(
new HeaderReference({
headerType: HeaderReferenceType.FIRST,
headerId: headerWrapper.Header.ReferenceId,
}),
);
return headerWrapper;
}
public createEvenPageHeader(): HeaderWrapper {
const headerWrapper = this.createHeader();
this.document.Body.DefaultSection.addChildElement(
new HeaderReference({
headerType: HeaderReferenceType.EVEN,
headerId: headerWrapper.Header.ReferenceId,
}),
);
return headerWrapper;
}
public createFirstPageFooter(): FooterWrapper {
const footerWrapper = this.createFooter();
this.document.Body.DefaultSection.addChildElement(
new FooterReference({
footerType: FooterReferenceType.FIRST,
footerId: footerWrapper.Footer.ReferenceId,
}),
);
return footerWrapper;
}
public createEvenPageFooter(): FooterWrapper {
const footerWrapper = this.createFooter();
this.document.Body.DefaultSection.addChildElement(
new FooterReference({
footerType: FooterReferenceType.EVEN,
footerId: footerWrapper.Footer.ReferenceId,
}),
);
return footerWrapper;
}
public getFooterByReferenceNumber(refId: number): FooterWrapper {
const entry = this.footers.map((item) => item.footer).find((h) => h.Footer.ReferenceId === refId);
if (entry) {
return entry;
}
throw new Error(`There is no footer with given reference id ${refId}`);
}
public getHeaderByReferenceNumber(refId: number): HeaderWrapper {
const entry = this.headers.map((item) => item.header).find((h) => h.Header.ReferenceId === refId);
if (entry) {
return entry;
}
throw new Error(`There is no header with given reference id ${refId}`);
}
public verifyUpdateFields(): void {
if (this.document.getTablesOfContents().length) {
this.settings.addUpdateFields();
}
}
private createHeader(header: Header): HeaderWrapper {
const wrapper = new HeaderWrapper(this.media, this.currentRelationshipId++);
for (const child of header.options.children) {
wrapper.add(child);
}
this.addHeaderToDocument(wrapper);
return wrapper;
}
private createFooter(footer: Footer): FooterWrapper {
const wrapper = new FooterWrapper(this.media, this.currentRelationshipId++);
for (const child of footer.options.children) {
wrapper.add(child);
}
this.addFooterToDocument(wrapper);
return wrapper;
}
private addHeaderToDocument(header: HeaderWrapper, type: HeaderReferenceType = HeaderReferenceType.DEFAULT): void {
this.headers.push({ header, type });
this.docRelationships.createRelationship(
@ -331,66 +269,6 @@ export class File {
);
}
private groupHeaders(headers: IDocumentHeader[], group: IHeaderFooterGroup<HeaderWrapper> = {}): IHeaderFooterGroup<HeaderWrapper> {
let newGroup = group;
for (const header of headers) {
switch (header.type) {
case HeaderReferenceType.FIRST:
newGroup = {
...newGroup,
first: header.header,
};
break;
case HeaderReferenceType.EVEN:
newGroup = {
...newGroup,
even: header.header,
};
break;
case HeaderReferenceType.DEFAULT:
default:
newGroup = {
...newGroup,
default: header.header,
};
break;
}
}
return newGroup;
}
private groupFooters(footers: IDocumentFooter[], group: IHeaderFooterGroup<FooterWrapper> = {}): IHeaderFooterGroup<FooterWrapper> {
let newGroup = group;
for (const footer of footers) {
switch (footer.type) {
case FooterReferenceType.FIRST:
newGroup = {
...newGroup,
first: footer.footer,
};
break;
case FooterReferenceType.EVEN:
newGroup = {
...newGroup,
even: footer.footer,
};
break;
case FooterReferenceType.DEFAULT:
default:
newGroup = {
...newGroup,
default: footer.footer,
};
break;
}
}
return newGroup;
}
public get Document(): Document {
return this.document;
}
@ -423,18 +301,10 @@ export class File {
return this.fileRelationships;
}
public get Header(): HeaderWrapper {
return this.headers[0].header;
}
public get Headers(): HeaderWrapper[] {
return this.headers.map((item) => item.header);
}
public get Footer(): FooterWrapper {
return this.footers[0].footer;
}
public get Footers(): FooterWrapper[] {
return this.footers.map((item) => item.footer);
}

View File

@ -7,21 +7,19 @@ import { Paragraph } from "./paragraph";
import { Table } from "./table";
describe("FooterWrapper", () => {
describe("#addParagraph", () => {
describe("#add", () => {
it("should call the underlying footer's addParagraph", () => {
const file = new FooterWrapper(new Media(), 1);
const spy = sinon.spy(file.Footer, "addParagraph");
file.addParagraph(new Paragraph());
const spy = sinon.spy(file.Footer, "add");
file.add(new Paragraph({}));
expect(spy.called).to.equal(true);
});
});
describe("#addTable", () => {
it("should call the underlying footer's addParagraph", () => {
const file = new FooterWrapper(new Media(), 1);
const spy = sinon.spy(file.Footer, "addTable");
file.addTable(
const spy = sinon.spy(file.Footer, "add");
file.add(
new Table({
rows: 1,
columns: 1,
@ -30,32 +28,10 @@ describe("FooterWrapper", () => {
expect(spy.called).to.equal(true);
});
});
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("#createParagraph", () => {
it("should call the underlying footer's createParagraph", () => {
const file = new FooterWrapper(new Media(), 1);
const spy = sinon.spy(file.Footer, "addParagraph");
file.createParagraph();
expect(spy.called).to.equal(true);
});
});
describe("#addImage", () => {
it("should call the underlying footer's addImage", () => {
const file = new FooterWrapper(new Media(), 1);
const spy = sinon.spy(file.Footer, "addParagraph");
const spy = sinon.spy(file.Footer, "add");
// tslint:disable-next-line:no-any
file.addImage({} as any);
@ -63,18 +39,6 @@ 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");
file.createImage("");
expect(spy.called).to.equal(true);
expect(fileSpy.called).to.equal(true);
});
});
describe("#addChildElement", () => {
it("should call the underlying footer's addChildElement", () => {
const file = new FooterWrapper(new Media(), 1);

View File

@ -3,7 +3,7 @@ import { XmlComponent } from "file/xml-components";
import { FooterReferenceType } from "./document";
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";
@ -21,38 +21,19 @@ export class FooterWrapper {
this.relationships = new Relationships();
}
public addParagraph(paragraph: Paragraph): void {
this.footer.addParagraph(paragraph);
public add(item: Paragraph | Table): void {
this.footer.add(item);
}
public createParagraph(text?: string): Paragraph {
const para = new Paragraph(text);
this.addParagraph(para);
return para;
}
public addTable(table: Table): void {
this.footer.addTable(table);
}
public createTable(rows: number, cols: number): Table {
return this.footer.createTable(rows, cols);
public addImage(image: Image): FooterWrapper {
this.footer.add(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 addImage(image: Image): FooterWrapper {
this.footer.addParagraph(image.Paragraph);
return this;
}
public get Footer(): Footer {
return this.footer;
}

View File

@ -38,28 +38,7 @@ export class Footer extends InitializableXmlComponent {
return this.refId;
}
public addParagraph(paragraph: Paragraph): Footer {
this.root.push(paragraph);
return this;
}
public createParagraph(text?: string): Paragraph {
const para = new Paragraph(text);
this.addParagraph(para);
return para;
}
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;
public add(item: Paragraph | Table): void {
this.root.push(item);
}
}

View File

@ -20,7 +20,7 @@ export class Footnote extends XmlComponent {
);
}
public addParagraph(paragraph: Paragraph): void {
public add(paragraph: Paragraph): void {
paragraph.addRunToFront(new FootnoteRefRun());
this.root.push(paragraph);
}

View File

@ -3,7 +3,7 @@ import { ContinuationSeperator } from "./continuation-seperator";
export class ContinuationSeperatorRun extends Run {
constructor() {
super();
super({});
this.root.push(new ContinuationSeperator());
}

View File

@ -3,9 +3,10 @@ import { FootnoteRef } from "./footnote-ref";
export class FootnoteRefRun extends Run {
constructor() {
super();
super({
style: "FootnoteReference",
});
this.style("FootnoteReference");
this.root.push(new FootnoteRef());
}
}

View File

@ -26,7 +26,7 @@ export class FootnoteReference extends XmlComponent {
export class FootnoteReferenceRun extends Run {
constructor(id: number) {
super();
super({});
this.properties.push(new Style("FootnoteReference"));

View File

@ -3,7 +3,7 @@ import { Seperator } from "./seperator";
export class SeperatorRun extends Run {
constructor() {
super();
super({});
this.root.push(new Seperator());
}

View File

@ -37,33 +37,33 @@ export class FootNotes extends XmlComponent {
);
const begin = new Footnote(-1, FootnoteType.SEPERATOR);
begin.addParagraph(
new Paragraph()
.spacing({
begin.add(
new Paragraph({
spacing: {
after: 0,
line: 240,
lineRule: "auto",
})
.addRun(new SeperatorRun()),
},
}).addRun(new SeperatorRun()),
);
this.root.push(begin);
const spacing = new Footnote(0, FootnoteType.CONTINUATION_SEPERATOR);
spacing.addParagraph(
new Paragraph()
.spacing({
spacing.add(
new Paragraph({
spacing: {
after: 0,
line: 240,
lineRule: "auto",
})
.addRun(new ContinuationSeperatorRun()),
},
}).addRun(new ContinuationSeperatorRun()),
);
this.root.push(spacing);
}
public createFootNote(paragraph: Paragraph): void {
const footnote = new Footnote(this.currentId);
footnote.addParagraph(paragraph);
footnote.add(paragraph);
this.root.push(footnote);
this.currentId++;

View File

@ -7,21 +7,19 @@ import { Paragraph } from "./paragraph";
import { Table } from "./table";
describe("HeaderWrapper", () => {
describe("#addParagraph", () => {
it("should call the underlying header's addParagraph", () => {
describe("#add", () => {
it("should call the underlying header's addChildElement for Paragraph", () => {
const wrapper = new HeaderWrapper(new Media(), 1);
const spy = sinon.spy(wrapper.Header, "addParagraph");
wrapper.addParagraph(new Paragraph());
const spy = sinon.spy(wrapper.Header, "add");
wrapper.add(new Paragraph({}));
expect(spy.called).to.equal(true);
});
});
describe("#addTable", () => {
it("should call the underlying header's addTable", () => {
it("should call the underlying header's addChildElement for Table", () => {
const wrapper = new HeaderWrapper(new Media(), 1);
const spy = sinon.spy(wrapper.Header, "addTable");
wrapper.addTable(
const spy = sinon.spy(wrapper.Header, "add");
wrapper.add(
new Table({
rows: 1,
columns: 1,
@ -32,30 +30,10 @@ 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("#createParagraph", () => {
it("should call the underlying header's createParagraph", () => {
const file = new HeaderWrapper(new Media(), 1);
const spy = sinon.spy(file.Header, "addParagraph");
file.createParagraph();
expect(spy.called).to.equal(true);
});
});
describe("#addImage", () => {
it("should call the underlying header's addImage", () => {
const file = new HeaderWrapper(new Media(), 1);
const spy = sinon.spy(file.Header, "addParagraph");
const spy = sinon.spy(file.Header, "add");
// tslint:disable-next-line:no-any
file.addImage({} as any);
@ -63,18 +41,6 @@ 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");
file.createImage("");
expect(spy.called).to.equal(true);
expect(fileSpy.called).to.equal(true);
});
});
describe("#addChildElement", () => {
it("should call the underlying header's addChildElement", () => {
const file = new HeaderWrapper(new Media(), 1);

View File

@ -3,7 +3,7 @@ import { XmlComponent } from "file/xml-components";
import { HeaderReferenceType } from "./document";
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";
@ -21,38 +21,21 @@ export class HeaderWrapper {
this.relationships = new Relationships();
}
public addParagraph(paragraph: Paragraph): void {
this.header.addParagraph(paragraph);
public add(item: Paragraph | Table): HeaderWrapper {
this.header.add(item);
return this;
}
public createParagraph(text?: string): Paragraph {
const para = new Paragraph(text);
this.addParagraph(para);
return para;
}
public addTable(table: Table): void {
this.header.addTable(table);
}
public createTable(rows: number, cols: number): Table {
return this.header.createTable(rows, cols);
public addImage(image: Image): HeaderWrapper {
this.header.add(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 addImage(image: Image): HeaderWrapper {
this.header.addParagraph(image.Paragraph);
return this;
}
public get Header(): Header {
return this.header;
}

14
src/file/header.ts Normal file
View File

@ -0,0 +1,14 @@
import { Paragraph } from "./paragraph";
import { Table } from "./table";
export interface IHeaderOptions {
readonly children: Array<Paragraph | Table>;
}
export class Header {
constructor(public readonly options: IHeaderOptions = { children: [] }) {}
}
export class Footer {
constructor(public readonly options: IHeaderOptions = { children: [] }) {}
}

View File

@ -50,26 +50,7 @@ export class Header extends InitializableXmlComponent {
return this.refId;
}
public addParagraph(paragraph: Paragraph): void {
this.root.push(paragraph);
}
public createParagraph(text?: string): Paragraph {
const para = new Paragraph(text);
this.addParagraph(para);
return para;
}
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;
public add(item: Paragraph | Table): void {
this.root.push(item);
}
}

View File

@ -11,3 +11,4 @@ export * from "./table-of-contents";
export * from "./xml-components";
export * from "./header-wrapper";
export * from "./footer-wrapper";
export * from "./header";

View File

@ -10,8 +10,4 @@ export class Image {
public get Run(): PictureRun {
return this.paragraph.Run;
}
public scale(factorX: number, factorY?: number): void {
this.paragraph.Run.scale(factorX, factorY);
}
}

View File

@ -5,6 +5,7 @@ import { stub } from "sinon";
import { Formatter } from "export/formatter";
import { File } from "../file";
import { Paragraph } from "../paragraph";
import { Media } from "./media";
describe("Media", () => {
@ -13,10 +14,10 @@ describe("Media", () => {
const file = new File();
const image = Media.addImage(file, "");
let tree = new Formatter().format(image.Paragraph);
let tree = new Formatter().format(new Paragraph(image));
expect(tree["w:p"]).to.be.an.instanceof(Array);
tree = new Formatter().format(image.Run);
tree = new Formatter().format(image);
expect(tree["w:r"]).to.be.an.instanceof(Array);
});
@ -26,7 +27,7 @@ describe("Media", () => {
const file = new File();
const image1 = Media.addImage(file, "test");
const tree = new Formatter().format(image1.Paragraph);
const tree = new Formatter().format(new Paragraph(image1));
const inlineElements = tree["w:p"][0]["w:r"][0]["w:drawing"][0]["wp:inline"];
const graphicData = inlineElements.find((x) => x["a:graphic"]);
@ -38,7 +39,7 @@ describe("Media", () => {
});
const image2 = Media.addImage(file, "test");
const tree2 = new Formatter().format(image2.Paragraph);
const tree2 = new Formatter().format(new Paragraph(image2));
const inlineElements2 = tree2["w:p"][0]["w:r"][0]["w:drawing"][0]["wp:inline"];
const graphicData2 = inlineElements2.find((x) => x["a:graphic"]);

View File

@ -1,20 +1,22 @@
import { IDrawingOptions } from "../drawing";
import { File } from "../file";
import { ImageParagraph } from "../paragraph";
import { FooterWrapper } from "../footer-wrapper";
import { HeaderWrapper } from "../header-wrapper";
import { PictureRun } from "../paragraph";
import { IMediaData } from "./data";
import { Image } from "./image";
// 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,
drawingOptions?: IDrawingOptions,
): Image {
): PictureRun {
// Workaround to expose id without exposing to API
const mediaData = file.Media.addMedia(buffer, width, height);
return new Image(new ImageParagraph(mediaData, drawingOptions));
return new PictureRun(mediaData, drawingOptions);
}
private static generateId(): string {

View File

@ -1,7 +1,8 @@
import { Attributes, XmlAttributeComponent, XmlComponent } from "file/xml-components";
import {
Alignment,
AlignmentOptions,
AlignmentType,
IIndentAttributesProperties,
Indent,
ISpacingProperties,
KeepLines,
@ -114,7 +115,7 @@ export class LevelBase extends XmlComponent {
this.root.push(new LevelJc(lvlJc));
}
this.paragraphProperties = new ParagraphProperties();
this.paragraphProperties = new ParagraphProperties({});
this.runProperties = new RunProperties();
this.root.push(this.paragraphProperties);
@ -198,25 +199,34 @@ export class LevelBase extends XmlComponent {
return this;
}
public highlight(color: string): Level {
this.addRunProperty(new formatting.Highlight(color));
return this;
}
public shadow(value: string, fill: string, color: string): Level {
this.addRunProperty(new formatting.Shading(value, fill, color));
return this;
}
// --------------------- Paragraph formatting ------------------------ //
public center(): Level {
this.addParagraphProperty(new Alignment(AlignmentOptions.CENTER));
this.addParagraphProperty(new Alignment(AlignmentType.CENTER));
return this;
}
public left(): Level {
this.addParagraphProperty(new Alignment(AlignmentOptions.LEFT));
this.addParagraphProperty(new Alignment(AlignmentType.LEFT));
return this;
}
public right(): Level {
this.addParagraphProperty(new Alignment(AlignmentOptions.RIGHT));
this.addParagraphProperty(new Alignment(AlignmentType.RIGHT));
return this;
}
public justified(): Level {
this.addParagraphProperty(new Alignment(AlignmentOptions.BOTH));
this.addParagraphProperty(new Alignment(AlignmentType.BOTH));
return this;
}
@ -235,7 +245,7 @@ export class LevelBase extends XmlComponent {
return this;
}
public indent(attrs: object): Level {
public indent(attrs: IIndentAttributesProperties): Level {
this.addParagraphProperty(new Indent(attrs));
return this;
}

View File

@ -175,9 +175,9 @@ describe("AbstractNumbering", () => {
"w:bottom": {
_attr: {
"w:color": "auto",
"w:space": "1",
"w:space": 1,
"w:val": "single",
"w:sz": "6",
"w:sz": 6,
},
},
},
@ -325,6 +325,24 @@ describe("AbstractNumbering", () => {
});
});
it("#highlight", () => {
const abstractNumbering = new AbstractNumbering(1);
const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.").highlight("005599");
const tree = new Formatter().format(level);
expect(tree["w:lvl"]).to.include({
"w:rPr": [{ "w:highlight": { _attr: { "w:val": "005599" } } }],
});
});
it("#shadow", () => {
const abstractNumbering = new AbstractNumbering(1);
const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.").shadow("pct10", "00FFFF", "FF0000");
const tree = new Formatter().format(level);
expect(tree["w:lvl"]).to.include({
"w:rPr": [{ "w:shd": { _attr: { "w:val": "pct10", "w:fill": "00FFFF", "w:color": "FF0000" } } }],
});
});
describe("#underline", () => {
it("should set underline to 'single' if no arguments are given", () => {
const abstractNumbering = new AbstractNumbering(1);

View File

@ -1,22 +1,23 @@
// http://officeopenxml.com/WPalignment.php
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
export enum AlignmentOptions {
export enum AlignmentType {
START = "start",
END = "end",
CENTER = "center",
BOTH = "both",
JUSTIFIED = "both",
DISTRIBUTE = "distribute",
LEFT = "left",
RIGHT = "right",
}
export class AlignmentAttributes extends XmlAttributeComponent<{ readonly val: AlignmentOptions }> {
export class AlignmentAttributes extends XmlAttributeComponent<{ readonly val: AlignmentType }> {
protected readonly xmlKeys = { val: "w:val" };
}
export class Alignment extends XmlComponent {
constructor(type: AlignmentOptions) {
constructor(type: AlignmentType) {
super("w:jc");
this.root.push(new AlignmentAttributes({ val: type }));
}

View File

@ -0,0 +1,17 @@
import { XmlAttributeComponent } from "file/xml-components";
export interface IBorderAttributesProperties {
readonly color: string;
readonly space: number;
readonly val: string;
readonly sz: number;
}
export class BorderAttributes extends XmlAttributeComponent<IBorderAttributesProperties> {
protected readonly xmlKeys = {
val: "w:val",
color: "w:color",
space: "w:space",
sz: "w:sz",
};
}

View File

@ -35,8 +35,8 @@ describe("ThematicBreak", () => {
"w:bottom": {
_attr: {
"w:color": "auto",
"w:space": "1",
"w:sz": "6",
"w:space": 1,
"w:sz": 6,
"w:val": "single",
},
},

View File

@ -1,63 +1,70 @@
// http://officeopenxml.com/WPborders.php
import { Attributes, XmlComponent } from "file/xml-components";
import { XmlComponent } from "file/xml-components";
import { BorderAttributes } from "./border-attributes";
interface IBorderPropertyOptions {
readonly color: string;
readonly space: number;
readonly value: string;
readonly size: number;
}
export interface IBorderOptions {
readonly top?: IBorderPropertyOptions;
readonly bottom?: IBorderPropertyOptions;
readonly left?: IBorderPropertyOptions;
readonly right?: IBorderPropertyOptions;
}
class BorderProperty extends XmlComponent {
public setProperties(color: string, space: string, value: string, size: string): XmlComponent {
const attrs = new Attributes({
color: color,
space: space,
val: value,
sz: size,
constructor(rootKey: string, options: IBorderPropertyOptions = { color: "auto", space: 1, value: "single", size: 6 }) {
super(rootKey);
const attrs = new BorderAttributes({
color: options.color,
space: options.space,
val: options.value,
sz: options.size,
});
this.root.push(attrs);
return this;
}
}
export class Border extends XmlComponent {
constructor() {
constructor(options: IBorderOptions) {
super("w:pBdr");
}
public addTopBorder(color: string = "auto", space: string = "1", value: string = "single", size: string = "6"): XmlComponent {
const top = new BorderProperty("w:top");
top.setProperties(color, space, value, size);
this.root.push(top);
if (options.top !== undefined) {
const borderProperty = new BorderProperty("w:top", options.top);
this.root.push(borderProperty);
}
return this;
}
if (options.bottom !== undefined) {
const borderProperty = new BorderProperty("w:bottom", options.bottom);
this.root.push(borderProperty);
}
public addBottomBorder(color: string = "auto", space: string = "1", value: string = "single", size: string = "6"): XmlComponent {
const bottom = new BorderProperty("w:bottom");
bottom.setProperties(color, space, value, size);
this.root.push(bottom);
if (options.left !== undefined) {
const borderProperty = new BorderProperty("w:left", options.left);
this.root.push(borderProperty);
}
return this;
}
public addLeftBorder(color: string = "auto", space: string = "1", value: string = "single", size: string = "6"): XmlComponent {
const left = new BorderProperty("w:left");
left.setProperties(color, space, value, size);
this.root.push(left);
return this;
}
public addRightBorder(color: string = "auto", space: string = "1", value: string = "single", size: string = "6"): XmlComponent {
const right = new BorderProperty("w:right");
right.setProperties(color, space, value, size);
this.root.push(right);
return this;
if (options.right !== undefined) {
const borderProperty = new BorderProperty("w:right", options.right);
this.root.push(borderProperty);
}
}
}
export class ThematicBreak extends XmlComponent {
constructor() {
super("w:pBdr");
const bottom = new BorderProperty("w:bottom");
bottom.setProperties("auto", "1", "single", "6");
const bottom = new BorderProperty("w:bottom", {
color: "auto",
space: 1,
value: "single",
size: 6,
});
this.root.push(bottom);
}
}

View File

@ -15,7 +15,7 @@ class Break extends XmlComponent {
export class PageBreak extends Run {
constructor() {
super();
super({});
this.root.push(new Break());
}
}

View File

@ -1,5 +1,15 @@
import { Attributes, XmlComponent } from "file/xml-components";
export enum HeadingLevel {
HEADING_1 = "Heading1",
HEADING_2 = "Heading2",
HEADING_3 = "Heading3",
HEADING_4 = "Heading4",
HEADING_5 = "Heading5",
HEADING_6 = "Heading6",
TITLE = "Title",
}
export class Style extends XmlComponent {
public readonly styleId: string;

View File

@ -1,12 +1,8 @@
// tslint:disable:object-literal-key-quotes
import { assert, expect } from "chai";
import { Formatter } from "export/formatter";
import { assert } from "chai";
import { ImageParagraph } from "./image";
import { EMPTY_OBJECT } from "file/xml-components";
describe("Image", () => {
let image: ImageParagraph;
@ -40,190 +36,4 @@ describe("Image", () => {
assert.isTrue(true);
});
});
describe("#scale()", () => {
it("should set the scale of the object properly", () => {
image.scale(2);
const tree = new Formatter().format(image);
expect(tree).to.deep.equal({
"w:p": [
{
"w:r": [
{
"w:drawing": [
{
"wp:inline": [
{
_attr: {
distB: 0,
distL: 0,
distR: 0,
distT: 0,
},
},
{
"wp:extent": {
_attr: {
cx: 20,
cy: 20,
},
},
},
{
"wp:effectExtent": {
_attr: {
b: 0,
l: 0,
r: 0,
t: 0,
},
},
},
{
"wp:docPr": {
_attr: {
descr: "",
id: 0,
name: "",
},
},
},
{
"wp:cNvGraphicFramePr": [
{
"a:graphicFrameLocks": {
_attr: {
noChangeAspect: 1,
"xmlns:a": "http://schemas.openxmlformats.org/drawingml/2006/main",
},
},
},
],
},
{
"a:graphic": [
{
_attr: {
"xmlns:a": "http://schemas.openxmlformats.org/drawingml/2006/main",
},
},
{
"a:graphicData": [
{
_attr: {
uri: "http://schemas.openxmlformats.org/drawingml/2006/picture",
},
},
{
"pic:pic": [
{
_attr: {
"xmlns:pic":
"http://schemas.openxmlformats.org/drawingml/2006/picture",
},
},
{
"pic:nvPicPr": [
{
"pic:cNvPr": {
_attr: {
desc: "",
id: 0,
name: "",
},
},
},
{
"pic:cNvPicPr": [
{
"a:picLocks": {
_attr: {
noChangeArrowheads: 1,
noChangeAspect: 1,
},
},
},
],
},
],
},
{
"pic:blipFill": [
{
"a:blip": {
_attr: {
cstate: "none",
"r:embed": "rId{test.png}",
},
},
},
{
"a:srcRect": EMPTY_OBJECT,
},
{
"a:stretch": [
{
"a:fillRect": EMPTY_OBJECT,
},
],
},
],
},
{
"pic:spPr": [
{
_attr: {
bwMode: "auto",
},
},
{
"a:xfrm": [
{
"a:ext": {
_attr: {
cx: 10,
cy: 10,
},
},
},
{
"a:off": {
_attr: {
x: 0,
y: 0,
},
},
},
],
},
{
"a:prstGeom": [
{
_attr: {
prst: "rect",
},
},
{
"a:avLst": EMPTY_OBJECT,
},
],
},
],
},
],
},
],
},
],
},
],
},
],
},
],
},
],
});
});
});
});

View File

@ -7,15 +7,11 @@ export class ImageParagraph extends Paragraph {
private readonly pictureRun: PictureRun;
constructor(imageData: IMediaData, drawingOptions?: IDrawingOptions) {
super();
super({});
this.pictureRun = new PictureRun(imageData, drawingOptions);
this.root.push(this.pictureRun);
}
public scale(factorX: number, factorY?: number): void {
this.pictureRun.scale(factorX, factorY);
}
public get Run(): PictureRun {
return this.pictureRun;
}

View File

@ -20,7 +20,10 @@ export class Hyperlink extends XmlComponent {
const attributes = new HyperlinkAttributes(props);
this.root.push(attributes);
this.textRun = new TextRun(text).style("Hyperlink");
this.textRun = new TextRun({
text: text,
style: "Hyperlink",
});
this.root.push(this.textRun);
}

View File

@ -9,12 +9,12 @@ describe("ParagraphOutlineLevel", () => {
describe("#constructor()", () => {
it("should create an outlineLevel with given value", () => {
outlineLevel = new OutlineLevel("0");
outlineLevel = new OutlineLevel(0);
const tree = new Formatter().format(outlineLevel);
expect(tree).to.deep.equal({
"w:outlineLvl": {
_attr: {
"w:val": "0",
"w:val": 0,
},
},
});

View File

@ -2,11 +2,9 @@
import { Attributes, XmlComponent } from "file/xml-components";
export class OutlineLevel extends XmlComponent {
public readonly level: string;
constructor(level: string) {
constructor(public readonly level: number) {
super("w:outlineLvl");
this.level = level;
this.root.push(
new Attributes({
val: level,

View File

@ -1,22 +1,16 @@
import { assert, expect } from "chai";
import { Formatter } from "export/formatter";
import * as file from "file";
import { Numbering } from "../numbering";
import { LeaderType } from "./formatting";
import { EMPTY_OBJECT } from "file/xml-components";
import { Numbering } from "../numbering";
import { AlignmentType, HeadingLevel, LeaderType } from "./formatting";
import { Paragraph } from "./paragraph";
describe("Paragraph", () => {
let paragraph: file.Paragraph;
beforeEach(() => {
paragraph = new file.Paragraph();
});
describe("#constructor()", () => {
it("should create valid JSON", () => {
const paragraph = new Paragraph("");
const stringifiedJson = JSON.stringify(paragraph);
try {
@ -28,28 +22,18 @@ describe("Paragraph", () => {
});
it("should create have valid properties", () => {
const paragraph = new Paragraph("");
const stringifiedJson = JSON.stringify(paragraph);
const newJson = JSON.parse(stringifiedJson);
assert.equal(newJson.root[0].rootKey, "w:pPr");
});
});
describe("#createTextRun", () => {
it("should add a new run to the paragraph and return it", () => {
const run = paragraph.createTextRun("this is a test run");
expect(run).to.be.instanceof(file.TextRun);
const tree = new Formatter().format(paragraph)["w:p"];
expect(tree)
.to.be.an("array")
.which.includes({
"w:r": [{ "w:t": [{ _attr: { "xml:space": "preserve" } }, "this is a test run"] }],
});
});
});
describe("#heading1()", () => {
it("should add heading style to JSON", () => {
paragraph.heading1();
const paragraph = new Paragraph({
heading: HeadingLevel.HEADING_1,
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
@ -63,7 +47,9 @@ describe("Paragraph", () => {
describe("#heading2()", () => {
it("should add heading style to JSON", () => {
paragraph.heading2();
const paragraph = new Paragraph({
heading: HeadingLevel.HEADING_2,
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
@ -77,7 +63,9 @@ describe("Paragraph", () => {
describe("#heading3()", () => {
it("should add heading style to JSON", () => {
paragraph.heading3();
const paragraph = new Paragraph({
heading: HeadingLevel.HEADING_3,
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
@ -91,7 +79,9 @@ describe("Paragraph", () => {
describe("#heading4()", () => {
it("should add heading style to JSON", () => {
paragraph.heading4();
const paragraph = new Paragraph({
heading: HeadingLevel.HEADING_4,
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
@ -105,7 +95,9 @@ describe("Paragraph", () => {
describe("#heading5()", () => {
it("should add heading style to JSON", () => {
paragraph.heading5();
const paragraph = new Paragraph({
heading: HeadingLevel.HEADING_5,
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
@ -119,7 +111,9 @@ describe("Paragraph", () => {
describe("#heading6()", () => {
it("should add heading style to JSON", () => {
paragraph.heading6();
const paragraph = new Paragraph({
heading: HeadingLevel.HEADING_6,
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
@ -133,7 +127,9 @@ describe("Paragraph", () => {
describe("#title()", () => {
it("should add title style to JSON", () => {
paragraph.title();
const paragraph = new Paragraph({
heading: HeadingLevel.TITLE,
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
@ -147,7 +143,9 @@ describe("Paragraph", () => {
describe("#center()", () => {
it("should add center alignment to JSON", () => {
paragraph.center();
const paragraph = new Paragraph({
alignment: AlignmentType.CENTER,
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
@ -161,7 +159,9 @@ describe("Paragraph", () => {
describe("#left()", () => {
it("should add left alignment to JSON", () => {
paragraph.left();
const paragraph = new Paragraph({
alignment: AlignmentType.LEFT,
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
@ -175,7 +175,9 @@ describe("Paragraph", () => {
describe("#right()", () => {
it("should add right alignment to JSON", () => {
paragraph.right();
const paragraph = new Paragraph({
alignment: AlignmentType.RIGHT,
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
@ -189,7 +191,9 @@ describe("Paragraph", () => {
describe("#start()", () => {
it("should add start alignment to JSON", () => {
paragraph.start();
const paragraph = new Paragraph({
alignment: AlignmentType.START,
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
@ -203,7 +207,9 @@ describe("Paragraph", () => {
describe("#end()", () => {
it("should add end alignment to JSON", () => {
paragraph.end();
const paragraph = new Paragraph({
alignment: AlignmentType.END,
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
@ -217,7 +223,9 @@ describe("Paragraph", () => {
describe("#distribute()", () => {
it("should add distribute alignment to JSON", () => {
paragraph.distribute();
const paragraph = new Paragraph({
alignment: AlignmentType.DISTRIBUTE,
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
@ -231,7 +239,9 @@ describe("Paragraph", () => {
describe("#justified()", () => {
it("should add justified alignment to JSON", () => {
paragraph.justified();
const paragraph = new Paragraph({
alignment: AlignmentType.JUSTIFIED,
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
@ -245,7 +255,11 @@ describe("Paragraph", () => {
describe("#maxRightTabStop()", () => {
it("should add maxRightTabStop to JSON", () => {
paragraph.maxRightTabStop();
const paragraph = new Paragraph({
tabStop: {
maxRight: {},
},
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
@ -272,7 +286,14 @@ describe("Paragraph", () => {
describe("#leftTabStop()", () => {
it("should add leftTabStop to JSON", () => {
paragraph.leftTabStop(100, LeaderType.HYPHEN);
const paragraph = new Paragraph({
tabStop: {
left: {
position: 100,
leader: LeaderType.HYPHEN,
},
},
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
@ -300,7 +321,14 @@ describe("Paragraph", () => {
describe("#rightTabStop()", () => {
it("should add rightTabStop to JSON", () => {
paragraph.rightTabStop(100, LeaderType.DOT);
const paragraph = new Paragraph({
tabStop: {
right: {
position: 100,
leader: LeaderType.DOT,
},
},
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
@ -328,7 +356,14 @@ describe("Paragraph", () => {
describe("#centerTabStop()", () => {
it("should add centerTabStop to JSON", () => {
paragraph.centerTabStop(100, LeaderType.MIDDLE_DOT);
const paragraph = new Paragraph({
tabStop: {
center: {
position: 100,
leader: LeaderType.MIDDLE_DOT,
},
},
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
@ -356,7 +391,9 @@ describe("Paragraph", () => {
describe("#contextualSpacing()", () => {
it("should add contextualSpacing to JSON, and set 1 if true", () => {
paragraph.contextualSpacing(true);
const paragraph = new Paragraph({
contextualSpacing: true,
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
@ -366,23 +403,13 @@ describe("Paragraph", () => {
],
});
});
it("should add contextualSpacing to JSON, and set 0 if false", () => {
paragraph.contextualSpacing(false);
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
{
"w:pPr": [{ "w:contextualSpacing": { _attr: { "w:val": 0 } } }],
},
],
});
});
});
describe("#thematicBreak()", () => {
it("should add thematic break to JSON", () => {
paragraph.thematicBreak();
const paragraph = new Paragraph({
thematicBreak: true,
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
@ -395,8 +422,8 @@ describe("Paragraph", () => {
_attr: {
"w:val": "single",
"w:color": "auto",
"w:space": "1",
"w:sz": "6",
"w:space": 1,
"w:sz": 6,
},
},
},
@ -411,9 +438,22 @@ describe("Paragraph", () => {
describe("#paragraphBorders()", () => {
it("should add a left and right border to a paragraph", () => {
paragraph.createBorder();
paragraph.Borders.addLeftBorder();
paragraph.Borders.addRightBorder();
const paragraph = new Paragraph({
border: {
left: {
color: "auto",
space: 1,
value: "single",
size: 6,
},
right: {
color: "auto",
space: 1,
value: "single",
size: 6,
},
},
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
@ -425,8 +465,8 @@ describe("Paragraph", () => {
"w:left": {
_attr: {
"w:color": "auto",
"w:space": "1",
"w:sz": "6",
"w:space": 1,
"w:sz": 6,
"w:val": "single",
},
},
@ -435,8 +475,8 @@ describe("Paragraph", () => {
"w:right": {
_attr: {
"w:color": "auto",
"w:space": "1",
"w:sz": "6",
"w:space": 1,
"w:sz": 6,
"w:val": "single",
},
},
@ -452,6 +492,7 @@ describe("Paragraph", () => {
describe("#pageBreak()", () => {
it("should add page break to JSON", () => {
const paragraph = new Paragraph({});
paragraph.pageBreak();
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
@ -466,7 +507,9 @@ describe("Paragraph", () => {
describe("#pageBreakBefore()", () => {
it("should add page break before to JSON", () => {
paragraph.pageBreakBefore();
const paragraph = new Paragraph({
pageBreakBefore: true,
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
@ -484,7 +527,11 @@ describe("Paragraph", () => {
describe("#bullet()", () => {
it("should default to 0 indent level if no bullet was specified", () => {
paragraph.bullet();
const paragraph = new Paragraph({
bullet: {
level: 0,
},
});
const tree = new Formatter().format(paragraph);
expect(tree)
.to.have.property("w:p")
@ -500,7 +547,11 @@ describe("Paragraph", () => {
});
it("should add list paragraph style to JSON", () => {
paragraph.bullet(0);
const paragraph = new Paragraph({
bullet: {
level: 0,
},
});
const tree = new Formatter().format(paragraph);
expect(tree)
.to.have.property("w:p")
@ -516,7 +567,11 @@ describe("Paragraph", () => {
});
it("it should add numbered properties", () => {
paragraph.bullet(1);
const paragraph = new Paragraph({
bullet: {
level: 1,
},
});
const tree = new Formatter().format(paragraph);
expect(tree)
.to.have.property("w:p")
@ -539,7 +594,12 @@ describe("Paragraph", () => {
numberedAbstract.createLevel(0, "lowerLetter", "%1)", "start");
const letterNumbering = numbering.createConcreteNumbering(numberedAbstract);
paragraph.setNumbering(letterNumbering, 0);
const paragraph = new Paragraph({
numbering: {
num: letterNumbering,
level: 0,
},
});
const tree = new Formatter().format(paragraph);
expect(tree)
.to.have.property("w:p")
@ -560,7 +620,12 @@ describe("Paragraph", () => {
numberedAbstract.createLevel(0, "lowerLetter", "%1)", "start");
const letterNumbering = numbering.createConcreteNumbering(numberedAbstract);
paragraph.setNumbering(letterNumbering, 0);
const paragraph = new Paragraph({
numbering: {
num: letterNumbering,
level: 0,
},
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
@ -582,7 +647,9 @@ describe("Paragraph", () => {
describe("#style", () => {
it("should set the paragraph style to the given styleId", () => {
paragraph.style("myFancyStyle");
const paragraph = new Paragraph({
style: "myFancyStyle",
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
@ -596,7 +663,9 @@ describe("Paragraph", () => {
describe("#indent", () => {
it("should set the paragraph indent to the given values", () => {
paragraph.indent({ left: 720 });
const paragraph = new Paragraph({
indent: { left: 720 },
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
@ -610,7 +679,9 @@ describe("Paragraph", () => {
describe("#spacing", () => {
it("should set the paragraph spacing to the given values", () => {
paragraph.spacing({ before: 90, line: 50 });
const paragraph = new Paragraph({
spacing: { before: 90, line: 50 },
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
@ -624,7 +695,9 @@ describe("Paragraph", () => {
describe("#keepLines", () => {
it("should set the paragraph keepLines sub-component", () => {
paragraph.keepLines();
const paragraph = new Paragraph({
keepLines: true,
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [{ "w:pPr": [{ "w:keepLines": EMPTY_OBJECT }] }],
@ -634,7 +707,9 @@ describe("Paragraph", () => {
describe("#keepNext", () => {
it("should set the paragraph keepNext sub-component", () => {
paragraph.keepNext();
const paragraph = new Paragraph({
keepNext: true,
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [{ "w:pPr": [{ "w:keepNext": EMPTY_OBJECT }] }],
@ -644,7 +719,9 @@ describe("Paragraph", () => {
describe("#bidirectional", () => {
it("set paragraph right to left layout", () => {
paragraph.bidirectional();
const paragraph = new Paragraph({
bidirectional: true,
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [{ "w:pPr": [{ "w:bidi": EMPTY_OBJECT }] }],
@ -654,12 +731,14 @@ describe("Paragraph", () => {
describe("#outlineLevel", () => {
it("should set paragraph outline level to the given value", () => {
paragraph.outlineLevel("0");
const paragraph = new Paragraph({
outlineLevel: 0,
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
{
"w:pPr": [{ "w:outlineLvl": { _attr: { "w:val": "0" } } }],
"w:pPr": [{ "w:outlineLvl": { _attr: { "w:val": 0 } } }],
},
],
});

View File

@ -4,43 +4,172 @@ import { Image } from "file/media";
import { Num } from "file/numbering/num";
import { XmlComponent } from "file/xml-components";
import { Alignment, AlignmentOptions } from "./formatting/alignment";
import { Alignment, AlignmentType } from "./formatting/alignment";
import { Bidirectional } from "./formatting/bidirectional";
import { Border, ThematicBreak } from "./formatting/border";
import { IBorderOptions, ThematicBreak } from "./formatting/border";
import { IIndentAttributesProperties, Indent } from "./formatting/indent";
import { KeepLines, KeepNext } from "./formatting/keep";
import { PageBreak, PageBreakBefore } from "./formatting/page-break";
import { ContextualSpacing, ISpacingProperties, Spacing } from "./formatting/spacing";
import { Style } from "./formatting/style";
import { HeadingLevel, Style } from "./formatting/style";
import { CenterTabStop, LeaderType, LeftTabStop, MaxRightTabStop, RightTabStop } from "./formatting/tab-stop";
import { NumberProperties } from "./formatting/unordered-list";
import { Bookmark, Hyperlink, OutlineLevel } from "./links";
import { ParagraphProperties } from "./properties";
import { PictureRun, Run, SequentialIdentifier, TextRun } from "./run";
interface ITabStopOptions {
readonly position: number;
readonly leader?: LeaderType;
}
export interface IParagraphOptions {
readonly text?: string;
readonly border?: IBorderOptions;
readonly spacing?: ISpacingProperties;
readonly outlineLevel?: number;
readonly alignment?: AlignmentType;
readonly heading?: HeadingLevel;
readonly bidirectional?: boolean;
readonly thematicBreak?: boolean;
readonly pageBreakBefore?: boolean;
readonly contextualSpacing?: boolean;
readonly indent?: IIndentAttributesProperties;
readonly keepLines?: boolean;
readonly keepNext?: boolean;
readonly tabStop?: {
readonly left?: ITabStopOptions;
readonly right?: ITabStopOptions;
readonly maxRight?: {
readonly leader?: LeaderType;
};
readonly center?: ITabStopOptions;
};
readonly style?: string;
readonly bullet?: {
readonly level: number;
};
readonly numbering?: {
readonly num: Num;
readonly level: number;
readonly custom?: boolean;
};
readonly children?: Array<TextRun | PictureRun | Hyperlink>;
}
export class Paragraph extends XmlComponent {
private readonly properties: ParagraphProperties;
constructor(text?: string) {
constructor(options: string | PictureRun | IParagraphOptions) {
super("w:p");
this.properties = new ParagraphProperties();
this.root.push(this.properties);
if (text !== undefined) {
this.root.push(new TextRun(text));
if (typeof options === "string") {
this.properties = new ParagraphProperties({});
this.root.push(this.properties);
this.root.push(new TextRun(options));
return;
}
}
public get paragraphProperties(): ParagraphProperties {
return this.properties;
}
if (options instanceof PictureRun) {
this.properties = new ParagraphProperties({});
this.root.push(this.properties);
this.root.push(options);
return;
}
public get Borders(): Border {
return this.properties.paragraphBorder;
}
this.properties = new ParagraphProperties({
border: options.border,
});
public createBorder(): Paragraph {
this.properties.createBorder();
return this;
this.root.push(this.properties);
if (options.text) {
this.root.push(new TextRun(options.text));
}
if (options.spacing) {
this.properties.push(new Spacing(options.spacing));
}
if (options.outlineLevel !== undefined) {
this.properties.push(new OutlineLevel(options.outlineLevel));
}
if (options.alignment) {
this.properties.push(new Alignment(options.alignment));
}
if (options.heading) {
this.properties.push(new Style(options.heading));
}
if (options.bidirectional) {
this.properties.push(new Bidirectional());
}
if (options.thematicBreak) {
this.properties.push(new ThematicBreak());
}
if (options.pageBreakBefore) {
this.properties.push(new PageBreakBefore());
}
if (options.contextualSpacing) {
this.properties.push(new ContextualSpacing(options.contextualSpacing));
}
if (options.indent) {
this.properties.push(new Indent(options.indent));
}
if (options.keepLines) {
this.properties.push(new KeepLines());
}
if (options.keepNext) {
this.properties.push(new KeepNext());
}
if (options.tabStop) {
if (options.tabStop.left) {
this.properties.push(new LeftTabStop(options.tabStop.left.position, options.tabStop.left.leader));
}
if (options.tabStop.right) {
this.properties.push(new RightTabStop(options.tabStop.right.position, options.tabStop.right.leader));
}
if (options.tabStop.maxRight) {
this.properties.push(new MaxRightTabStop(options.tabStop.maxRight.leader));
}
if (options.tabStop.center) {
this.properties.push(new CenterTabStop(options.tabStop.center.position, options.tabStop.center.leader));
}
}
if (options.style) {
this.properties.push(new Style(options.style));
}
if (options.bullet) {
this.properties.push(new Style("ListParagraph"));
this.properties.push(new NumberProperties(1, options.bullet.level));
}
if (options.numbering) {
if (!options.numbering.custom) {
this.properties.push(new Style("ListParagraph"));
}
this.properties.push(new NumberProperties(options.numbering.num.id, options.numbering.level));
}
if (options.children) {
for (const child of options.children) {
this.root.push(child);
}
}
}
public addRun(run: Run): Paragraph {
@ -61,12 +190,6 @@ export class Paragraph extends XmlComponent {
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);
@ -74,158 +197,11 @@ export class Paragraph extends XmlComponent {
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(AlignmentOptions.CENTER));
return this;
}
public left(): Paragraph {
this.properties.push(new Alignment(AlignmentOptions.LEFT));
return this;
}
public right(): Paragraph {
this.properties.push(new Alignment(AlignmentOptions.RIGHT));
return this;
}
public start(): Paragraph {
this.properties.push(new Alignment(AlignmentOptions.START));
return this;
}
public end(): Paragraph {
this.properties.push(new Alignment(AlignmentOptions.END));
return this;
}
public distribute(): Paragraph {
this.properties.push(new Alignment(AlignmentOptions.DISTRIBUTE));
return this;
}
public justified(): Paragraph {
this.properties.push(new Alignment(AlignmentOptions.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(leader?: LeaderType): Paragraph {
this.properties.push(new MaxRightTabStop(leader));
return this;
}
public leftTabStop(position: number, leader?: LeaderType): Paragraph {
this.properties.push(new LeftTabStop(position, leader));
return this;
}
public rightTabStop(position: number, leader?: LeaderType): Paragraph {
this.properties.push(new RightTabStop(position, leader));
return this;
}
public centerTabStop(position: number, leader?: LeaderType): Paragraph {
this.properties.push(new CenterTabStop(position, leader));
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 contextualSpacing(value: boolean): Paragraph {
this.properties.push(new ContextualSpacing(value));
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;
@ -236,18 +212,8 @@ export class Paragraph extends XmlComponent {
return this;
}
public bidirectional(): Paragraph {
this.properties.push(new Bidirectional());
return this;
}
public addSequentialIdentifier(identifier: string): Paragraph {
this.root.push(new SequentialIdentifier(identifier));
return this;
}
public outlineLevel(level: string): Paragraph {
this.properties.push(new OutlineLevel(level));
return this;
}
}

View File

@ -1,17 +1,19 @@
// http://officeopenxml.com/WPparagraphProperties.php
import { IgnoreIfEmptyXmlComponent, XmlComponent } from "file/xml-components";
import { Border } from "./formatting/border";
import { Border, IBorderOptions } from "./formatting/border";
interface IParagraphPropertiesOptions {
readonly border?: IBorderOptions;
}
export class ParagraphProperties extends IgnoreIfEmptyXmlComponent {
public readonly paragraphBorder: Border;
constructor() {
constructor(options: IParagraphPropertiesOptions) {
super("w:pPr");
this.paragraphBorder = new Border();
}
public createBorder(): void {
this.push(this.paragraphBorder);
if (options.border) {
this.push(new Border(options.border));
}
}
public push(item: XmlComponent): void {

View File

@ -113,7 +113,7 @@ export class Imprint extends XmlComponent {
}
}
export class Shadow extends XmlComponent {
/* export class Shadow extends XmlComponent {
constructor() {
super("w:shadow");
this.root.push(
@ -122,7 +122,7 @@ export class Shadow extends XmlComponent {
}),
);
}
}
} */
export class SmallCaps extends XmlComponent {
constructor() {
@ -178,3 +178,51 @@ export class RightToLeft extends XmlComponent {
);
}
}
export class Highlight extends XmlComponent {
constructor(color: string) {
super("w:highlight");
this.root.push(
new Attributes({
val: color,
}),
);
}
}
export class HighlightComplexScript extends XmlComponent {
constructor(color: string) {
super("w:highlightCs");
this.root.push(
new Attributes({
val: color,
}),
);
}
}
export class Shading extends XmlComponent {
constructor(value: string, fill: string, color: string) {
super("w:shd");
this.root.push(
new Attributes({
val: value,
fill: fill,
color: color,
}),
);
}
}
export class ShadowComplexScript extends XmlComponent {
constructor(value: string, fill: string, color: string) {
super("w:shdCs");
this.root.push(
new Attributes({
val: value,
fill: fill,
color: color,
}),
);
}
}

View File

@ -4,21 +4,15 @@ import { IMediaData } from "../../media/data";
import { Run } from "../run";
export class PictureRun extends Run {
private readonly drawing: Drawing;
constructor(imageData: IMediaData, drawingOptions?: IDrawingOptions) {
super();
super({});
if (imageData === undefined) {
throw new Error("imageData cannot be undefined");
}
this.drawing = new Drawing(imageData, drawingOptions);
const drawing = new Drawing(imageData, drawingOptions);
this.root.push(this.drawing);
}
public scale(factorX: number = 1, factorY: number = factorX): void {
this.drawing.scale(factorX, factorY);
this.root.push(drawing);
}
}

View File

@ -1,19 +1,17 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { ShadingType } from "file/table";
import { Run } from "./";
import { UnderlineType } from "./underline";
describe("Run", () => {
let run: Run;
beforeEach(() => {
run = new Run();
});
describe("#bold()", () => {
it("it should add bold to the properties", () => {
run.bold();
const run = new Run({
bold: true,
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
@ -36,7 +34,9 @@ describe("Run", () => {
describe("#italics()", () => {
it("it should add italics to the properties", () => {
run.italics();
const run = new Run({
italics: true,
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
@ -59,7 +59,9 @@ describe("Run", () => {
describe("#underline()", () => {
it("should default to 'single' and no color", () => {
run.underline();
const run = new Run({
underline: {},
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:u": { _attr: { "w:val": "single" } } }] }],
@ -67,7 +69,12 @@ describe("Run", () => {
});
it("should set the style type and color if given", () => {
run.underline("double", "990011");
const run = new Run({
underline: {
type: UnderlineType.DOUBLE,
color: "990011",
},
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:u": { _attr: { "w:val": "double", "w:color": "990011" } } }] }],
@ -77,7 +84,9 @@ describe("Run", () => {
describe("#smallCaps()", () => {
it("it should add smallCaps to the properties", () => {
run.smallCaps();
const run = new Run({
smallCaps: true,
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:smallCaps": {} }] }],
@ -87,7 +96,9 @@ describe("Run", () => {
describe("#caps()", () => {
it("it should add caps to the properties", () => {
run.allCaps();
const run = new Run({
allCaps: true,
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:caps": {} }] }],
@ -97,7 +108,9 @@ describe("Run", () => {
describe("#strike()", () => {
it("it should add strike to the properties", () => {
run.strike();
const run = new Run({
strike: true,
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:strike": { _attr: { "w:val": true } } }] }],
@ -107,7 +120,9 @@ describe("Run", () => {
describe("#doubleStrike()", () => {
it("it should add caps to the properties", () => {
run.doubleStrike();
const run = new Run({
doubleStrike: true,
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:dstrike": { _attr: { "w:val": true } } }] }],
@ -115,8 +130,65 @@ describe("Run", () => {
});
});
describe("#highlight()", () => {
it("it should add highlight to the properties", () => {
const run = new Run({
highlight: "005599",
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{
"w:rPr": [
{ "w:highlight": { _attr: { "w:val": "005599" } } },
{
"w:highlightCs": {
_attr: {
"w:val": "005599",
},
},
},
],
},
],
});
});
});
describe("#shadow()", () => {
it("it should add shadow to the properties", () => {
const run = new Run({
shading: {
type: ShadingType.PERCENT_10,
fill: "00FFFF",
color: "FF0000",
},
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{
"w:rPr": [
{ "w:shd": { _attr: { "w:val": "pct10", "w:fill": "00FFFF", "w:color": "FF0000" } } },
{
"w:shdCs": {
_attr: {
"w:val": "pct10",
"w:fill": "00FFFF",
"w:color": "FF0000",
},
},
},
],
},
],
});
});
});
describe("#break()", () => {
it("it should add break to the run", () => {
const run = new Run({});
run.break();
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
@ -127,6 +199,7 @@ describe("Run", () => {
describe("#tab()", () => {
it("it should add break to the run", () => {
const run = new Run({});
run.tab();
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
@ -136,12 +209,12 @@ describe("Run", () => {
});
describe("#font()", () => {
it("should allow chaining calls", () => {
expect(run.font("Times")).to.equal(run);
});
it("should set the font as named", () => {
run.font("Times");
const run = new Run({
font: {
name: "Times",
},
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
@ -157,7 +230,9 @@ describe("Run", () => {
describe("#color", () => {
it("should set the run to the color given", () => {
run.color("001122");
const run = new Run({
color: "001122",
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:color": { _attr: { "w:val": "001122" } } }] }],
@ -167,7 +242,9 @@ describe("Run", () => {
describe("#size", () => {
it("should set the run to the given size", () => {
run.size(24);
const run = new Run({
size: 24,
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
@ -181,7 +258,9 @@ describe("Run", () => {
describe("#rtl", () => {
it("should set the run to the RTL mode", () => {
run.rightToLeft();
const run = new Run({
rightToLeft: true,
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:rtl": { _attr: { "w:val": true } } }] }],
@ -191,6 +270,7 @@ describe("Run", () => {
describe("#numberOfTotalPages", () => {
it("should set the run to the RTL mode", () => {
const run = new Run({});
run.numberOfTotalPages();
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
@ -221,6 +301,7 @@ describe("Run", () => {
describe("#pageNumber", () => {
it("should set the run to the RTL mode", () => {
const run = new Run({});
run.pageNumber();
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
@ -236,7 +317,9 @@ describe("Run", () => {
describe("#style", () => {
it("should set the style to the given styleId", () => {
run.style("myRunStyle");
const run = new Run({
style: "myRunStyle",
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:rStyle": { _attr: { "w:val": "myRunStyle" } } }] }],

View File

@ -1,4 +1,7 @@
// http://officeopenxml.com/WPtext.php
import { ShadingType } from "file/table";
import { XmlComponent } from "file/xml-components";
import { Break } from "./break";
import { Caps, SmallCaps } from "./caps";
import { Begin, End, Separate } from "./field";
@ -7,9 +10,13 @@ import {
BoldComplexScript,
Color,
DoubleStrike,
Highlight,
HighlightComplexScript,
Italics,
ItalicsComplexScript,
RightToLeft,
Shading,
ShadowComplexScript,
Size,
SizeComplexScript,
Strike,
@ -20,50 +27,113 @@ import { RunFonts } from "./run-fonts";
import { SubScript, SuperScript } from "./script";
import { Style } from "./style";
import { Tab } from "./tab";
import { Underline } from "./underline";
import { Underline, UnderlineType } from "./underline";
import { XmlComponent } from "file/xml-components";
export interface IRunOptions {
readonly bold?: true;
readonly italics?: true;
readonly underline?: {
readonly color?: string;
readonly type?: UnderlineType;
};
readonly color?: string;
readonly size?: number;
readonly rightToLeft?: boolean;
readonly smallCaps?: boolean;
readonly allCaps?: boolean;
readonly strike?: boolean;
readonly doubleStrike?: boolean;
readonly subScript?: boolean;
readonly superScript?: boolean;
readonly style?: string;
readonly font?: {
readonly name: string;
readonly hint?: string;
};
readonly highlight?: string;
readonly shading?: {
readonly type: ShadingType;
readonly fill: string;
readonly color: string;
};
}
export class Run extends XmlComponent {
protected readonly properties: RunProperties;
constructor() {
constructor(options: IRunOptions) {
super("w:r");
this.properties = new RunProperties();
this.root.push(this.properties);
}
public bold(): Run {
this.properties.push(new Bold());
this.properties.push(new BoldComplexScript());
return this;
}
if (options.bold) {
this.properties.push(new Bold());
this.properties.push(new BoldComplexScript());
}
public italics(): Run {
this.properties.push(new Italics());
this.properties.push(new ItalicsComplexScript());
return this;
}
if (options.italics) {
this.properties.push(new Italics());
this.properties.push(new ItalicsComplexScript());
}
public underline(underlineType?: string, color?: string): Run {
this.properties.push(new Underline(underlineType, color));
return this;
}
if (options.underline) {
this.properties.push(new Underline(options.underline.type, options.underline.color));
}
public color(color: string): Run {
this.properties.push(new Color(color));
return this;
}
if (options.color) {
this.properties.push(new Color(options.color));
}
public size(size: number): Run {
this.properties.push(new Size(size));
this.properties.push(new SizeComplexScript(size));
return this;
}
if (options.size) {
this.properties.push(new Size(options.size));
this.properties.push(new SizeComplexScript(options.size));
}
public rightToLeft(): Run {
this.properties.push(new RightToLeft());
return this;
if (options.rightToLeft) {
this.properties.push(new RightToLeft());
}
if (options.smallCaps) {
this.properties.push(new SmallCaps());
}
if (options.allCaps) {
this.properties.push(new Caps());
}
if (options.strike) {
this.properties.push(new Strike());
}
if (options.doubleStrike) {
this.properties.push(new DoubleStrike());
}
if (options.subScript) {
this.properties.push(new SubScript());
}
if (options.superScript) {
this.properties.push(new SuperScript());
}
if (options.style) {
this.properties.push(new Style(options.style));
}
if (options.font) {
this.properties.push(new RunFonts(options.font.name, options.font.hint));
}
if (options.highlight) {
this.properties.push(new Highlight(options.highlight));
this.properties.push(new HighlightComplexScript(options.highlight));
}
if (options.shading) {
this.properties.push(new Shading(options.shading.type, options.shading.fill, options.shading.color));
this.properties.push(new ShadowComplexScript(options.shading.type, options.shading.fill, options.shading.color));
}
}
public break(): Run {
@ -99,44 +169,4 @@ export class Run extends XmlComponent {
this.root.push(new End());
return this;
}
public smallCaps(): Run {
this.properties.push(new SmallCaps());
return this;
}
public allCaps(): Run {
this.properties.push(new Caps());
return this;
}
public strike(): Run {
this.properties.push(new Strike());
return this;
}
public doubleStrike(): Run {
this.properties.push(new DoubleStrike());
return this;
}
public subScript(): Run {
this.properties.push(new SubScript());
return this;
}
public superScript(): Run {
this.properties.push(new SuperScript());
return this;
}
public font(fontName: string, hint?: string | undefined): Run {
this.properties.push(new RunFonts(fontName, hint));
return this;
}
public style(styleId: string): Run {
this.properties.push(new Style(styleId));
return this;
}
}

View File

@ -4,7 +4,7 @@ import { SequentialIdentifierInstruction } from "./sequential-identifier-instruc
export class SequentialIdentifier extends Run {
constructor(identifier: string) {
super();
super({});
this.root.push(new Begin(true));
this.root.push(new SequentialIdentifierInstruction(identifier));
this.root.push(new Separate());

View File

@ -1,9 +1,19 @@
import { Run } from "../run";
import { IRunOptions, Run } from "./run";
import { Text } from "./run-components/text";
export interface ITextRunOptions extends IRunOptions {
readonly text: string;
}
export class TextRun extends Run {
constructor(text: string) {
super();
this.root.push(new Text(text));
constructor(options: ITextRunOptions | string) {
if (typeof options === "string") {
super({});
this.root.push(new Text(options));
return;
}
super(options);
this.root.push(new Text(options.text));
}
}

View File

@ -1,5 +1,25 @@
import { Attributes, XmlComponent } from "file/xml-components";
export enum UnderlineType {
SINGLE = "single",
WORDS = "words",
DOUBLE = "double",
THICK = "thick",
DOTTED = "dotted",
DOTTEDHEAVY = "dottedHeavy",
DASH = "dash",
DASHEDHEAVY = "dashedHeavy",
DASHLONG = "dashLong",
DASHLONGHEAVY = "dashLongHeavy",
DOTDASH = "dotDash",
DASHDOTHEAVY = "dashDotHeavy",
DOTDOTDASH = "dotDotDash",
DASHDOTDOTHEAVY = "dashDotDotHeavy",
WAVE = "wave",
WAVYHEAVY = "wavyHeavy",
WAVYDOUBLE = "wavyDouble",
}
export abstract class BaseUnderline extends XmlComponent {
constructor(underlineType: string, color?: string) {
super("w:u");

View File

@ -4,6 +4,6 @@ import { XmlComponent } from "file/xml-components";
export class ParagraphPropertiesDefaults extends XmlComponent {
constructor() {
super("w:pPrDefault");
this.root.push(new ParagraphProperties());
this.root.push(new ParagraphProperties({}));
}
}

View File

@ -307,5 +307,51 @@ describe("CharacterStyle", () => {
],
});
});
it("#highlight", () => {
const style = new CharacterStyle("myStyleId").highlight("005599");
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "character", "w:styleId": "myStyleId" } },
{
"w:rPr": [{ "w:highlight": { _attr: { "w:val": "005599" } } }],
},
{
"w:uiPriority": {
_attr: {
"w:val": "99",
},
},
},
{
"w:unhideWhenUsed": EMPTY_OBJECT,
},
],
});
});
it("#shadow", () => {
const style = new CharacterStyle("myStyleId").shadow("pct10", "00FFFF", "FF0000");
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "character", "w:styleId": "myStyleId" } },
{
"w:rPr": [{ "w:shd": { _attr: { "w:val": "pct10", "w:fill": "00FFFF", "w:color": "FF0000" } } }],
},
{
"w:uiPriority": {
_attr: {
"w:val": "99",
},
},
},
{
"w:unhideWhenUsed": EMPTY_OBJECT,
},
],
});
});
});
});

View File

@ -58,4 +58,12 @@ export class CharacterStyle extends Style {
this.root.push(new SemiHidden());
return this;
}
public highlight(color: string): CharacterStyle {
return this.addRunProperty(new formatting.Highlight(color));
}
public shadow(value: string, fill: string, color: string): CharacterStyle {
return this.addRunProperty(new formatting.Shading(value, fill, color));
}
}

View File

@ -166,9 +166,9 @@ describe("ParagraphStyle", () => {
"w:bottom": {
_attr: {
"w:color": "auto",
"w:space": "1",
"w:space": 1,
"w:val": "single",
"w:sz": "6",
"w:sz": 6,
},
},
},
@ -231,12 +231,12 @@ describe("ParagraphStyle", () => {
});
it("#outlineLevel", () => {
const style = new ParagraphStyle("myStyleId").outlineLevel("1");
const style = new ParagraphStyle("myStyleId").outlineLevel(1);
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
{ "w:pPr": [{ "w:outlineLvl": { _attr: { "w:val": "1" } } }] },
{ "w:pPr": [{ "w:outlineLvl": { _attr: { "w:val": 1 } } }] },
],
});
});
@ -375,6 +375,32 @@ describe("ParagraphStyle", () => {
});
});
it("#highlight", () => {
const style = new ParagraphStyle("myStyleId").highlight("005599");
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
{
"w:rPr": [{ "w:highlight": { _attr: { "w:val": "005599" } } }],
},
],
});
});
it("#shadow", () => {
const style = new ParagraphStyle("myStyleId").shadow("pct10", "00FFFF", "FF0000");
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
{
"w:rPr": [{ "w:shd": { _attr: { "w:val": "pct10", "w:fill": "00FFFF", "w:color": "FF0000" } } }],
},
],
});
});
describe("#underline", () => {
it("should set underline to 'single' if no arguments are given", () => {
const style = new ParagraphStyle("myStyleId").underline();

View File

@ -1,6 +1,6 @@
import {
Alignment,
AlignmentOptions,
AlignmentType,
Indent,
ISpacingProperties,
KeepLines,
@ -24,7 +24,7 @@ export class ParagraphStyle extends Style {
constructor(styleId: string, name?: string) {
super({ type: "paragraph", styleId: styleId }, name);
this.paragraphProperties = new ParagraphProperties();
this.paragraphProperties = new ParagraphProperties({});
this.runProperties = new RunProperties();
this.root.push(this.paragraphProperties);
this.root.push(this.runProperties);
@ -35,7 +35,7 @@ export class ParagraphStyle extends Style {
return this;
}
public outlineLevel(level: string): ParagraphStyle {
public outlineLevel(level: number): ParagraphStyle {
this.paragraphProperties.push(new OutlineLevel(level));
return this;
}
@ -114,22 +114,30 @@ export class ParagraphStyle extends Style {
return this.addRunProperty(new formatting.CharacterSpacing(value));
}
public highlight(color: string): ParagraphStyle {
return this.addRunProperty(new formatting.Highlight(color));
}
public shadow(value: string, fill: string, color: string): ParagraphStyle {
return this.addRunProperty(new formatting.Shading(value, fill, color));
}
// --------------------- Paragraph formatting ------------------------ //
public center(): ParagraphStyle {
return this.addParagraphProperty(new Alignment(AlignmentOptions.CENTER));
return this.addParagraphProperty(new Alignment(AlignmentType.CENTER));
}
public left(): ParagraphStyle {
return this.addParagraphProperty(new Alignment(AlignmentOptions.LEFT));
return this.addParagraphProperty(new Alignment(AlignmentType.LEFT));
}
public right(): ParagraphStyle {
return this.addParagraphProperty(new Alignment(AlignmentOptions.RIGHT));
return this.addParagraphProperty(new Alignment(AlignmentType.RIGHT));
}
public justified(): ParagraphStyle {
return this.addParagraphProperty(new Alignment(AlignmentOptions.BOTH));
return this.addParagraphProperty(new Alignment(AlignmentType.BOTH));
}
public thematicBreak(): ParagraphStyle {

View File

@ -16,16 +16,16 @@ export class TableOfContents extends XmlComponent {
const content = new StructuredDocumentTagContent();
const beginParagraph = new Paragraph();
const beginRun = new Run();
const beginParagraph = new Paragraph({});
const beginRun = new Run({});
beginRun.addChildElement(new Begin(true));
beginRun.addChildElement(new FieldInstruction(properties));
beginRun.addChildElement(new Separate());
beginParagraph.addRun(beginRun);
content.addChildElement(beginParagraph);
const endParagraph = new Paragraph();
const endRun = new Run();
const endParagraph = new Paragraph({});
const endRun = new Run({});
endRun.addChildElement(new End());
endParagraph.addRun(endRun);
content.addChildElement(endParagraph);

View File

@ -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;
@ -18,31 +22,21 @@ export class TableCell extends XmlComponent {
this.root.push(this.properties);
}
public addParagraph(content: Paragraph): TableCell {
this.root.push(content);
return this;
}
public add(item: Paragraph | Table): TableCell {
this.root.push(item);
public addTable(content: Table): TableCell {
this.root.push(content);
return this;
}
public prepForXml(): IXmlableObject | undefined {
// Cells must end with a paragraph
if (!(this.root[this.root.length - 1] instanceof Paragraph)) {
this.createParagraph();
const para = new Paragraph({});
this.add(para);
}
return super.prepForXml();
}
public createParagraph(text?: string): Paragraph {
const para = new Paragraph(text);
this.addParagraph(para);
return para;
}
public setVerticalAlign(type: VerticalAlign): TableCell {
this.properties.setVerticalAlign(type);

View File

@ -1,2 +1,3 @@
export * from "./table-properties";
export * from "./table-float-properties";
export * from "./table-layout";

View File

@ -3,6 +3,7 @@ import { expect } from "chai";
import { Formatter } from "export/formatter";
import { WidthType } from "../table-cell";
import { TableLayoutType } from "./table-layout";
import { TableProperties } from "./table-properties";
describe("TableProperties", () => {
@ -35,9 +36,10 @@ describe("TableProperties", () => {
});
});
describe("#setFixedWidthLayout", () => {
describe("#setLayout", () => {
it("sets the table to fixed width layout", () => {
const tp = new TableProperties().setFixedWidthLayout();
const tp = new TableProperties();
tp.setLayout(TableLayoutType.FIXED);
const tree = new Formatter().format(tp);
expect(tree).to.deep.equal({
"w:tblPr": [{ "w:tblLayout": { _attr: { "w:type": "fixed" } } }],

View File

@ -23,9 +23,8 @@ export class TableProperties extends IgnoreIfEmptyXmlComponent {
return this;
}
public setFixedWidthLayout(): TableProperties {
this.root.push(new TableLayout(TableLayoutType.FIXED));
return this;
public setLayout(type: TableLayoutType): void {
this.root.push(new TableLayout(type));
}
public setBorder(): TableProperties {

View File

@ -9,6 +9,7 @@ import { Table } from "./table";
import { RelativeHorizontalPosition, RelativeVerticalPosition, TableAnchorType } from "./table-properties";
import { EMPTY_OBJECT } from "file/xml-components";
import { TableLayoutType } from "./table-properties/table-layout";
const DEFAULT_TABLE_PROPERTIES = {
"w:tblCellMar": [
@ -134,6 +135,22 @@ describe("Table", () => {
],
});
});
it("sets the table to fixed width layout", () => {
const table = new Table({
rows: 1,
columns: 1,
layout: TableLayoutType.FIXED,
});
const tree = new Formatter().format(table);
expect(tree)
.to.have.property("w:tbl")
.which.is.an("array")
.with.has.length.at.least(1);
expect(tree["w:tbl"][0]).to.deep.equal({
"w:tblPr": [DEFAULT_TABLE_PROPERTIES, BORDERS, WIDTHS, { "w:tblLayout": { _attr: { "w:type": "fixed" } } }],
});
});
});
describe("#getRow and Row#getCell", () => {
@ -146,19 +163,19 @@ describe("Table", () => {
table
.getRow(0)
.getCell(0)
.addParagraph(new Paragraph("A1"));
.add(new Paragraph("A1"));
table
.getRow(0)
.getCell(1)
.addParagraph(new Paragraph("B1"));
.add(new Paragraph("B1"));
table
.getRow(1)
.getCell(0)
.addParagraph(new Paragraph("A2"));
.add(new Paragraph("A2"));
table
.getRow(1)
.getCell(1)
.addParagraph(new Paragraph("B2"));
.add(new Paragraph("B2"));
const tree = new Formatter().format(table);
const cell = (c) => ({
"w:tc": [
@ -204,10 +221,10 @@ describe("Table", () => {
rows: 2,
columns: 2,
});
table.getCell(0, 0).addParagraph(new Paragraph("A1"));
table.getCell(0, 1).addParagraph(new Paragraph("B1"));
table.getCell(1, 0).addParagraph(new Paragraph("A2"));
table.getCell(1, 1).addParagraph(new Paragraph("B2"));
table.getCell(0, 0).add(new Paragraph("A1"));
table.getCell(0, 1).add(new Paragraph("B1"));
table.getCell(1, 0).add(new Paragraph("A2"));
table.getCell(1, 1).add(new Paragraph("B2"));
const tree = new Formatter().format(table);
const cell = (c) => ({
"w:tc": [
@ -252,23 +269,6 @@ describe("Table", () => {
// });
// });
describe("#setFixedWidthLayout", () => {
it("sets the table to fixed width layout", () => {
const table = new Table({
rows: 1,
columns: 1,
}).setFixedWidthLayout();
const tree = new Formatter().format(table);
expect(tree)
.to.have.property("w:tbl")
.which.is.an("array")
.with.has.length.at.least(1);
expect(tree["w:tbl"][0]).to.deep.equal({
"w:tblPr": [DEFAULT_TABLE_PROPERTIES, BORDERS, WIDTHS, { "w:tblLayout": { _attr: { "w:type": "fixed" } } }],
});
});
});
describe("Cell", () => {
describe("#prepForXml", () => {
it("inserts a paragraph at the end of the cell if it is empty", () => {
@ -295,7 +295,7 @@ describe("Table", () => {
rows: 1,
columns: 1,
});
parentTable.getCell(0, 0).addTable(
parentTable.getCell(0, 0).add(
new Table({
rows: 1,
columns: 1,
@ -322,7 +322,7 @@ describe("Table", () => {
rows: 1,
columns: 1,
});
parentTable.getCell(0, 0).addParagraph(new Paragraph("Hello"));
parentTable.getCell(0, 0).add(new Paragraph("Hello"));
const tree = new Formatter().format(parentTable);
expect(tree)
.to.have.property("w:tbl")
@ -341,37 +341,6 @@ describe("Table", () => {
});
});
});
describe("#createParagraph", () => {
it("inserts a new paragraph in the cell", () => {
const table = new Table({
rows: 1,
columns: 1,
});
const para = table.getCell(0, 0).createParagraph("Test paragraph");
expect(para).to.be.an.instanceof(Paragraph);
const tree = new Formatter().format(table);
expect(tree)
.to.have.property("w:tbl")
.which.is.an("array");
const row = tree["w:tbl"].find((x) => x["w:tr"]);
expect(row).not.to.be.undefined;
expect(row["w:tr"])
.to.be.an("array")
.which.has.length.at.least(1);
expect(row["w:tr"].find((x) => x["w:tc"])).to.deep.equal({
"w:tc": [
{
"w:p": [
{
"w:r": [{ "w:t": [{ _attr: { "xml:space": "preserve" } }, "Test paragraph"] }],
},
],
},
],
});
});
});
});
describe("#float", () => {

View File

@ -5,6 +5,7 @@ import { TableGrid } from "./grid";
import { TableCell, WidthType } from "./table-cell";
import { TableColumn } from "./table-column";
import { ITableFloatOptions, TableProperties } from "./table-properties";
import { TableLayoutType } from "./table-properties/table-layout";
import { TableRow } from "./table-row";
/*
0-width columns don't get rendered correctly, so we need
@ -30,6 +31,7 @@ export interface ITableOptions {
readonly left?: number;
};
readonly float?: ITableFloatOptions;
readonly layout?: TableLayoutType;
}
export class Table extends XmlComponent {
@ -44,6 +46,7 @@ export class Table extends XmlComponent {
columnWidths = Array<number>(columns).fill(100),
margins: { marginUnitType, top, bottom, right, left } = { marginUnitType: WidthType.AUTO, top: 0, bottom: 0, right: 0, left: 0 },
float,
layout,
}: ITableOptions) {
super("w:tbl");
this.properties = new TableProperties();
@ -73,6 +76,10 @@ export class Table extends XmlComponent {
if (float) {
this.properties.setTableFloatProperties(float);
}
if (layout) {
this.properties.setLayout(layout);
}
}
public getRow(index: number): TableRow {
@ -94,9 +101,4 @@ export class Table extends XmlComponent {
public getCell(row: number, col: number): TableCell {
return this.getRow(row).getCell(col);
}
public setFixedWidthLayout(): Table {
this.properties.setFixedWidthLayout();
return this;
}
}

View File

@ -3,6 +3,7 @@ import { XmlAttributeComponent } from "./default-attributes";
export interface IAttributesProperties {
readonly val?: string | number | boolean;
readonly color?: string;
readonly fill?: string;
readonly space?: string;
readonly sz?: string;
readonly type?: string;
@ -26,6 +27,7 @@ export class Attributes extends XmlAttributeComponent<IAttributesProperties> {
protected readonly xmlKeys = {
val: "w:val",
color: "w:color",
fill: "w:fill",
space: "w:space",
sz: "w:sz",
type: "w:type",