Merge pull request #734 from dolanmiu/feat/strong-style

Add declarative break()
This commit is contained in:
Dolan
2020-12-24 00:21:07 +00:00
committed by GitHub
7 changed files with 50 additions and 24 deletions

View File

@ -204,7 +204,10 @@ class DocumentCreator {
alignment: AlignmentType.CENTER, alignment: AlignmentType.CENTER,
children: [ children: [
new TextRun(`Mobile: ${phoneNumber} | LinkedIn: ${profileUrl} | Email: ${email}`), new TextRun(`Mobile: ${phoneNumber} | LinkedIn: ${profileUrl} | Email: ${email}`),
new TextRun("Address: 58 Elm Avenue, Kent ME4 6ER, UK").break(), new TextRun({
text: "Address: 58 Elm Avenue, Kent ME4 6ER, UK",
break: 1,
}),
], ],
}); });
} }

View File

@ -80,6 +80,7 @@ doc.addSection({
children: [ children: [
new TextRun("This is a demo "), new TextRun("This is a demo "),
new DeletedTextRun({ new DeletedTextRun({
break: 1,
text: "in order", text: "in order",
color: "red", color: "red",
bold: true, bold: true,
@ -95,7 +96,7 @@ doc.addSection({
id: 2, id: 2,
author: "Firstname Lastname", author: "Firstname Lastname",
date: "2020-10-06T09:00:00Z", date: "2020-10-06T09:00:00Z",
}).break(), }),
new InsertedTextRun({ new InsertedTextRun({
text: "to show how to ", text: "to show how to ",
bold: false, bold: false,

View File

@ -158,6 +158,15 @@ Sometimes you would want to put text underneath another line of text but inside
```ts ```ts
const text = new TextRun({ const text = new TextRun({
text: "break", text: "break",
break: true, break: 1,
});
```
Adding two breaks:
```ts
const text = new TextRun({
text: "break",
break: 2,
}); });
``` ```

View File

@ -239,13 +239,29 @@ describe("Run", () => {
describe("#break()", () => { describe("#break()", () => {
it("it should add break to the run", () => { it("it should add break to the run", () => {
const run = new Run({}); const run = new Run({
run.break(); break: 1,
});
const tree = new Formatter().format(run); const tree = new Formatter().format(run);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:r": [{ "w:br": {} }], "w:r": [{ "w:br": {} }],
}); });
}); });
it("it should add two breaks to the run", () => {
const run = new Run({
break: 2,
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{ "w:br": {} },
{
"w:br": {},
},
],
});
});
}); });
describe("#font()", () => { describe("#font()", () => {

View File

@ -11,6 +11,7 @@ import { Text } from "./run-components/text";
export interface IRunOptions extends IRunPropertiesOptions { export interface IRunOptions extends IRunPropertiesOptions {
readonly children?: (Begin | FieldInstruction | Separate | End | PageNumber | FootnoteReferenceRun | string)[]; readonly children?: (Begin | FieldInstruction | Separate | End | PageNumber | FootnoteReferenceRun | string)[];
readonly break?: number;
readonly text?: string; readonly text?: string;
} }
@ -62,10 +63,11 @@ export class Run extends XmlComponent {
} else if (options.text) { } else if (options.text) {
this.root.push(new Text(options.text)); this.root.push(new Text(options.text));
} }
}
public break(): Run { if (options.break) {
this.root.splice(1, 0, new Break()); for (let i = 0; i < options.break; i++) {
return this; this.root.splice(1, 0, new Break());
}
}
} }
} }

View File

@ -89,11 +89,12 @@ describe("DeletedTextRun", () => {
describe("#break()", () => { describe("#break()", () => {
it("should add a break", () => { it("should add a break", () => {
const deletedTextRun = new DeletedTextRun({ const deletedTextRun = new DeletedTextRun({
break: 1,
children: ["some text"], children: ["some text"],
id: 0, id: 0,
date: "123", date: "123",
author: "Author", author: "Author",
}).break(); });
const tree = new Formatter().format(deletedTextRun); const tree = new Formatter().format(deletedTextRun);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:del": [ "w:del": [

View File

@ -1,6 +1,6 @@
import { XmlComponent } from "file/xml-components"; import { XmlComponent } from "file/xml-components";
import { FootnoteReferenceRun, IRunOptions, IRunPropertiesOptions, RunProperties } from "../../index"; import { IRunOptions, RunProperties } from "../../index";
import { Break } from "../../paragraph/run/break"; import { Break } from "../../paragraph/run/break";
import { Begin, End, Separate } from "../../paragraph/run/field"; import { Begin, End, Separate } from "../../paragraph/run/field";
import { PageNumber } from "../../paragraph/run/run"; import { PageNumber } from "../../paragraph/run/run";
@ -8,10 +8,7 @@ import { ChangeAttributes, IChangedAttributesProperties } from "../track-revisio
import { DeletedNumberOfPages, DeletedNumberOfPagesSection, DeletedPage } from "./deleted-page-number"; import { DeletedNumberOfPages, DeletedNumberOfPagesSection, DeletedPage } from "./deleted-page-number";
import { DeletedText } from "./deleted-text"; import { DeletedText } from "./deleted-text";
interface IDeletedRunOptions extends IRunPropertiesOptions, IChangedAttributesProperties { interface IDeletedRunOptions extends IRunOptions, IChangedAttributesProperties {}
readonly children?: (Begin | Separate | End | PageNumber | FootnoteReferenceRun | string)[];
readonly text?: string;
}
export class DeletedTextRun extends XmlComponent { export class DeletedTextRun extends XmlComponent {
protected readonly deletedTextRunWrapper: DeletedTextRunWrapper; protected readonly deletedTextRunWrapper: DeletedTextRunWrapper;
@ -25,14 +22,9 @@ export class DeletedTextRun extends XmlComponent {
date: options.date, date: options.date,
}), }),
); );
this.deletedTextRunWrapper = new DeletedTextRunWrapper(options as IRunOptions); this.deletedTextRunWrapper = new DeletedTextRunWrapper(options);
this.addChildElement(this.deletedTextRunWrapper); this.addChildElement(this.deletedTextRunWrapper);
} }
public break(): DeletedTextRun {
this.deletedTextRunWrapper.break();
return this;
}
} }
class DeletedTextRunWrapper extends XmlComponent { class DeletedTextRunWrapper extends XmlComponent {
@ -74,9 +66,11 @@ class DeletedTextRunWrapper extends XmlComponent {
} else if (options.text) { } else if (options.text) {
this.root.push(new DeletedText(options.text)); this.root.push(new DeletedText(options.text));
} }
}
public break(): void { if (options.break) {
this.root.splice(1, 0, new Break()); for (let i = 0; i < options.break; i++) {
this.root.splice(1, 0, new Break());
}
}
} }
} }