Add math limit component (#2510)

This commit is contained in:
yaokailun
2024-01-03 08:13:13 +08:00
committed by GitHub
parent 13cf3eee5a
commit c4ed19e589
9 changed files with 210 additions and 0 deletions

View File

@ -21,6 +21,8 @@ import {
Packer, Packer,
Paragraph, Paragraph,
TextRun, TextRun,
MathLimitLower,
MathLimitUpper,
} from "docx"; } from "docx";
const doc = new Document({ const doc = new Document({
@ -316,6 +318,23 @@ const doc = new Document({
}), }),
], ],
}), }),
new Paragraph({
children: [
new Math({
children: [
new MathLimitUpper({
children: [new MathRun("x")],
limit: [new MathRun("-")],
}),
new MathRun("="),
new MathLimitLower({
children: [new MathRun("lim")],
limit: [new MathRun("x→0")],
}),
],
}),
],
}),
], ],
}, },
], ],

View File

@ -263,3 +263,23 @@ new MathAngledBrackets({
], ],
}), }),
``` ```
### Limit
#### Limit Upper
```ts
new MathLimitUpper({
children: [new MathRun("x")],
limit: [new MathRun("-")],
}),
```
#### Limit Lower
```ts
new MathLimitLower({
children: [new MathRun("lim")],
limit: [new MathRun("x→0")],
}),
```

View File

@ -6,3 +6,6 @@ export * from "./math-sub-script";
export * from "./math-sum"; export * from "./math-sum";
export * from "./math-integral"; export * from "./math-integral";
export * from "./math-super-script"; export * from "./math-super-script";
export * from "./math-limit";
export * from "./math-limit-upper";
export * from "./math-limit-lower";

View File

@ -0,0 +1,45 @@
import { describe, expect, it } from "vitest";
import { Formatter } from "@export/formatter";
import { MathRun } from "../math-run";
import { MathLimitLower } from "./math-limit-lower";
describe("MathLimitLower", () => {
describe("#constructor()", () => {
it("should create a MathLimitLower with correct root key", () => {
const mathLimitLower = new MathLimitLower({
children: [new MathRun("lim")],
limit: [new MathRun("x→0")],
});
const tree = new Formatter().format(mathLimitLower);
expect(tree).to.deep.equal({
"m:limLow": [
{
"m:e": [
{
"m:r": [
{
"m:t": ["lim"],
},
],
},
],
},
{
"m:lim": [
{
"m:r": [
{
"m:t": ["x→0"],
},
],
},
],
},
],
});
});
});
});

View File

@ -0,0 +1,19 @@
// http://www.datypic.com/sc/ooxml/e-m_limLow-1.html
import { XmlComponent } from "@file/xml-components";
import { MathComponent } from "../math-component";
import { MathBase } from "./math-base";
import { MathLimit } from "./math-limit";
export interface IMathLimitLowerOptions {
readonly children: readonly MathComponent[];
readonly limit: readonly MathComponent[];
}
export class MathLimitLower extends XmlComponent {
public constructor(options: IMathLimitLowerOptions) {
super("m:limLow");
this.root.push(new MathBase(options.children));
this.root.push(new MathLimit(options.limit));
}
}

View File

@ -0,0 +1,45 @@
import { describe, expect, it } from "vitest";
import { Formatter } from "@export/formatter";
import { MathRun } from "../math-run";
import { MathLimitUpper } from "./math-limit-upper";
describe("MathLimitUpper", () => {
describe("#constructor()", () => {
it("should create a MathLimitUpper with correct root key", () => {
const mathLimitUpper = new MathLimitUpper({
children: [new MathRun("x")],
limit: [new MathRun("-")],
});
const tree = new Formatter().format(mathLimitUpper);
expect(tree).to.deep.equal({
"m:limUpp": [
{
"m:e": [
{
"m:r": [
{
"m:t": ["x"],
},
],
},
],
},
{
"m:lim": [
{
"m:r": [
{
"m:t": ["-"],
},
],
},
],
},
],
});
});
});
});

View File

@ -0,0 +1,19 @@
// http://www.datypic.com/sc/ooxml/e-m_limUpp-1.html
import { XmlComponent } from "@file/xml-components";
import { MathComponent } from "../math-component";
import { MathBase } from "./math-base";
import { MathLimit } from "./math-limit";
export interface IMathLimitUpperOptions {
readonly children: readonly MathComponent[];
readonly limit: readonly MathComponent[];
}
export class MathLimitUpper extends XmlComponent {
public constructor(options: IMathLimitUpperOptions) {
super("m:limUpp");
this.root.push(new MathBase(options.children));
this.root.push(new MathLimit(options.limit));
}
}

View File

@ -0,0 +1,27 @@
import { describe, expect, it } from "vitest";
import { Formatter } from "@export/formatter";
import { MathRun } from "../math-run";
import { MathLimit } from "./math-limit";
describe("MathLimit", () => {
describe("#constructor()", () => {
it("should create a MathLimit with correct root key", () => {
const mathLimit = new MathLimit([new MathRun("x→0")]);
const tree = new Formatter().format(mathLimit);
expect(tree).to.deep.equal({
"m:lim": [
{
"m:r": [
{
"m:t": ["x→0"],
},
],
},
],
});
});
});
});

View File

@ -0,0 +1,13 @@
// http://www.datypic.com/sc/ooxml/e-m_lim-1.html
import { XmlComponent } from "@file/xml-components";
import { MathComponent } from "../math-component";
export class MathLimit extends XmlComponent {
public constructor(children: readonly MathComponent[]) {
super("m:lim");
for (const child of children) {
this.root.push(child);
}
}
}