unify Border type

This commit is contained in:
Tom Hunkapiller
2021-05-23 07:14:00 +03:00
parent 496fcb55fa
commit 5155cdaf45
8 changed files with 116 additions and 243 deletions

44
src/file/border/border.ts Normal file
View File

@ -0,0 +1,44 @@
// Note that the border type is identical in all places,
// regardless of where it's used like paragraph/table/etc.
//
// http://officeopenxml.com/WPborders.php
// http://officeopenxml.com/WPtableBorders.php
// http://officeopenxml.com/WPtableCellProperties-Borders.php
//
// This describes the CT_Border type.
// <xsd:complexType name="CT_Border">
// <xsd:attribute name="val" type="ST_Border" use="required"/>
// <xsd:attribute name="color" type="ST_HexColor" use="optional" default="auto"/>
// <xsd:attribute name="themeColor" type="ST_ThemeColor" use="optional"/>
// <xsd:attribute name="themeTint" type="ST_UcharHexNumber" use="optional"/>
// <xsd:attribute name="themeShade" type="ST_UcharHexNumber" use="optional"/>
// <xsd:attribute name="sz" type="ST_EighthPointMeasure" use="optional"/>
// <xsd:attribute name="space" type="ST_PointMeasure" use="optional" default="0"/>
// <xsd:attribute name="shadow" type="s:ST_OnOff" use="optional"/>
// <xsd:attribute name="frame" type="s:ST_OnOff" use="optional"/>
// </xsd:complexType>
import { BorderStyle } from "file/styles";
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
export interface IBorderOptions {
readonly style: BorderStyle;
readonly color?: string;
readonly size?: number;
readonly space?: number;
}
export class BorderElement extends XmlComponent {
constructor(elementName: string, options: IBorderOptions) {
super(elementName);
this.root.push(new TableBordersAttributes(options));
}
}
class TableBordersAttributes extends XmlAttributeComponent<IBorderOptions> {
protected readonly xmlKeys = {
style: "w:val",
color: "w:color",
size: "w:sz",
space: "w:space",
};
}

1
src/file/border/index.ts Normal file
View File

@ -0,0 +1 @@
export * from "./border";

View File

@ -70,22 +70,22 @@ describe("PageBorders", () => {
expect(tree["w:pgBorders"][0]).to.deep.equal({ _attr: { "w:display": "firstPage", "w:zOrder": "back" } });
expect(tree["w:pgBorders"][1]).to.deep.equal({
"w:top": {
_attr: { "w:color": "001122", "w:size": 10, "w:val": "doubleWave" },
_attr: { "w:color": "001122", "w:sz": 10, "w:val": "doubleWave" },
},
});
expect(tree["w:pgBorders"][2]).to.deep.equal({
"w:right": {
_attr: { "w:color": "223344", "w:size": 20, "w:val": "double" },
_attr: { "w:color": "223344", "w:sz": 20, "w:val": "double" },
},
});
expect(tree["w:pgBorders"][3]).to.deep.equal({
"w:bottom": {
_attr: { "w:color": "556677", "w:size": 30, "w:val": "single" },
_attr: { "w:color": "556677", "w:sz": 30, "w:val": "single" },
},
});
expect(tree["w:pgBorders"][4]).to.deep.equal({
"w:left": {
_attr: { "w:color": "889900", "w:size": 40, "w:val": "dotted" },
_attr: { "w:color": "889900", "w:sz": 40, "w:val": "dotted" },
},
});
});

View File

@ -1,6 +1,6 @@
// http://officeopenxml.com/WPsectionBorders.php
import { BorderStyle } from "file/styles";
import { IgnoreIfEmptyXmlComponent, XmlAttributeComponent, XmlComponent } from "file/xml-components";
import { BorderElement, IBorderOptions } from "file/border";
import { IgnoreIfEmptyXmlComponent, XmlAttributeComponent } from "file/xml-components";
export enum PageBorderDisplay {
ALL_PAGES = "allPages",
@ -24,36 +24,12 @@ export interface IPageBorderAttributes {
readonly zOrder?: PageBorderZOrder;
}
export interface IPageBorderConfiguration {
readonly style?: BorderStyle;
readonly size?: number;
readonly color?: string;
readonly space?: number;
}
export interface IPageBordersOptions {
readonly pageBorders?: IPageBorderAttributes;
readonly pageBorderTop?: IPageBorderConfiguration;
readonly pageBorderRight?: IPageBorderConfiguration;
readonly pageBorderBottom?: IPageBorderConfiguration;
readonly pageBorderLeft?: IPageBorderConfiguration;
}
class PageBordeAttributes extends XmlAttributeComponent<IPageBorderConfiguration> {
protected readonly xmlKeys = {
style: "w:val",
size: "w:size",
color: "w:color",
space: "w:space",
};
}
class PageBorder extends XmlComponent {
constructor(key: string, options: IPageBorderConfiguration) {
super(key);
this.root.push(new PageBordeAttributes(options));
}
readonly pageBorderTop?: IBorderOptions;
readonly pageBorderRight?: IBorderOptions;
readonly pageBorderBottom?: IBorderOptions;
readonly pageBorderLeft?: IBorderOptions;
}
class PageBordersAttributes extends XmlAttributeComponent<IPageBorderAttributes> {
@ -85,16 +61,16 @@ export class PageBorders extends IgnoreIfEmptyXmlComponent {
}
if (options.pageBorderTop) {
this.root.push(new PageBorder("w:top", options.pageBorderTop));
this.root.push(new BorderElement("w:top", options.pageBorderTop));
}
if (options.pageBorderRight) {
this.root.push(new PageBorder("w:right", options.pageBorderRight));
this.root.push(new BorderElement("w:right", options.pageBorderRight));
}
if (options.pageBorderBottom) {
this.root.push(new PageBorder("w:bottom", options.pageBorderBottom));
this.root.push(new BorderElement("w:bottom", options.pageBorderBottom));
}
if (options.pageBorderLeft) {
this.root.push(new PageBorder("w:left", options.pageBorderLeft));
this.root.push(new BorderElement("w:left", options.pageBorderLeft));
}
}
}

View File

@ -1,34 +1,13 @@
import { BorderStyle } from "file/styles";
import { BorderElement, IBorderOptions } from "file/border";
import { IgnoreIfEmptyXmlComponent, XmlAttributeComponent, XmlComponent } from "file/xml-components";
interface ITableCellBorderPropertyOptions {
readonly style: BorderStyle;
readonly size: number;
readonly color: string;
}
class CellBorderAttributes extends XmlAttributeComponent<{
readonly style: BorderStyle;
readonly size: number;
readonly color: string;
}> {
protected readonly xmlKeys = { style: "w:val", size: "w:sz", color: "w:color" };
}
class BaseTableCellBorder extends XmlComponent {
constructor(key: string, options: ITableCellBorderPropertyOptions) {
super(key);
this.root.push(new CellBorderAttributes(options));
}
}
export interface ITableCellBorders {
readonly top?: ITableCellBorderPropertyOptions;
readonly start?: ITableCellBorderPropertyOptions;
readonly left?: ITableCellBorderPropertyOptions;
readonly bottom?: ITableCellBorderPropertyOptions;
readonly end?: ITableCellBorderPropertyOptions;
readonly right?: ITableCellBorderPropertyOptions;
readonly top?: IBorderOptions;
readonly start?: IBorderOptions;
readonly left?: IBorderOptions;
readonly bottom?: IBorderOptions;
readonly end?: IBorderOptions;
readonly right?: IBorderOptions;
}
export class TableCellBorders extends IgnoreIfEmptyXmlComponent {
@ -36,22 +15,22 @@ export class TableCellBorders extends IgnoreIfEmptyXmlComponent {
super("w:tcBorders");
if (options.top) {
this.root.push(new BaseTableCellBorder("w:top", options.top));
this.root.push(new BorderElement("w:top", options.top));
}
if (options.start) {
this.root.push(new BaseTableCellBorder("w:start", options.start));
this.root.push(new BorderElement("w:start", options.start));
}
if (options.left) {
this.root.push(new BaseTableCellBorder("w:left", options.left));
this.root.push(new BorderElement("w:left", options.left));
}
if (options.bottom) {
this.root.push(new BaseTableCellBorder("w:bottom", options.bottom));
this.root.push(new BorderElement("w:bottom", options.bottom));
}
if (options.end) {
this.root.push(new BaseTableCellBorder("w:end", options.end));
this.root.push(new BorderElement("w:end", options.end));
}
if (options.right) {
this.root.push(new BaseTableCellBorder("w:right", options.right));
this.root.push(new BorderElement("w:right", options.right));
}
}
}

View File

@ -18,7 +18,6 @@ describe("TableBorders", () => {
"w:top": {
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
@ -28,7 +27,6 @@ describe("TableBorders", () => {
"w:left": {
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
@ -38,7 +36,6 @@ describe("TableBorders", () => {
"w:bottom": {
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
@ -48,7 +45,6 @@ describe("TableBorders", () => {
"w:right": {
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
@ -58,7 +54,6 @@ describe("TableBorders", () => {
"w:insideH": {
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
@ -68,7 +63,6 @@ describe("TableBorders", () => {
"w:insideV": {
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
@ -96,7 +90,6 @@ describe("TableBorders", () => {
"w:top": {
_attr: {
"w:color": "red",
"w:space": 0,
"w:sz": 1,
"w:val": "double",
},
@ -106,7 +99,6 @@ describe("TableBorders", () => {
"w:left": {
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
@ -116,7 +108,6 @@ describe("TableBorders", () => {
"w:bottom": {
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
@ -126,7 +117,6 @@ describe("TableBorders", () => {
"w:right": {
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
@ -136,7 +126,6 @@ describe("TableBorders", () => {
"w:insideH": {
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
@ -146,7 +135,6 @@ describe("TableBorders", () => {
"w:insideV": {
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
@ -174,7 +162,6 @@ describe("TableBorders", () => {
"w:top": {
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
@ -184,7 +171,6 @@ describe("TableBorders", () => {
"w:left": {
_attr: {
"w:color": "red",
"w:space": 0,
"w:sz": 1,
"w:val": "double",
},
@ -194,7 +180,6 @@ describe("TableBorders", () => {
"w:bottom": {
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
@ -204,7 +189,6 @@ describe("TableBorders", () => {
"w:right": {
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
@ -214,7 +198,6 @@ describe("TableBorders", () => {
"w:insideH": {
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
@ -224,7 +207,6 @@ describe("TableBorders", () => {
"w:insideV": {
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
@ -252,7 +234,6 @@ describe("TableBorders", () => {
"w:top": {
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
@ -262,7 +243,6 @@ describe("TableBorders", () => {
"w:left": {
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
@ -272,7 +252,6 @@ describe("TableBorders", () => {
"w:bottom": {
_attr: {
"w:color": "red",
"w:space": 0,
"w:sz": 1,
"w:val": "double",
},
@ -282,7 +261,6 @@ describe("TableBorders", () => {
"w:right": {
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
@ -292,7 +270,6 @@ describe("TableBorders", () => {
"w:insideH": {
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
@ -302,7 +279,6 @@ describe("TableBorders", () => {
"w:insideV": {
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
@ -330,7 +306,6 @@ describe("TableBorders", () => {
"w:top": {
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
@ -340,7 +315,6 @@ describe("TableBorders", () => {
"w:left": {
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
@ -350,7 +324,6 @@ describe("TableBorders", () => {
"w:bottom": {
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
@ -360,7 +333,6 @@ describe("TableBorders", () => {
"w:right": {
_attr: {
"w:color": "red",
"w:space": 0,
"w:sz": 1,
"w:val": "double",
},
@ -370,7 +342,6 @@ describe("TableBorders", () => {
"w:insideH": {
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
@ -380,7 +351,6 @@ describe("TableBorders", () => {
"w:insideV": {
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
@ -408,7 +378,6 @@ describe("TableBorders", () => {
"w:top": {
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
@ -418,7 +387,6 @@ describe("TableBorders", () => {
"w:left": {
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
@ -428,7 +396,6 @@ describe("TableBorders", () => {
"w:bottom": {
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
@ -438,7 +405,6 @@ describe("TableBorders", () => {
"w:right": {
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
@ -448,7 +414,6 @@ describe("TableBorders", () => {
"w:insideH": {
_attr: {
"w:color": "red",
"w:space": 0,
"w:sz": 1,
"w:val": "double",
},
@ -458,7 +423,6 @@ describe("TableBorders", () => {
"w:insideV": {
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
@ -486,7 +450,6 @@ describe("TableBorders", () => {
"w:top": {
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
@ -496,7 +459,6 @@ describe("TableBorders", () => {
"w:left": {
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
@ -506,7 +468,6 @@ describe("TableBorders", () => {
"w:bottom": {
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
@ -516,7 +477,6 @@ describe("TableBorders", () => {
"w:right": {
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
@ -526,7 +486,6 @@ describe("TableBorders", () => {
"w:insideH": {
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
@ -536,7 +495,6 @@ describe("TableBorders", () => {
"w:insideV": {
_attr: {
"w:color": "red",
"w:space": 0,
"w:sz": 1,
"w:val": "double",
},
@ -558,7 +516,6 @@ describe("TableBorders", () => {
"w:top": {
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 0,
"w:val": "none",
},
@ -568,7 +525,6 @@ describe("TableBorders", () => {
"w:left": {
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 0,
"w:val": "none",
},
@ -578,7 +534,6 @@ describe("TableBorders", () => {
"w:bottom": {
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 0,
"w:val": "none",
},
@ -588,7 +543,6 @@ describe("TableBorders", () => {
"w:right": {
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 0,
"w:val": "none",
},
@ -598,7 +552,6 @@ describe("TableBorders", () => {
"w:insideH": {
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 0,
"w:val": "none",
},
@ -608,7 +561,6 @@ describe("TableBorders", () => {
"w:insideV": {
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 0,
"w:val": "none",
},

View File

@ -1,155 +1,76 @@
// http://officeopenxml.com/WPtableBorders.php
import { BorderElement, IBorderOptions } from "file/border";
import { BorderStyle } from "file/styles";
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
import { XmlComponent } from "file/xml-components";
export interface ITableBordersOptions {
readonly top?: {
readonly style: BorderStyle;
readonly size: number;
readonly color: string;
};
readonly bottom?: {
readonly style: BorderStyle;
readonly size: number;
readonly color: string;
};
readonly left?: {
readonly style: BorderStyle;
readonly size: number;
readonly color: string;
};
readonly right?: {
readonly style: BorderStyle;
readonly size: number;
readonly color: string;
};
readonly insideHorizontal?: {
readonly style: BorderStyle;
readonly size: number;
readonly color: string;
};
readonly insideVertical?: {
readonly style: BorderStyle;
readonly size: number;
readonly color: string;
};
readonly top?: IBorderOptions;
readonly bottom?: IBorderOptions;
readonly left?: IBorderOptions;
readonly right?: IBorderOptions;
readonly insideHorizontal?: IBorderOptions;
readonly insideVertical?: IBorderOptions;
}
const NONE_BORDER = {
style: BorderStyle.NONE,
size: 0,
color: "auto",
};
const DEFAULT_BORDER = {
style: BorderStyle.SINGLE,
size: 4,
color: "auto",
};
export class TableBorders extends XmlComponent {
public static readonly NONE = {
top: {
style: BorderStyle.NONE,
size: 0,
color: "auto",
},
bottom: {
style: BorderStyle.NONE,
size: 0,
color: "auto",
},
left: {
style: BorderStyle.NONE,
size: 0,
color: "auto",
},
right: {
style: BorderStyle.NONE,
size: 0,
color: "auto",
},
insideHorizontal: {
style: BorderStyle.NONE,
size: 0,
color: "auto",
},
insideVertical: {
style: BorderStyle.NONE,
size: 0,
color: "auto",
},
top: NONE_BORDER,
bottom: NONE_BORDER,
left: NONE_BORDER,
right: NONE_BORDER,
insideHorizontal: NONE_BORDER,
insideVertical: NONE_BORDER,
};
constructor(options: ITableBordersOptions) {
super("w:tblBorders");
if (options.top) {
this.root.push(new TableBordersElement("w:top", options.top.style, options.top.size, 0, options.top.color));
this.root.push(new BorderElement("w:top", options.top));
} else {
this.root.push(new TableBordersElement("w:top", BorderStyle.SINGLE, 4, 0, "auto"));
this.root.push(new BorderElement("w:top", DEFAULT_BORDER));
}
if (options.left) {
this.root.push(new TableBordersElement("w:left", options.left.style, options.left.size, 0, options.left.color));
this.root.push(new BorderElement("w:left", options.left));
} else {
this.root.push(new TableBordersElement("w:left", BorderStyle.SINGLE, 4, 0, "auto"));
this.root.push(new BorderElement("w:left", DEFAULT_BORDER));
}
if (options.bottom) {
this.root.push(new TableBordersElement("w:bottom", options.bottom.style, options.bottom.size, 0, options.bottom.color));
this.root.push(new BorderElement("w:bottom", options.bottom));
} else {
this.root.push(new TableBordersElement("w:bottom", BorderStyle.SINGLE, 4, 0, "auto"));
this.root.push(new BorderElement("w:bottom", DEFAULT_BORDER));
}
if (options.right) {
this.root.push(new TableBordersElement("w:right", options.right.style, options.right.size, 0, options.right.color));
this.root.push(new BorderElement("w:right", options.right));
} else {
this.root.push(new TableBordersElement("w:right", BorderStyle.SINGLE, 4, 0, "auto"));
this.root.push(new BorderElement("w:right", DEFAULT_BORDER));
}
if (options.insideHorizontal) {
this.root.push(
new TableBordersElement(
"w:insideH",
options.insideHorizontal.style,
options.insideHorizontal.size,
0,
options.insideHorizontal.color,
),
);
this.root.push(new BorderElement("w:insideH", options.insideHorizontal));
} else {
this.root.push(new TableBordersElement("w:insideH", BorderStyle.SINGLE, 4, 0, "auto"));
this.root.push(new BorderElement("w:insideH", DEFAULT_BORDER));
}
if (options.insideVertical) {
this.root.push(
new TableBordersElement(
"w:insideV",
options.insideVertical.style,
options.insideVertical.size,
0,
options.insideVertical.color,
),
);
this.root.push(new BorderElement("w:insideV", options.insideVertical));
} else {
this.root.push(new TableBordersElement("w:insideV", BorderStyle.SINGLE, 4, 0, "auto"));
this.root.push(new BorderElement("w:insideV", DEFAULT_BORDER));
}
}
}
class TableBordersElement extends XmlComponent {
constructor(elementName: string, value: string, size: number, space: number, color: string) {
super(elementName);
this.root.push(
new TableBordersAttributes({
value,
size,
space,
color,
}),
);
}
}
class TableBordersAttributes extends XmlAttributeComponent<{
readonly value: string;
readonly size: number;
readonly space: number;
readonly color: string;
}> {
protected readonly xmlKeys = {
value: "w:val",
size: "w:sz",
space: "w:space",
color: "w:color",
};
}

View File

@ -51,12 +51,12 @@ const DEFAULT_TABLE_PROPERTIES = {
const BORDERS = {
"w:tblBorders": [
{ "w:top": { _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } } },
{ "w:left": { _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } } },
{ "w:bottom": { _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } } },
{ "w:right": { _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } } },
{ "w:insideH": { _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } } },
{ "w:insideV": { _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } } },
{ "w:top": { _attr: { "w:val": "single", "w:sz": 4, "w:color": "auto" } } },
{ "w:left": { _attr: { "w:val": "single", "w:sz": 4, "w:color": "auto" } } },
{ "w:bottom": { _attr: { "w:val": "single", "w:sz": 4, "w:color": "auto" } } },
{ "w:right": { _attr: { "w:val": "single", "w:sz": 4, "w:color": "auto" } } },
{ "w:insideH": { _attr: { "w:val": "single", "w:sz": 4, "w:color": "auto" } } },
{ "w:insideV": { _attr: { "w:val": "single", "w:sz": 4, "w:color": "auto" } } },
],
};