Skip to content

Commit

Permalink
Add math limit component (#2510)
Browse files Browse the repository at this point in the history
  • Loading branch information
YaoKaiLun committed Jan 3, 2024
1 parent 13cf3ee commit c4ed19e
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 0 deletions.
19 changes: 19 additions & 0 deletions demo/55-math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {
Packer,
Paragraph,
TextRun,
MathLimitLower,
MathLimitUpper,
} from "docx";

const doc = new Document({
Expand Down Expand Up @@ -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")],
}),
],
}),
],
}),
],
},
],
Expand Down
20 changes: 20 additions & 0 deletions docs/usage/math.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")],
}),
```
3 changes: 3 additions & 0 deletions src/file/paragraph/math/n-ary/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ export * from "./math-sub-script";
export * from "./math-sum";
export * from "./math-integral";
export * from "./math-super-script";
export * from "./math-limit";
export * from "./math-limit-upper";
export * from "./math-limit-lower";
45 changes: 45 additions & 0 deletions src/file/paragraph/math/n-ary/math-limit-lower.spec.ts
Original file line number Diff line number Diff line change
@@ -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"],
},
],
},
],
},
],
});
});
});
});
19 changes: 19 additions & 0 deletions src/file/paragraph/math/n-ary/math-limit-lower.ts
Original file line number Diff line number Diff line change
@@ -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));
}
}
45 changes: 45 additions & 0 deletions src/file/paragraph/math/n-ary/math-limit-upper.spec.ts
Original file line number Diff line number Diff line change
@@ -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": ["-"],
},
],
},
],
},
],
});
});
});
});
19 changes: 19 additions & 0 deletions src/file/paragraph/math/n-ary/math-limit-upper.ts
Original file line number Diff line number Diff line change
@@ -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));
}
}
27 changes: 27 additions & 0 deletions src/file/paragraph/math/n-ary/math-limit.spec.ts
Original file line number Diff line number Diff line change
@@ -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"],
},
],
},
],
});
});
});
});
13 changes: 13 additions & 0 deletions src/file/paragraph/math/n-ary/math-limit.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

0 comments on commit c4ed19e

Please sign in to comment.