Merge pull request #1031 from askoufis/column-break
Export new `ColumnBreak` class via more generic `Break` class
This commit is contained in:
28
demo/67-column-break.ts
Normal file
28
demo/67-column-break.ts
Normal 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);
|
||||
});
|
@ -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();
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
@ -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";
|
||||
|
@ -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 { ColumnBreak, PageBreak } 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
|
||||
|
@ -4,8 +4,8 @@ import { DocumentWrapper } from "../document-wrapper";
|
||||
import { IShadingAttributesProperties, Shading } from "../shading";
|
||||
import { Alignment, AlignmentType } from "./formatting/alignment";
|
||||
import { Border, IBordersOptions, ThematicBreak } from "./formatting/border";
|
||||
import { PageBreakBefore } from "./formatting/break";
|
||||
import { IIndentAttributesProperties, Indent } from "./formatting/indent";
|
||||
import { PageBreakBefore } from "./formatting/page-break";
|
||||
import { ISpacingProperties, Spacing } from "./formatting/spacing";
|
||||
import { HeadingLevel, Style } from "./formatting/style";
|
||||
import { LeaderType, TabStop, TabStopPosition, TabStopType } from "./formatting/tab-stop";
|
||||
|
Reference in New Issue
Block a user