Export new ColumnBreak class via more generic Break class

This commit is contained in:
askoufis
2021-07-08 17:39:21 +10:00
parent bde4aa4657
commit 439ab8441e
6 changed files with 74 additions and 7 deletions

28
demo/67-column-break.ts Normal file
View File

@ -0,0 +1,28 @@
// Section with 2 columns including a column break
// Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs";
import { Document, Packer, Paragraph, ColumnBreak, TextRun } from "../build";
const doc = new Document({
sections: [
{
properties: {
column: {
space: 708,
count: 2,
},
},
children: [
new Paragraph({ children: [
new TextRun('This text will be in the first column.'),
new ColumnBreak(),
new TextRun('This text will be in the second column.'),
] }),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
});

View File

@ -2,7 +2,7 @@ import { expect } from "chai";
import { Formatter } from "export/formatter";
import { PageBreak, PageBreakBefore } from "./page-break";
import { ColumnBreak, PageBreak, PageBreakBefore } from "./break";
describe("PageBreak", () => {
let pageBreak: PageBreak;
@ -29,6 +29,31 @@ describe("PageBreak", () => {
});
});
describe("ColumnBreak", () => {
let columnBreak: ColumnBreak;
beforeEach(() => {
columnBreak = new ColumnBreak();
});
describe("#constructor()", () => {
it("should create a Column Break with correct attributes", () => {
const tree = new Formatter().format(columnBreak);
expect(tree).to.deep.equal({
"w:r": [
{
"w:br": {
_attr: {
"w:type": "column",
},
},
},
],
});
});
});
});
describe("PageBreakBefore", () => {
it("should create page break before", () => {
const pageBreakBefore = new PageBreakBefore();

View File

@ -2,12 +2,18 @@
import { Attributes, XmlComponent } from "file/xml-components";
import { Run } from "../run";
enum BreakType {
COLUMN = "column",
PAGE = "page",
// textWrapping breaks are the default and already exposed via the "Run" class
}
class Break extends XmlComponent {
constructor() {
constructor(type: BreakType) {
super("w:br");
this.root.push(
new Attributes({
type: "page",
type,
}),
);
}
@ -16,7 +22,14 @@ class Break extends XmlComponent {
export class PageBreak extends Run {
constructor() {
super({});
this.root.push(new Break());
this.root.push(new Break(BreakType.PAGE));
}
}
export class ColumnBreak extends Run {
constructor() {
super({});
this.root.push(new Break(BreakType.COLUMN));
}
}

View File

@ -1,7 +1,7 @@
export * from "./alignment";
export * from "./border";
export * from "./indent";
export * from "./page-break";
export * from "./break";
export * from "./spacing";
export * from "./style";
export * from "./tab-stop";

View File

@ -6,7 +6,7 @@ import { IContext, IXmlableObject, XmlComponent } from "file/xml-components";
import { TargetModeType } from "../relationships/relationship/relationship";
import { DeletedTextRun, InsertedTextRun } from "../track-revision";
import { PageBreak } from "./formatting/page-break";
import { PageBreak, ColumnBreak } from "./formatting/break";
import { Bookmark, ConcreteHyperlink, ExternalHyperlink, InternalHyperlink } from "./links";
import { Math } from "./math";
import { IParagraphPropertiesOptions, ParagraphProperties } from "./properties";
@ -18,6 +18,7 @@ export type ParagraphChild =
| SymbolRun
| Bookmark
| PageBreak
| ColumnBreak
| SequentialIdentifier
| FootnoteReferenceRun
| InternalHyperlink

View File

@ -5,7 +5,7 @@ import { IShadingAttributesProperties, Shading } from "../shading";
import { Alignment, AlignmentType } from "./formatting/alignment";
import { Border, IBordersOptions, ThematicBreak } from "./formatting/border";
import { IIndentAttributesProperties, Indent } from "./formatting/indent";
import { PageBreakBefore } from "./formatting/page-break";
import { PageBreakBefore } from "./formatting/break";
import { ISpacingProperties, Spacing } from "./formatting/spacing";
import { HeadingLevel, Style } from "./formatting/style";
import { LeaderType, TabStop, TabStopPosition, TabStopType } from "./formatting/tab-stop";