More math components

This commit is contained in:
Dolan
2020-10-10 13:41:26 +01:00
parent 32a84a5ad0
commit 700a74fd4c
85 changed files with 1716 additions and 123 deletions

View File

@ -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 }));

View File

@ -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);

View File

@ -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,
},
},
},
],
});
});
});
});

View File

@ -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());
}
}
}

View File

@ -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,
},
},
});
});
});
});

View File

@ -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 }));
}
}

View File

@ -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": [
{

View File

@ -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);
}
}

View File

@ -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"],
},
],
},

View File

@ -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));
}
}

View File

@ -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,
},
},
});
});
});
});

View File

@ -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 }));
}
}

View File

@ -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": [
{

View File

@ -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);
}
}