diff --git a/demo/47-math.ts b/demo/47-math.ts deleted file mode 100644 index 5cad1707de..0000000000 --- a/demo/47-math.ts +++ /dev/null @@ -1,44 +0,0 @@ -// 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, MathSum, 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, - }), - ], - }), - new Paragraph({ - children: [ - new Math({ - children: [ - new MathSum({ - child: new MathRun("test"), - }), - ], - }), - ], - }), - ], -}); - -Packer.toBuffer(doc).then((buffer) => { - fs.writeFileSync("My Document.docx", buffer); -}); diff --git a/demo/55-math.ts b/demo/55-math.ts new file mode 100644 index 0000000000..92993a695a --- /dev/null +++ b/demo/55-math.ts @@ -0,0 +1,259 @@ +// 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, + MathAngledBrackets, + MathCurlyBrackets, + MathDenominator, + MathFraction, + MathFunction, + MathNumerator, + MathPreSubSuperScript, + MathRadical, + MathRoundBrackets, + MathRun, + MathSquareBrackets, + MathSubScript, + MathSubSuperScript, + MathSum, + MathSuperScript, + 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, + }), + ], + }), + new Paragraph({ + children: [ + new Math({ + children: [ + new MathSum({ + child: new MathRun("test"), + }), + new MathSum({ + child: new MathSuperScript({ + child: new MathRun("e"), + superScript: new MathRun("2"), + }), + subScript: new MathRun("i"), + }), + new MathSum({ + child: new MathRadical({ + child: new MathRun("i"), + }), + subScript: new MathRun("i"), + superScript: new MathRun("10"), + }), + ], + }), + ], + }), + new Paragraph({ + children: [ + new Math({ + children: [ + new MathSuperScript({ + child: new MathRun("test"), + superScript: new MathRun("hello"), + }), + ], + }), + ], + }), + new Paragraph({ + children: [ + new Math({ + children: [ + new MathSubScript({ + child: new MathRun("test"), + subScript: new MathRun("hello"), + }), + ], + }), + ], + }), + new Paragraph({ + children: [ + new Math({ + children: [ + new MathSubScript({ + child: new MathRun("x"), + subScript: new MathSuperScript({ + child: new MathRun("y"), + superScript: new MathRun("2"), + }), + }), + ], + }), + ], + }), + new Paragraph({ + children: [ + new Math({ + children: [ + new MathSubSuperScript({ + child: new MathRun("test"), + superScript: new MathRun("hello"), + subScript: new MathRun("world"), + }), + ], + }), + ], + }), + new Paragraph({ + children: [ + new Math({ + children: [ + new MathPreSubSuperScript({ + child: new MathRun("test"), + superScript: new MathRun("hello"), + subScript: new MathRun("world"), + }), + ], + }), + ], + }), + new Paragraph({ + children: [ + new Math({ + children: [ + new MathSubScript({ + child: new MathFraction({ + numerator: new MathNumerator("1"), + denominator: new MathDenominator("2"), + }), + subScript: new MathRun("4"), + }), + ], + }), + ], + }), + new Paragraph({ + children: [ + new Math({ + children: [ + new MathSubScript({ + child: new MathRadical({ + child: new MathFraction({ + numerator: new MathNumerator("1"), + denominator: new MathDenominator("2"), + }), + degree: new MathRun("4"), + }), + subScript: new MathRun("x"), + }), + ], + }), + ], + }), + new Paragraph({ + children: [ + new Math({ + children: [ + new MathRadical({ + child: new MathRun("4"), + }), + ], + }), + ], + }), + new Paragraph({ + children: [ + new Math({ + children: [ + new MathFunction({ + name: new MathSuperScript({ + child: new MathRun("cos"), + superScript: new MathRun("-1"), + }), + child: new MathRun("100"), + }), + new MathRun("×"), + new MathFunction({ + name: new MathRun("sin"), + child: new MathRun("360"), + }), + new MathRun("= x"), + ], + }), + ], + }), + new Paragraph({ + children: [ + new Math({ + children: [ + new MathRoundBrackets({ + child: new MathFraction({ + numerator: new MathNumerator("1"), + denominator: new MathDenominator("2"), + }), + }), + new MathSquareBrackets({ + child: new MathFraction({ + numerator: new MathNumerator("1"), + denominator: new MathDenominator("2"), + }), + }), + new MathCurlyBrackets({ + child: new MathFraction({ + numerator: new MathNumerator("1"), + denominator: new MathDenominator("2"), + }), + }), + new MathAngledBrackets({ + child: new MathFraction({ + numerator: new MathNumerator("1"), + denominator: new MathDenominator("2"), + }), + }), + ], + }), + ], + }), + new Paragraph({ + children: [ + new Math({ + children: [ + new MathFraction({ + numerator: new Math({ + children: [ + new MathRadical({ + child: new MathRun("4"), + }), + ], + }), + demoninator: new MathRun("2a"), + }), + ], + }), + ], + }), + ], +}); + +Packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/package-lock.json b/package-lock.json index d73f9f5d71..7d28a68d7d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1088,7 +1088,7 @@ }, "awesome-typescript-loader": { "version": "3.5.0", - "resolved": "http://registry.npmjs.org/awesome-typescript-loader/-/awesome-typescript-loader-3.5.0.tgz", + "resolved": "https://registry.npmjs.org/awesome-typescript-loader/-/awesome-typescript-loader-3.5.0.tgz", "integrity": "sha512-qzgm9SEvodVkSi9QY7Me1/rujg+YBNMjayNSAyzNghwTEez++gXoPCwMvpbHRG7wrOkDCiF6dquvv9ESmUBAuw==", "dev": true, "requires": { @@ -1665,7 +1665,7 @@ }, "chai": { "version": "3.5.0", - "resolved": "http://registry.npmjs.org/chai/-/chai-3.5.0.tgz", + "resolved": "https://registry.npmjs.org/chai/-/chai-3.5.0.tgz", "integrity": "sha1-TQJjewZ/6Vi9v906QOxW/vc3Mkc=", "dev": true, "requires": { @@ -2153,7 +2153,7 @@ }, "deep-eql": { "version": "0.1.3", - "resolved": "http://registry.npmjs.org/deep-eql/-/deep-eql-0.1.3.tgz", + "resolved": "https://registry.npmjs.org/deep-eql/-/deep-eql-0.1.3.tgz", "integrity": "sha1-71WKyrjeJSBs1xOQbXTlaTDrafI=", "dev": true, "requires": { @@ -4373,7 +4373,7 @@ "is-buffer": { "version": "1.1.6", "resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-1.1.6.tgz", - "integrity": "sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w==", + "integrity": "sha1-76ouqdqg16suoTqXsritUf776L4=", "dev": true }, "is-ci": { @@ -4843,7 +4843,7 @@ }, "jsesc": { "version": "1.3.0", - "resolved": "https://registry.npmjs.org/jsesc/-/jsesc-1.3.0.tgz", + "resolved": "http://registry.npmjs.org/jsesc/-/jsesc-1.3.0.tgz", "integrity": "sha1-RsP+yMGJKxKwgz25vHYiF226s0s=", "dev": true }, @@ -5098,7 +5098,7 @@ }, "load-json-file": { "version": "1.1.0", - "resolved": "http://registry.npmjs.org/load-json-file/-/load-json-file-1.1.0.tgz", + "resolved": "https://registry.npmjs.org/load-json-file/-/load-json-file-1.1.0.tgz", "integrity": "sha1-lWkFcI1YtLq0wiYbBPWfMcmTdMA=", "dev": true, "requires": { @@ -5395,7 +5395,7 @@ "minimatch": { "version": "3.0.4", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", - "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", + "integrity": "sha1-UWbihkV/AzBgZL5Ul+jbsMPTIIM=", "dev": true, "requires": { "brace-expansion": "^1.1.7" @@ -5585,7 +5585,7 @@ }, "ncp": { "version": "1.0.1", - "resolved": "http://registry.npmjs.org/ncp/-/ncp-1.0.1.tgz", + "resolved": "https://registry.npmjs.org/ncp/-/ncp-1.0.1.tgz", "integrity": "sha1-0VNn5cuHQyuhF9K/gP30Wuz7QkY=", "dev": true }, @@ -5603,7 +5603,7 @@ }, "next-tick": { "version": "1.0.0", - "resolved": "http://registry.npmjs.org/next-tick/-/next-tick-1.0.0.tgz", + "resolved": "https://registry.npmjs.org/next-tick/-/next-tick-1.0.0.tgz", "integrity": "sha1-yobR/ogoFpsBICCOPchCS524NCw=", "dev": true }, @@ -7116,7 +7116,7 @@ }, "safe-regex": { "version": "1.1.0", - "resolved": "http://registry.npmjs.org/safe-regex/-/safe-regex-1.1.0.tgz", + "resolved": "https://registry.npmjs.org/safe-regex/-/safe-regex-1.1.0.tgz", "integrity": "sha1-QKNmnzsHfR6UPURinhV91IAjvy4=", "dev": true, "requires": { @@ -7719,7 +7719,7 @@ }, "strip-eof": { "version": "1.0.0", - "resolved": "http://registry.npmjs.org/strip-eof/-/strip-eof-1.0.0.tgz", + "resolved": "https://registry.npmjs.org/strip-eof/-/strip-eof-1.0.0.tgz", "integrity": "sha1-u0P/VZim6wXYm1n80SnJgzE2Br8=", "dev": true }, @@ -7996,10 +7996,13 @@ } }, "tslint-immutable": { - "version": "4.9.1", - "resolved": "https://registry.npmjs.org/tslint-immutable/-/tslint-immutable-4.9.1.tgz", - "integrity": "sha512-iIFCq08H4YyNIX0bV5N6fGQtAmjc4OQZKQCgBP5WHgQaITyGAHPVmAw+Yf7qe0zbRCvCDZdrdEC/191fLGFiww==", - "dev": true + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/tslint-immutable/-/tslint-immutable-6.0.1.tgz", + "integrity": "sha512-3GQ6HffN64gLmT/N1YzyVMqyf6uBjMvhNaevK8B0K01/QC0OU5AQZrH4TjMHo1IdG3JpqsZvuRy9IW1LA3zjwA==", + "dev": true, + "requires": { + "tsutils": "^2.28.0 || ^3.0.0" + } }, "tsutils": { "version": "2.29.0", @@ -8654,7 +8657,7 @@ }, "load-json-file": { "version": "2.0.0", - "resolved": "http://registry.npmjs.org/load-json-file/-/load-json-file-2.0.0.tgz", + "resolved": "https://registry.npmjs.org/load-json-file/-/load-json-file-2.0.0.tgz", "integrity": "sha1-eUfkIUmvgNaWy/eXvKq8/h/inKg=", "dev": true, "requires": { @@ -8855,7 +8858,7 @@ }, "winston": { "version": "2.1.1", - "resolved": "http://registry.npmjs.org/winston/-/winston-2.1.1.tgz", + "resolved": "https://registry.npmjs.org/winston/-/winston-2.1.1.tgz", "integrity": "sha1-PJNJ0ZYgf9G9/51LxD73JRDjoS4=", "dev": true, "requires": { diff --git a/package.json b/package.json index 51e1b246c3..f9a27f0fcb 100644 --- a/package.json +++ b/package.json @@ -89,7 +89,7 @@ "sinon": "^9.0.2", "ts-node": "^9.0.0", "tslint": "^6.1.3", - "tslint-immutable": "^4.9.0", + "tslint-immutable": "^6.0.1", "typedoc": "^0.16.11", "typescript": "2.9.2", "webpack": "^3.10.0" diff --git a/src/file/drawing/inline/inline.ts b/src/file/drawing/inline/inline.ts index 7c40e7c3b3..bf83e36764 100644 --- a/src/file/drawing/inline/inline.ts +++ b/src/file/drawing/inline/inline.ts @@ -12,7 +12,7 @@ export class Inline extends XmlComponent { private readonly extent: Extent; private readonly graphic: Graphic; - constructor(readonly mediaData: IMediaData, private readonly dimensions: IMediaDataDimensions) { + constructor(mediaData: IMediaData, private readonly dimensions: IMediaDataDimensions) { super("wp:inline"); this.root.push( diff --git a/src/file/paragraph/math/brackets/index.ts b/src/file/paragraph/math/brackets/index.ts new file mode 100644 index 0000000000..e6559cde29 --- /dev/null +++ b/src/file/paragraph/math/brackets/index.ts @@ -0,0 +1,4 @@ +export * from "./math-round-brackets"; +export * from "./math-square-brackets"; +export * from "./math-curly-brackets"; +export * from "./math-angled-brackets"; diff --git a/src/file/paragraph/math/brackets/math-angled-brackets.spec.ts b/src/file/paragraph/math/brackets/math-angled-brackets.spec.ts new file mode 100644 index 0000000000..735e0da272 --- /dev/null +++ b/src/file/paragraph/math/brackets/math-angled-brackets.spec.ts @@ -0,0 +1,51 @@ +import { expect } from "chai"; + +import { Formatter } from "export/formatter"; + +import { MathRun } from "../math-run"; +import { MathAngledBrackets } from "./math-angled-brackets"; + +describe("MathAngledBrackets", () => { + describe("#constructor()", () => { + it("should create a MathAngledBrackets with correct root key", () => { + const mathAngledBrackets = new MathAngledBrackets({ + child: new MathRun("60"), + }); + + const tree = new Formatter().format(mathAngledBrackets); + expect(tree).to.deep.equal({ + "m:d": [ + { + "m:dPr": [ + { + "m:begChr": { + _attr: { + "m:val": "〈", + }, + }, + }, + { + "m:endChr": { + _attr: { + "m:val": "〉", + }, + }, + }, + ], + }, + { + "m:e": [ + { + "m:r": [ + { + "m:t": ["60"], + }, + ], + }, + ], + }, + ], + }); + }); + }); +}); diff --git a/src/file/paragraph/math/brackets/math-angled-brackets.ts b/src/file/paragraph/math/brackets/math-angled-brackets.ts new file mode 100644 index 0000000000..fbafb7e738 --- /dev/null +++ b/src/file/paragraph/math/brackets/math-angled-brackets.ts @@ -0,0 +1,20 @@ +// http://www.datypic.com/sc/ooxml/e-m_d-1.html +import { XmlComponent } from "file/xml-components"; + +import { MathComponent } from "../math-component"; +import { MathBase } from "../n-ary"; +import { MathBracketProperties } from "./math-bracket-properties"; + +export class MathAngledBrackets extends XmlComponent { + constructor(options: { readonly child: MathComponent }) { + super("m:d"); + + this.root.push( + new MathBracketProperties({ + beginningCharacter: "〈", + endingCharacter: "〉", + }), + ); + this.root.push(new MathBase(options.child)); + } +} diff --git a/src/file/paragraph/math/brackets/math-beginning-character.spec.ts b/src/file/paragraph/math/brackets/math-beginning-character.spec.ts new file mode 100644 index 0000000000..bf9196c0a9 --- /dev/null +++ b/src/file/paragraph/math/brackets/math-beginning-character.spec.ts @@ -0,0 +1,22 @@ +import { expect } from "chai"; + +import { Formatter } from "export/formatter"; + +import { MathBeginningCharacter } from "./math-beginning-character"; + +describe("MathBeginningCharacter", () => { + describe("#constructor()", () => { + it("should create a MathBeginningCharacter with correct root key", () => { + const mathBeginningCharacter = new MathBeginningCharacter("["); + + const tree = new Formatter().format(mathBeginningCharacter); + expect(tree).to.deep.equal({ + "m:begChr": { + _attr: { + "m:val": "[", + }, + }, + }); + }); + }); +}); diff --git a/src/file/paragraph/math/brackets/math-beginning-character.ts b/src/file/paragraph/math/brackets/math-beginning-character.ts new file mode 100644 index 0000000000..aa9e06d87a --- /dev/null +++ b/src/file/paragraph/math/brackets/math-beginning-character.ts @@ -0,0 +1,14 @@ +// http://www.datypic.com/sc/ooxml/e-m_begChr-1.html +import { XmlAttributeComponent, XmlComponent } from "file/xml-components"; + +class MathBeginningCharacterAttributes extends XmlAttributeComponent<{ readonly character: string }> { + protected readonly xmlKeys = { character: "m:val" }; +} + +export class MathBeginningCharacter extends XmlComponent { + constructor(character: string) { + super("m:begChr"); + + this.root.push(new MathBeginningCharacterAttributes({ character })); + } +} diff --git a/src/file/paragraph/math/brackets/math-bracket-properties.spec.ts b/src/file/paragraph/math/brackets/math-bracket-properties.spec.ts new file mode 100644 index 0000000000..d80976d8f6 --- /dev/null +++ b/src/file/paragraph/math/brackets/math-bracket-properties.spec.ts @@ -0,0 +1,45 @@ +import { expect } from "chai"; + +import { Formatter } from "export/formatter"; + +import { MathBracketProperties } from "./math-bracket-properties"; + +describe("MathBracketProperties", () => { + describe("#constructor()", () => { + it("should create a MathBracketProperties with correct root key", () => { + const mathBracketProperties = new MathBracketProperties(); + + const tree = new Formatter().format(mathBracketProperties); + expect(tree).to.deep.equal({ + "m:dPr": {}, + }); + }); + + it("should create a MathBracketProperties with correct root key and add brackets", () => { + const mathBracketProperties = new MathBracketProperties({ + beginningCharacter: "[", + endingCharacter: "]", + }); + + const tree = new Formatter().format(mathBracketProperties); + expect(tree).to.deep.equal({ + "m:dPr": [ + { + "m:begChr": { + _attr: { + "m:val": "[", + }, + }, + }, + { + "m:endChr": { + _attr: { + "m:val": "]", + }, + }, + }, + ], + }); + }); + }); +}); diff --git a/src/file/paragraph/math/brackets/math-bracket-properties.ts b/src/file/paragraph/math/brackets/math-bracket-properties.ts new file mode 100644 index 0000000000..5bba7bf935 --- /dev/null +++ b/src/file/paragraph/math/brackets/math-bracket-properties.ts @@ -0,0 +1,16 @@ +// http://www.datypic.com/sc/ooxml/e-m_dPr-1.html +import { XmlComponent } from "file/xml-components"; + +import { MathBeginningCharacter } from "./math-beginning-character"; +import { MathEndingCharacter } from "./math-ending-char"; + +export class MathBracketProperties extends XmlComponent { + constructor(options?: { readonly beginningCharacter: string; readonly endingCharacter: string }) { + super("m:dPr"); + + if (!!options) { + this.root.push(new MathBeginningCharacter(options.beginningCharacter)); + this.root.push(new MathEndingCharacter(options.endingCharacter)); + } + } +} diff --git a/src/file/paragraph/math/brackets/math-curly-brackets.spec.ts b/src/file/paragraph/math/brackets/math-curly-brackets.spec.ts new file mode 100644 index 0000000000..6e44621b21 --- /dev/null +++ b/src/file/paragraph/math/brackets/math-curly-brackets.spec.ts @@ -0,0 +1,51 @@ +import { expect } from "chai"; + +import { Formatter } from "export/formatter"; + +import { MathRun } from "../math-run"; +import { MathCurlyBrackets } from "./math-curly-brackets"; + +describe("MathCurlyBrackets", () => { + describe("#constructor()", () => { + it("should create a MathCurlyBrackets with correct root key", () => { + const mathCurlyBrackets = new MathCurlyBrackets({ + child: new MathRun("60"), + }); + + const tree = new Formatter().format(mathCurlyBrackets); + expect(tree).to.deep.equal({ + "m:d": [ + { + "m:dPr": [ + { + "m:begChr": { + _attr: { + "m:val": "{", + }, + }, + }, + { + "m:endChr": { + _attr: { + "m:val": "}", + }, + }, + }, + ], + }, + { + "m:e": [ + { + "m:r": [ + { + "m:t": ["60"], + }, + ], + }, + ], + }, + ], + }); + }); + }); +}); diff --git a/src/file/paragraph/math/brackets/math-curly-brackets.ts b/src/file/paragraph/math/brackets/math-curly-brackets.ts new file mode 100644 index 0000000000..4d540ec8bc --- /dev/null +++ b/src/file/paragraph/math/brackets/math-curly-brackets.ts @@ -0,0 +1,20 @@ +// http://www.datypic.com/sc/ooxml/e-m_d-1.html +import { XmlComponent } from "file/xml-components"; + +import { MathComponent } from "../math-component"; +import { MathBase } from "../n-ary"; +import { MathBracketProperties } from "./math-bracket-properties"; + +export class MathCurlyBrackets extends XmlComponent { + constructor(options: { readonly child: MathComponent }) { + super("m:d"); + + this.root.push( + new MathBracketProperties({ + beginningCharacter: "{", + endingCharacter: "}", + }), + ); + this.root.push(new MathBase(options.child)); + } +} diff --git a/src/file/paragraph/math/brackets/math-ending-char.ts b/src/file/paragraph/math/brackets/math-ending-char.ts new file mode 100644 index 0000000000..86d0a0a58c --- /dev/null +++ b/src/file/paragraph/math/brackets/math-ending-char.ts @@ -0,0 +1,14 @@ +// http://www.datypic.com/sc/ooxml/e-m_endChr-1.html +import { XmlAttributeComponent, XmlComponent } from "file/xml-components"; + +class MathEndingCharacterAttributes extends XmlAttributeComponent<{ readonly character: string }> { + protected readonly xmlKeys = { character: "m:val" }; +} + +export class MathEndingCharacter extends XmlComponent { + constructor(character: string) { + super("m:endChr"); + + this.root.push(new MathEndingCharacterAttributes({ character })); + } +} diff --git a/src/file/paragraph/math/brackets/math-ending-character.spec.ts b/src/file/paragraph/math/brackets/math-ending-character.spec.ts new file mode 100644 index 0000000000..ba28ac7840 --- /dev/null +++ b/src/file/paragraph/math/brackets/math-ending-character.spec.ts @@ -0,0 +1,22 @@ +import { expect } from "chai"; + +import { Formatter } from "export/formatter"; + +import { MathEndingCharacter } from "./math-ending-char"; + +describe("MathEndingCharacter", () => { + describe("#constructor()", () => { + it("should create a MathEndingCharacter with correct root key", () => { + const mathEndingCharacter = new MathEndingCharacter("]"); + + const tree = new Formatter().format(mathEndingCharacter); + expect(tree).to.deep.equal({ + "m:endChr": { + _attr: { + "m:val": "]", + }, + }, + }); + }); + }); +}); diff --git a/src/file/paragraph/math/brackets/math-round-brackets.spec.ts b/src/file/paragraph/math/brackets/math-round-brackets.spec.ts new file mode 100644 index 0000000000..cb28126a20 --- /dev/null +++ b/src/file/paragraph/math/brackets/math-round-brackets.spec.ts @@ -0,0 +1,36 @@ +import { expect } from "chai"; + +import { Formatter } from "export/formatter"; + +import { MathRun } from "../math-run"; +import { MathRoundBrackets } from "./math-round-brackets"; + +describe("MathRoundBrackets", () => { + describe("#constructor()", () => { + it("should create a MathRoundBrackets with correct root key", () => { + const mathRoundBrackets = new MathRoundBrackets({ + child: new MathRun("60"), + }); + + const tree = new Formatter().format(mathRoundBrackets); + expect(tree).to.deep.equal({ + "m:d": [ + { + "m:dPr": {}, + }, + { + "m:e": [ + { + "m:r": [ + { + "m:t": ["60"], + }, + ], + }, + ], + }, + ], + }); + }); + }); +}); diff --git a/src/file/paragraph/math/brackets/math-round-brackets.ts b/src/file/paragraph/math/brackets/math-round-brackets.ts new file mode 100644 index 0000000000..2302fa30f1 --- /dev/null +++ b/src/file/paragraph/math/brackets/math-round-brackets.ts @@ -0,0 +1,15 @@ +// http://www.datypic.com/sc/ooxml/e-m_d-1.html +import { XmlComponent } from "file/xml-components"; + +import { MathComponent } from "../math-component"; +import { MathBase } from "../n-ary"; +import { MathBracketProperties } from "./math-bracket-properties"; + +export class MathRoundBrackets extends XmlComponent { + constructor(options: { readonly child: MathComponent }) { + super("m:d"); + + this.root.push(new MathBracketProperties()); + this.root.push(new MathBase(options.child)); + } +} diff --git a/src/file/paragraph/math/brackets/math-square-brackets.spec.ts b/src/file/paragraph/math/brackets/math-square-brackets.spec.ts new file mode 100644 index 0000000000..03b8aef03d --- /dev/null +++ b/src/file/paragraph/math/brackets/math-square-brackets.spec.ts @@ -0,0 +1,51 @@ +import { expect } from "chai"; + +import { Formatter } from "export/formatter"; + +import { MathRun } from "../math-run"; +import { MathSquareBrackets } from "./math-square-brackets"; + +describe("MathSquareBrackets", () => { + describe("#constructor()", () => { + it("should create a MathSquareBrackets with correct root key", () => { + const mathSquareBrackets = new MathSquareBrackets({ + child: new MathRun("60"), + }); + + const tree = new Formatter().format(mathSquareBrackets); + expect(tree).to.deep.equal({ + "m:d": [ + { + "m:dPr": [ + { + "m:begChr": { + _attr: { + "m:val": "[", + }, + }, + }, + { + "m:endChr": { + _attr: { + "m:val": "]", + }, + }, + }, + ], + }, + { + "m:e": [ + { + "m:r": [ + { + "m:t": ["60"], + }, + ], + }, + ], + }, + ], + }); + }); + }); +}); diff --git a/src/file/paragraph/math/brackets/math-square-brackets.ts b/src/file/paragraph/math/brackets/math-square-brackets.ts new file mode 100644 index 0000000000..3c3d385357 --- /dev/null +++ b/src/file/paragraph/math/brackets/math-square-brackets.ts @@ -0,0 +1,20 @@ +// http://www.datypic.com/sc/ooxml/e-m_d-1.html +import { XmlComponent } from "file/xml-components"; + +import { MathComponent } from "../math-component"; +import { MathBase } from "../n-ary"; +import { MathBracketProperties } from "./math-bracket-properties"; + +export class MathSquareBrackets extends XmlComponent { + constructor(options: { readonly child: MathComponent }) { + super("m:d"); + + this.root.push( + new MathBracketProperties({ + beginningCharacter: "[", + endingCharacter: "]", + }), + ); + this.root.push(new MathBase(options.child)); + } +} diff --git a/src/file/paragraph/math/fraction/math-denominator.ts b/src/file/paragraph/math/fraction/math-denominator.ts index da9f283bdc..f640324c2e 100644 --- a/src/file/paragraph/math/fraction/math-denominator.ts +++ b/src/file/paragraph/math/fraction/math-denominator.ts @@ -3,7 +3,7 @@ import { XmlComponent } from "file/xml-components"; import { MathRun } from "../math-run"; export class MathDenominator extends XmlComponent { - constructor(readonly text: string) { + constructor(text: string) { super("m:den"); this.root.push(new MathRun(text)); diff --git a/src/file/paragraph/math/fraction/math-fraction.ts b/src/file/paragraph/math/fraction/math-fraction.ts index 062c3ef575..574a989bba 100644 --- a/src/file/paragraph/math/fraction/math-fraction.ts +++ b/src/file/paragraph/math/fraction/math-fraction.ts @@ -9,7 +9,7 @@ export interface IMathFractionOptions { } export class MathFraction extends XmlComponent { - constructor(readonly options: IMathFractionOptions) { + constructor(options: IMathFractionOptions) { super("m:f"); this.root.push(options.numerator); diff --git a/src/file/paragraph/math/fraction/math-numerator.ts b/src/file/paragraph/math/fraction/math-numerator.ts index 68799328cb..f7e68715b8 100644 --- a/src/file/paragraph/math/fraction/math-numerator.ts +++ b/src/file/paragraph/math/fraction/math-numerator.ts @@ -3,7 +3,7 @@ import { XmlComponent } from "file/xml-components"; import { MathRun } from "../math-run"; export class MathNumerator extends XmlComponent { - constructor(readonly text: string) { + constructor(text: string) { super("m:num"); this.root.push(new MathRun(text)); diff --git a/src/file/paragraph/math/function/index.ts b/src/file/paragraph/math/function/index.ts new file mode 100644 index 0000000000..58243c2710 --- /dev/null +++ b/src/file/paragraph/math/function/index.ts @@ -0,0 +1,3 @@ +export * from "./math-function"; +export * from "./math-function-name"; +export * from "./math-function-properties"; diff --git a/src/file/paragraph/math/function/math-function-name.spec.ts b/src/file/paragraph/math/function/math-function-name.spec.ts new file mode 100644 index 0000000000..bdb38d7596 --- /dev/null +++ b/src/file/paragraph/math/function/math-function-name.spec.ts @@ -0,0 +1,27 @@ +import { expect } from "chai"; + +import { Formatter } from "export/formatter"; + +import { MathRun } from "../math-run"; +import { MathFunctionName } from "./math-function-name"; + +describe("MathFunctionName", () => { + describe("#constructor()", () => { + it("should create a MathFunctionName with correct root key", () => { + const mathFunctionName = new MathFunctionName(new MathRun("2")); + + const tree = new Formatter().format(mathFunctionName); + expect(tree).to.deep.equal({ + "m:fName": [ + { + "m:r": [ + { + "m:t": ["2"], + }, + ], + }, + ], + }); + }); + }); +}); diff --git a/src/file/paragraph/math/function/math-function-name.ts b/src/file/paragraph/math/function/math-function-name.ts new file mode 100644 index 0000000000..011e82133a --- /dev/null +++ b/src/file/paragraph/math/function/math-function-name.ts @@ -0,0 +1,11 @@ +// http://www.datypic.com/sc/ooxml/e-m_fName-1.html +import { XmlComponent } from "file/xml-components"; +import { MathComponent } from "../math-component"; + +export class MathFunctionName extends XmlComponent { + constructor(child: MathComponent) { + super("m:fName"); + + this.root.push(child); + } +} diff --git a/src/file/paragraph/math/function/math-function-properties.spec.ts b/src/file/paragraph/math/function/math-function-properties.spec.ts new file mode 100644 index 0000000000..63ec2e4f60 --- /dev/null +++ b/src/file/paragraph/math/function/math-function-properties.spec.ts @@ -0,0 +1,18 @@ +import { expect } from "chai"; + +import { Formatter } from "export/formatter"; + +import { MathFunctionProperties } from "./math-function-properties"; + +describe("MathFunctionProperties", () => { + describe("#constructor()", () => { + it("should create a MathFunctionProperties with correct root key", () => { + const mathFunctionProperties = new MathFunctionProperties(); + + const tree = new Formatter().format(mathFunctionProperties); + expect(tree).to.deep.equal({ + "m:funcPr": {}, + }); + }); + }); +}); diff --git a/src/file/paragraph/math/function/math-function-properties.ts b/src/file/paragraph/math/function/math-function-properties.ts new file mode 100644 index 0000000000..f6e8d608ac --- /dev/null +++ b/src/file/paragraph/math/function/math-function-properties.ts @@ -0,0 +1,8 @@ +// http://www.datypic.com/sc/ooxml/e-m_radPr-1.html +import { XmlComponent } from "file/xml-components"; + +export class MathFunctionProperties extends XmlComponent { + constructor() { + super("m:funcPr"); + } +} diff --git a/src/file/paragraph/math/function/math-function.spec.ts b/src/file/paragraph/math/function/math-function.spec.ts new file mode 100644 index 0000000000..4320a853c6 --- /dev/null +++ b/src/file/paragraph/math/function/math-function.spec.ts @@ -0,0 +1,48 @@ +import { expect } from "chai"; + +import { Formatter } from "export/formatter"; + +import { MathRun } from "../math-run"; +import { MathFunction } from "./math-function"; + +describe("MathFunction", () => { + describe("#constructor()", () => { + it("should create a MathFunction with correct root key", () => { + const mathFunction = new MathFunction({ + name: new MathRun("sin"), + child: new MathRun("60"), + }); + + const tree = new Formatter().format(mathFunction); + expect(tree).to.deep.equal({ + "m:func": [ + { + "m:funcPr": {}, + }, + { + "m:fName": [ + { + "m:r": [ + { + "m:t": ["sin"], + }, + ], + }, + ], + }, + { + "m:e": [ + { + "m:r": [ + { + "m:t": ["60"], + }, + ], + }, + ], + }, + ], + }); + }); + }); +}); diff --git a/src/file/paragraph/math/function/math-function.ts b/src/file/paragraph/math/function/math-function.ts new file mode 100644 index 0000000000..b3de75ddb8 --- /dev/null +++ b/src/file/paragraph/math/function/math-function.ts @@ -0,0 +1,22 @@ +// http://www.datypic.com/sc/ooxml/e-m_func-1.html +import { XmlComponent } from "file/xml-components"; + +import { MathComponent } from "../math-component"; +import { MathBase } from "../n-ary"; +import { MathFunctionName } from "./math-function-name"; +import { MathFunctionProperties } from "./math-function-properties"; + +export interface IMathFunctionOptions { + readonly child: MathComponent; + readonly name: MathComponent; +} + +export class MathFunction extends XmlComponent { + constructor(options: IMathFunctionOptions) { + super("m:func"); + + this.root.push(new MathFunctionProperties()); + this.root.push(new MathFunctionName(options.name)); + this.root.push(new MathBase(options.child)); + } +} diff --git a/src/file/paragraph/math/index.ts b/src/file/paragraph/math/index.ts index 063678689c..b42ec908f6 100644 --- a/src/file/paragraph/math/index.ts +++ b/src/file/paragraph/math/index.ts @@ -2,3 +2,8 @@ export * from "./math"; export * from "./math-run"; export * from "./fraction"; export * from "./n-ary"; +export * from "./script"; +export * from "./math-component"; +export * from "./radical"; +export * from "./function"; +export * from "./brackets"; diff --git a/src/file/paragraph/math/math-component.ts b/src/file/paragraph/math/math-component.ts new file mode 100644 index 0000000000..afe83235f4 --- /dev/null +++ b/src/file/paragraph/math/math-component.ts @@ -0,0 +1,21 @@ +import { MathAngledBrackets, MathCurlyBrackets, MathRoundBrackets, MathSquareBrackets } from "./brackets"; +import { MathFraction } from "./fraction"; +import { MathFunction } from "./function"; +import { MathRun } from "./math-run"; +import { MathSum } from "./n-ary"; +import { MathRadical } from "./radical"; +import { MathSubScript, MathSubSuperScript, MathSuperScript } from "./script"; + +export type MathComponent = + | MathRun + | MathFraction + | MathSum + | MathSuperScript + | MathSubScript + | MathSubSuperScript + | MathRadical + | MathFunction + | MathRoundBrackets + | MathCurlyBrackets + | MathAngledBrackets + | MathSquareBrackets; diff --git a/src/file/paragraph/math/math-run.ts b/src/file/paragraph/math/math-run.ts index 3b7cceb362..0d2c48910f 100644 --- a/src/file/paragraph/math/math-run.ts +++ b/src/file/paragraph/math/math-run.ts @@ -4,7 +4,7 @@ import { XmlComponent } from "file/xml-components"; import { MathText } from "./math-text"; export class MathRun extends XmlComponent { - constructor(readonly text: string) { + constructor(text: string) { super("m:r"); this.root.push(new MathText(text)); diff --git a/src/file/paragraph/math/math-text.ts b/src/file/paragraph/math/math-text.ts index 907294aab9..6087c7e611 100644 --- a/src/file/paragraph/math/math-text.ts +++ b/src/file/paragraph/math/math-text.ts @@ -1,7 +1,7 @@ import { XmlComponent } from "file/xml-components"; export class MathText extends XmlComponent { - constructor(readonly text: string) { + constructor(text: string) { super("m:t"); this.root.push(text); diff --git a/src/file/paragraph/math/math.ts b/src/file/paragraph/math/math.ts index 43272ed941..69ebae4dff 100644 --- a/src/file/paragraph/math/math.ts +++ b/src/file/paragraph/math/math.ts @@ -1,16 +1,14 @@ // 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"; -import { MathSum } from "./n-ary"; +import { MathComponent } from "./math-component"; export interface IMathOptions { - readonly children: Array; + readonly children: MathComponent[]; } export class Math extends XmlComponent { - constructor(readonly options: IMathOptions) { + constructor(options: IMathOptions) { super("m:oMath"); for (const child of options.children) { diff --git a/src/file/paragraph/math/n-ary/math-accent-character.ts b/src/file/paragraph/math/n-ary/math-accent-character.ts index 3a5a10d7d2..00182bd1f2 100644 --- a/src/file/paragraph/math/n-ary/math-accent-character.ts +++ b/src/file/paragraph/math/n-ary/math-accent-character.ts @@ -6,7 +6,7 @@ class MathAccentCharacterAttributes extends XmlAttributeComponent<{ readonly acc } export class MathAccentCharacter extends XmlComponent { - constructor(readonly accent: string) { + constructor(accent: string) { super("m:chr"); this.root.push(new MathAccentCharacterAttributes({ accent })); diff --git a/src/file/paragraph/math/n-ary/math-base.ts b/src/file/paragraph/math/n-ary/math-base.ts index e75231cdb2..7c06a67c23 100644 --- a/src/file/paragraph/math/n-ary/math-base.ts +++ b/src/file/paragraph/math/n-ary/math-base.ts @@ -1,10 +1,10 @@ // http://www.datypic.com/sc/ooxml/e-m_e-1.html import { XmlComponent } from "file/xml-components"; -import { MathRun } from "../math-run"; +import { MathComponent } from "../math-component"; export class MathBase extends XmlComponent { - constructor(readonly run: MathRun) { + constructor(run: MathComponent) { super("m:e"); this.root.push(run); diff --git a/src/file/paragraph/math/n-ary/math-naray-properties.spec.ts b/src/file/paragraph/math/n-ary/math-naray-properties.spec.ts index 01ac55fb03..70aa74a1c4 100644 --- a/src/file/paragraph/math/n-ary/math-naray-properties.spec.ts +++ b/src/file/paragraph/math/n-ary/math-naray-properties.spec.ts @@ -7,7 +7,7 @@ import { MathNArayProperties } from "./math-naray-properties"; describe("MathNArayProperties", () => { describe("#constructor()", () => { it("should create a MathNArayProperties with correct root key", () => { - const mathNArayProperties = new MathNArayProperties("∑"); + const mathNArayProperties = new MathNArayProperties("∑", true, true); const tree = new Formatter().format(mathNArayProperties); expect(tree).to.deep.equal({ @@ -29,5 +29,105 @@ describe("MathNArayProperties", () => { ], }); }); + + it("should add super-script hide attributes", () => { + const mathNArayProperties = new MathNArayProperties("∑", false, true); + + const tree = new Formatter().format(mathNArayProperties); + expect(tree).to.deep.equal({ + "m:naryPr": [ + { + "m:chr": { + _attr: { + "m:val": "∑", + }, + }, + }, + { + "m:limLoc": { + _attr: { + "m:val": "undOvr", + }, + }, + }, + { + "m:supHide": { + _attr: { + "m:val": 1, + }, + }, + }, + ], + }); + }); + + it("should add sub-script hide attributes", () => { + const mathNArayProperties = new MathNArayProperties("∑", true, false); + + const tree = new Formatter().format(mathNArayProperties); + expect(tree).to.deep.equal({ + "m:naryPr": [ + { + "m:chr": { + _attr: { + "m:val": "∑", + }, + }, + }, + { + "m:limLoc": { + _attr: { + "m:val": "undOvr", + }, + }, + }, + { + "m:subHide": { + _attr: { + "m:val": 1, + }, + }, + }, + ], + }); + }); + + it("should add both super-script and sub-script hide attributes", () => { + const mathNArayProperties = new MathNArayProperties("∑", false, false); + + const tree = new Formatter().format(mathNArayProperties); + expect(tree).to.deep.equal({ + "m:naryPr": [ + { + "m:chr": { + _attr: { + "m:val": "∑", + }, + }, + }, + { + "m:limLoc": { + _attr: { + "m:val": "undOvr", + }, + }, + }, + { + "m:supHide": { + _attr: { + "m:val": 1, + }, + }, + }, + { + "m:subHide": { + _attr: { + "m:val": 1, + }, + }, + }, + ], + }); + }); }); }); diff --git a/src/file/paragraph/math/n-ary/math-naray-properties.ts b/src/file/paragraph/math/n-ary/math-naray-properties.ts index 4b28b58794..f8e91e746a 100644 --- a/src/file/paragraph/math/n-ary/math-naray-properties.ts +++ b/src/file/paragraph/math/n-ary/math-naray-properties.ts @@ -3,12 +3,22 @@ import { XmlComponent } from "file/xml-components"; import { MathAccentCharacter } from "./math-accent-character"; import { MathLimitLocation } from "./math-limit-location"; +import { MathSubScriptHide } from "./math-sub-script-hide"; +import { MathSuperScriptHide } from "./math-super-script-hide"; export class MathNArayProperties extends XmlComponent { - constructor(readonly accent: string) { + constructor(accent: string, hasSuperScript: boolean, hasSubScript: boolean) { super("m:naryPr"); this.root.push(new MathAccentCharacter(accent)); this.root.push(new MathLimitLocation()); + + if (!hasSuperScript) { + this.root.push(new MathSuperScriptHide()); + } + + if (!hasSubScript) { + this.root.push(new MathSubScriptHide()); + } } } diff --git a/src/file/paragraph/math/n-ary/math-sub-script-hide.spec.ts b/src/file/paragraph/math/n-ary/math-sub-script-hide.spec.ts new file mode 100644 index 0000000000..2e9845172f --- /dev/null +++ b/src/file/paragraph/math/n-ary/math-sub-script-hide.spec.ts @@ -0,0 +1,22 @@ +import { expect } from "chai"; + +import { Formatter } from "export/formatter"; + +import { MathSubScriptHide } from "./math-sub-script-hide"; + +describe("MathSubScriptHide", () => { + describe("#constructor()", () => { + it("should create a MathSubScriptHide with correct root key", () => { + const mathSubScriptHide = new MathSubScriptHide(); + + const tree = new Formatter().format(mathSubScriptHide); + expect(tree).to.deep.equal({ + "m:subHide": { + _attr: { + "m:val": 1, + }, + }, + }); + }); + }); +}); diff --git a/src/file/paragraph/math/n-ary/math-sub-script-hide.ts b/src/file/paragraph/math/n-ary/math-sub-script-hide.ts new file mode 100644 index 0000000000..ef735744e0 --- /dev/null +++ b/src/file/paragraph/math/n-ary/math-sub-script-hide.ts @@ -0,0 +1,14 @@ +// http://www.datypic.com/sc/ooxml/e-m_subHide-1.html +import { XmlAttributeComponent, XmlComponent } from "file/xml-components"; + +class MathSubScriptHideAttributes extends XmlAttributeComponent<{ readonly hide: number }> { + protected readonly xmlKeys = { hide: "m:val" }; +} + +export class MathSubScriptHide extends XmlComponent { + constructor() { + super("m:subHide"); + + this.root.push(new MathSubScriptHideAttributes({ hide: 1 })); + } +} diff --git a/src/file/paragraph/math/n-ary/math-sub-script.spec.ts b/src/file/paragraph/math/n-ary/math-sub-script.spec.ts index d09908b192..5a4009811e 100644 --- a/src/file/paragraph/math/n-ary/math-sub-script.spec.ts +++ b/src/file/paragraph/math/n-ary/math-sub-script.spec.ts @@ -3,14 +3,14 @@ import { expect } from "chai"; import { Formatter } from "export/formatter"; import { MathRun } from "../math-run"; -import { MathSubScript } from "./math-sub-script"; +import { MathSubScriptElement } from "./math-sub-script"; -describe("MathSubScript", () => { +describe("MathSubScriptElement", () => { describe("#constructor()", () => { - it("should create a MathSubScript with correct root key", () => { - const mathSubScript = new MathSubScript(new MathRun("2+2")); + it("should create a MathSubScriptElement with correct root key", () => { + const mathSubScriptElement = new MathSubScriptElement(new MathRun("2+2")); - const tree = new Formatter().format(mathSubScript); + const tree = new Formatter().format(mathSubScriptElement); expect(tree).to.deep.equal({ "m:sub": [ { diff --git a/src/file/paragraph/math/n-ary/math-sub-script.ts b/src/file/paragraph/math/n-ary/math-sub-script.ts index 25fe8da894..aaa8530e74 100644 --- a/src/file/paragraph/math/n-ary/math-sub-script.ts +++ b/src/file/paragraph/math/n-ary/math-sub-script.ts @@ -1,12 +1,12 @@ -// http://www.datypic.com/sc/ooxml/e-m_e-1.html +// http://www.datypic.com/sc/ooxml/e-m_sub-3.html import { XmlComponent } from "file/xml-components"; -import { MathRun } from "../math-run"; +import { MathComponent } from "../math-component"; -export class MathSubScript extends XmlComponent { - constructor(readonly run: MathRun) { +export class MathSubScriptElement extends XmlComponent { + constructor(child: MathComponent) { super("m:sub"); - this.root.push(run); + this.root.push(child); } } diff --git a/src/file/paragraph/math/n-ary/math-sum.spec.ts b/src/file/paragraph/math/n-ary/math-sum.spec.ts index e168792b1f..9f0d213aa4 100644 --- a/src/file/paragraph/math/n-ary/math-sum.spec.ts +++ b/src/file/paragraph/math/n-ary/math-sum.spec.ts @@ -40,7 +40,7 @@ describe("MathSum", () => { { "m:r": [ { - "m:t": ["1"], + "m:t": ["2"], }, ], }, @@ -51,7 +51,7 @@ describe("MathSum", () => { { "m:r": [ { - "m:t": ["1"], + "m:t": ["3"], }, ], }, diff --git a/src/file/paragraph/math/n-ary/math-sum.ts b/src/file/paragraph/math/n-ary/math-sum.ts index 2664b87fad..a16603982d 100644 --- a/src/file/paragraph/math/n-ary/math-sum.ts +++ b/src/file/paragraph/math/n-ary/math-sum.ts @@ -1,25 +1,32 @@ // http://www.datypic.com/sc/ooxml/e-m_nary-1.html import { XmlComponent } from "file/xml-components"; -import { MathRun } from "../math-run"; +import { MathComponent } from "../math-component"; import { MathBase } from "./math-base"; import { MathNArayProperties } from "./math-naray-properties"; -import { MathSubScript } from "./math-sub-script"; -import { MathSuperScript } from "./math-super-script"; +import { MathSubScriptElement } from "./math-sub-script"; +import { MathSuperScriptElement } from "./math-super-script"; export interface IMathSumOptions { - readonly child: MathRun; - readonly subScript?: MathRun; - readonly superScript?: MathRun; + readonly child: MathComponent; + readonly subScript?: MathComponent; + readonly superScript?: MathComponent; } export class MathSum extends XmlComponent { - constructor(readonly options: IMathSumOptions) { + constructor(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 MathNArayProperties("∑", !!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.child)); } } diff --git a/src/file/paragraph/math/n-ary/math-super-script-hide.spec.ts b/src/file/paragraph/math/n-ary/math-super-script-hide.spec.ts new file mode 100644 index 0000000000..89a28564ac --- /dev/null +++ b/src/file/paragraph/math/n-ary/math-super-script-hide.spec.ts @@ -0,0 +1,22 @@ +import { expect } from "chai"; + +import { Formatter } from "export/formatter"; + +import { MathSuperScriptHide } from "./math-super-script-hide"; + +describe("MathSuperScriptHide", () => { + describe("#constructor()", () => { + it("should create a MathSuperScriptHide with correct root key", () => { + const mathSuperScriptHide = new MathSuperScriptHide(); + + const tree = new Formatter().format(mathSuperScriptHide); + expect(tree).to.deep.equal({ + "m:supHide": { + _attr: { + "m:val": 1, + }, + }, + }); + }); + }); +}); diff --git a/src/file/paragraph/math/n-ary/math-super-script-hide.ts b/src/file/paragraph/math/n-ary/math-super-script-hide.ts new file mode 100644 index 0000000000..a55c15a7ad --- /dev/null +++ b/src/file/paragraph/math/n-ary/math-super-script-hide.ts @@ -0,0 +1,14 @@ +// http://www.datypic.com/sc/ooxml/e-m_subHide-1.html +import { XmlAttributeComponent, XmlComponent } from "file/xml-components"; + +class MathSuperScriptHideAttributes extends XmlAttributeComponent<{ readonly hide: number }> { + protected readonly xmlKeys = { hide: "m:val" }; +} + +export class MathSuperScriptHide extends XmlComponent { + constructor() { + super("m:supHide"); + + this.root.push(new MathSuperScriptHideAttributes({ hide: 1 })); + } +} diff --git a/src/file/paragraph/math/n-ary/math-super-script.spec.ts b/src/file/paragraph/math/n-ary/math-super-script.spec.ts index 7270276c28..85a5195c0f 100644 --- a/src/file/paragraph/math/n-ary/math-super-script.spec.ts +++ b/src/file/paragraph/math/n-ary/math-super-script.spec.ts @@ -3,14 +3,14 @@ import { expect } from "chai"; import { Formatter } from "export/formatter"; import { MathRun } from "../math-run"; -import { MathSuperScript } from "./math-super-script"; +import { MathSuperScriptElement } from "./math-super-script"; -describe("MathSuperScript", () => { +describe("MathSuperScriptElement", () => { describe("#constructor()", () => { - it("should create a MathSuperScript with correct root key", () => { - const mathSuperScript = new MathSuperScript(new MathRun("2+2")); + it("should create a MathSuperScriptElement with correct root key", () => { + const mathSuperScriptElement = new MathSuperScriptElement(new MathRun("2+2")); - const tree = new Formatter().format(mathSuperScript); + const tree = new Formatter().format(mathSuperScriptElement); expect(tree).to.deep.equal({ "m:sup": [ { diff --git a/src/file/paragraph/math/n-ary/math-super-script.ts b/src/file/paragraph/math/n-ary/math-super-script.ts index 8296c59f6a..1d29bda52b 100644 --- a/src/file/paragraph/math/n-ary/math-super-script.ts +++ b/src/file/paragraph/math/n-ary/math-super-script.ts @@ -1,12 +1,12 @@ -// http://www.datypic.com/sc/ooxml/e-m_e-1.html +// http://www.datypic.com/sc/ooxml/e-m_sup-3.html import { XmlComponent } from "file/xml-components"; -import { MathRun } from "../math-run"; +import { MathComponent } from "../math-component"; -export class MathSuperScript extends XmlComponent { - constructor(readonly run: MathRun) { +export class MathSuperScriptElement extends XmlComponent { + constructor(child: MathComponent) { super("m:sup"); - this.root.push(run); + this.root.push(child); } } diff --git a/src/file/paragraph/math/radical/index.ts b/src/file/paragraph/math/radical/index.ts new file mode 100644 index 0000000000..78240ccd13 --- /dev/null +++ b/src/file/paragraph/math/radical/index.ts @@ -0,0 +1,3 @@ +export * from "./math-degree"; +export * from "./math-radical"; +export * from "./math-radical-properties"; diff --git a/src/file/paragraph/math/radical/math-degree-hide.spec.ts b/src/file/paragraph/math/radical/math-degree-hide.spec.ts new file mode 100644 index 0000000000..0c3f335a13 --- /dev/null +++ b/src/file/paragraph/math/radical/math-degree-hide.spec.ts @@ -0,0 +1,22 @@ +import { expect } from "chai"; + +import { Formatter } from "export/formatter"; + +import { MathDegreeHide } from "./math-degree-hide"; + +describe("MathDegreeHide", () => { + describe("#constructor()", () => { + it("should create a MathDegreeHide with correct root key", () => { + const mathDegreeHide = new MathDegreeHide(); + + const tree = new Formatter().format(mathDegreeHide); + expect(tree).to.deep.equal({ + "m:degHide": { + _attr: { + "m:val": 1, + }, + }, + }); + }); + }); +}); diff --git a/src/file/paragraph/math/radical/math-degree-hide.ts b/src/file/paragraph/math/radical/math-degree-hide.ts new file mode 100644 index 0000000000..c1b9dd2ae5 --- /dev/null +++ b/src/file/paragraph/math/radical/math-degree-hide.ts @@ -0,0 +1,14 @@ +// http://www.datypic.com/sc/ooxml/e-m_degHide-1.html +import { XmlAttributeComponent, XmlComponent } from "file/xml-components"; + +class MathDegreeHideAttributes extends XmlAttributeComponent<{ readonly hide: number }> { + protected readonly xmlKeys = { hide: "m:val" }; +} + +export class MathDegreeHide extends XmlComponent { + constructor() { + super("m:degHide"); + + this.root.push(new MathDegreeHideAttributes({ hide: 1 })); + } +} diff --git a/src/file/paragraph/math/radical/math-degree.spec.ts b/src/file/paragraph/math/radical/math-degree.spec.ts new file mode 100644 index 0000000000..6a1c28656b --- /dev/null +++ b/src/file/paragraph/math/radical/math-degree.spec.ts @@ -0,0 +1,36 @@ +import { expect } from "chai"; + +import { Formatter } from "export/formatter"; + +import { MathRun } from "../math-run"; +import { MathDegree } from "./math-degree"; + +describe("MathDegree", () => { + describe("#constructor()", () => { + it("should create a MathDegree with correct root key", () => { + const mathDegree = new MathDegree(); + + const tree = new Formatter().format(mathDegree); + expect(tree).to.deep.equal({ + "m:deg": {}, + }); + }); + + it("should create a MathDegree with correct root key with child", () => { + const mathDegree = new MathDegree(new MathRun("2")); + + const tree = new Formatter().format(mathDegree); + expect(tree).to.deep.equal({ + "m:deg": [ + { + "m:r": [ + { + "m:t": ["2"], + }, + ], + }, + ], + }); + }); + }); +}); diff --git a/src/file/paragraph/math/radical/math-degree.ts b/src/file/paragraph/math/radical/math-degree.ts new file mode 100644 index 0000000000..402ee8ffda --- /dev/null +++ b/src/file/paragraph/math/radical/math-degree.ts @@ -0,0 +1,13 @@ +// http://www.datypic.com/sc/ooxml/e-m_deg-1.html +import { XmlComponent } from "file/xml-components"; +import { MathComponent } from "../math-component"; + +export class MathDegree extends XmlComponent { + constructor(child?: MathComponent) { + super("m:deg"); + + if (!!child) { + this.root.push(child); + } + } +} diff --git a/src/file/paragraph/math/radical/math-radical-properties.spec.ts b/src/file/paragraph/math/radical/math-radical-properties.spec.ts new file mode 100644 index 0000000000..12d29a2962 --- /dev/null +++ b/src/file/paragraph/math/radical/math-radical-properties.spec.ts @@ -0,0 +1,35 @@ +import { expect } from "chai"; + +import { Formatter } from "export/formatter"; + +import { MathRadicalProperties } from "./math-radical-properties"; + +describe("MathRadicalProperties", () => { + describe("#constructor()", () => { + it("should create a MathRadicalProperties with correct root key", () => { + const mathRadicalProperties = new MathRadicalProperties(true); + + const tree = new Formatter().format(mathRadicalProperties); + expect(tree).to.deep.equal({ + "m:radPr": {}, + }); + }); + + it("should create a MathRadicalProperties with correct root key with degree hide", () => { + const mathRadicalProperties = new MathRadicalProperties(false); + + const tree = new Formatter().format(mathRadicalProperties); + expect(tree).to.deep.equal({ + "m:radPr": [ + { + "m:degHide": { + _attr: { + "m:val": 1, + }, + }, + }, + ], + }); + }); + }); +}); diff --git a/src/file/paragraph/math/radical/math-radical-properties.ts b/src/file/paragraph/math/radical/math-radical-properties.ts new file mode 100644 index 0000000000..b4e90ed4df --- /dev/null +++ b/src/file/paragraph/math/radical/math-radical-properties.ts @@ -0,0 +1,13 @@ +// http://www.datypic.com/sc/ooxml/e-m_radPr-1.html +import { XmlComponent } from "file/xml-components"; +import { MathDegreeHide } from "./math-degree-hide"; + +export class MathRadicalProperties extends XmlComponent { + constructor(hasDegree: boolean) { + super("m:radPr"); + + if (!hasDegree) { + this.root.push(new MathDegreeHide()); + } + } +} diff --git a/src/file/paragraph/math/radical/math-radical.spec.ts b/src/file/paragraph/math/radical/math-radical.spec.ts new file mode 100644 index 0000000000..7d7d7c4225 --- /dev/null +++ b/src/file/paragraph/math/radical/math-radical.spec.ts @@ -0,0 +1,48 @@ +import { expect } from "chai"; + +import { Formatter } from "export/formatter"; + +import { MathRun } from "../math-run"; +import { MathRadical } from "./math-radical"; + +describe("MathRadical", () => { + describe("#constructor()", () => { + it("should create a MathRadical with correct root key", () => { + const mathRadical = new MathRadical({ + child: new MathRun("e"), + degree: new MathRun("2"), + }); + + const tree = new Formatter().format(mathRadical); + expect(tree).to.deep.equal({ + "m:rad": [ + { + "m:radPr": {}, + }, + { + "m:deg": [ + { + "m:r": [ + { + "m:t": ["2"], + }, + ], + }, + ], + }, + { + "m:e": [ + { + "m:r": [ + { + "m:t": ["e"], + }, + ], + }, + ], + }, + ], + }); + }); + }); +}); diff --git a/src/file/paragraph/math/radical/math-radical.ts b/src/file/paragraph/math/radical/math-radical.ts new file mode 100644 index 0000000000..76fc2dc7ca --- /dev/null +++ b/src/file/paragraph/math/radical/math-radical.ts @@ -0,0 +1,22 @@ +// http://www.datypic.com/sc/ooxml/e-m_rad-1.html +import { XmlComponent } from "file/xml-components"; + +import { MathComponent } from "../math-component"; +import { MathBase } from "../n-ary"; +import { MathDegree } from "./math-degree"; +import { MathRadicalProperties } from "./math-radical-properties"; + +export interface IMathRadicalOptions { + readonly child: MathComponent; + readonly degree?: MathComponent; +} + +export class MathRadical extends XmlComponent { + constructor(options: IMathRadicalOptions) { + super("m:rad"); + + this.root.push(new MathRadicalProperties(!!options.degree)); + this.root.push(new MathDegree(options.degree)); + this.root.push(new MathBase(options.child)); + } +} diff --git a/src/file/paragraph/math/script/index.ts b/src/file/paragraph/math/script/index.ts new file mode 100644 index 0000000000..a83e54975e --- /dev/null +++ b/src/file/paragraph/math/script/index.ts @@ -0,0 +1,4 @@ +export * from "./super-script"; +export * from "./sub-script"; +export * from "./sub-super-script"; +export * from "./pre-sub-super-script"; diff --git a/src/file/paragraph/math/script/pre-sub-super-script/index.ts b/src/file/paragraph/math/script/pre-sub-super-script/index.ts new file mode 100644 index 0000000000..8660a75017 --- /dev/null +++ b/src/file/paragraph/math/script/pre-sub-super-script/index.ts @@ -0,0 +1,2 @@ +export * from "./math-pre-sub-super-script-function"; +export * from "./math-pre-sub-super-script-function-properties"; diff --git a/src/file/paragraph/math/script/pre-sub-super-script/math-pre-sub-super-script-function-properties.spec.ts b/src/file/paragraph/math/script/pre-sub-super-script/math-pre-sub-super-script-function-properties.spec.ts new file mode 100644 index 0000000000..a6f1d5383e --- /dev/null +++ b/src/file/paragraph/math/script/pre-sub-super-script/math-pre-sub-super-script-function-properties.spec.ts @@ -0,0 +1,17 @@ +import { expect } from "chai"; + +import { Formatter } from "export/formatter"; +import { MathPreSubSuperScriptProperties } from "./math-pre-sub-super-script-function-properties"; + +describe("MathPreSubSuperScriptProperties", () => { + describe("#constructor()", () => { + it("should create a MathPreSubSuperScriptProperties with correct root key", () => { + const mathPreSubSuperScriptProperties = new MathPreSubSuperScriptProperties(); + + const tree = new Formatter().format(mathPreSubSuperScriptProperties); + expect(tree).to.deep.equal({ + "m:sPrePr": {}, + }); + }); + }); +}); diff --git a/src/file/paragraph/math/script/pre-sub-super-script/math-pre-sub-super-script-function-properties.ts b/src/file/paragraph/math/script/pre-sub-super-script/math-pre-sub-super-script-function-properties.ts new file mode 100644 index 0000000000..fed0cb3564 --- /dev/null +++ b/src/file/paragraph/math/script/pre-sub-super-script/math-pre-sub-super-script-function-properties.ts @@ -0,0 +1,8 @@ +// http://www.datypic.com/sc/ooxml/e-m_sPrePr-1.html +import { XmlComponent } from "file/xml-components"; + +export class MathPreSubSuperScriptProperties extends XmlComponent { + constructor() { + super("m:sPrePr"); + } +} diff --git a/src/file/paragraph/math/script/pre-sub-super-script/math-pre-sub-super-script-function.spec.ts b/src/file/paragraph/math/script/pre-sub-super-script/math-pre-sub-super-script-function.spec.ts new file mode 100644 index 0000000000..956a2ff445 --- /dev/null +++ b/src/file/paragraph/math/script/pre-sub-super-script/math-pre-sub-super-script-function.spec.ts @@ -0,0 +1,60 @@ +import { expect } from "chai"; + +import { Formatter } from "export/formatter"; + +import { MathRun } from "../../math-run"; +import { MathPreSubSuperScript } from "./math-pre-sub-super-script-function"; + +describe("MathPreSubScript", () => { + describe("#constructor()", () => { + it("should create a MathPreSubScript with correct root key", () => { + const mathPreSubScript = new MathPreSubSuperScript({ + child: new MathRun("e"), + subScript: new MathRun("2"), + superScript: new MathRun("5"), + }); + + const tree = new Formatter().format(mathPreSubScript); + expect(tree).to.deep.equal({ + "m:sPre": [ + { + "m:sPrePr": {}, + }, + { + "m:e": [ + { + "m:r": [ + { + "m:t": ["e"], + }, + ], + }, + ], + }, + { + "m:sub": [ + { + "m:r": [ + { + "m:t": ["2"], + }, + ], + }, + ], + }, + { + "m:sup": [ + { + "m:r": [ + { + "m:t": ["5"], + }, + ], + }, + ], + }, + ], + }); + }); + }); +}); diff --git a/src/file/paragraph/math/script/pre-sub-super-script/math-pre-sub-super-script-function.ts b/src/file/paragraph/math/script/pre-sub-super-script/math-pre-sub-super-script-function.ts new file mode 100644 index 0000000000..a0bdc321a0 --- /dev/null +++ b/src/file/paragraph/math/script/pre-sub-super-script/math-pre-sub-super-script-function.ts @@ -0,0 +1,23 @@ +// http://www.datypic.com/sc/ooxml/e-m_sPre-1.html +import { XmlComponent } from "file/xml-components"; + +import { MathComponent } from "../../math-component"; +import { MathBase, MathSubScriptElement, MathSuperScriptElement } from "../../n-ary"; +import { MathPreSubSuperScriptProperties } from "./math-pre-sub-super-script-function-properties"; + +export interface IMathPreSubSuperScriptOptions { + readonly child: MathComponent; + readonly subScript: MathComponent; + readonly superScript: MathComponent; +} + +export class MathPreSubSuperScript extends XmlComponent { + constructor(options: IMathPreSubSuperScriptOptions) { + super("m:sPre"); + + this.root.push(new MathPreSubSuperScriptProperties()); + this.root.push(new MathBase(options.child)); + this.root.push(new MathSubScriptElement(options.subScript)); + this.root.push(new MathSuperScriptElement(options.superScript)); + } +} diff --git a/src/file/paragraph/math/script/sub-script/index.ts b/src/file/paragraph/math/script/sub-script/index.ts new file mode 100644 index 0000000000..0f1c27fe51 --- /dev/null +++ b/src/file/paragraph/math/script/sub-script/index.ts @@ -0,0 +1,2 @@ +export * from "./math-sub-script-function"; +export * from "./math-sub-script-function-properties"; diff --git a/src/file/paragraph/math/script/sub-script/math-sub-script-function-properties.spec.ts b/src/file/paragraph/math/script/sub-script/math-sub-script-function-properties.spec.ts new file mode 100644 index 0000000000..be229e066b --- /dev/null +++ b/src/file/paragraph/math/script/sub-script/math-sub-script-function-properties.spec.ts @@ -0,0 +1,17 @@ +import { expect } from "chai"; + +import { Formatter } from "export/formatter"; +import { MathSubScriptProperties } from "./math-sub-script-function-properties"; + +describe("MathSubScriptProperties", () => { + describe("#constructor()", () => { + it("should create a MathSubScriptProperties with correct root key", () => { + const mathSubScriptProperties = new MathSubScriptProperties(); + + const tree = new Formatter().format(mathSubScriptProperties); + expect(tree).to.deep.equal({ + "m:sSubPr": {}, + }); + }); + }); +}); diff --git a/src/file/paragraph/math/script/sub-script/math-sub-script-function-properties.ts b/src/file/paragraph/math/script/sub-script/math-sub-script-function-properties.ts new file mode 100644 index 0000000000..bf37b1d885 --- /dev/null +++ b/src/file/paragraph/math/script/sub-script/math-sub-script-function-properties.ts @@ -0,0 +1,8 @@ +// http://www.datypic.com/sc/ooxml/e-m_sSubPr-1.html +import { XmlComponent } from "file/xml-components"; + +export class MathSubScriptProperties extends XmlComponent { + constructor() { + super("m:sSubPr"); + } +} diff --git a/src/file/paragraph/math/script/sub-script/math-sub-script-function.spec.ts b/src/file/paragraph/math/script/sub-script/math-sub-script-function.spec.ts new file mode 100644 index 0000000000..dd33598d81 --- /dev/null +++ b/src/file/paragraph/math/script/sub-script/math-sub-script-function.spec.ts @@ -0,0 +1,48 @@ +import { expect } from "chai"; + +import { Formatter } from "export/formatter"; + +import { MathRun } from "../../math-run"; +import { MathSubScript } from "./math-sub-script-function"; + +describe("MathSubScript", () => { + describe("#constructor()", () => { + it("should create a MathSubScript with correct root key", () => { + const mathSubScript = new MathSubScript({ + child: new MathRun("e"), + subScript: new MathRun("2"), + }); + + const tree = new Formatter().format(mathSubScript); + expect(tree).to.deep.equal({ + "m:sSub": [ + { + "m:sSubPr": {}, + }, + { + "m:e": [ + { + "m:r": [ + { + "m:t": ["e"], + }, + ], + }, + ], + }, + { + "m:sub": [ + { + "m:r": [ + { + "m:t": ["2"], + }, + ], + }, + ], + }, + ], + }); + }); + }); +}); diff --git a/src/file/paragraph/math/script/sub-script/math-sub-script-function.ts b/src/file/paragraph/math/script/sub-script/math-sub-script-function.ts new file mode 100644 index 0000000000..a5c26cd06c --- /dev/null +++ b/src/file/paragraph/math/script/sub-script/math-sub-script-function.ts @@ -0,0 +1,21 @@ +// http://www.datypic.com/sc/ooxml/e-m_sSub-1.html +import { XmlComponent } from "file/xml-components"; + +import { MathComponent } from "../../math-component"; +import { MathBase, MathSubScriptElement } from "../../n-ary"; +import { MathSubScriptProperties } from "./math-sub-script-function-properties"; + +export interface IMathSubScriptOptions { + readonly child: MathComponent; + readonly subScript: MathComponent; +} + +export class MathSubScript extends XmlComponent { + constructor(options: IMathSubScriptOptions) { + super("m:sSub"); + + this.root.push(new MathSubScriptProperties()); + this.root.push(new MathBase(options.child)); + this.root.push(new MathSubScriptElement(options.subScript)); + } +} diff --git a/src/file/paragraph/math/script/sub-super-script/index.ts b/src/file/paragraph/math/script/sub-super-script/index.ts new file mode 100644 index 0000000000..88ddeb69fa --- /dev/null +++ b/src/file/paragraph/math/script/sub-super-script/index.ts @@ -0,0 +1,2 @@ +export * from "./math-sub-super-script-function"; +export * from "./math-sub-super-script-function-properties"; diff --git a/src/file/paragraph/math/script/sub-super-script/math-sub-super-script-function-properties.spec.ts b/src/file/paragraph/math/script/sub-super-script/math-sub-super-script-function-properties.spec.ts new file mode 100644 index 0000000000..b0f934823a --- /dev/null +++ b/src/file/paragraph/math/script/sub-super-script/math-sub-super-script-function-properties.spec.ts @@ -0,0 +1,17 @@ +import { expect } from "chai"; + +import { Formatter } from "export/formatter"; +import { MathSubSuperScriptProperties } from "./math-sub-super-script-function-properties"; + +describe("MathSubSuperScriptProperties", () => { + describe("#constructor()", () => { + it("should create a MathSubSuperScriptProperties with correct root key", () => { + const mathSubSuperScriptProperties = new MathSubSuperScriptProperties(); + + const tree = new Formatter().format(mathSubSuperScriptProperties); + expect(tree).to.deep.equal({ + "m:sSubSupPr": {}, + }); + }); + }); +}); diff --git a/src/file/paragraph/math/script/sub-super-script/math-sub-super-script-function-properties.ts b/src/file/paragraph/math/script/sub-super-script/math-sub-super-script-function-properties.ts new file mode 100644 index 0000000000..b3218a7d3a --- /dev/null +++ b/src/file/paragraph/math/script/sub-super-script/math-sub-super-script-function-properties.ts @@ -0,0 +1,8 @@ +// http://www.datypic.com/sc/ooxml/e-m_sSubSupPr-1.html +import { XmlComponent } from "file/xml-components"; + +export class MathSubSuperScriptProperties extends XmlComponent { + constructor() { + super("m:sSubSupPr"); + } +} diff --git a/src/file/paragraph/math/script/sub-super-script/math-sub-super-script-function.spec.ts b/src/file/paragraph/math/script/sub-super-script/math-sub-super-script-function.spec.ts new file mode 100644 index 0000000000..5e7f976db7 --- /dev/null +++ b/src/file/paragraph/math/script/sub-super-script/math-sub-super-script-function.spec.ts @@ -0,0 +1,60 @@ +import { expect } from "chai"; + +import { Formatter } from "export/formatter"; + +import { MathRun } from "../../math-run"; +import { MathSubSuperScript } from "./math-sub-super-script-function"; + +describe("MathSubScript", () => { + describe("#constructor()", () => { + it("should create a MathSubScript with correct root key", () => { + const mathSubScript = new MathSubSuperScript({ + child: new MathRun("e"), + subScript: new MathRun("2"), + superScript: new MathRun("5"), + }); + + const tree = new Formatter().format(mathSubScript); + expect(tree).to.deep.equal({ + "m:sSubSup": [ + { + "m:sSubSupPr": {}, + }, + { + "m:e": [ + { + "m:r": [ + { + "m:t": ["e"], + }, + ], + }, + ], + }, + { + "m:sub": [ + { + "m:r": [ + { + "m:t": ["2"], + }, + ], + }, + ], + }, + { + "m:sup": [ + { + "m:r": [ + { + "m:t": ["5"], + }, + ], + }, + ], + }, + ], + }); + }); + }); +}); diff --git a/src/file/paragraph/math/script/sub-super-script/math-sub-super-script-function.ts b/src/file/paragraph/math/script/sub-super-script/math-sub-super-script-function.ts new file mode 100644 index 0000000000..91bdfe6781 --- /dev/null +++ b/src/file/paragraph/math/script/sub-super-script/math-sub-super-script-function.ts @@ -0,0 +1,23 @@ +// http://www.datypic.com/sc/ooxml/e-m_sSubSup-1.html +import { XmlComponent } from "file/xml-components"; + +import { MathComponent } from "../../math-component"; +import { MathBase, MathSubScriptElement, MathSuperScriptElement } from "../../n-ary"; +import { MathSubSuperScriptProperties } from "./math-sub-super-script-function-properties"; + +export interface IMathSubSuperScriptOptions { + readonly child: MathComponent; + readonly subScript: MathComponent; + readonly superScript: MathComponent; +} + +export class MathSubSuperScript extends XmlComponent { + constructor(options: IMathSubSuperScriptOptions) { + super("m:sSubSup"); + + this.root.push(new MathSubSuperScriptProperties()); + this.root.push(new MathBase(options.child)); + this.root.push(new MathSubScriptElement(options.subScript)); + this.root.push(new MathSuperScriptElement(options.superScript)); + } +} diff --git a/src/file/paragraph/math/script/super-script/index.ts b/src/file/paragraph/math/script/super-script/index.ts new file mode 100644 index 0000000000..2864fe9ac5 --- /dev/null +++ b/src/file/paragraph/math/script/super-script/index.ts @@ -0,0 +1,2 @@ +export * from "./math-super-script-function"; +export * from "./math-super-script-function-properties"; diff --git a/src/file/paragraph/math/script/super-script/math-super-script-function-properties.spec.ts b/src/file/paragraph/math/script/super-script/math-super-script-function-properties.spec.ts new file mode 100644 index 0000000000..f95920f152 --- /dev/null +++ b/src/file/paragraph/math/script/super-script/math-super-script-function-properties.spec.ts @@ -0,0 +1,17 @@ +import { expect } from "chai"; + +import { Formatter } from "export/formatter"; +import { MathSuperScriptProperties } from "./math-super-script-function-properties"; + +describe("MathSuperScriptProperties", () => { + describe("#constructor()", () => { + it("should create a MathSuperScriptProperties with correct root key", () => { + const mathSuperScriptProperties = new MathSuperScriptProperties(); + + const tree = new Formatter().format(mathSuperScriptProperties); + expect(tree).to.deep.equal({ + "m:sSupPr": {}, + }); + }); + }); +}); diff --git a/src/file/paragraph/math/script/super-script/math-super-script-function-properties.ts b/src/file/paragraph/math/script/super-script/math-super-script-function-properties.ts new file mode 100644 index 0000000000..ea5dbac20b --- /dev/null +++ b/src/file/paragraph/math/script/super-script/math-super-script-function-properties.ts @@ -0,0 +1,8 @@ +// http://www.datypic.com/sc/ooxml/e-m_sSupPr-1.html +import { XmlComponent } from "file/xml-components"; + +export class MathSuperScriptProperties extends XmlComponent { + constructor() { + super("m:sSupPr"); + } +} diff --git a/src/file/paragraph/math/script/super-script/math-super-script-function.spec.ts b/src/file/paragraph/math/script/super-script/math-super-script-function.spec.ts new file mode 100644 index 0000000000..6e6beb5e04 --- /dev/null +++ b/src/file/paragraph/math/script/super-script/math-super-script-function.spec.ts @@ -0,0 +1,48 @@ +import { expect } from "chai"; + +import { Formatter } from "export/formatter"; + +import { MathRun } from "../../math-run"; +import { MathSuperScript } from "./math-super-script-function"; + +describe("MathSuperScript", () => { + describe("#constructor()", () => { + it("should create a MathSuperScript with correct root key", () => { + const mathSuperScript = new MathSuperScript({ + child: new MathRun("e"), + superScript: new MathRun("2"), + }); + + const tree = new Formatter().format(mathSuperScript); + expect(tree).to.deep.equal({ + "m:sSup": [ + { + "m:sSupPr": {}, + }, + { + "m:e": [ + { + "m:r": [ + { + "m:t": ["e"], + }, + ], + }, + ], + }, + { + "m:sup": [ + { + "m:r": [ + { + "m:t": ["2"], + }, + ], + }, + ], + }, + ], + }); + }); + }); +}); diff --git a/src/file/paragraph/math/script/super-script/math-super-script-function.ts b/src/file/paragraph/math/script/super-script/math-super-script-function.ts new file mode 100644 index 0000000000..d48ece6985 --- /dev/null +++ b/src/file/paragraph/math/script/super-script/math-super-script-function.ts @@ -0,0 +1,21 @@ +// http://www.datypic.com/sc/ooxml/e-m_sSup-1.html +import { XmlComponent } from "file/xml-components"; + +import { MathComponent } from "../../math-component"; +import { MathBase, MathSuperScriptElement } from "../../n-ary"; +import { MathSuperScriptProperties } from "./math-super-script-function-properties"; + +export interface IMathSuperScriptOptions { + readonly child: MathComponent; + readonly superScript: MathComponent; +} + +export class MathSuperScript extends XmlComponent { + constructor(options: IMathSuperScriptOptions) { + super("m:sSup"); + + this.root.push(new MathSuperScriptProperties()); + this.root.push(new MathBase(options.child)); + this.root.push(new MathSuperScriptElement(options.superScript)); + } +} diff --git a/src/file/paragraph/paragraph.ts b/src/file/paragraph/paragraph.ts index 68fa7d00d4..60efb0da34 100644 --- a/src/file/paragraph/paragraph.ts +++ b/src/file/paragraph/paragraph.ts @@ -2,11 +2,11 @@ import { FootnoteReferenceRun } from "file/footnotes/footnote/run/reference-run"; import { IXmlableObject, XmlComponent } from "file/xml-components"; +import { File } from "../file"; +import { DeletedTextRun, InsertedTextRun } from "../track-revision"; import { PageBreak } from "./formatting/page-break"; import { Bookmark, HyperlinkRef } from "./links"; import { Math } from "./math"; -import { File } from "../file"; -import { InsertedTextRun, DeletedTextRun } from "../track-revision"; import { IParagraphPropertiesOptions, ParagraphProperties } from "./properties"; import { PictureRun, Run, SequentialIdentifier, SymbolRun, TextRun } from "./run"; diff --git a/src/file/settings/settings.ts b/src/file/settings/settings.ts index 2fa4a27f0a..d828d21fb5 100644 --- a/src/file/settings/settings.ts +++ b/src/file/settings/settings.ts @@ -1,7 +1,7 @@ import { XmlAttributeComponent, XmlComponent } from "file/xml-components"; import { Compatibility } from "./compatibility"; -import { UpdateFields } from "./update-fields"; import { TrackRevisions } from "./track-revisions"; +import { UpdateFields } from "./update-fields"; export interface ISettingsAttributesProperties { readonly wpc?: string; @@ -46,8 +46,8 @@ export class SettingsAttributes extends XmlAttributeComponent { describe("#constructor", () => { diff --git a/src/file/track-revision/track-revision-components/deleted-text-run.ts b/src/file/track-revision/track-revision-components/deleted-text-run.ts index 2342f70104..56d384b31b 100644 --- a/src/file/track-revision/track-revision-components/deleted-text-run.ts +++ b/src/file/track-revision/track-revision-components/deleted-text-run.ts @@ -1,12 +1,11 @@ -import { IChangedAttributesProperties, ChangeAttributes } from "../track-revision"; import { XmlComponent } from "file/xml-components"; -import { IRunOptions, RunProperties, IRunPropertiesOptions, FootnoteReferenceRun } from "../../index"; +import { FootnoteReferenceRun, IRunOptions, IRunPropertiesOptions, RunProperties } from "../../index"; import { Break } from "../../paragraph/run/break"; -import { Begin, Separate, End } from "../../paragraph/run/field"; +import { Begin, End, Separate } from "../../paragraph/run/field"; import { PageNumber } from "../../paragraph/run/run"; - -import { DeletedPage, DeletedNumberOfPages, DeletedNumberOfPagesSection } from "./deleted-page-number"; +import { ChangeAttributes, IChangedAttributesProperties } from "../track-revision"; +import { DeletedNumberOfPages, DeletedNumberOfPagesSection, DeletedPage } from "./deleted-page-number"; import { DeletedText } from "./deleted-text"; interface IDeletedRunOptions extends IRunPropertiesOptions, IChangedAttributesProperties { diff --git a/src/file/track-revision/track-revision-components/inserted-text-run.ts b/src/file/track-revision/track-revision-components/inserted-text-run.ts index 49bd7f53ae..3d39e92c1c 100644 --- a/src/file/track-revision/track-revision-components/inserted-text-run.ts +++ b/src/file/track-revision/track-revision-components/inserted-text-run.ts @@ -1,6 +1,7 @@ -import { IChangedAttributesProperties, ChangeAttributes } from "../track-revision"; import { XmlComponent } from "file/xml-components"; -import { TextRun, IRunOptions } from "../../index"; + +import { IRunOptions, TextRun } from "../../index"; +import { ChangeAttributes, IChangedAttributesProperties } from "../track-revision"; interface IInsertedRunOptions extends IChangedAttributesProperties, IRunOptions {} diff --git a/tslint.json b/tslint.json index d6eb606eba..cb979f920a 100644 --- a/tslint.json +++ b/tslint.json @@ -21,6 +21,8 @@ "no-duplicate-imports": true, "unnecessary-constructor": true, "file-name-casing": [true, "kebab-case"], + "interface-name": [true, "always-prefix"], + "ordered-imports": true, // Functional Programming Rules "no-parameter-reassignment": true, "readonly-keyword": true,