Merge pull request #1031 from askoufis/column-break

Export new `ColumnBreak` class via more generic `Break` class
This commit is contained in:
Dolan
2021-07-08 10:10:16 +01:00
committed by GitHub
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 { Formatter } from "export/formatter";
import { PageBreak, PageBreakBefore } from "./page-break"; import { ColumnBreak, PageBreak, PageBreakBefore } from "./break";
describe("PageBreak", () => { describe("PageBreak", () => {
let pageBreak: 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", () => { describe("PageBreakBefore", () => {
it("should create page break before", () => { it("should create page break before", () => {
const pageBreakBefore = new PageBreakBefore(); const pageBreakBefore = new PageBreakBefore();

View File

@ -2,12 +2,18 @@
import { Attributes, XmlComponent } from "file/xml-components"; import { Attributes, XmlComponent } from "file/xml-components";
import { Run } from "../run"; 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 { class Break extends XmlComponent {
constructor() { constructor(type: BreakType) {
super("w:br"); super("w:br");
this.root.push( this.root.push(
new Attributes({ new Attributes({
type: "page", type,
}), }),
); );
} }
@ -16,7 +22,14 @@ class Break extends XmlComponent {
export class PageBreak extends Run { export class PageBreak extends Run {
constructor() { constructor() {
super({}); 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 "./alignment";
export * from "./border"; export * from "./border";
export * from "./indent"; export * from "./indent";
export * from "./page-break"; export * from "./break";
export * from "./spacing"; export * from "./spacing";
export * from "./style"; export * from "./style";
export * from "./tab-stop"; 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 { TargetModeType } from "../relationships/relationship/relationship";
import { DeletedTextRun, InsertedTextRun } from "../track-revision"; import { DeletedTextRun, InsertedTextRun } from "../track-revision";
import { PageBreak } from "./formatting/page-break"; import { ColumnBreak, PageBreak } from "./formatting/break";
import { Bookmark, ConcreteHyperlink, ExternalHyperlink, InternalHyperlink } from "./links"; import { Bookmark, ConcreteHyperlink, ExternalHyperlink, InternalHyperlink } from "./links";
import { Math } from "./math"; import { Math } from "./math";
import { IParagraphPropertiesOptions, ParagraphProperties } from "./properties"; import { IParagraphPropertiesOptions, ParagraphProperties } from "./properties";
@ -18,6 +18,7 @@ export type ParagraphChild =
| SymbolRun | SymbolRun
| Bookmark | Bookmark
| PageBreak | PageBreak
| ColumnBreak
| SequentialIdentifier | SequentialIdentifier
| FootnoteReferenceRun | FootnoteReferenceRun
| InternalHyperlink | InternalHyperlink

View File

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