From 9ab6df99b185680ef1a99ebb657a65a8313f8894 Mon Sep 17 00:00:00 2001 From: tvale1 Date: Sat, 8 Oct 2022 12:56:35 +1100 Subject: [PATCH 1/5] Add MathIntegral class --- demo/55-math.ts | 30 ++++++++++++++++++ src/file/paragraph/math/math-component.ts | 2 ++ src/file/paragraph/math/n-ary/index.ts | 1 + .../paragraph/math/n-ary/math-integral.ts | 31 +++++++++++++++++++ .../math/n-ary/math-n-ary-properties.ts | 4 ++- 5 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 src/file/paragraph/math/n-ary/math-integral.ts diff --git a/demo/55-math.ts b/demo/55-math.ts index e818d13e3b..887921900a 100644 --- a/demo/55-math.ts +++ b/demo/55-math.ts @@ -16,6 +16,7 @@ import { MathSubScript, MathSubSuperScript, MathSum, + MathIntegral, MathSuperScript, Packer, Paragraph, @@ -90,6 +91,35 @@ const doc = new Document({ }), ], }), + new Paragraph({ + children: [ + new Math({ + children: [ + new MathIntegral({ + children: [new MathRun("test")], + }), + new MathIntegral({ + children: [ + new MathSuperScript({ + children: [new MathRun("e")], + superScript: [new MathRun("2")], + }), + ], + subScript: [new MathRun("i")], + }), + new MathIntegral({ + children: [ + new MathRadical({ + children: [new MathRun("i")], + }), + ], + subScript: [new MathRun("i")], + superScript: [new MathRun("10")], + }), + ], + }), + ], + }), new Paragraph({ children: [ new Math({ diff --git a/src/file/paragraph/math/math-component.ts b/src/file/paragraph/math/math-component.ts index 8251235455..e38fdd36e3 100644 --- a/src/file/paragraph/math/math-component.ts +++ b/src/file/paragraph/math/math-component.ts @@ -3,6 +3,7 @@ import { MathFraction } from "./fraction"; import { MathFunction } from "./function"; import { MathRun } from "./math-run"; import { MathSum } from "./n-ary"; +import { MathIntegral } from "./n-ary"; import { MathRadical } from "./radical"; import { MathSubScript, MathSubSuperScript, MathSuperScript } from "./script"; @@ -10,6 +11,7 @@ export type MathComponent = | MathRun | MathFraction | MathSum + | MathIntegral | MathSuperScript | MathSubScript | MathSubSuperScript diff --git a/src/file/paragraph/math/n-ary/index.ts b/src/file/paragraph/math/n-ary/index.ts index 59df698e9c..920d08de74 100644 --- a/src/file/paragraph/math/n-ary/index.ts +++ b/src/file/paragraph/math/n-ary/index.ts @@ -4,4 +4,5 @@ export * from "./math-limit-location"; export * from "./math-n-ary-properties"; export * from "./math-sub-script"; export * from "./math-sum"; +export * from "./math-integral"; export * from "./math-super-script"; diff --git a/src/file/paragraph/math/n-ary/math-integral.ts b/src/file/paragraph/math/n-ary/math-integral.ts new file mode 100644 index 0000000000..3189d31c08 --- /dev/null +++ b/src/file/paragraph/math/n-ary/math-integral.ts @@ -0,0 +1,31 @@ +import { XmlComponent } from "@file/xml-components"; + +import { MathComponent } from "../math-component"; +import { MathBase } from "./math-base"; +import { MathNAryProperties } from "./math-n-ary-properties"; +import { MathSubScriptElement } from "./math-sub-script"; +import { MathSuperScriptElement } from "./math-super-script"; + +export interface IMathIntegralOptions { + readonly children: readonly MathComponent[]; + readonly subScript?: readonly MathComponent[]; + readonly superScript?: readonly MathComponent[]; +} + +export class MathIntegral extends XmlComponent { + public constructor(options: IMathIntegralOptions) { + super("m:nary"); + + this.root.push(new MathNAryProperties("", !!options.superScript, !!options.subScript)); + + if (!!options.subScript) { + this.root.push(new MathSubScriptElement(options.subScript)); + } + + if (!!options.superScript) { + this.root.push(new MathSuperScriptElement(options.superScript)); + } + + this.root.push(new MathBase(options.children)); + } +} diff --git a/src/file/paragraph/math/n-ary/math-n-ary-properties.ts b/src/file/paragraph/math/n-ary/math-n-ary-properties.ts index c3f9d42912..1163de7508 100644 --- a/src/file/paragraph/math/n-ary/math-n-ary-properties.ts +++ b/src/file/paragraph/math/n-ary/math-n-ary-properties.ts @@ -10,7 +10,9 @@ export class MathNAryProperties extends XmlComponent { public constructor(accent: string, hasSuperScript: boolean, hasSubScript: boolean) { super("m:naryPr"); - this.root.push(new MathAccentCharacter(accent)); + if (!!accent){ + this.root.push(new MathAccentCharacter(accent)); + } this.root.push(new MathLimitLocation()); if (!hasSuperScript) { From 3ccdd2585cf6c6d64cc7d73a16bd74ac99728858 Mon Sep 17 00:00:00 2001 From: Dolan Miu Date: Sun, 16 Oct 2022 00:29:16 +0100 Subject: [PATCH 2/5] Fix prettier and linting --- src/file/paragraph/math/math-component.ts | 3 +-- src/file/paragraph/math/n-ary/index.ts | 2 +- src/file/paragraph/math/n-ary/math-n-ary-properties.ts | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/file/paragraph/math/math-component.ts b/src/file/paragraph/math/math-component.ts index e38fdd36e3..ae30a7d3c6 100644 --- a/src/file/paragraph/math/math-component.ts +++ b/src/file/paragraph/math/math-component.ts @@ -2,8 +2,7 @@ import { MathAngledBrackets, MathCurlyBrackets, MathRoundBrackets, MathSquareBra import { MathFraction } from "./fraction"; import { MathFunction } from "./function"; import { MathRun } from "./math-run"; -import { MathSum } from "./n-ary"; -import { MathIntegral } from "./n-ary"; +import { MathSum, MathIntegral } from "./n-ary"; import { MathRadical } from "./radical"; import { MathSubScript, MathSubSuperScript, MathSuperScript } from "./script"; diff --git a/src/file/paragraph/math/n-ary/index.ts b/src/file/paragraph/math/n-ary/index.ts index 920d08de74..b255d17e21 100644 --- a/src/file/paragraph/math/n-ary/index.ts +++ b/src/file/paragraph/math/n-ary/index.ts @@ -4,5 +4,5 @@ export * from "./math-limit-location"; export * from "./math-n-ary-properties"; export * from "./math-sub-script"; export * from "./math-sum"; -export * from "./math-integral"; +export * from "./math-integral"; export * from "./math-super-script"; diff --git a/src/file/paragraph/math/n-ary/math-n-ary-properties.ts b/src/file/paragraph/math/n-ary/math-n-ary-properties.ts index 1163de7508..b8a491df39 100644 --- a/src/file/paragraph/math/n-ary/math-n-ary-properties.ts +++ b/src/file/paragraph/math/n-ary/math-n-ary-properties.ts @@ -10,7 +10,7 @@ export class MathNAryProperties extends XmlComponent { public constructor(accent: string, hasSuperScript: boolean, hasSubScript: boolean) { super("m:naryPr"); - if (!!accent){ + if (!!accent) { this.root.push(new MathAccentCharacter(accent)); } this.root.push(new MathLimitLocation()); From 640639264d098b05104644beb489f781d22f8e62 Mon Sep 17 00:00:00 2001 From: Dolan Miu Date: Sun, 16 Oct 2022 00:33:44 +0100 Subject: [PATCH 3/5] Add MathIntegral tests --- .../math/n-ary/math-integral.spec.ts | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/file/paragraph/math/n-ary/math-integral.spec.ts diff --git a/src/file/paragraph/math/n-ary/math-integral.spec.ts b/src/file/paragraph/math/n-ary/math-integral.spec.ts new file mode 100644 index 0000000000..0944a3fb2b --- /dev/null +++ b/src/file/paragraph/math/n-ary/math-integral.spec.ts @@ -0,0 +1,68 @@ +import { expect } from "chai"; + +import { Formatter } from "@export/formatter"; + +import { MathRun } from "../math-run"; +import { MathIntegral } from "./math-integral"; + +describe("MathIntegral", () => { + describe("#constructor()", () => { + it("should create a MathIntegral with correct root key", () => { + const mathIntegral = new MathIntegral({ + children: [new MathRun("1")], + subScript: [new MathRun("2")], + superScript: [new MathRun("3")], + }); + + const tree = new Formatter().format(mathIntegral); + expect(tree).to.deep.equal({ + "m:nary": [ + { + "m:naryPr": [ + { + "m:limLoc": { + _attr: { + "m:val": "undOvr", + }, + }, + }, + ], + }, + { + "m:sub": [ + { + "m:r": [ + { + "m:t": ["2"], + }, + ], + }, + ], + }, + { + "m:sup": [ + { + "m:r": [ + { + "m:t": ["3"], + }, + ], + }, + ], + }, + { + "m:e": [ + { + "m:r": [ + { + "m:t": ["1"], + }, + ], + }, + ], + }, + ], + }); + }); + }); +}); From e80acea63f5cb5577603e69d40b4ddfbe9dd1f80 Mon Sep 17 00:00:00 2001 From: Dolan Miu Date: Sun, 16 Oct 2022 00:38:02 +0100 Subject: [PATCH 4/5] Add more MathIntegral tests --- .../math/n-ary/math-integral.spec.ts | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/file/paragraph/math/n-ary/math-integral.spec.ts b/src/file/paragraph/math/n-ary/math-integral.spec.ts index 0944a3fb2b..7820b7b6ac 100644 --- a/src/file/paragraph/math/n-ary/math-integral.spec.ts +++ b/src/file/paragraph/math/n-ary/math-integral.spec.ts @@ -64,5 +64,39 @@ describe("MathIntegral", () => { ], }); }); + + it("should create a MathIntegral with correct root key without sub-script and super-scripts", () => { + const mathIntegral = new MathIntegral({ + children: [new MathRun("1")], + }); + + const tree = new Formatter().format(mathIntegral); + expect(tree).to.deep.equal({ + "m:nary": [ + { + "m:naryPr": [ + { + "m:limLoc": { + _attr: { + "m:val": "undOvr", + }, + }, + }, + ], + }, + { + "m:e": [ + { + "m:r": [ + { + "m:t": ["1"], + }, + ], + }, + ], + }, + ], + }); + }); }); }); From 6dcf3f25e94786247e0f84966049e494a4c8a2b4 Mon Sep 17 00:00:00 2001 From: Dolan Miu Date: Sun, 16 Oct 2022 00:44:40 +0100 Subject: [PATCH 5/5] Fix test --- .../paragraph/math/n-ary/math-integral.spec.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/file/paragraph/math/n-ary/math-integral.spec.ts b/src/file/paragraph/math/n-ary/math-integral.spec.ts index 7820b7b6ac..a5f776cc42 100644 --- a/src/file/paragraph/math/n-ary/math-integral.spec.ts +++ b/src/file/paragraph/math/n-ary/math-integral.spec.ts @@ -82,6 +82,20 @@ describe("MathIntegral", () => { }, }, }, + { + "m:supHide": { + _attr: { + "m:val": 1, + }, + }, + }, + { + "m:subHide": { + _attr: { + "m:val": 1, + }, + }, + }, ], }, {