further clean up border code; update paragraph borders to use shared type; BREAKING: paragraph border value attr renamed to style, to match other usages of borders

This commit is contained in:
Tom Hunkapiller
2021-05-23 08:00:49 +03:00
parent 54ab55b92c
commit 08bd2744b6
18 changed files with 100 additions and 115 deletions

View File

@ -1,7 +1,7 @@
// Creates two paragraphs, one with a border and one without
// Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs";
import { Document, Packer, Paragraph } from "../build";
import { BorderStyle, Document, Packer, Paragraph } from "../build";
const doc = new Document({
sections: [
@ -14,13 +14,13 @@ const doc = new Document({
top: {
color: "auto",
space: 1,
value: "single",
style: BorderStyle.SINGLE,
size: 6,
},
bottom: {
color: "auto",
space: 1,
value: "single",
style: BorderStyle.SINGLE,
size: 6,
},
},

View File

@ -1,7 +1,16 @@
// Text Frame (Text Box) example
// Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs";
import { Document, FrameAnchorType, HorizontalPositionAlign, Packer, Paragraph, TextRun, VerticalPositionAlign } from "../build";
import {
BorderStyle,
Document,
FrameAnchorType,
HorizontalPositionAlign,
Packer,
Paragraph,
TextRun,
VerticalPositionAlign,
} from "../build";
const doc = new Document({
sections: [
@ -29,25 +38,25 @@ const doc = new Document({
top: {
color: "auto",
space: 1,
value: "single",
style: BorderStyle.SINGLE,
size: 6,
},
bottom: {
color: "auto",
space: 1,
value: "single",
style: BorderStyle.SINGLE,
size: 6,
},
left: {
color: "auto",
space: 1,
value: "single",
style: BorderStyle.SINGLE,
size: 6,
},
right: {
color: "auto",
space: 1,
value: "single",
style: BorderStyle.SINGLE,
size: 6,
},
},

View File

@ -1,9 +1,11 @@
// Note that the border type is identical in all places,
// regardless of where it's used like paragraph/table/etc.
// PageBorders are a superset, but we're not using any of those extras.
//
// http://officeopenxml.com/WPborders.php
// http://officeopenxml.com/WPtableBorders.php
// http://officeopenxml.com/WPtableCellProperties-Borders.php
// http://officeopenxml.com/WPsectionBorders.php
//
// This describes the CT_Border type.
// <xsd:complexType name="CT_Border">
@ -17,7 +19,6 @@
// <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 {
@ -42,3 +43,33 @@ class TableBordersAttributes extends XmlAttributeComponent<IBorderOptions> {
space: "w:space",
};
}
export enum BorderStyle {
SINGLE = "single",
DASH_DOT_STROKED = "dashDotStroked",
DASHED = "dashed",
DASH_SMALL_GAP = "dashSmallGap",
DOT_DASH = "dotDash",
DOT_DOT_DASH = "dotDotDash",
DOTTED = "dotted",
DOUBLE = "double",
DOUBLE_WAVE = "doubleWave",
INSET = "inset",
NIL = "nil",
NONE = "none",
OUTSET = "outset",
THICK = "thick",
THICK_THIN_LARGE_GAP = "thickThinLargeGap",
THICK_THIN_MEDIUM_GAP = "thickThinMediumGap",
THICK_THIN_SMALL_GAP = "thickThinSmallGap",
THIN_THICK_LARGE_GAP = "thinThickLargeGap",
THIN_THICK_MEDIUM_GAP = "thinThickMediumGap",
THIN_THICK_SMALL_GAP = "thinThickSmallGap",
THIN_THICK_THIN_LARGE_GAP = "thinThickThinLargeGap",
THIN_THICK_THIN_MEDIUM_GAP = "thinThickThinMediumGap",
THIN_THICK_THIN_SMALL_GAP = "thinThickThinSmallGap",
THREE_D_EMBOSS = "threeDEmboss",
THREE_D_ENGRAVE = "threeDEngrave",
TRIPLE = "triple",
WAVE = "wave",
}

View File

@ -1,7 +1,7 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { BorderStyle } from "file/styles";
import { BorderStyle } from "file/border";
import { PageBorderDisplay, PageBorders, PageBorderZOrder } from "./page-borders";

View File

@ -16,3 +16,4 @@ export * from "./header";
export * from "./footnotes";
export * from "./track-revision";
export * from "./shared";
export * from "./border";

View File

@ -1,15 +0,0 @@
import { XmlAttributeComponent } from "file/xml-components";
export class BorderAttributes extends XmlAttributeComponent<{
readonly color: string;
readonly space: number;
readonly val: string;
readonly sz: number;
}> {
protected readonly xmlKeys = {
val: "w:val",
color: "w:color",
space: "w:space",
sz: "w:sz",
};
}

View File

@ -2,6 +2,7 @@ import { expect } from "chai";
import { Formatter } from "export/formatter";
import { BorderStyle } from "file/border";
import { Border, ThematicBreak } from "./border";
describe("Border", () => {
@ -11,25 +12,25 @@ describe("Border", () => {
top: {
color: "red",
space: 1,
value: "test",
style: BorderStyle.WAVE,
size: 2,
},
bottom: {
color: "red",
space: 3,
value: "test",
style: BorderStyle.WAVE,
size: 4,
},
left: {
color: "red",
space: 5,
value: "test",
style: BorderStyle.WAVE,
size: 6,
},
right: {
color: "red",
space: 7,
value: "test",
style: BorderStyle.WAVE,
size: 8,
},
});
@ -44,7 +45,7 @@ describe("Border", () => {
"w:color": "red",
"w:space": 1,
"w:sz": 2,
"w:val": "test",
"w:val": "wave",
},
},
},
@ -54,7 +55,7 @@ describe("Border", () => {
"w:color": "red",
"w:space": 3,
"w:sz": 4,
"w:val": "test",
"w:val": "wave",
},
},
},
@ -64,7 +65,7 @@ describe("Border", () => {
"w:color": "red",
"w:space": 5,
"w:sz": 6,
"w:val": "test",
"w:val": "wave",
},
},
},
@ -74,7 +75,7 @@ describe("Border", () => {
"w:color": "red",
"w:space": 7,
"w:sz": 8,
"w:val": "test",
"w:val": "wave",
},
},
},

View File

@ -1,57 +1,32 @@
// http://officeopenxml.com/WPborders.php
import { BorderElement, BorderStyle, IBorderOptions } from "file/border";
import { XmlComponent } from "file/xml-components";
import { BorderAttributes } from "./border-attributes";
interface IBorderPropertyOptions {
readonly color: string;
readonly space: number;
readonly value: string;
readonly size: number;
}
export interface IBorderOptions {
readonly top?: IBorderPropertyOptions;
readonly bottom?: IBorderPropertyOptions;
readonly left?: IBorderPropertyOptions;
readonly right?: IBorderPropertyOptions;
}
class BorderProperty extends XmlComponent {
constructor(rootKey: string, options: IBorderPropertyOptions = { color: "auto", space: 1, value: "single", size: 6 }) {
super(rootKey);
const attrs = new BorderAttributes({
color: options.color,
space: options.space,
val: options.value,
sz: options.size,
});
this.root.push(attrs);
}
export interface IBordersOptions {
readonly top?: IBorderOptions;
readonly bottom?: IBorderOptions;
readonly left?: IBorderOptions;
readonly right?: IBorderOptions;
}
export class Border extends XmlComponent {
constructor(options: IBorderOptions) {
constructor(options: IBordersOptions) {
super("w:pBdr");
if (options.top !== undefined) {
const borderProperty = new BorderProperty("w:top", options.top);
this.root.push(borderProperty);
if (options.top) {
this.root.push(new BorderElement("w:top", options.top));
}
if (options.bottom !== undefined) {
const borderProperty = new BorderProperty("w:bottom", options.bottom);
this.root.push(borderProperty);
if (options.bottom) {
this.root.push(new BorderElement("w:bottom", options.bottom));
}
if (options.left !== undefined) {
const borderProperty = new BorderProperty("w:left", options.left);
this.root.push(borderProperty);
if (options.left) {
this.root.push(new BorderElement("w:left", options.left));
}
if (options.right !== undefined) {
const borderProperty = new BorderProperty("w:right", options.right);
this.root.push(borderProperty);
if (options.right) {
this.root.push(new BorderElement("w:right", options.right));
}
}
}
@ -59,10 +34,10 @@ export class Border extends XmlComponent {
export class ThematicBreak extends XmlComponent {
constructor() {
super("w:pBdr");
const bottom = new BorderProperty("w:bottom", {
const bottom = new BorderElement("w:bottom", {
color: "auto",
space: 1,
value: "single",
style: BorderStyle.SINGLE,
size: 6,
});
this.root.push(bottom);

View File

@ -2,7 +2,9 @@ import { assert, expect } from "chai";
import { SinonStub, stub } from "sinon";
import * as convenienceFunctions from "convenience-functions";
import { Formatter } from "export/formatter";
import { BorderStyle } from "file/border";
import { EMPTY_OBJECT } from "file/xml-components";
import { IViewWrapper } from "../document-wrapper";
@ -467,13 +469,13 @@ describe("Paragraph", () => {
left: {
color: "auto",
space: 1,
value: "single",
style: BorderStyle.SINGLE,
size: 6,
},
right: {
color: "auto",
space: 1,
value: "single",
style: BorderStyle.SINGLE,
size: 6,
},
},

View File

@ -4,7 +4,7 @@ import { DocumentWrapper } from "../document-wrapper";
import { IShadingAttributesProperties, Shading } from "../shading";
import { Alignment, AlignmentType } from "./formatting/alignment";
import { Bidirectional } from "./formatting/bidirectional";
import { Border, IBorderOptions, ThematicBreak } from "./formatting/border";
import { Border, IBordersOptions, ThematicBreak } from "./formatting/border";
import { IIndentAttributesProperties, Indent } from "./formatting/indent";
import { KeepLines, KeepNext } from "./formatting/keep";
import { PageBreakBefore } from "./formatting/page-break";
@ -30,7 +30,7 @@ export interface IParagraphStylePropertiesOptions {
}
export interface IParagraphPropertiesOptions extends IParagraphStylePropertiesOptions {
readonly border?: IBorderOptions;
readonly border?: IBordersOptions;
readonly heading?: HeadingLevel;
readonly bidirectional?: boolean;
readonly pageBreakBefore?: boolean;

View File

@ -4,6 +4,19 @@
// http://officeopenxml.com/WPshading.php
// http://officeopenxml.com/WPtableShading.php
// http://officeopenxml.com/WPtableCellProperties-Shading.php
//
// This describes the CT_Shd type.
// <xsd:complexType name="CT_Shd">
// <xsd:attribute name="val" type="ST_Shd" use="required"/>
// <xsd:attribute name="color" type="ST_HexColor" use="optional"/>
// <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="fill" type="ST_HexColor" use="optional"/>
// <xsd:attribute name="themeFill" type="ST_ThemeColor" use="optional"/>
// <xsd:attribute name="themeFillTint" type="ST_UcharHexNumber" use="optional"/>
// <xsd:attribute name="themeFillShade" type="ST_UcharHexNumber" use="optional"/>
// </xsd:complexType>
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
export interface IShadingAttributesProperties {

View File

@ -1,29 +0,0 @@
export enum BorderStyle {
SINGLE = "single",
DASH_DOT_STROKED = "dashDotStroked",
DASHED = "dashed",
DASH_SMALL_GAP = "dashSmallGap",
DOT_DASH = "dotDash",
DOT_DOT_DASH = "dotDotDash",
DOTTED = "dotted",
DOUBLE = "double",
DOUBLE_WAVE = "doubleWave",
INSET = "inset",
NIL = "nil",
NONE = "none",
OUTSET = "outset",
THICK = "thick",
THICK_THIN_LARGE_GAP = "thickThinLargeGap",
THICK_THIN_MEDIUM_GAP = "thickThinMediumGap",
THICK_THIN_SMALL_GAP = "thickThinSmallGap",
THIN_THICK_LARGE_GAP = "thinThickLargeGap",
THIN_THICK_MEDIUM_GAP = "thinThickMediumGap",
THIN_THICK_SMALL_GAP = "thinThickSmallGap",
THIN_THICK_THIN_LARGE_GAP = "thinThickThinLargeGap",
THIN_THICK_THIN_MEDIUM_GAP = "thinThickThinMediumGap",
THIN_THICK_THIN_SMALL_GAP = "thinThickThinSmallGap",
THREE_D_EMBOSS = "threeDEmboss",
THREE_D_ENGRAVE = "threeDEngrave",
TRIPLE = "triple",
WAVE = "wave",
}

View File

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

View File

@ -3,7 +3,6 @@ import { BaseXmlComponent, ImportedXmlComponent, XmlComponent } from "file/xml-c
import { StyleForCharacter, StyleForParagraph } from "./style";
import { ICharacterStyleOptions } from "./style/character-style";
import { IParagraphStyleOptions } from "./style/paragraph-style";
export * from "./border";
export interface IStylesOptions {
readonly default?: IDefaultStylesOptions;

View File

@ -1,7 +1,7 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { BorderStyle } from "file/styles";
import { BorderStyle } from "file/border";
import { VerticalAlign, VerticalMergeType, WidthType } from "./table-cell-components";
import { TableCellProperties } from "./table-cell-properties";

View File

@ -1,7 +1,7 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { BorderStyle } from "file/styles";
import { BorderStyle } from "file/border";
import { ShadingType } from "file/shading";
import { TableCell } from "./table-cell";

View File

@ -1,7 +1,7 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { BorderStyle } from "file/styles";
import { BorderStyle } from "file/border";
import { TableBorders } from "./table-borders";

View File

@ -1,6 +1,5 @@
// http://officeopenxml.com/WPtableBorders.php
import { BorderElement, IBorderOptions } from "file/border";
import { BorderStyle } from "file/styles";
import { BorderElement, BorderStyle, IBorderOptions } from "file/border";
import { XmlComponent } from "file/xml-components";
export interface ITableBordersOptions {