diff --git a/demo/26-paragraph-borders.ts b/demo/26-paragraph-borders.ts index c64be50429..788bbfafcf 100644 --- a/demo/26-paragraph-borders.ts +++ b/demo/26-paragraph-borders.ts @@ -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, }, }, diff --git a/demo/61-text-frame.ts b/demo/61-text-frame.ts index e10e3b6f3c..d6563418cf 100644 --- a/demo/61-text-frame.ts +++ b/demo/61-text-frame.ts @@ -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, }, }, diff --git a/src/file/border/border.ts b/src/file/border/border.ts index 0fe0b24e95..2898a773af 100644 --- a/src/file/border/border.ts +++ b/src/file/border/border.ts @@ -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. // @@ -17,7 +19,6 @@ // // // -import { BorderStyle } from "file/styles"; import { XmlAttributeComponent, XmlComponent } from "file/xml-components"; export interface IBorderOptions { @@ -42,3 +43,33 @@ class TableBordersAttributes extends XmlAttributeComponent { 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", +} diff --git a/src/file/document/body/section-properties/page-border/page-borders.spec.ts b/src/file/document/body/section-properties/page-border/page-borders.spec.ts index 5e79480192..fef63e81b6 100644 --- a/src/file/document/body/section-properties/page-border/page-borders.spec.ts +++ b/src/file/document/body/section-properties/page-border/page-borders.spec.ts @@ -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"; diff --git a/src/file/index.ts b/src/file/index.ts index d13d0a01b4..24317b8598 100644 --- a/src/file/index.ts +++ b/src/file/index.ts @@ -16,3 +16,4 @@ export * from "./header"; export * from "./footnotes"; export * from "./track-revision"; export * from "./shared"; +export * from "./border"; diff --git a/src/file/paragraph/formatting/border-attributes.ts b/src/file/paragraph/formatting/border-attributes.ts deleted file mode 100644 index 304128426b..0000000000 --- a/src/file/paragraph/formatting/border-attributes.ts +++ /dev/null @@ -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", - }; -} diff --git a/src/file/paragraph/formatting/border.spec.ts b/src/file/paragraph/formatting/border.spec.ts index 6e89ffcfc8..55c6c7d41d 100644 --- a/src/file/paragraph/formatting/border.spec.ts +++ b/src/file/paragraph/formatting/border.spec.ts @@ -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", }, }, }, diff --git a/src/file/paragraph/formatting/border.ts b/src/file/paragraph/formatting/border.ts index 9fdb9b65c3..fd29cd34ee 100644 --- a/src/file/paragraph/formatting/border.ts +++ b/src/file/paragraph/formatting/border.ts @@ -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); diff --git a/src/file/paragraph/paragraph.spec.ts b/src/file/paragraph/paragraph.spec.ts index 165e9669cc..9dfaad18c8 100644 --- a/src/file/paragraph/paragraph.spec.ts +++ b/src/file/paragraph/paragraph.spec.ts @@ -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, }, }, diff --git a/src/file/paragraph/properties.ts b/src/file/paragraph/properties.ts index cc0cb8e506..40491109e5 100644 --- a/src/file/paragraph/properties.ts +++ b/src/file/paragraph/properties.ts @@ -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; diff --git a/src/file/shading/shading.ts b/src/file/shading/shading.ts index e6bdace860..e00cedd5de 100644 --- a/src/file/shading/shading.ts +++ b/src/file/shading/shading.ts @@ -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. +// +// +// +// +// +// +// +// +// +// +// import { XmlAttributeComponent, XmlComponent } from "file/xml-components"; export interface IShadingAttributesProperties { diff --git a/src/file/styles/border/border-style.ts b/src/file/styles/border/border-style.ts deleted file mode 100644 index 00f4117d31..0000000000 --- a/src/file/styles/border/border-style.ts +++ /dev/null @@ -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", -} diff --git a/src/file/styles/border/index.ts b/src/file/styles/border/index.ts deleted file mode 100644 index e62e5f7dcc..0000000000 --- a/src/file/styles/border/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./border-style"; diff --git a/src/file/styles/styles.ts b/src/file/styles/styles.ts index 1906b45ba0..071a386c00 100644 --- a/src/file/styles/styles.ts +++ b/src/file/styles/styles.ts @@ -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; diff --git a/src/file/table/table-cell/table-cell-properties.spec.ts b/src/file/table/table-cell/table-cell-properties.spec.ts index 1534ee3169..5e48b1684f 100644 --- a/src/file/table/table-cell/table-cell-properties.spec.ts +++ b/src/file/table/table-cell/table-cell-properties.spec.ts @@ -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"; diff --git a/src/file/table/table-cell/table-cell.spec.ts b/src/file/table/table-cell/table-cell.spec.ts index fcd4d4b8bc..3abca3009d 100644 --- a/src/file/table/table-cell/table-cell.spec.ts +++ b/src/file/table/table-cell/table-cell.spec.ts @@ -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"; diff --git a/src/file/table/table-properties/table-borders.spec.ts b/src/file/table/table-properties/table-borders.spec.ts index e1e2f6b898..85decf8804 100644 --- a/src/file/table/table-properties/table-borders.spec.ts +++ b/src/file/table/table-properties/table-borders.spec.ts @@ -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"; diff --git a/src/file/table/table-properties/table-borders.ts b/src/file/table/table-properties/table-borders.ts index 58778281ef..923a7bb019 100644 --- a/src/file/table/table-properties/table-borders.ts +++ b/src/file/table/table-properties/table-borders.ts @@ -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 {