Merge pull request #26 from felipeochoa/styles

Standardizing formatting methods
This commit is contained in:
Dolan
2017-03-12 21:26:47 +00:00
committed by GitHub
12 changed files with 424 additions and 43 deletions

View File

@ -0,0 +1,15 @@
import { XmlAttributeComponent, XmlComponent } from "../xml-components";
type alignmentOptions = "left" | "center" | "right" | "both";
class AlignmentAttributes extends XmlAttributeComponent<{val: alignmentOptions}> {
protected xmlKeys = {val: "w:val"};
}
export class Alignment extends XmlComponent {
constructor(type: alignmentOptions) {
super("w:jc");
this.root.push(new AlignmentAttributes({val: type}));
}
}

View File

@ -0,0 +1,9 @@
export { Alignment } from "./alignment";
export { ThematicBreak } from "./border";
export { Indent } from "./indent";
export { PageBreak } from "./page-break";
export { ParagraphProperties } from "./properties";
export { ISpacingProperties, Spacing } from "./spacing";
export { Style } from "./style";
export { LeftTabStop, MaxRightTabStop } from "./tab-stop";
export { NumberProperties } from "./unordered-list";

View File

@ -1,7 +1,8 @@
import { Num } from "../../numbering/num";
import { TextRun } from "../run/text-run";
import { Attributes, XmlComponent } from "../xml-components";
import { XmlComponent } from "../xml-components";
import { Alignment } from "./alignment";
import { ThematicBreak } from "./border";
import { Indent } from "./indent";
import { PageBreak } from "./page-break";
@ -11,16 +12,6 @@ import { Style } from "./style";
import { LeftTabStop, MaxRightTabStop } from "./tab-stop";
import { NumberProperties } from "./unordered-list";
class Alignment extends XmlComponent {
constructor(type: string) {
super("w:jc");
this.root.push(new Attributes({
val: type,
}));
}
}
export class Paragraph extends XmlComponent {
private properties: ParagraphProperties;

View File

@ -1,4 +1,4 @@
import { Attributes, XmlComponent } from "../xml-components";
import { XmlAttributeComponent, XmlComponent } from "../xml-components";
class TabStop extends XmlComponent {
@ -8,11 +8,17 @@ class TabStop extends XmlComponent {
}
}
export type tabOptions = "left" | "right";
class TabAttributes extends XmlAttributeComponent<{val: tabOptions, pos: string | number}> {
protected xmlKeys = {val: "w:val", pos: "w:pos"};
}
class Tab extends XmlComponent {
constructor(value: string, position: string | number) {
constructor(value: tabOptions, position: string | number) {
super("w:tab");
this.root.push(new Attributes({
this.root.push(new TabAttributes({
val: value,
pos: position,
}));

View File

@ -1,5 +1,7 @@
import { Attributes, XmlComponent } from "../xml-components";
export { Underline } from "./underline";
export { SubScript, SuperScript } from "./script";
export { RunFonts } from "./run-fonts";
export class Bold extends XmlComponent {

View File

@ -1,10 +1,10 @@
import { Break } from "./break";
import { Caps, SmallCaps } from "./caps";
import { Bold, Italics } from "./formatting";
import { Bold, Color, DoubleStrike, Italics, Size, Strike } from "./formatting";
import { RunProperties } from "./properties";
import { RunFonts } from "./run-fonts";
import { SubScript, SuperScript } from "./script";
import { DoubleStrike, Strike } from "./strike";
import { Style } from "./style";
import { Tab } from "./tab";
import { Underline } from "./underline";
@ -29,8 +29,18 @@ export class Run extends XmlComponent {
return this;
}
public underline(): Run {
this.properties.push(new Underline());
public underline(underlineType?: string, color?: string): Run {
this.properties.push(new Underline(underlineType, color));
return this;
}
public color(color: string): Run {
this.properties.push(new Color(color));
return this;
}
public size(size: number): Run {
this.properties.push(new Size(size));
return this;
}
@ -78,4 +88,9 @@ export class Run extends XmlComponent {
this.properties.push(new RunFonts(fontName));
return this;
}
public style(styleId: string): Run {
this.properties.push(new Style(styleId));
return this;
}
}

View File

@ -1,15 +0,0 @@
import { XmlComponent } from "../xml-components";
export class Strike extends XmlComponent {
constructor() {
super("w:strike");
}
}
export class DoubleStrike extends XmlComponent {
constructor() {
super("w:dstrike");
}
}

13
ts/docx/run/style.ts Normal file
View File

@ -0,0 +1,13 @@
import { XmlAttributeComponent, XmlComponent } from "../xml-components";
class StyleAttributes extends XmlAttributeComponent<{val: string}> {
protected xmlKeys = {val: "w:val"};
}
export class Style extends XmlComponent {
constructor(styleId: string) {
super("w:rStyle");
this.root.push(new StyleAttributes({val: styleId}));
}
}

View File

@ -1,6 +1,4 @@
import { Indent } from "../../docx/paragraph/indent";
import { ParagraphProperties } from "../../docx/paragraph/properties";
import { ISpacingProperties, Spacing } from "../../docx/paragraph/spacing";
import * as paragraph from "../../docx/paragraph/formatting";
import * as formatting from "../../docx/run/formatting";
import { RunProperties } from "../../docx/run/properties";
import { XmlAttributeComponent, XmlComponent } from "../../docx/xml-components";
@ -40,12 +38,12 @@ export class Style extends XmlComponent {
export class ParagraphStyle extends Style {
private paragraphProperties: ParagraphProperties;
private paragraphProperties: paragraph.ParagraphProperties;
private runProperties: RunProperties;
constructor(styleId: string, name?: string) {
super({type: "paragraph", styleId: styleId}, name);
this.paragraphProperties = new ParagraphProperties();
this.paragraphProperties = new paragraph.ParagraphProperties();
this.runProperties = new RunProperties();
this.root.push(this.paragraphProperties);
this.root.push(this.runProperties);
@ -74,6 +72,8 @@ export class ParagraphStyle extends Style {
return this;
}
// ---------- Run formatting ---------------------- //
public size(twips: number): ParagraphStyle {
this.addRunProperty(new formatting.Size(twips));
return this;
@ -89,6 +89,36 @@ export class ParagraphStyle extends Style {
return this;
}
public smallCaps(): ParagraphStyle {
this.addRunProperty(new formatting.SmallCaps());
return this;
}
public allCaps(): ParagraphStyle {
this.addRunProperty(new formatting.Caps());
return this;
}
public strike(): ParagraphStyle {
this.addRunProperty(new formatting.Strike());
return this;
}
public doubleStrike(): ParagraphStyle {
this.addRunProperty(new formatting.DoubleStrike());
return this;
}
public subScript(): ParagraphStyle {
this.addRunProperty(new formatting.SubScript());
return this;
}
public superScript(): ParagraphStyle {
this.addRunProperty(new formatting.SuperScript());
return this;
}
public underline(underlineType?: string, color?: string): ParagraphStyle {
this.addRunProperty(new formatting.Underline(underlineType, color));
return this;
@ -99,13 +129,55 @@ export class ParagraphStyle extends Style {
return this;
}
public indent(left: number, hanging?: number): ParagraphStyle {
this.addParagraphProperty(new Indent(left, hanging));
public font(fontName: string): ParagraphStyle {
this.addRunProperty(new formatting.RunFonts(fontName));
return this;
}
public spacing(params: ISpacingProperties): ParagraphStyle {
this.addParagraphProperty(new Spacing(params));
// --------------------- Paragraph formatting ------------------------ //
public center(): ParagraphStyle {
this.addParagraphProperty(new paragraph.Alignment("center"));
return this;
}
public left(): ParagraphStyle {
this.addParagraphProperty(new paragraph.Alignment("left"));
return this;
}
public right(): ParagraphStyle {
this.addParagraphProperty(new paragraph.Alignment("right"));
return this;
}
public justified(): ParagraphStyle {
this.addParagraphProperty(new paragraph.Alignment("both"));
return this;
}
public thematicBreak(): ParagraphStyle {
this.addParagraphProperty(new paragraph.ThematicBreak());
return this;
}
public maxRightTabStop(): ParagraphStyle {
this.addParagraphProperty(new paragraph.MaxRightTabStop());
return this;
}
public leftTabStop(position: number): ParagraphStyle {
this.addParagraphProperty(new paragraph.LeftTabStop(position));
return this;
}
public indent(left: number, hanging?: number): ParagraphStyle {
this.addParagraphProperty(new paragraph.Indent(left, hanging));
return this;
}
public spacing(params: paragraph.ISpacingProperties): ParagraphStyle {
this.addParagraphProperty(new paragraph.Spacing(params));
return this;
};
}

View File

@ -32,6 +32,26 @@ describe("Run", () => {
const newJson = Utility.jsonify(run);
assert.equal(newJson.root[0].root[0].rootKey, "w:u");
});
it("should default to 'single' and no color", () => {
run.underline();
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{"w:rPr": [{"w:u": [{_attr: {"w:val": "single"}}]}]},
],
});
});
it("should set the style type and color if given", () => {
run.underline("double", "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"}}]}]},
],
});
});
});
describe("#smallCaps()", () => {
@ -97,4 +117,40 @@ describe("Run", () => {
});
});
});
describe("#color", () => {
it("should set the run to the color given", () => {
run.color("001122");
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{"w:rPr": [{"w:color": [{_attr: {"w:val": "001122"}}]}]},
],
});
});
});
describe("#size", () => {
it("should set the run to the given size", () => {
run.size(24);
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{"w:rPr": [{"w:sz": [{_attr: {"w:val": 24}}]}]},
],
});
});
});
describe("#style", () => {
it("should set the style to the given styleId", () => {
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,5 +1,5 @@
import { assert } from "chai";
import { DoubleStrike, Strike } from "../../../docx/run/strike";
import { DoubleStrike, Strike } from "../../../docx/run/formatting";
import { Utility } from "../../utility";
describe("Strike", () => {

View File

@ -212,6 +212,120 @@ describe("ParagraphStyle", () => {
],
});
});
it("#center", () => {
const style = new ParagraphStyle("myStyleId")
.center();
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{_attr: {"w:type": "paragraph", "w:styleId": "myStyleId"}},
{"w:pPr": [
{"w:jc": [{_attr: {"w:val": "center"}}]},
]},
{"w:rPr": []},
],
});
});
it("#left", () => {
const style = new ParagraphStyle("myStyleId")
.left();
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{_attr: {"w:type": "paragraph", "w:styleId": "myStyleId"}},
{"w:pPr": [
{"w:jc": [{_attr: {"w:val": "left"}}]},
]},
{"w:rPr": []},
],
});
});
it("#right", () => {
const style = new ParagraphStyle("myStyleId")
.right();
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{_attr: {"w:type": "paragraph", "w:styleId": "myStyleId"}},
{"w:pPr": [
{"w:jc": [{_attr: {"w:val": "right"}}]},
]},
{"w:rPr": []},
],
});
});
it("#justified", () => {
const style = new ParagraphStyle("myStyleId")
.justified();
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{_attr: {"w:type": "paragraph", "w:styleId": "myStyleId"}},
{"w:pPr": [
{"w:jc": [{_attr: {"w:val": "both"}}]},
]},
{"w:rPr": []},
],
});
});
it("#thematicBreak", () => {
const style = new ParagraphStyle("myStyleId")
.thematicBreak();
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{_attr: {"w:type": "paragraph", "w:styleId": "myStyleId"}},
{"w:pPr": [
{"w:pBdr": [{"w:bottom": [{_attr: {
"w:color": "auto",
"w:space": "1",
"w:val": "single",
"w:sz": "6",
}}]}]},
]},
{"w:rPr": []},
],
});
});
it("#leftTabStop", () => {
const style = new ParagraphStyle("myStyleId")
.leftTabStop(1200);
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{_attr: {"w:type": "paragraph", "w:styleId": "myStyleId"}},
{"w:pPr": [
{"w:tabs": [
{"w:tab": [{_attr: {"w:val": "left", "w:pos": 1200}}]},
]},
]},
{"w:rPr": []},
],
});
});
it("#maxRightTabStop", () => {
const style = new ParagraphStyle("myStyleId")
.maxRightTabStop();
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{_attr: {"w:type": "paragraph", "w:styleId": "myStyleId"}},
{"w:pPr": [
{"w:tabs": [
{"w:tab": [{_attr: {"w:val": "right", "w:pos": 9026}}]},
]},
]},
{"w:rPr": []},
],
});
});
});
describe("formatting methods: run properties", () => {
@ -230,6 +344,109 @@ describe("ParagraphStyle", () => {
});
});
it("#smallCaps", () => {
const style = new ParagraphStyle("myStyleId")
.smallCaps();
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{_attr: {"w:type": "paragraph", "w:styleId": "myStyleId"}},
{"w:pPr": []},
{"w:rPr": [
{"w:smallCaps": [{_attr: {"w:val": true}}]},
]},
],
});
});
it("#allCaps", () => {
const style = new ParagraphStyle("myStyleId")
.allCaps();
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{_attr: {"w:type": "paragraph", "w:styleId": "myStyleId"}},
{"w:pPr": []},
{"w:rPr": [
{"w:caps": [{_attr: {"w:val": true}}]},
]},
],
});
});
it("#strike", () => {
const style = new ParagraphStyle("myStyleId")
.strike();
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{_attr: {"w:type": "paragraph", "w:styleId": "myStyleId"}},
{"w:pPr": []},
{"w:rPr": [
{"w:strike": [{_attr: {"w:val": true}}]},
]},
],
});
});
it("#doubleStrike", () => {
const style = new ParagraphStyle("myStyleId")
.doubleStrike();
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{_attr: {"w:type": "paragraph", "w:styleId": "myStyleId"}},
{"w:pPr": []},
{"w:rPr": [
{"w:dstrike": [{_attr: {"w:val": true}}]},
]},
],
});
});
it("#subScript", () => {
const style = new ParagraphStyle("myStyleId")
.subScript();
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{_attr: {"w:type": "paragraph", "w:styleId": "myStyleId"}},
{"w:pPr": []},
{"w:rPr": [
{"w:vertAlign": [{_attr: {"w:val": "subscript"}}]},
]},
],
});
});
it("#superScript", () => {
const style = new ParagraphStyle("myStyleId")
.superScript();
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{_attr: {"w:type": "paragraph", "w:styleId": "myStyleId"}},
{"w:pPr": []},
{"w:rPr": [
{"w:vertAlign": [{_attr: {"w:val": "superscript"}}]},
]},
],
});
});
it("#font", () => {
const style = new ParagraphStyle("myStyleId")
.font("Times");
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{_attr: {"w:type": "paragraph", "w:styleId": "myStyleId"}},
{"w:pPr": []},
{"w:rPr": [{"w:rFonts": [{_attr: {"w:ascii": "Times", "w:hAnsi": "Times"}}]}]},
],
});
});
it("#bold", () => {
const style = new ParagraphStyle("myStyleId")
.bold();