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,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);