From a1b9be453bbace564b3c9034aab9462bb4dc9977 Mon Sep 17 00:00:00 2001 From: Dolan Date: Thu, 15 Aug 2019 00:47:55 +0100 Subject: [PATCH] Add math run and fraction --- demo/47-math.ts | 33 ++++++++++++++ src/file/paragraph/index.ts | 1 + src/file/paragraph/math/fraction/index.ts | 3 ++ .../math/fraction/math-denominator.spec.ts | 25 +++++++++++ .../math/fraction/math-denominator.ts | 11 +++++ .../math/fraction/math-fraction.spec.ts | 45 +++++++++++++++++++ .../paragraph/math/fraction/math-fraction.ts | 18 ++++++++ .../math/fraction/math-numerator.spec.ts | 25 +++++++++++ .../paragraph/math/fraction/math-numerator.ts | 11 +++++ src/file/paragraph/math/index.ts | 3 ++ src/file/paragraph/math/math-run.spec.ts | 21 +++++++++ src/file/paragraph/math/math-run.ts | 11 +++++ src/file/paragraph/math/math-text.spec.ts | 17 +++++++ src/file/paragraph/math/math-text.ts | 9 ++++ src/file/paragraph/math/math.spec.ts | 38 ++++++++++++++++ src/file/paragraph/math/math.ts | 19 ++++++++ src/file/paragraph/paragraph.ts | 3 +- 17 files changed, 292 insertions(+), 1 deletion(-) create mode 100644 demo/47-math.ts create mode 100644 src/file/paragraph/math/fraction/index.ts create mode 100644 src/file/paragraph/math/fraction/math-denominator.spec.ts create mode 100644 src/file/paragraph/math/fraction/math-denominator.ts create mode 100644 src/file/paragraph/math/fraction/math-fraction.spec.ts create mode 100644 src/file/paragraph/math/fraction/math-fraction.ts create mode 100644 src/file/paragraph/math/fraction/math-numerator.spec.ts create mode 100644 src/file/paragraph/math/fraction/math-numerator.ts create mode 100644 src/file/paragraph/math/index.ts create mode 100644 src/file/paragraph/math/math-run.spec.ts create mode 100644 src/file/paragraph/math/math-run.ts create mode 100644 src/file/paragraph/math/math-text.spec.ts create mode 100644 src/file/paragraph/math/math-text.ts create mode 100644 src/file/paragraph/math/math.spec.ts create mode 100644 src/file/paragraph/math/math.ts diff --git a/demo/47-math.ts b/demo/47-math.ts new file mode 100644 index 0000000000..7862feac97 --- /dev/null +++ b/demo/47-math.ts @@ -0,0 +1,33 @@ +// Simple example to add text to a document +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Math, MathDenominator, MathFraction, MathNumerator, MathRun, Packer, Paragraph, TextRun } from "../build"; + +const doc = new Document(); + +doc.addSection({ + properties: {}, + children: [ + new Paragraph({ + children: [ + new Math({ + children: [ + new MathRun("2+2"), + new MathFraction({ + numerator: new MathNumerator("hi"), + denominator: new MathDenominator("2"), + }), + ], + }), + new TextRun({ + text: "Foo Bar", + bold: true, + }), + ], + }), + ], +}); + +Packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/src/file/paragraph/index.ts b/src/file/paragraph/index.ts index 222cb1bf4f..a2ab11a21e 100644 --- a/src/file/paragraph/index.ts +++ b/src/file/paragraph/index.ts @@ -4,3 +4,4 @@ export * from "./properties"; export * from "./run"; export * from "./links"; export * from "./image"; +export * from "./math"; diff --git a/src/file/paragraph/math/fraction/index.ts b/src/file/paragraph/math/fraction/index.ts new file mode 100644 index 0000000000..c8af438329 --- /dev/null +++ b/src/file/paragraph/math/fraction/index.ts @@ -0,0 +1,3 @@ +export * from "./math-fraction"; +export * from "./math-denominator"; +export * from "./math-numerator"; diff --git a/src/file/paragraph/math/fraction/math-denominator.spec.ts b/src/file/paragraph/math/fraction/math-denominator.spec.ts new file mode 100644 index 0000000000..cca8ae0ee1 --- /dev/null +++ b/src/file/paragraph/math/fraction/math-denominator.spec.ts @@ -0,0 +1,25 @@ +import { expect } from "chai"; + +import { Formatter } from "export/formatter"; + +import { MathDenominator } from "./math-denominator"; + +describe("MathDenominator", () => { + describe("#constructor()", () => { + it("should create a MathDenominator with correct root key", () => { + const mathDenominator = new MathDenominator("2+2"); + const tree = new Formatter().format(mathDenominator); + expect(tree).to.deep.equal({ + "m:den": [ + { + "m:r": [ + { + "m:t": ["2+2"], + }, + ], + }, + ], + }); + }); + }); +}); diff --git a/src/file/paragraph/math/fraction/math-denominator.ts b/src/file/paragraph/math/fraction/math-denominator.ts new file mode 100644 index 0000000000..da9f283bdc --- /dev/null +++ b/src/file/paragraph/math/fraction/math-denominator.ts @@ -0,0 +1,11 @@ +import { XmlComponent } from "file/xml-components"; + +import { MathRun } from "../math-run"; + +export class MathDenominator extends XmlComponent { + constructor(readonly text: string) { + super("m:den"); + + this.root.push(new MathRun(text)); + } +} diff --git a/src/file/paragraph/math/fraction/math-fraction.spec.ts b/src/file/paragraph/math/fraction/math-fraction.spec.ts new file mode 100644 index 0000000000..9a7386e85e --- /dev/null +++ b/src/file/paragraph/math/fraction/math-fraction.spec.ts @@ -0,0 +1,45 @@ +import { expect } from "chai"; + +import { Formatter } from "export/formatter"; + +import { MathDenominator } from "./math-denominator"; +import { MathFraction } from "./math-fraction"; +import { MathNumerator } from "./math-numerator"; + +describe("MathFraction", () => { + describe("#constructor()", () => { + it("should create a MathFraction with correct root key", () => { + const mathFraction = new MathFraction({ + numerator: new MathNumerator("2"), + denominator: new MathDenominator("2"), + }); + const tree = new Formatter().format(mathFraction); + expect(tree).to.deep.equal({ + "m:f": [ + { + "m:num": [ + { + "m:r": [ + { + "m:t": ["2"], + }, + ], + }, + ], + }, + { + "m:den": [ + { + "m:r": [ + { + "m:t": ["2"], + }, + ], + }, + ], + }, + ], + }); + }); + }); +}); diff --git a/src/file/paragraph/math/fraction/math-fraction.ts b/src/file/paragraph/math/fraction/math-fraction.ts new file mode 100644 index 0000000000..062c3ef575 --- /dev/null +++ b/src/file/paragraph/math/fraction/math-fraction.ts @@ -0,0 +1,18 @@ +import { XmlComponent } from "file/xml-components"; + +import { MathDenominator } from "./math-denominator"; +import { MathNumerator } from "./math-numerator"; + +export interface IMathFractionOptions { + readonly numerator: MathNumerator; + readonly denominator: MathDenominator; +} + +export class MathFraction extends XmlComponent { + constructor(readonly options: IMathFractionOptions) { + super("m:f"); + + this.root.push(options.numerator); + this.root.push(options.denominator); + } +} diff --git a/src/file/paragraph/math/fraction/math-numerator.spec.ts b/src/file/paragraph/math/fraction/math-numerator.spec.ts new file mode 100644 index 0000000000..1cc5fc4aa4 --- /dev/null +++ b/src/file/paragraph/math/fraction/math-numerator.spec.ts @@ -0,0 +1,25 @@ +import { expect } from "chai"; + +import { Formatter } from "export/formatter"; + +import { MathNumerator } from "./math-numerator"; + +describe("MathNumerator", () => { + describe("#constructor()", () => { + it("should create a MathNumerator with correct root key", () => { + const mathNumerator = new MathNumerator("2+2"); + const tree = new Formatter().format(mathNumerator); + expect(tree).to.deep.equal({ + "m:num": [ + { + "m:r": [ + { + "m:t": ["2+2"], + }, + ], + }, + ], + }); + }); + }); +}); diff --git a/src/file/paragraph/math/fraction/math-numerator.ts b/src/file/paragraph/math/fraction/math-numerator.ts new file mode 100644 index 0000000000..68799328cb --- /dev/null +++ b/src/file/paragraph/math/fraction/math-numerator.ts @@ -0,0 +1,11 @@ +import { XmlComponent } from "file/xml-components"; + +import { MathRun } from "../math-run"; + +export class MathNumerator extends XmlComponent { + constructor(readonly text: string) { + super("m:num"); + + this.root.push(new MathRun(text)); + } +} diff --git a/src/file/paragraph/math/index.ts b/src/file/paragraph/math/index.ts new file mode 100644 index 0000000000..9f3b22b5fa --- /dev/null +++ b/src/file/paragraph/math/index.ts @@ -0,0 +1,3 @@ +export * from "./math"; +export * from "./math-run"; +export * from "./fraction"; diff --git a/src/file/paragraph/math/math-run.spec.ts b/src/file/paragraph/math/math-run.spec.ts new file mode 100644 index 0000000000..2773a0fb9c --- /dev/null +++ b/src/file/paragraph/math/math-run.spec.ts @@ -0,0 +1,21 @@ +import { expect } from "chai"; + +import { Formatter } from "export/formatter"; + +import { MathRun } from "./math-run"; + +describe("MathRun", () => { + describe("#constructor()", () => { + it("should create a MathRun with correct root key", () => { + const mathRun = new MathRun("2+2"); + const tree = new Formatter().format(mathRun); + expect(tree).to.deep.equal({ + "m:r": [ + { + "m:t": ["2+2"], + }, + ], + }); + }); + }); +}); diff --git a/src/file/paragraph/math/math-run.ts b/src/file/paragraph/math/math-run.ts new file mode 100644 index 0000000000..5b9b9536c4 --- /dev/null +++ b/src/file/paragraph/math/math-run.ts @@ -0,0 +1,11 @@ +import { XmlComponent } from "file/xml-components"; + +import { MathText } from "./math-text"; + +export class MathRun extends XmlComponent { + constructor(readonly text: string) { + super("m:r"); + + this.root.push(new MathText(text)); + } +} diff --git a/src/file/paragraph/math/math-text.spec.ts b/src/file/paragraph/math/math-text.spec.ts new file mode 100644 index 0000000000..0001816ed1 --- /dev/null +++ b/src/file/paragraph/math/math-text.spec.ts @@ -0,0 +1,17 @@ +import { expect } from "chai"; + +import { Formatter } from "export/formatter"; + +import { MathText } from "./math-text"; + +describe("MathText", () => { + describe("#constructor()", () => { + it("should create a MathText with correct root key", () => { + const mathText = new MathText("2+2"); + const tree = new Formatter().format(mathText); + expect(tree).to.deep.equal({ + "m:t": ["2+2"], + }); + }); + }); +}); diff --git a/src/file/paragraph/math/math-text.ts b/src/file/paragraph/math/math-text.ts new file mode 100644 index 0000000000..907294aab9 --- /dev/null +++ b/src/file/paragraph/math/math-text.ts @@ -0,0 +1,9 @@ +import { XmlComponent } from "file/xml-components"; + +export class MathText extends XmlComponent { + constructor(readonly text: string) { + super("m:t"); + + this.root.push(text); + } +} diff --git a/src/file/paragraph/math/math.spec.ts b/src/file/paragraph/math/math.spec.ts new file mode 100644 index 0000000000..d5c4f6f494 --- /dev/null +++ b/src/file/paragraph/math/math.spec.ts @@ -0,0 +1,38 @@ +import { expect } from "chai"; + +import { Formatter } from "export/formatter"; + +import { Math } from "./math"; +import { MathRun } from "./math-run"; + +describe("Math", () => { + describe("#constructor()", () => { + it("should create a Math with correct root key", () => { + const math = new Math({ + children: [], + }); + const tree = new Formatter().format(math); + expect(tree).to.deep.equal({ + "m:oMath": {}, + }); + }); + + it("should be able to add children", () => { + const math = new Math({ + children: [new MathRun("2+2")], + }); + const tree = new Formatter().format(math); + expect(tree).to.deep.equal({ + "m:oMath": [ + { + "m:r": [ + { + "m:t": ["2+2"], + }, + ], + }, + ], + }); + }); + }); +}); diff --git a/src/file/paragraph/math/math.ts b/src/file/paragraph/math/math.ts new file mode 100644 index 0000000000..eee6fdff65 --- /dev/null +++ b/src/file/paragraph/math/math.ts @@ -0,0 +1,19 @@ +// http://www.datypic.com/sc/ooxml/e-m_oMath-1.html +import { XmlComponent } from "file/xml-components"; + +import { MathFraction } from "./fraction"; +import { MathRun } from "./math-run"; + +export interface IMathOptions { + readonly children: Array; +} + +export class Math extends XmlComponent { + constructor(readonly options: IMathOptions) { + super("m:oMath"); + + for (const child of options.children) { + this.root.push(child); + } + } +} diff --git a/src/file/paragraph/paragraph.ts b/src/file/paragraph/paragraph.ts index 3f39ba68bc..c0375fac5c 100644 --- a/src/file/paragraph/paragraph.ts +++ b/src/file/paragraph/paragraph.ts @@ -15,6 +15,7 @@ import { HeadingLevel, Style } from "./formatting/style"; import { CenterTabStop, LeaderType, LeftTabStop, MaxRightTabStop, RightTabStop } from "./formatting/tab-stop"; import { NumberProperties } from "./formatting/unordered-list"; import { Bookmark, Hyperlink, OutlineLevel } from "./links"; +import { Math } from "./math"; import { ParagraphProperties } from "./properties"; import { PictureRun, Run, SequentialIdentifier, TextRun } from "./run"; @@ -54,7 +55,7 @@ export interface IParagraphOptions { readonly level: number; readonly custom?: boolean; }; - readonly children?: Array; + readonly children?: Array; } export class Paragraph extends XmlComponent {