adds textDirection to table cells
This commit is contained in:
@ -1,7 +1,7 @@
|
|||||||
// Example of how you would create a table and add data to it
|
// Example of how you would create a table and add data to it
|
||||||
// Import from 'docx' rather than '../build' if you install from npm
|
// Import from 'docx' rather than '../build' if you install from npm
|
||||||
import * as fs from "fs";
|
import * as fs from "fs";
|
||||||
import { Document, HeadingLevel, Packer, Paragraph, Table, TableCell, TableRow, VerticalAlign } from "../build";
|
import { Document, HeadingLevel, Packer, Paragraph, Table, TableCell, TableRow, VerticalAlign, TextDirection } from "../build";
|
||||||
|
|
||||||
const doc = new Document();
|
const doc = new Document();
|
||||||
|
|
||||||
@ -17,6 +17,14 @@ const table = new Table({
|
|||||||
children: [new Paragraph({}), new Paragraph({})],
|
children: [new Paragraph({}), new Paragraph({})],
|
||||||
verticalAlign: VerticalAlign.CENTER,
|
verticalAlign: VerticalAlign.CENTER,
|
||||||
}),
|
}),
|
||||||
|
new TableCell({
|
||||||
|
children: [new Paragraph({text: "bottom to top"}), new Paragraph({})],
|
||||||
|
textDirection: TextDirection.BOTTOMTOTOPLEFTTORIGHT
|
||||||
|
}),
|
||||||
|
new TableCell({
|
||||||
|
children: [new Paragraph({text: "top to bottom"}), new Paragraph({})],
|
||||||
|
textDirection: TextDirection.TOPTOBOTTOMRIGHTTOLEFT
|
||||||
|
}),
|
||||||
],
|
],
|
||||||
}),
|
}),
|
||||||
new TableRow({
|
new TableRow({
|
||||||
@ -38,6 +46,22 @@ const table = new Table({
|
|||||||
],
|
],
|
||||||
verticalAlign: VerticalAlign.CENTER,
|
verticalAlign: VerticalAlign.CENTER,
|
||||||
}),
|
}),
|
||||||
|
new TableCell({
|
||||||
|
children: [
|
||||||
|
new Paragraph({
|
||||||
|
text: "Text above should be vertical from bottom to top",
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
verticalAlign: VerticalAlign.CENTER,
|
||||||
|
}),
|
||||||
|
new TableCell({
|
||||||
|
children: [
|
||||||
|
new Paragraph({
|
||||||
|
text: "Text above should be vertical from top to bottom",
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
verticalAlign: VerticalAlign.CENTER,
|
||||||
|
}),
|
||||||
],
|
],
|
||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
|
@ -158,6 +158,31 @@ export class VAlign extends XmlComponent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export enum TextDirection {
|
||||||
|
BOTTOMTOTOPLEFTTORIGHT = "btLr",
|
||||||
|
LEFTTORIGHTTOPTOBOTTOM = "lrTb",
|
||||||
|
TOPTOBOTTOMRIGHTTOLEFT = "tbRl",
|
||||||
|
}
|
||||||
|
|
||||||
|
class TDirectionAttributes extends XmlAttributeComponent<{ readonly val: TextDirection }> {
|
||||||
|
protected readonly xmlKeys = { val: "w:val" };
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Text Direction within a table cell
|
||||||
|
*/
|
||||||
|
export class TDirection extends XmlComponent {
|
||||||
|
constructor(value: TextDirection) {
|
||||||
|
super("w:textDirection");
|
||||||
|
|
||||||
|
this.root.push(
|
||||||
|
new TDirectionAttributes({
|
||||||
|
val: value,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export enum WidthType {
|
export enum WidthType {
|
||||||
/** Auto. */
|
/** Auto. */
|
||||||
AUTO = "auto",
|
AUTO = "auto",
|
||||||
|
@ -6,6 +6,8 @@ import {
|
|||||||
GridSpan,
|
GridSpan,
|
||||||
TableCellBorders,
|
TableCellBorders,
|
||||||
TableCellWidth,
|
TableCellWidth,
|
||||||
|
TDirection,
|
||||||
|
TextDirection,
|
||||||
VAlign,
|
VAlign,
|
||||||
VerticalAlign,
|
VerticalAlign,
|
||||||
VerticalMerge,
|
VerticalMerge,
|
||||||
@ -61,4 +63,10 @@ export class TableCellProperties extends IgnoreIfEmptyXmlComponent {
|
|||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public setTextDirection(type: TextDirection): TableCellProperties {
|
||||||
|
this.root.push(new TDirection(type));
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ import { BorderStyle } from "file/styles";
|
|||||||
|
|
||||||
import { ShadingType } from "../shading";
|
import { ShadingType } from "../shading";
|
||||||
import { TableCell } from "./table-cell";
|
import { TableCell } from "./table-cell";
|
||||||
import { TableCellBorders, TableCellWidth, VerticalAlign, VerticalMergeType, WidthType } from "./table-cell-components";
|
import { TableCellBorders, TableCellWidth, TextDirection, VerticalAlign, VerticalMergeType, WidthType } from "./table-cell-components";
|
||||||
|
|
||||||
describe("TableCellBorders", () => {
|
describe("TableCellBorders", () => {
|
||||||
describe("#prepForXml", () => {
|
describe("#prepForXml", () => {
|
||||||
@ -271,6 +271,34 @@ describe("TableCell", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should create with text direction", () => {
|
||||||
|
const cell = new TableCell({
|
||||||
|
children: [],
|
||||||
|
textDirection: TextDirection.BOTTOMTOTOPLEFTTORIGHT,
|
||||||
|
});
|
||||||
|
|
||||||
|
const tree = new Formatter().format(cell);
|
||||||
|
|
||||||
|
expect(tree).to.deep.equal({
|
||||||
|
"w:tc": [
|
||||||
|
{
|
||||||
|
"w:tcPr": [
|
||||||
|
{
|
||||||
|
"w:textDirection": {
|
||||||
|
_attr: {
|
||||||
|
"w:val": "btLr",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"w:p": {},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it("should create with vertical merge", () => {
|
it("should create with vertical merge", () => {
|
||||||
const cell = new TableCell({
|
const cell = new TableCell({
|
||||||
children: [],
|
children: [],
|
||||||
|
@ -7,13 +7,14 @@ import { File } from "../../file";
|
|||||||
import { ITableShadingAttributesProperties } from "../shading";
|
import { ITableShadingAttributesProperties } from "../shading";
|
||||||
import { Table } from "../table";
|
import { Table } from "../table";
|
||||||
import { ITableCellMarginOptions } from "./cell-margin/table-cell-margins";
|
import { ITableCellMarginOptions } from "./cell-margin/table-cell-margins";
|
||||||
import { VerticalAlign, VerticalMergeType, WidthType } from "./table-cell-components";
|
import { TextDirection, VerticalAlign, VerticalMergeType, WidthType } from "./table-cell-components";
|
||||||
import { TableCellProperties } from "./table-cell-properties";
|
import { TableCellProperties } from "./table-cell-properties";
|
||||||
|
|
||||||
export interface ITableCellOptions {
|
export interface ITableCellOptions {
|
||||||
readonly shading?: ITableShadingAttributesProperties;
|
readonly shading?: ITableShadingAttributesProperties;
|
||||||
readonly margins?: ITableCellMarginOptions;
|
readonly margins?: ITableCellMarginOptions;
|
||||||
readonly verticalAlign?: VerticalAlign;
|
readonly verticalAlign?: VerticalAlign;
|
||||||
|
readonly textDirection?: TextDirection;
|
||||||
readonly verticalMerge?: VerticalMergeType;
|
readonly verticalMerge?: VerticalMergeType;
|
||||||
readonly width?: {
|
readonly width?: {
|
||||||
readonly size: number | string;
|
readonly size: number | string;
|
||||||
@ -63,6 +64,10 @@ export class TableCell extends XmlComponent {
|
|||||||
this.properties.setVerticalAlign(options.verticalAlign);
|
this.properties.setVerticalAlign(options.verticalAlign);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (options.textDirection) {
|
||||||
|
this.properties.setTextDirection(options.textDirection);
|
||||||
|
}
|
||||||
|
|
||||||
if (options.verticalMerge) {
|
if (options.verticalMerge) {
|
||||||
this.properties.addVerticalMerge(options.verticalMerge);
|
this.properties.addVerticalMerge(options.verticalMerge);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user