Merge pull request #460 from dolanmiu/feat/table-alignment

Overlap option for tables
This commit is contained in:
Dolan
2019-11-24 03:35:26 +00:00
committed by GitHub
8 changed files with 79 additions and 3 deletions

View File

@ -3,6 +3,7 @@
import * as fs from "fs";
import {
Document,
OverlapType,
Packer,
Paragraph,
RelativeHorizontalPosition,
@ -43,6 +44,7 @@ const table = new Table({
verticalAnchor: TableAnchorType.MARGIN,
relativeHorizontalPosition: RelativeHorizontalPosition.RIGHT,
relativeVerticalPosition: RelativeVerticalPosition.BOTTOM,
overlap: OverlapType.NEVER,
},
width: {
size: 4535,

View File

@ -1,4 +1,5 @@
// http://officeopenxml.com/WPalignment.php
// http://officeopenxml.com/WPtableAlignment.php
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
export enum AlignmentType {

View File

@ -2,3 +2,4 @@ export * from "./table-properties";
export * from "./table-float-properties";
export * from "./table-layout";
export * from "./table-borders";
export * from "./table-overlap";

View File

@ -3,11 +3,12 @@ import { expect } from "chai";
import { Formatter } from "export/formatter";
import { RelativeHorizontalPosition, RelativeVerticalPosition, TableAnchorType, TableFloatProperties } from "./table-float-properties";
import { OverlapType } from "./table-overlap";
describe("Table Float Properties", () => {
describe("#constructor", () => {
it("should construct a TableFloatProperties with all options", () => {
const tfp = new TableFloatProperties({
const properties = new TableFloatProperties({
horizontalAnchor: TableAnchorType.MARGIN,
verticalAnchor: TableAnchorType.PAGE,
absoluteHorizontalPosition: 10,
@ -19,8 +20,32 @@ describe("Table Float Properties", () => {
leftFromText: 50,
rightFromText: 60,
});
const tree = new Formatter().format(tfp);
expect(tree).to.be.deep.equal(DEFAULT_TFP);
const tree = new Formatter().format(properties);
expect(tree).to.deep.equal(DEFAULT_TFP);
});
it("should add overlap", () => {
const properties = new TableFloatProperties({
overlap: OverlapType.NEVER,
});
const tree = new Formatter().format(properties);
expect(tree).to.deep.equal({
"w:tblpPr": [
{
_attr: {
overlap: "never",
},
},
{
"w:tblOverlap": {
_attr: {
"w:val": "never",
},
},
},
],
});
});
});
});

View File

@ -1,5 +1,7 @@
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
import { OverlapType, TableOverlap } from "./table-overlap";
export enum TableAnchorType {
MARGIN = "margin",
PAGE = "page",
@ -109,6 +111,7 @@ export interface ITableFloatOptions {
* to the right of the table. The value is in twentieths of a point. If omitted, the value is assumed to be zero.
*/
readonly rightFromText?: number;
readonly overlap?: OverlapType;
}
export class TableFloatOptionsAttributes extends XmlAttributeComponent<ITableFloatOptions> {
@ -130,5 +133,9 @@ export class TableFloatProperties extends XmlComponent {
constructor(options: ITableFloatOptions) {
super("w:tblpPr");
this.root.push(new TableFloatOptionsAttributes(options));
if (options.overlap) {
this.root.push(new TableOverlap(options.overlap));
}
}
}

View File

@ -0,0 +1,22 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { OverlapType, TableOverlap } from "./table-overlap";
describe("TableOverlap", () => {
describe("#constructor", () => {
it("sets the width attribute to the value given", () => {
const tableOverlap = new TableOverlap(OverlapType.OVERLAP);
const tree = new Formatter().format(tableOverlap);
expect(tree).to.deep.equal({
"w:tblOverlap": {
_attr: {
"w:val": "overlap",
},
},
});
});
});
});

View File

@ -0,0 +1,17 @@
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
export enum OverlapType {
NEVER = "never",
OVERLAP = "overlap",
}
class TableOverlapAttributes extends XmlAttributeComponent<{ readonly val: OverlapType }> {
protected readonly xmlKeys = { val: "w:val" };
}
export class TableOverlap extends XmlComponent {
constructor(type: OverlapType) {
super("w:tblOverlap");
this.root.push(new TableOverlapAttributes({ val: type }));
}
}

View File

@ -1,3 +1,4 @@
// http://officeopenxml.com/WPtableProperties.php
import { IgnoreIfEmptyXmlComponent } from "file/xml-components";
import { Alignment, AlignmentType } from "../../paragraph";