remove more duplicate classes; add additional values functions; clean up tests

This commit is contained in:
Tom Hunkapiller
2021-05-24 11:28:10 +03:00
parent a56119e7cd
commit ce2a0fb864
38 changed files with 311 additions and 362 deletions

View File

@ -1,8 +1,19 @@
// http://officeopenxml.com/WPtableGrid.php
// <xsd:complexType name="CT_TblGridCol">
// <xsd:attribute name="w" type="s:ST_TwipsMeasure"/>
// </xsd:complexType>
// <xsd:complexType name="CT_TblGridBase">
// <xsd:sequence>
// <xsd:element name="gridCol" type="CT_TblGridCol" minOccurs="0" maxOccurs="unbounded"/>
// </xsd:sequence>
// </xsd:complexType>
import { twipsMeasureValue } from "file/values";
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
export class TableGrid extends XmlComponent {
constructor(widths: number[]) {
constructor(widths: number[] | string[]) {
super("w:tblGrid");
for (const width of widths) {
this.root.push(new GridCol(width));
@ -10,15 +21,15 @@ export class TableGrid extends XmlComponent {
}
}
class GridColAttributes extends XmlAttributeComponent<{ readonly w: number }> {
class GridColAttributes extends XmlAttributeComponent<{ readonly w: number | string }> {
protected readonly xmlKeys = { w: "w:w" };
}
export class GridCol extends XmlComponent {
constructor(width?: number) {
constructor(width?: number | string) {
super("w:gridCol");
if (width !== undefined) {
this.root.push(new GridColAttributes({ w: width }));
this.root.push(new GridColAttributes({ w: twipsMeasureValue(width) }));
}
}
}

View File

@ -129,6 +129,19 @@ export class TableFloatOptionsAttributes extends XmlAttributeComponent<ITableFlo
};
}
// <xsd:complexType name="CT_TblPPr">
// <xsd:attribute name="leftFromText" type="s:ST_TwipsMeasure"/>
// <xsd:attribute name="rightFromText" type="s:ST_TwipsMeasure"/>
// <xsd:attribute name="topFromText" type="s:ST_TwipsMeasure"/>
// <xsd:attribute name="bottomFromText" type="s:ST_TwipsMeasure"/>
// <xsd:attribute name="vertAnchor" type="ST_VAnchor"/>
// <xsd:attribute name="horzAnchor" type="ST_HAnchor"/>
// <xsd:attribute name="tblpXSpec" type="s:ST_XAlign"/>
// <xsd:attribute name="tblpX" type="ST_SignedTwipsMeasure"/>
// <xsd:attribute name="tblpYSpec" type="s:ST_YAlign"/>
// <xsd:attribute name="tblpY" type="ST_SignedTwipsMeasure"/>
// </xsd:complexType>
export class TableFloatProperties extends XmlComponent {
constructor(options: ITableFloatOptions) {
super("w:tblpPr");

View File

@ -9,6 +9,15 @@ class TableLayoutAttributes extends XmlAttributeComponent<{ readonly type: Table
protected readonly xmlKeys = { type: "w:type" };
}
// <xsd:complexType name="CT_TblLayoutType">
// <xsd:attribute name="type" type="ST_TblLayoutType"/>
// </xsd:complexType>
// <xsd:simpleType name="ST_TblLayoutType">
// <xsd:restriction base="xsd:string">
// <xsd:enumeration value="fixed"/>
// <xsd:enumeration value="autofit"/>
// </xsd:restriction>
// </xsd:simpleType>
export class TableLayout extends XmlComponent {
constructor(type: TableLayoutType) {
super("w:tblLayout");

View File

@ -5,6 +5,16 @@ export enum OverlapType {
OVERLAP = "overlap",
}
// <xsd:complexType name="CT_TblOverlap">
// <xsd:attribute name="val" type="ST_TblOverlap" use="required"/>
// </xsd:complexType>
// <xsd:simpleType name="ST_TblOverlap">
// <xsd:restriction base="xsd:string">
// <xsd:enumeration value="never"/>
// <xsd:enumeration value="overlap"/>
// </xsd:restriction>
// </xsd:simpleType>
class TableOverlapAttributes extends XmlAttributeComponent<{ readonly val: OverlapType }> {
protected readonly xmlKeys = { val: "w:val" };
}

View File

@ -21,7 +21,7 @@
// <xsd:element name="tblDescription" type="CT_String" minOccurs="0" maxOccurs="1"/>
// </xsd:sequence>
// </xsd:complexType>
import { IgnoreIfEmptyXmlComponent } from "file/xml-components";
import { IgnoreIfEmptyXmlComponent, OnOffElement, StringValueElement } from "file/xml-components";
import { Alignment, AlignmentType } from "../../paragraph";
import { IShadingAttributesProperties, Shading } from "../../shading";
@ -30,8 +30,6 @@ import { ITableBordersOptions, TableBorders } from "./table-borders";
import { ITableCellMarginOptions, TableCellMargin, TableCellMarginElementType } from "./table-cell-margin";
import { ITableFloatOptions, TableFloatProperties } from "./table-float-properties";
import { TableLayout, TableLayoutType } from "./table-layout";
import { TableStyle } from "./table-style";
import { VisuallyRightToLeft } from "./visually-right-to-left";
export interface ITablePropertiesOptions {
readonly width?: ITableWidthProperties;
@ -51,15 +49,15 @@ export class TableProperties extends IgnoreIfEmptyXmlComponent {
super("w:tblPr");
if (options.style) {
this.root.push(new TableStyle(options.style));
this.root.push(new StringValueElement("w:tblStyle", options.style));
}
if (options.float) {
this.root.push(new TableFloatProperties(options.float));
}
if (options.visuallyRightToLeft) {
this.root.push(new VisuallyRightToLeft());
if (options.visuallyRightToLeft !== undefined) {
this.root.push(new OnOffElement("w:bidiVisual", options.visuallyRightToLeft));
}
if (options.width) {

View File

@ -1,22 +0,0 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { TableStyle } from "./table-style";
describe("TableStyle", () => {
describe("#constructor", () => {
it("should create", () => {
const tableStyle = new TableStyle("test-id");
const tree = new Formatter().format(tableStyle);
expect(tree).to.deep.equal({
"w:tblStyle": {
_attr: {
"w:val": "test-id",
},
},
});
});
});
});

View File

@ -1,13 +0,0 @@
import { Attributes, XmlComponent } from "file/xml-components";
export class TableStyle extends XmlComponent {
constructor(styleId: string) {
super("w:tblStyle");
this.root.push(
new Attributes({
val: styleId,
}),
);
}
}

View File

@ -1,14 +0,0 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { VisuallyRightToLeft } from "./visually-right-to-left";
describe("VisuallyRightToLeft", () => {
it("should create", () => {
const visuallyRightToLeft = new VisuallyRightToLeft();
const tree = new Formatter().format(visuallyRightToLeft);
expect(tree).to.deep.equal({
"w:bidiVisual": {},
});
});
});

View File

@ -1,8 +0,0 @@
// https://c-rex.net/projects/samples/ooxml/e1/Part4/OOXML_P4_DOCX_bidiVisual_topic_ID0EOXIQ.html
import { XmlComponent } from "file/xml-components";
export class VisuallyRightToLeft extends XmlComponent {
constructor() {
super("w:bidiVisual");
}
}

View File

@ -1,5 +1,18 @@
import { twipsMeasureValue } from "file/values";
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
// <xsd:complexType name="CT_Height">
// <xsd:attribute name="val" type="s:ST_TwipsMeasure"/>
// <xsd:attribute name="hRule" type="ST_HeightRule"/>
// </xsd:complexType>
// <xsd:simpleType name="ST_HeightRule">
// <xsd:restriction base="xsd:string">
// <xsd:enumeration value="auto"/>
// <xsd:enumeration value="exact"/>
// <xsd:enumeration value="atLeast"/>
// </xsd:restriction>
// </xsd:simpleType>
export enum HeightRule {
/** Height is determined based on the content, so value is ignored. */
AUTO = "auto",
@ -10,19 +23,19 @@ export enum HeightRule {
}
export class TableRowHeightAttributes extends XmlAttributeComponent<{
readonly value: number;
readonly value: number | string;
readonly rule: HeightRule;
}> {
protected readonly xmlKeys = { value: "w:val", rule: "w:hRule" };
}
export class TableRowHeight extends XmlComponent {
constructor(value: number, rule: HeightRule) {
constructor(value: number | string, rule: HeightRule) {
super("w:trHeight");
this.root.push(
new TableRowHeightAttributes({
value: value,
value: twipsMeasureValue(value),
rule: rule,
}),
);

View File

@ -17,13 +17,13 @@ describe("TableRowProperties", () => {
it("sets cantSplit to avoid row been paginated", () => {
const rowProperties = new TableRowProperties({ cantSplit: true });
const tree = new Formatter().format(rowProperties);
expect(tree).to.deep.equal({ "w:trPr": [{ "w:cantSplit": { _attr: { "w:val": true } } }] });
expect(tree).to.deep.equal({ "w:trPr": [{ "w:cantSplit": {} }] });
});
it("sets row as table header (repeat row on each page of table)", () => {
const rowProperties = new TableRowProperties({ tableHeader: true });
const tree = new Formatter().format(rowProperties);
expect(tree).to.deep.equal({ "w:trPr": [{ "w:tblHeader": { _attr: { "w:val": true } } }] });
expect(tree).to.deep.equal({ "w:trPr": [{ "w:tblHeader": {} }] });
});
it("sets row height exact", () => {

View File

@ -1,5 +1,33 @@
// http://officeopenxml.com/WPtableRowProperties.php
import { IgnoreIfEmptyXmlComponent, XmlAttributeComponent, XmlComponent } from "file/xml-components";
// <xsd:complexType name="CT_TrPrBase">
// <xsd:choice maxOccurs="unbounded">
// <xsd:element name="cnfStyle" type="CT_Cnf" minOccurs="0" maxOccurs="1"/>
// <xsd:element name="divId" type="CT_DecimalNumber" minOccurs="0"/>
// <xsd:element name="gridBefore" type="CT_DecimalNumber" minOccurs="0"/>
// <xsd:element name="gridAfter" type="CT_DecimalNumber" minOccurs="0"/>
// <xsd:element name="wBefore" type="CT_TblWidth" minOccurs="0" maxOccurs="1"/>
// <xsd:element name="wAfter" type="CT_TblWidth" minOccurs="0" maxOccurs="1"/>
// <xsd:element name="cantSplit" type="CT_OnOff" minOccurs="0"/>
// <xsd:element name="trHeight" type="CT_Height" minOccurs="0"/>
// <xsd:element name="tblHeader" type="CT_OnOff" minOccurs="0"/>
// <xsd:element name="tblCellSpacing" type="CT_TblWidth" minOccurs="0" maxOccurs="1"/>
// <xsd:element name="jc" type="CT_JcTable" minOccurs="0" maxOccurs="1"/>
// <xsd:element name="hidden" type="CT_OnOff" minOccurs="0"/>
// </xsd:choice>
// </xsd:complexType>
// <xsd:complexType name="CT_TrPr">
// <xsd:complexContent>
// <xsd:extension base="CT_TrPrBase">
// <xsd:sequence>
// <xsd:element name="ins" type="CT_TrackChange" minOccurs="0"/>
// <xsd:element name="del" type="CT_TrackChange" minOccurs="0"/>
// <xsd:element name="trPrChange" type="CT_TrPrChange" minOccurs="0"/>
// </xsd:sequence>
// </xsd:extension>
// </xsd:complexContent>
// </xsd:complexType>
import { IgnoreIfEmptyXmlComponent, OnOffElement } from "file/xml-components";
import { HeightRule, TableRowHeight } from "./table-row-height";
@ -7,7 +35,7 @@ export interface ITableRowPropertiesOptions {
readonly cantSplit?: boolean;
readonly tableHeader?: boolean;
readonly height?: {
readonly value: number;
readonly value: number | string;
readonly rule: HeightRule;
};
}
@ -16,12 +44,12 @@ export class TableRowProperties extends IgnoreIfEmptyXmlComponent {
constructor(options: ITableRowPropertiesOptions) {
super("w:trPr");
if (options.cantSplit) {
this.root.push(new CantSplit());
if (options.cantSplit !== undefined) {
this.root.push(new OnOffElement("w:cantSplit", options.cantSplit));
}
if (options.tableHeader) {
this.root.push(new TableHeader());
if (options.tableHeader !== undefined) {
this.root.push(new OnOffElement("w:tblHeader", options.tableHeader));
}
if (options.height) {
@ -29,25 +57,3 @@ export class TableRowProperties extends IgnoreIfEmptyXmlComponent {
}
}
}
class CantSplitAttributes extends XmlAttributeComponent<{ readonly val: boolean }> {
protected readonly xmlKeys = { val: "w:val" };
}
export class CantSplit extends XmlComponent {
constructor() {
super("w:cantSplit");
this.root.push(new CantSplitAttributes({ val: true }));
}
}
class TableHeaderAttributes extends XmlAttributeComponent<{ readonly val: boolean }> {
protected readonly xmlKeys = { val: "w:val" };
}
export class TableHeader extends XmlComponent {
constructor() {
super("w:tblHeader");
this.root.push(new TableHeaderAttributes({ val: true }));
}
}

View File

@ -53,11 +53,7 @@ describe("TableRow", () => {
{
"w:trPr": [
{
"w:cantSplit": {
_attr: {
"w:val": true,
},
},
"w:cantSplit": {},
},
],
},
@ -76,11 +72,7 @@ describe("TableRow", () => {
{
"w:trPr": [
{
"w:tblHeader": {
_attr: {
"w:val": true,
},
},
"w:tblHeader": {},
},
],
},
@ -141,11 +133,7 @@ describe("TableRow", () => {
{
"w:trPr": [
{
"w:tblHeader": {
_attr: {
"w:val": true,
},
},
"w:tblHeader": {},
},
],
},

View File

@ -1,4 +1,5 @@
// http://officeopenxml.com/WPtableWidth.php
import { measurementOrPercentValue } from "file/values";
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
// <xsd:simpleType name="ST_TblWidth">
@ -37,6 +38,6 @@ export class TableWidthElement extends XmlComponent {
constructor(name: string, { type = WidthType.AUTO, size }: ITableWidthProperties) {
super(name);
// super("w:tblW");
this.root.push(new TableWidthAttributes({ type: type, size: type === WidthType.PERCENTAGE ? `${size}%` : size }));
this.root.push(new TableWidthAttributes({ type: type, size: measurementOrPercentValue(size) }));
}
}

View File

@ -282,7 +282,7 @@ describe("Table", () => {
"w:tblW": {
_attr: {
"w:type": "pct",
"w:w": "100%",
"w:w": 100,
},
},
},