added border specs, createBorder method, and color attribute to setProperties
This commit is contained in:
@ -10,14 +10,11 @@ let paragraph = new docx.Paragraph("No border!");
|
|||||||
|
|
||||||
doc.addParagraph(paragraph);
|
doc.addParagraph(paragraph);
|
||||||
|
|
||||||
let borderParagraph = new docx.Paragraph("I have a border on all but one side!");
|
let borderParagraph = new docx.Paragraph("I have borders on my top and bottom sides!").createBorder();
|
||||||
console.log(borderParagraph.Borders);
|
|
||||||
borderParagraph.Borders.addTopBorder();
|
borderParagraph.Borders.addTopBorder();
|
||||||
borderParagraph.Borders.addBottomBorder();
|
borderParagraph.Borders.addBottomBorder();
|
||||||
borderParagraph.Borders.addLeftBorder();
|
|
||||||
console.log(borderParagraph.Borders);
|
|
||||||
|
|
||||||
doc.addParagraph(borderParagraph);
|
doc.addParagraph(borderParagraph);
|
||||||
|
|
||||||
let exporter = new docx.LocalPacker(doc);
|
let exporter = new docx.LocalPacker(doc);
|
||||||
exporter.packPdf('My Document');
|
exporter.pack('My Document');
|
@ -2,11 +2,12 @@
|
|||||||
import { Attributes, XmlComponent } from "file/xml-components";
|
import { Attributes, XmlComponent } from "file/xml-components";
|
||||||
|
|
||||||
class BorderProperty extends XmlComponent {
|
class BorderProperty extends XmlComponent {
|
||||||
public setProperties(space: string, value: string, size: string): XmlComponent {
|
public setProperties(color: string, space: string, value: string, size: string): XmlComponent {
|
||||||
const attrs = new Attributes({
|
const attrs = new Attributes({
|
||||||
|
color: color,
|
||||||
space: space,
|
space: space,
|
||||||
val: value,
|
val: value,
|
||||||
sz: size
|
sz: size,
|
||||||
});
|
});
|
||||||
this.root.push(attrs);
|
this.root.push(attrs);
|
||||||
|
|
||||||
@ -19,33 +20,33 @@ export class Border extends XmlComponent {
|
|||||||
super("w:pBdr");
|
super("w:pBdr");
|
||||||
}
|
}
|
||||||
|
|
||||||
public addTopBorder(space: string = "1", value: string = "single", size: string = "6"): XmlComponent {
|
public addTopBorder(color: string = "auto", space: string = "1", value: string = "single", size: string = "6"): XmlComponent {
|
||||||
const top = new BorderProperty("w:top");
|
const top = new BorderProperty("w:top");
|
||||||
top.setProperties(space, value, size);
|
top.setProperties(color, space, value, size);
|
||||||
this.root.push(top);
|
this.root.push(top);
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public addBottomBorder(space: string = "1", value: string = "single", size: string = "6"): XmlComponent {
|
public addBottomBorder(color: string = "auto", space: string = "1", value: string = "single", size: string = "6"): XmlComponent {
|
||||||
const bottom = new BorderProperty("w:bottom");
|
const bottom = new BorderProperty("w:bottom");
|
||||||
bottom.setProperties(space, value, size);
|
bottom.setProperties(color, space, value, size);
|
||||||
this.root.push(bottom);
|
this.root.push(bottom);
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public addLeftBorder(space: string = "1", value: string = "single", size: string = "6"): XmlComponent {
|
public addLeftBorder(color: string = "auto", space: string = "1", value: string = "single", size: string = "6"): XmlComponent {
|
||||||
const left = new BorderProperty("w:left");
|
const left = new BorderProperty("w:left");
|
||||||
left.setProperties(space, value, size);
|
left.setProperties(color, space, value, size);
|
||||||
this.root.push(left);
|
this.root.push(left);
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public addRightBorder(space: string = "1", value: string = "single", size: string = "6"): XmlComponent {
|
public addRightBorder(color: string = "auto", space: string = "1", value: string = "single", size: string = "6"): XmlComponent {
|
||||||
const right = new BorderProperty("w:right");
|
const right = new BorderProperty("w:right");
|
||||||
right.setProperties(space, value, size);
|
right.setProperties(color, space, value, size);
|
||||||
this.root.push(right);
|
this.root.push(right);
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
@ -56,6 +57,7 @@ export class ThematicBreak extends XmlComponent {
|
|||||||
constructor() {
|
constructor() {
|
||||||
super("w:pBdr");
|
super("w:pBdr");
|
||||||
const bottom = new BorderProperty("w:bottom");
|
const bottom = new BorderProperty("w:bottom");
|
||||||
|
bottom.setProperties("auto", "1", "single", "6");
|
||||||
this.root.push(bottom);
|
this.root.push(bottom);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -144,6 +144,51 @@ describe("Paragraph", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("#paragraphBorders()", () => {
|
||||||
|
it("should add a left and right border to a paragraph", () => {
|
||||||
|
paragraph.createBorder();
|
||||||
|
paragraph.Borders.addLeftBorder();
|
||||||
|
paragraph.Borders.addRightBorder();
|
||||||
|
const tree = new Formatter().format(paragraph);
|
||||||
|
expect(tree).to.deep.equal({
|
||||||
|
"w:p": [
|
||||||
|
{
|
||||||
|
"w:pPr": [
|
||||||
|
{
|
||||||
|
"w:pBdr": [
|
||||||
|
{
|
||||||
|
"w:left": [
|
||||||
|
{
|
||||||
|
_attr: {
|
||||||
|
"w:color": "auto",
|
||||||
|
"w:space": "1",
|
||||||
|
"w:sz": "6",
|
||||||
|
"w:val": "single",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"w:right": [
|
||||||
|
{
|
||||||
|
_attr: {
|
||||||
|
"w:color": "auto",
|
||||||
|
"w:space": "1",
|
||||||
|
"w:sz": "6",
|
||||||
|
"w:val": "single",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe("#pageBreak()", () => {
|
describe("#pageBreak()", () => {
|
||||||
it("should add page break to JSON", () => {
|
it("should add page break to JSON", () => {
|
||||||
paragraph.pageBreak();
|
paragraph.pageBreak();
|
||||||
|
@ -6,7 +6,7 @@ import { XmlComponent } from "file/xml-components";
|
|||||||
|
|
||||||
import { Alignment } from "./formatting/alignment";
|
import { Alignment } from "./formatting/alignment";
|
||||||
import { Bidirectional } from "./formatting/bidirectional";
|
import { Bidirectional } from "./formatting/bidirectional";
|
||||||
import { ThematicBreak, Border } from "./formatting/border";
|
import { Border, ThematicBreak } from "./formatting/border";
|
||||||
import { Indent } from "./formatting/indent";
|
import { Indent } from "./formatting/indent";
|
||||||
import { KeepLines, KeepNext } from "./formatting/keep";
|
import { KeepLines, KeepNext } from "./formatting/keep";
|
||||||
import { PageBreak, PageBreakBefore } from "./formatting/page-break";
|
import { PageBreak, PageBreakBefore } from "./formatting/page-break";
|
||||||
@ -34,6 +34,11 @@ export class Paragraph extends XmlComponent {
|
|||||||
return this.properties.paragraphBorder;
|
return this.properties.paragraphBorder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public createBorder(): Paragraph {
|
||||||
|
this.properties.createBorder();
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public addRun(run: Run): Paragraph {
|
public addRun(run: Run): Paragraph {
|
||||||
this.root.push(run);
|
this.root.push(run);
|
||||||
return this;
|
return this;
|
||||||
|
@ -8,6 +8,9 @@ export class ParagraphProperties extends XmlComponent {
|
|||||||
constructor() {
|
constructor() {
|
||||||
super("w:pPr");
|
super("w:pPr");
|
||||||
this.paragraphBorder = new Border();
|
this.paragraphBorder = new Border();
|
||||||
|
}
|
||||||
|
|
||||||
|
public createBorder(): void {
|
||||||
this.push(this.paragraphBorder);
|
this.push(this.paragraphBorder);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user