Make Paragraph declaritive

This commit is contained in:
Dolan
2019-06-12 01:03:36 +01:00
parent 5c1a731314
commit cb42c74a8d
33 changed files with 451 additions and 497 deletions

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

@ -31,9 +31,9 @@ describe("ThematicBreak", () => {
const newJson = Utility.jsonify(thematicBreak);
const attributes = {
color: "auto",
space: "1",
space: 1,
val: "single",
sz: "6",
sz: 6,
};
assert.equal(JSON.stringify(newJson.root[0].root[0].root), JSON.stringify(attributes));
});

View File

@ -1,13 +1,30 @@
// 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);
@ -16,48 +33,40 @@ class BorderProperty extends XmlComponent {
}
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

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

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

View File

@ -9,15 +9,9 @@ describe("ParagraphOutlineLevel", () => {
describe("#constructor()", () => {
it("should create an outlineLevel with given value", () => {
outlineLevel = new OutlineLevel("0");
outlineLevel = new OutlineLevel(0);
const newJson = Utility.jsonify(outlineLevel);
assert.equal(newJson.root[0].root.val, "0");
});
it("should create an outlineLevel with blank val", () => {
outlineLevel = new OutlineLevel("");
const newJson = Utility.jsonify(outlineLevel);
assert.equal(newJson.root[0].root.val, "");
});
});
});

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,165 @@ 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 runs?: Run[];
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;
};
}
export class Paragraph extends XmlComponent {
private readonly properties: ParagraphProperties;
constructor(text?: string) {
constructor(options: string | 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;
}
this.properties = new ParagraphProperties({
border: options.border,
});
public get Borders(): Border {
return this.properties.paragraphBorder;
}
this.root.push(this.properties);
public createBorder(): Paragraph {
this.properties.createBorder();
return this;
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.runs) {
for (const run of options.runs) {
this.root.push(run);
}
}
}
public addRun(run: Run): Paragraph {
@ -61,12 +183,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 +190,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 +205,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 {