Add Math Summation

This commit is contained in:
Dolan
2019-08-20 20:40:40 +01:00
parent a1b9be453b
commit 4f8d435e16
19 changed files with 359 additions and 2 deletions

View File

@ -1,7 +1,7 @@
// 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";
import { Document, Math, MathDenominator, MathFraction, MathNumerator, MathRun, MathSum, Packer, Paragraph, TextRun } from "../build";
const doc = new Document();
@ -25,6 +25,17 @@ doc.addSection({
}),
],
}),
new Paragraph({
children: [
new Math({
children: [
new MathSum({
child: new MathRun("test"),
}),
],
}),
],
}),
],
});

View File

@ -1,3 +1,4 @@
export * from "./math";
export * from "./math-run";
export * from "./fraction";
export * from "./n-ary";

View File

@ -1,3 +1,4 @@
// http://www.datypic.com/sc/ooxml/e-m_r-1.html
import { XmlComponent } from "file/xml-components";
import { MathText } from "./math-text";

View File

@ -3,9 +3,10 @@ import { XmlComponent } from "file/xml-components";
import { MathFraction } from "./fraction";
import { MathRun } from "./math-run";
import { MathSum } from "./n-ary";
export interface IMathOptions {
readonly children: Array<MathRun | MathFraction>;
readonly children: Array<MathRun | MathFraction | MathSum>;
}
export class Math extends XmlComponent {

View File

@ -0,0 +1,7 @@
export * from "./math-accent-character";
export * from "./math-base";
export * from "./math-limit-location";
export * from "./math-naray-properties";
export * from "./math-sub-script";
export * from "./math-sum";
export * from "./math-super-script";

View File

@ -0,0 +1,22 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { MathAccentCharacter } from "./math-accent-character";
describe("MathAccentCharacter", () => {
describe("#constructor()", () => {
it("should create a MathAccentCharacter with correct root key", () => {
const mathAccentCharacter = new MathAccentCharacter("∑");
const tree = new Formatter().format(mathAccentCharacter);
expect(tree).to.deep.equal({
"m:chr": {
_attr: {
"m:val": "∑",
},
},
});
});
});
});

View File

@ -0,0 +1,14 @@
// http://www.datypic.com/sc/ooxml/e-m_chr-1.html
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
class MathAccentCharacterAttributes extends XmlAttributeComponent<{ readonly accent: string }> {
protected readonly xmlKeys = { accent: "m:val" };
}
export class MathAccentCharacter extends XmlComponent {
constructor(readonly accent: string) {
super("m:chr");
this.root.push(new MathAccentCharacterAttributes({ accent }));
}
}

View File

@ -0,0 +1,27 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { MathRun } from "../math-run";
import { MathBase } from "./math-base";
describe("MathBase", () => {
describe("#constructor()", () => {
it("should create a MathBase with correct root key", () => {
const mathBase = new MathBase(new MathRun("2+2"));
const tree = new Formatter().format(mathBase);
expect(tree).to.deep.equal({
"m:e": [
{
"m:r": [
{
"m:t": ["2+2"],
},
],
},
],
});
});
});
});

View File

@ -0,0 +1,12 @@
// http://www.datypic.com/sc/ooxml/e-m_e-1.html
import { XmlComponent } from "file/xml-components";
import { MathRun } from "../math-run";
export class MathBase extends XmlComponent {
constructor(readonly run: MathRun) {
super("m:e");
this.root.push(run);
}
}

View File

@ -0,0 +1,22 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { MathLimitLocation } from "./math-limit-location";
describe("MathLimitLocation", () => {
describe("#constructor()", () => {
it("should create a MathLimitLocation with correct root key", () => {
const mathLimitLocation = new MathLimitLocation();
const tree = new Formatter().format(mathLimitLocation);
expect(tree).to.deep.equal({
"m:limLoc": {
_attr: {
"m:val": "undOvr",
},
},
});
});
});
});

View File

@ -0,0 +1,14 @@
// http://www.datypic.com/sc/ooxml/e-m_limLoc-1.html
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
class MathLimitLocationAttributes extends XmlAttributeComponent<{ readonly value: string }> {
protected readonly xmlKeys = { value: "m:val" };
}
export class MathLimitLocation extends XmlComponent {
constructor() {
super("m:limLoc");
this.root.push(new MathLimitLocationAttributes({ value: "undOvr" }));
}
}

View File

@ -0,0 +1,33 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { MathNArayProperties } from "./math-naray-properties";
describe("MathNArayProperties", () => {
describe("#constructor()", () => {
it("should create a MathNArayProperties with correct root key", () => {
const mathNArayProperties = new MathNArayProperties("∑");
const tree = new Formatter().format(mathNArayProperties);
expect(tree).to.deep.equal({
"m:naryPr": [
{
"m:chr": {
_attr: {
"m:val": "∑",
},
},
},
{
"m:limLoc": {
_attr: {
"m:val": "undOvr",
},
},
},
],
});
});
});
});

View File

@ -0,0 +1,14 @@
// http://www.datypic.com/sc/ooxml/e-m_naryPr-1.html
import { XmlComponent } from "file/xml-components";
import { MathAccentCharacter } from "./math-accent-character";
import { MathLimitLocation } from "./math-limit-location";
export class MathNArayProperties extends XmlComponent {
constructor(readonly accent: string) {
super("m:naryPr");
this.root.push(new MathAccentCharacter(accent));
this.root.push(new MathLimitLocation());
}
}

View File

@ -0,0 +1,27 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { MathRun } from "../math-run";
import { MathSubScript } from "./math-sub-script";
describe("MathSubScript", () => {
describe("#constructor()", () => {
it("should create a MathSubScript with correct root key", () => {
const mathSubScript = new MathSubScript(new MathRun("2+2"));
const tree = new Formatter().format(mathSubScript);
expect(tree).to.deep.equal({
"m:sub": [
{
"m:r": [
{
"m:t": ["2+2"],
},
],
},
],
});
});
});
});

View File

@ -0,0 +1,12 @@
// http://www.datypic.com/sc/ooxml/e-m_e-1.html
import { XmlComponent } from "file/xml-components";
import { MathRun } from "../math-run";
export class MathSubScript extends XmlComponent {
constructor(readonly run: MathRun) {
super("m:sub");
this.root.push(run);
}
}

View File

@ -0,0 +1,75 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { MathRun } from "../math-run";
import { MathSum } from "./math-sum";
describe("MathSum", () => {
describe("#constructor()", () => {
it("should create a MathSum with correct root key", () => {
const mathSum = new MathSum({
child: new MathRun("1"),
subScript: new MathRun("2"),
superScript: new MathRun("3"),
});
const tree = new Formatter().format(mathSum);
expect(tree).to.deep.equal({
"m:nary": [
{
"m:naryPr": [
{
"m:chr": {
_attr: {
"m:val": "∑",
},
},
},
{
"m:limLoc": {
_attr: {
"m:val": "undOvr",
},
},
},
],
},
{
"m:sub": [
{
"m:r": [
{
"m:t": ["1"],
},
],
},
],
},
{
"m:sup": [
{
"m:r": [
{
"m:t": ["1"],
},
],
},
],
},
{
"m:e": [
{
"m:r": [
{
"m:t": ["1"],
},
],
},
],
},
],
});
});
});
});

View File

@ -0,0 +1,25 @@
// http://www.datypic.com/sc/ooxml/e-m_nary-1.html
import { XmlComponent } from "file/xml-components";
import { MathRun } from "../math-run";
import { MathBase } from "./math-base";
import { MathNArayProperties } from "./math-naray-properties";
import { MathSubScript } from "./math-sub-script";
import { MathSuperScript } from "./math-super-script";
export interface IMathSumOptions {
readonly child: MathRun;
readonly subScript?: MathRun;
readonly superScript?: MathRun;
}
export class MathSum extends XmlComponent {
constructor(readonly options: IMathSumOptions) {
super("m:nary");
this.root.push(new MathNArayProperties("∑"));
this.root.push(new MathSubScript(options.child));
this.root.push(new MathSuperScript(options.child));
this.root.push(new MathBase(options.child));
}
}

View File

@ -0,0 +1,27 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { MathRun } from "../math-run";
import { MathSuperScript } from "./math-super-script";
describe("MathSuperScript", () => {
describe("#constructor()", () => {
it("should create a MathSuperScript with correct root key", () => {
const mathSuperScript = new MathSuperScript(new MathRun("2+2"));
const tree = new Formatter().format(mathSuperScript);
expect(tree).to.deep.equal({
"m:sup": [
{
"m:r": [
{
"m:t": ["2+2"],
},
],
},
],
});
});
});
});

View File

@ -0,0 +1,12 @@
// http://www.datypic.com/sc/ooxml/e-m_e-1.html
import { XmlComponent } from "file/xml-components";
import { MathRun } from "../math-run";
export class MathSuperScript extends XmlComponent {
constructor(readonly run: MathRun) {
super("m:sup");
this.root.push(run);
}
}