Introduce some functional programming techniques

This commit is contained in:
Dolan
2018-11-02 02:51:57 +00:00
parent 9cfd835171
commit 7980f14efb
108 changed files with 749 additions and 659 deletions

View File

@ -1,10 +1,18 @@
// http://officeopenxml.com/WPalignment.php
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
export type AlignmentOptions = "start" | "end" | "center" | "both" | "distribute" | "left" | "right";
export enum AlignmentOptions {
START = "start",
END = "end",
CENTER = "center",
BOTH = "both",
DISTRIBUTE = "distribute",
LEFT = "left",
RIGHT = "right",
}
export class AlignmentAttributes extends XmlAttributeComponent<{ val: AlignmentOptions }> {
protected xmlKeys = { val: "w:val" };
export class AlignmentAttributes extends XmlAttributeComponent<{ readonly val: AlignmentOptions }> {
protected readonly xmlKeys = { val: "w:val" };
}
export class Alignment extends XmlComponent {

View File

@ -2,15 +2,15 @@
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
export interface IIndentAttributesProperties {
left?: number;
hanging?: number;
firstLine?: number;
start?: number;
end?: number;
readonly left?: number;
readonly hanging?: number;
readonly firstLine?: number;
readonly start?: number;
readonly end?: number;
}
class IndentAttributes extends XmlAttributeComponent<IIndentAttributesProperties> {
protected xmlKeys = {
protected readonly xmlKeys = {
left: "w:left",
hanging: "w:hanging",
firstLine: "w:firstLine",

View File

@ -2,14 +2,14 @@
import { Attributes, XmlAttributeComponent, XmlComponent } from "file/xml-components";
export interface ISpacingProperties {
after?: number;
before?: number;
line?: number;
lineRule?: string;
readonly after?: number;
readonly before?: number;
readonly line?: number;
readonly lineRule?: string;
}
class SpacingAttributes extends XmlAttributeComponent<ISpacingProperties> {
protected xmlKeys = {
protected readonly xmlKeys = {
after: "w:after",
before: "w:before",
line: "w:line",

View File

@ -2,7 +2,7 @@ import { assert } from "chai";
import { Utility } from "tests/utility";
import { LeftTabStop, MaxRightTabStop, RightTabStop } from "./tab-stop";
import { LeaderType, LeftTabStop, MaxRightTabStop, RightTabStop } from "./tab-stop";
describe("LeftTabStop", () => {
let tabStop: LeftTabStop;
@ -32,7 +32,7 @@ describe("RightTabStop", () => {
let tabStop: RightTabStop;
beforeEach(() => {
tabStop = new RightTabStop(100, "dot");
tabStop = new RightTabStop(100, LeaderType.DOT);
});
describe("#constructor()", () => {

View File

@ -8,11 +8,32 @@ export class TabStop extends XmlComponent {
}
}
export type TabValue = "left" | "right" | "center" | "bar" | "clear" | "decimal" | "end" | "num" | "start";
export type LeaderType = "dot" | "hyphen" | "middleDot" | "none" | "underscore";
export enum TabValue {
LEFT = "left",
RIGHT = "right",
CENTER = "center",
BAR = "bar",
CLEAR = "clear",
DECIMAL = "decimal",
END = "end",
NUM = "num",
START = "start",
}
export class TabAttributes extends XmlAttributeComponent<{ val: TabValue; pos: string | number; leader?: LeaderType }> {
protected xmlKeys = { val: "w:val", pos: "w:pos", leader: "w:leader" };
export enum LeaderType {
DOT = "dot",
HYPHEN = "hyphen",
MIDDLE_DOT = "middleDot",
NONE = "none",
UNDERSCORE = "underscore",
}
export class TabAttributes extends XmlAttributeComponent<{
readonly val: TabValue;
readonly pos: string | number;
readonly leader?: LeaderType;
}> {
protected readonly xmlKeys = { val: "w:val", pos: "w:pos", leader: "w:leader" };
}
export class TabStopItem extends XmlComponent {
@ -30,24 +51,24 @@ export class TabStopItem extends XmlComponent {
export class MaxRightTabStop extends TabStop {
constructor(leader?: LeaderType) {
super(new TabStopItem("right", 9026, leader));
super(new TabStopItem(TabValue.RIGHT, 9026, leader));
}
}
export class LeftTabStop extends TabStop {
constructor(position: number, leader?: LeaderType) {
super(new TabStopItem("left", position, leader));
super(new TabStopItem(TabValue.LEFT, position, leader));
}
}
export class RightTabStop extends TabStop {
constructor(position: number, leader?: LeaderType) {
super(new TabStopItem("right", position, leader));
super(new TabStopItem(TabValue.RIGHT, position, leader));
}
}
export class CenterTabStop extends TabStop {
constructor(position: number, leader?: LeaderType) {
super(new TabStopItem("center", position, leader));
super(new TabStopItem(TabValue.CENTER, position, leader));
}
}

View File

@ -1,23 +1,23 @@
import { XmlAttributeComponent } from "file/xml-components";
export interface IBookmarkStartAttributesProperties {
id: string;
name: string;
readonly id: string;
readonly name: string;
}
export class BookmarkStartAttributes extends XmlAttributeComponent<IBookmarkStartAttributesProperties> {
protected xmlKeys = {
protected readonly xmlKeys = {
id: "w:id",
name: "w:name",
};
}
export interface IBookmarkEndAttributesProperties {
id: string;
readonly id: string;
}
export class BookmarkEndAttributes extends XmlAttributeComponent<IBookmarkEndAttributesProperties> {
protected xmlKeys = {
protected readonly xmlKeys = {
id: "w:id",
};
}

View File

@ -1,16 +1,12 @@
// http://officeopenxml.com/WPbookmark.php
import { XmlComponent } from "file/xml-components";
import { TextRun } from "../run";
import { BookmarkEndAttributes, BookmarkStartAttributes } from "./bookmark-attributes";
export class Bookmark {
public linkId: number;
public readonly linkId: number;
public readonly start: BookmarkStart;
public readonly text: TextRun;
public readonly end: BookmarkEnd;
constructor(name: string, text: string, relationshipsCount: number) {
@ -23,7 +19,7 @@ export class Bookmark {
}
export class BookmarkStart extends XmlComponent {
public linkId: number;
public readonly linkId: number;
constructor(name: string, relationshipsCount: number) {
super("w:bookmarkStart");
@ -39,7 +35,7 @@ export class BookmarkStart extends XmlComponent {
}
export class BookmarkEnd extends XmlComponent {
public linkId: number;
public readonly linkId: number;
constructor(relationshipsCount: number) {
super("w:bookmarkEnd");

View File

@ -1,13 +1,13 @@
import { XmlAttributeComponent } from "file/xml-components";
export interface IHyperlinkAttributesProperties {
id?: string;
anchor?: string;
history: number;
readonly id?: string;
readonly anchor?: string;
readonly history: number;
}
export class HyperlinkAttributes extends XmlAttributeComponent<IHyperlinkAttributesProperties> {
protected xmlKeys = {
protected readonly xmlKeys = {
id: "r:id",
history: "w:history",
anchor: "w:anchor",

View File

@ -1,11 +1,10 @@
// http://officeopenxml.com/WPhyperlink.php
import { XmlComponent } from "file/xml-components";
import { TextRun } from "../run";
import { HyperlinkAttributes, IHyperlinkAttributesProperties } from "./hyperlink-attributes";
export class Hyperlink extends XmlComponent {
public linkId: number;
public readonly linkId: number;
constructor(text: string, relationshipsCount: number, anchor?: string) {
super("w:hyperlink");
@ -14,14 +13,10 @@ export class Hyperlink extends XmlComponent {
const props: IHyperlinkAttributesProperties = {
history: 1,
anchor: anchor ? anchor : undefined,
id: !anchor ? `rId${this.linkId}` : undefined,
};
if (anchor) {
props.anchor = anchor;
} else {
props.id = `rId${this.linkId}`;
}
const attributes = new HyperlinkAttributes(props);
this.root.push(attributes);
this.root.push(new TextRun(text).style("Hyperlink"));

View File

@ -4,7 +4,7 @@ import { Image } from "file/media";
import { Num } from "file/numbering/num";
import { XmlComponent } from "file/xml-components";
import { Alignment } from "./formatting/alignment";
import { Alignment, AlignmentOptions } from "./formatting/alignment";
import { Bidirectional } from "./formatting/bidirectional";
import { Border, ThematicBreak } from "./formatting/border";
import { IIndentAttributesProperties, Indent } from "./formatting/indent";
@ -110,37 +110,37 @@ export class Paragraph extends XmlComponent {
}
public center(): Paragraph {
this.properties.push(new Alignment("center"));
this.properties.push(new Alignment(AlignmentOptions.CENTER));
return this;
}
public left(): Paragraph {
this.properties.push(new Alignment("left"));
this.properties.push(new Alignment(AlignmentOptions.LEFT));
return this;
}
public right(): Paragraph {
this.properties.push(new Alignment("right"));
this.properties.push(new Alignment(AlignmentOptions.RIGHT));
return this;
}
public start(): Paragraph {
this.properties.push(new Alignment("start"));
this.properties.push(new Alignment(AlignmentOptions.START));
return this;
}
public end(): Paragraph {
this.properties.push(new Alignment("end"));
this.properties.push(new Alignment(AlignmentOptions.END));
return this;
}
public distribute(): Paragraph {
this.properties.push(new Alignment("distribute"));
this.properties.push(new Alignment(AlignmentOptions.DISTRIBUTE));
return this;
}
public justified(): Paragraph {
this.properties.push(new Alignment("both"));
this.properties.push(new Alignment(AlignmentOptions.BOTH));
return this;
}

View File

@ -3,7 +3,7 @@ import { XmlComponent } from "file/xml-components";
import { Border } from "./formatting/border";
export class ParagraphProperties extends XmlComponent {
public paragraphBorder: Border;
public readonly paragraphBorder: Border;
constructor() {
super("w:pPr");

View File

@ -1,26 +1,32 @@
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
class FidCharAttrs extends XmlAttributeComponent<{ type: "begin" | "end" | "separate"; dirty?: boolean }> {
protected xmlKeys = { type: "w:fldCharType", dirty: "w:dirty" };
enum FieldCharacterType {
BEGIN = "begin",
END = "end",
SEPARATE = "separate",
}
class FidCharAttrs extends XmlAttributeComponent<{ readonly type: FieldCharacterType; readonly dirty?: boolean }> {
protected readonly xmlKeys = { type: "w:fldCharType", dirty: "w:dirty" };
}
export class Begin extends XmlComponent {
constructor(dirty?: boolean) {
super("w:fldChar");
this.root.push(new FidCharAttrs({ type: "begin", dirty }));
this.root.push(new FidCharAttrs({ type: FieldCharacterType.BEGIN, dirty }));
}
}
export class Separate extends XmlComponent {
constructor(dirty?: boolean) {
super("w:fldChar");
this.root.push(new FidCharAttrs({ type: "separate", dirty }));
this.root.push(new FidCharAttrs({ type: FieldCharacterType.SEPARATE, dirty }));
}
}
export class End extends XmlComponent {
constructor(dirty?: boolean) {
super("w:fldChar");
this.root.push(new FidCharAttrs({ type: "end", dirty }));
this.root.push(new FidCharAttrs({ type: FieldCharacterType.END, dirty }));
}
}

View File

@ -1,13 +1,14 @@
import { SpaceType } from "file/space-type";
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
class TextAttributes extends XmlAttributeComponent<{ space: "default" | "preserve" }> {
protected xmlKeys = { space: "xml:space" };
class TextAttributes extends XmlAttributeComponent<{ readonly space: SpaceType }> {
protected readonly xmlKeys = { space: "xml:space" };
}
export class Page extends XmlComponent {
constructor() {
super("w:instrText");
this.root.push(new TextAttributes({ space: "preserve" }));
this.root.push(new TextAttributes({ space: SpaceType.PRESERVE }));
this.root.push("PAGE");
}
}

View File

@ -18,15 +18,7 @@ export class PictureRun extends Run {
this.root.push(this.drawing);
}
public scale(factorX: number, factorY?: number): void {
if (!factorX) {
factorX = 1;
}
if (!factorY) {
factorY = factorX;
}
public scale(factorX: number = 1, factorY: number = factorX): void {
this.drawing.scale(factorX, factorY);
}
}

View File

@ -1,13 +1,14 @@
import { SpaceType } from "file/space-type";
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
class TextAttributes extends XmlAttributeComponent<{ space: "default" | "preserve" }> {
protected xmlKeys = { space: "xml:space" };
class TextAttributes extends XmlAttributeComponent<{ readonly space: SpaceType }> {
protected readonly xmlKeys = { space: "xml:space" };
}
export class Text extends XmlComponent {
constructor(text: string) {
super("w:t");
this.root.push(new TextAttributes({ space: "preserve" }));
this.root.push(new TextAttributes({ space: SpaceType.PRESERVE }));
if (text) {
this.root.push(text);
}

View File

@ -1,15 +1,15 @@
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
interface IRunFontAttributesProperties {
ascii: string;
cs: string;
eastAsia: string;
hAnsi: string;
hint?: string;
readonly ascii: string;
readonly cs: string;
readonly eastAsia: string;
readonly hAnsi: string;
readonly hint?: string;
}
class RunFontAttributes extends XmlAttributeComponent<IRunFontAttributesProperties> {
protected xmlKeys = {
protected readonly xmlKeys = {
ascii: "w:ascii",
cs: "w:cs",
eastAsia: "w:eastAsia",

View File

@ -25,7 +25,7 @@ import { Underline } from "./underline";
import { XmlComponent } from "file/xml-components";
export class Run extends XmlComponent {
protected properties: RunProperties;
protected readonly properties: RunProperties;
constructor() {
super("w:r");

View File

@ -1,13 +1,9 @@
// http://officeopenxml.com/WPfieldInstructions.php
import { SpaceType } from "file/space-type";
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
enum SpaceType {
DEFAULT = "default",
PRESERVE = "preserve",
}
class TextAttributes extends XmlAttributeComponent<{ space: SpaceType }> {
protected xmlKeys = { space: "xml:space" };
class TextAttributes extends XmlAttributeComponent<{ readonly space: SpaceType }> {
protected readonly xmlKeys = { space: "xml:space" };
}
export class SequentialIdentifierInstruction extends XmlComponent {

View File

@ -1,7 +1,7 @@
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
class StyleAttributes extends XmlAttributeComponent<{ val: string }> {
protected xmlKeys = { val: "w:val" };
class StyleAttributes extends XmlAttributeComponent<{ readonly val: string }> {
protected readonly xmlKeys = { val: "w:val" };
}
export class Style extends XmlComponent {