Add Math Summation
This commit is contained in:
@ -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"),
|
||||
}),
|
||||
],
|
||||
}),
|
||||
],
|
||||
}),
|
||||
],
|
||||
});
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
export * from "./math";
|
||||
export * from "./math-run";
|
||||
export * from "./fraction";
|
||||
export * from "./n-ary";
|
||||
|
@ -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";
|
||||
|
@ -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 {
|
||||
|
7
src/file/paragraph/math/n-ary/index.ts
Normal file
7
src/file/paragraph/math/n-ary/index.ts
Normal 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";
|
22
src/file/paragraph/math/n-ary/math-accent-character.spec.ts
Normal file
22
src/file/paragraph/math/n-ary/math-accent-character.spec.ts
Normal 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": "∑",
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
14
src/file/paragraph/math/n-ary/math-accent-character.ts
Normal file
14
src/file/paragraph/math/n-ary/math-accent-character.ts
Normal 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 }));
|
||||
}
|
||||
}
|
27
src/file/paragraph/math/n-ary/math-base.spec.ts
Normal file
27
src/file/paragraph/math/n-ary/math-base.spec.ts
Normal 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"],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
12
src/file/paragraph/math/n-ary/math-base.ts
Normal file
12
src/file/paragraph/math/n-ary/math-base.ts
Normal 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);
|
||||
}
|
||||
}
|
22
src/file/paragraph/math/n-ary/math-limit-location.spec.ts
Normal file
22
src/file/paragraph/math/n-ary/math-limit-location.spec.ts
Normal 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",
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
14
src/file/paragraph/math/n-ary/math-limit-location.ts
Normal file
14
src/file/paragraph/math/n-ary/math-limit-location.ts
Normal 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" }));
|
||||
}
|
||||
}
|
33
src/file/paragraph/math/n-ary/math-naray-properties.spec.ts
Normal file
33
src/file/paragraph/math/n-ary/math-naray-properties.spec.ts
Normal 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",
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
14
src/file/paragraph/math/n-ary/math-naray-properties.ts
Normal file
14
src/file/paragraph/math/n-ary/math-naray-properties.ts
Normal 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());
|
||||
}
|
||||
}
|
27
src/file/paragraph/math/n-ary/math-sub-script.spec.ts
Normal file
27
src/file/paragraph/math/n-ary/math-sub-script.spec.ts
Normal 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"],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
12
src/file/paragraph/math/n-ary/math-sub-script.ts
Normal file
12
src/file/paragraph/math/n-ary/math-sub-script.ts
Normal 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);
|
||||
}
|
||||
}
|
75
src/file/paragraph/math/n-ary/math-sum.spec.ts
Normal file
75
src/file/paragraph/math/n-ary/math-sum.spec.ts
Normal 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"],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
25
src/file/paragraph/math/n-ary/math-sum.ts
Normal file
25
src/file/paragraph/math/n-ary/math-sum.ts
Normal 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));
|
||||
}
|
||||
}
|
27
src/file/paragraph/math/n-ary/math-super-script.spec.ts
Normal file
27
src/file/paragraph/math/n-ary/math-super-script.spec.ts
Normal 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"],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
12
src/file/paragraph/math/n-ary/math-super-script.ts
Normal file
12
src/file/paragraph/math/n-ary/math-super-script.ts
Normal 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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user