Add shading to paragraph

This commit is contained in:
Dolan
2020-10-28 01:05:31 +00:00
parent ed52ef358b
commit 60eb686d05
8 changed files with 124 additions and 22 deletions

View File

@ -28,6 +28,18 @@ doc.addSection({
}), }),
], ],
}), }),
new Paragraph({
shading: {
type: ShadingType.DIAGONAL_CROSS,
color: "00FFFF",
fill: "FF0000",
},
children: [
new TextRun({
text: "Hello World for entire paragraph",
}),
],
}),
], ],
}), }),
}, },

View File

@ -142,6 +142,21 @@ const paragraph = new Paragraph({
}); });
``` ```
## Shading
Add color to an entire paragraph block
```ts
const paragraph = new Paragraph({
text: "shading",
shading: {
type: ShadingType.REVERSE_DIAGONAL_STRIPE,
color: "00FFFF",
fill: "FF0000",
},
});
```
## Spacing ## Spacing
Adding spacing between paragraphs Adding spacing between paragraphs

View File

@ -77,40 +77,78 @@ const text = new TextRun({
}); });
``` ```
### Shading and Highlighting
```ts
const text = new TextRun({
text: "shading",
shading: {
type: ShadingType.REVERSE_DIAGONAL_STRIPE,
color: "00FFFF",
fill: "FF0000",
},
});
```
```ts
const text = new TextRun({
text: "highlighting",
highlight: "yellow",
});
```
### Strike through ### Strike through
```ts ```ts
text.strike(); const text = new TextRun({
text: "strike",
strike: true,
});
``` ```
### Double strike through ### Double strike through
```ts ```ts
text.doubleStrike(); const text = new TextRun({
text: "doubleStrike",
doubleStrike: true,
});
``` ```
### Superscript ### Superscript
```ts ```ts
text.superScript(); const text = new TextRun({
text: "superScript",
superScript: true,
});
``` ```
### Subscript ### Subscript
```ts ```ts
text.subScript(); const text = new TextRun({
text: "subScript",
subScript: true,
});
``` ```
### All Capitals ### All Capitals
```ts ```ts
text.allCaps(); const text = new TextRun({
text: "allCaps",
allCaps: true,
});
``` ```
### Small Capitals ### Small Capitals
```ts ```ts
text.smallCaps(); const text = new TextRun({
text: "smallCaps",
smallCaps: true,
});
``` ```
## Break ## Break
@ -118,13 +156,8 @@ text.smallCaps();
Sometimes you would want to put text underneath another line of text but inside the same paragraph. Sometimes you would want to put text underneath another line of text but inside the same paragraph.
```ts ```ts
text.break(); const text = new TextRun({
``` text: "break",
break: true,
## Chaining });
What if you want to create a paragraph which is **_bold_** and **_italic_**?
```ts
paragraph.bold().italics();
``` ```

View File

@ -12,19 +12,19 @@ describe("DocumentBackground", () => {
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:background": { "w:background": {
_attr: { _attr: {
"w:color": "auto", "w:color": "FFFFFF",
}, },
}, },
}); });
}); });
it("should create a DocumentBackground with no options and set color to value", () => { it("should create a DocumentBackground with no options and set color to value", () => {
const documentBackground = new DocumentBackground({ color: "ffffff" }); const documentBackground = new DocumentBackground({ color: "ffff00" });
const tree = new Formatter().format(documentBackground); const tree = new Formatter().format(documentBackground);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:background": { "w:background": {
_attr: { _attr: {
"w:color": "ffffff", "w:color": "ffff00",
}, },
}, },
}); });
@ -32,7 +32,7 @@ describe("DocumentBackground", () => {
it("should create a DocumentBackground with no options and set other values", () => { it("should create a DocumentBackground with no options and set other values", () => {
const documentBackground = new DocumentBackground({ const documentBackground = new DocumentBackground({
color: "ffffff", color: "ffff00",
themeColor: "test", themeColor: "test",
themeShade: "test", themeShade: "test",
themeTint: "test", themeTint: "test",
@ -41,7 +41,7 @@ describe("DocumentBackground", () => {
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:background": { "w:background": {
_attr: { _attr: {
"w:color": "ffffff", "w:color": "ffff00",
"w:themeColor": "test", "w:themeColor": "test",
"w:themeShade": "test", "w:themeShade": "test",
"w:themeTint": "test", "w:themeTint": "test",

View File

@ -29,7 +29,7 @@ export class DocumentBackground extends XmlComponent {
this.root.push( this.root.push(
new DocumentBackgroundAttributes({ new DocumentBackgroundAttributes({
color: options.color ? options.color : "auto", color: options.color ? options.color : "FFFFFF",
themeColor: options.themeColor, themeColor: options.themeColor,
themeShade: options.themeShade, themeShade: options.themeShade,
themeTint: options.themeTint, themeTint: options.themeTint,

View File

@ -41,7 +41,7 @@ describe("Document", () => {
{ {
"w:background": { "w:background": {
_attr: { _attr: {
"w:color": "auto", "w:color": "FFFFFF",
}, },
}, },
}, },

View File

@ -9,6 +9,7 @@ import { File } from "../file";
import { AlignmentType, HeadingLevel, LeaderType, PageBreak, TabStopPosition, TabStopType } from "./formatting"; import { AlignmentType, HeadingLevel, LeaderType, PageBreak, TabStopPosition, TabStopType } from "./formatting";
import { Bookmark, HyperlinkRef } from "./links"; import { Bookmark, HyperlinkRef } from "./links";
import { Paragraph } from "./paragraph"; import { Paragraph } from "./paragraph";
import { ShadingType } from "../table/shading";
describe("Paragraph", () => { describe("Paragraph", () => {
describe("#constructor()", () => { describe("#constructor()", () => {
@ -761,6 +762,36 @@ describe("Paragraph", () => {
}); });
}); });
describe("#shading", () => {
it("should set paragraph outline level to the given value", () => {
const paragraph = new Paragraph({
shading: {
type: ShadingType.REVERSE_DIAGONAL_STRIPE,
color: "00FFFF",
fill: "FF0000",
},
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
{
"w:pPr": [
{
"w:shd": {
_attr: {
"w:color": "00FFFF",
"w:fill": "FF0000",
"w:val": "reverseDiagStripe",
},
},
},
],
},
],
});
});
});
describe("#prepForXml", () => { describe("#prepForXml", () => {
it("should set paragraph outline level to the given value", () => { it("should set paragraph outline level to the given value", () => {
const paragraph = new Paragraph({ const paragraph = new Paragraph({

View File

@ -1,5 +1,6 @@
// http://officeopenxml.com/WPparagraphProperties.php // http://officeopenxml.com/WPparagraphProperties.php
import { IgnoreIfEmptyXmlComponent, XmlComponent } from "file/xml-components"; import { IgnoreIfEmptyXmlComponent, XmlComponent } from "file/xml-components";
import { ShadingType } from "../table/shading";
import { Alignment, AlignmentType } from "./formatting/alignment"; import { Alignment, AlignmentType } from "./formatting/alignment";
import { Bidirectional } from "./formatting/bidirectional"; import { Bidirectional } from "./formatting/bidirectional";
import { Border, IBorderOptions, ThematicBreak } from "./formatting/border"; import { Border, IBorderOptions, ThematicBreak } from "./formatting/border";
@ -11,6 +12,7 @@ import { HeadingLevel, Style } from "./formatting/style";
import { LeaderType, TabStop, TabStopPosition, TabStopType } from "./formatting/tab-stop"; import { LeaderType, TabStop, TabStopPosition, TabStopType } from "./formatting/tab-stop";
import { NumberProperties } from "./formatting/unordered-list"; import { NumberProperties } from "./formatting/unordered-list";
import { OutlineLevel } from "./links"; import { OutlineLevel } from "./links";
import { Shading } from "./run/formatting";
export interface IParagraphStylePropertiesOptions { export interface IParagraphStylePropertiesOptions {
readonly alignment?: AlignmentType; readonly alignment?: AlignmentType;
@ -44,6 +46,11 @@ export interface IParagraphPropertiesOptions extends IParagraphStylePropertiesOp
readonly level: number; readonly level: number;
readonly custom?: boolean; readonly custom?: boolean;
}; };
readonly shading?: {
readonly type: ShadingType;
readonly fill: string;
readonly color: string;
};
} }
export class ParagraphProperties extends IgnoreIfEmptyXmlComponent { export class ParagraphProperties extends IgnoreIfEmptyXmlComponent {
@ -131,6 +138,10 @@ export class ParagraphProperties extends IgnoreIfEmptyXmlComponent {
if (options.leftTabStop) { if (options.leftTabStop) {
this.push(new TabStop(TabStopType.LEFT, options.leftTabStop)); this.push(new TabStop(TabStopType.LEFT, options.leftTabStop));
} }
if (options.shading) {
this.push(new Shading(options.shading.type, options.shading.fill, options.shading.color));
}
} }
public push(item: XmlComponent): void { public push(item: XmlComponent): void {