Add math run and fraction
This commit is contained in:
33
demo/47-math.ts
Normal file
33
demo/47-math.ts
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
// 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, 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,
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
Packer.toBuffer(doc).then((buffer) => {
|
||||||
|
fs.writeFileSync("My Document.docx", buffer);
|
||||||
|
});
|
@ -4,3 +4,4 @@ export * from "./properties";
|
|||||||
export * from "./run";
|
export * from "./run";
|
||||||
export * from "./links";
|
export * from "./links";
|
||||||
export * from "./image";
|
export * from "./image";
|
||||||
|
export * from "./math";
|
||||||
|
3
src/file/paragraph/math/fraction/index.ts
Normal file
3
src/file/paragraph/math/fraction/index.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
export * from "./math-fraction";
|
||||||
|
export * from "./math-denominator";
|
||||||
|
export * from "./math-numerator";
|
25
src/file/paragraph/math/fraction/math-denominator.spec.ts
Normal file
25
src/file/paragraph/math/fraction/math-denominator.spec.ts
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
import { expect } from "chai";
|
||||||
|
|
||||||
|
import { Formatter } from "export/formatter";
|
||||||
|
|
||||||
|
import { MathDenominator } from "./math-denominator";
|
||||||
|
|
||||||
|
describe("MathDenominator", () => {
|
||||||
|
describe("#constructor()", () => {
|
||||||
|
it("should create a MathDenominator with correct root key", () => {
|
||||||
|
const mathDenominator = new MathDenominator("2+2");
|
||||||
|
const tree = new Formatter().format(mathDenominator);
|
||||||
|
expect(tree).to.deep.equal({
|
||||||
|
"m:den": [
|
||||||
|
{
|
||||||
|
"m:r": [
|
||||||
|
{
|
||||||
|
"m:t": ["2+2"],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
11
src/file/paragraph/math/fraction/math-denominator.ts
Normal file
11
src/file/paragraph/math/fraction/math-denominator.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import { XmlComponent } from "file/xml-components";
|
||||||
|
|
||||||
|
import { MathRun } from "../math-run";
|
||||||
|
|
||||||
|
export class MathDenominator extends XmlComponent {
|
||||||
|
constructor(readonly text: string) {
|
||||||
|
super("m:den");
|
||||||
|
|
||||||
|
this.root.push(new MathRun(text));
|
||||||
|
}
|
||||||
|
}
|
45
src/file/paragraph/math/fraction/math-fraction.spec.ts
Normal file
45
src/file/paragraph/math/fraction/math-fraction.spec.ts
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
import { expect } from "chai";
|
||||||
|
|
||||||
|
import { Formatter } from "export/formatter";
|
||||||
|
|
||||||
|
import { MathDenominator } from "./math-denominator";
|
||||||
|
import { MathFraction } from "./math-fraction";
|
||||||
|
import { MathNumerator } from "./math-numerator";
|
||||||
|
|
||||||
|
describe("MathFraction", () => {
|
||||||
|
describe("#constructor()", () => {
|
||||||
|
it("should create a MathFraction with correct root key", () => {
|
||||||
|
const mathFraction = new MathFraction({
|
||||||
|
numerator: new MathNumerator("2"),
|
||||||
|
denominator: new MathDenominator("2"),
|
||||||
|
});
|
||||||
|
const tree = new Formatter().format(mathFraction);
|
||||||
|
expect(tree).to.deep.equal({
|
||||||
|
"m:f": [
|
||||||
|
{
|
||||||
|
"m:num": [
|
||||||
|
{
|
||||||
|
"m:r": [
|
||||||
|
{
|
||||||
|
"m:t": ["2"],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"m:den": [
|
||||||
|
{
|
||||||
|
"m:r": [
|
||||||
|
{
|
||||||
|
"m:t": ["2"],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
18
src/file/paragraph/math/fraction/math-fraction.ts
Normal file
18
src/file/paragraph/math/fraction/math-fraction.ts
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import { XmlComponent } from "file/xml-components";
|
||||||
|
|
||||||
|
import { MathDenominator } from "./math-denominator";
|
||||||
|
import { MathNumerator } from "./math-numerator";
|
||||||
|
|
||||||
|
export interface IMathFractionOptions {
|
||||||
|
readonly numerator: MathNumerator;
|
||||||
|
readonly denominator: MathDenominator;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class MathFraction extends XmlComponent {
|
||||||
|
constructor(readonly options: IMathFractionOptions) {
|
||||||
|
super("m:f");
|
||||||
|
|
||||||
|
this.root.push(options.numerator);
|
||||||
|
this.root.push(options.denominator);
|
||||||
|
}
|
||||||
|
}
|
25
src/file/paragraph/math/fraction/math-numerator.spec.ts
Normal file
25
src/file/paragraph/math/fraction/math-numerator.spec.ts
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
import { expect } from "chai";
|
||||||
|
|
||||||
|
import { Formatter } from "export/formatter";
|
||||||
|
|
||||||
|
import { MathNumerator } from "./math-numerator";
|
||||||
|
|
||||||
|
describe("MathNumerator", () => {
|
||||||
|
describe("#constructor()", () => {
|
||||||
|
it("should create a MathNumerator with correct root key", () => {
|
||||||
|
const mathNumerator = new MathNumerator("2+2");
|
||||||
|
const tree = new Formatter().format(mathNumerator);
|
||||||
|
expect(tree).to.deep.equal({
|
||||||
|
"m:num": [
|
||||||
|
{
|
||||||
|
"m:r": [
|
||||||
|
{
|
||||||
|
"m:t": ["2+2"],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
11
src/file/paragraph/math/fraction/math-numerator.ts
Normal file
11
src/file/paragraph/math/fraction/math-numerator.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import { XmlComponent } from "file/xml-components";
|
||||||
|
|
||||||
|
import { MathRun } from "../math-run";
|
||||||
|
|
||||||
|
export class MathNumerator extends XmlComponent {
|
||||||
|
constructor(readonly text: string) {
|
||||||
|
super("m:num");
|
||||||
|
|
||||||
|
this.root.push(new MathRun(text));
|
||||||
|
}
|
||||||
|
}
|
3
src/file/paragraph/math/index.ts
Normal file
3
src/file/paragraph/math/index.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
export * from "./math";
|
||||||
|
export * from "./math-run";
|
||||||
|
export * from "./fraction";
|
21
src/file/paragraph/math/math-run.spec.ts
Normal file
21
src/file/paragraph/math/math-run.spec.ts
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
import { expect } from "chai";
|
||||||
|
|
||||||
|
import { Formatter } from "export/formatter";
|
||||||
|
|
||||||
|
import { MathRun } from "./math-run";
|
||||||
|
|
||||||
|
describe("MathRun", () => {
|
||||||
|
describe("#constructor()", () => {
|
||||||
|
it("should create a MathRun with correct root key", () => {
|
||||||
|
const mathRun = new MathRun("2+2");
|
||||||
|
const tree = new Formatter().format(mathRun);
|
||||||
|
expect(tree).to.deep.equal({
|
||||||
|
"m:r": [
|
||||||
|
{
|
||||||
|
"m:t": ["2+2"],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
11
src/file/paragraph/math/math-run.ts
Normal file
11
src/file/paragraph/math/math-run.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import { XmlComponent } from "file/xml-components";
|
||||||
|
|
||||||
|
import { MathText } from "./math-text";
|
||||||
|
|
||||||
|
export class MathRun extends XmlComponent {
|
||||||
|
constructor(readonly text: string) {
|
||||||
|
super("m:r");
|
||||||
|
|
||||||
|
this.root.push(new MathText(text));
|
||||||
|
}
|
||||||
|
}
|
17
src/file/paragraph/math/math-text.spec.ts
Normal file
17
src/file/paragraph/math/math-text.spec.ts
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
import { expect } from "chai";
|
||||||
|
|
||||||
|
import { Formatter } from "export/formatter";
|
||||||
|
|
||||||
|
import { MathText } from "./math-text";
|
||||||
|
|
||||||
|
describe("MathText", () => {
|
||||||
|
describe("#constructor()", () => {
|
||||||
|
it("should create a MathText with correct root key", () => {
|
||||||
|
const mathText = new MathText("2+2");
|
||||||
|
const tree = new Formatter().format(mathText);
|
||||||
|
expect(tree).to.deep.equal({
|
||||||
|
"m:t": ["2+2"],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
9
src/file/paragraph/math/math-text.ts
Normal file
9
src/file/paragraph/math/math-text.ts
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
import { XmlComponent } from "file/xml-components";
|
||||||
|
|
||||||
|
export class MathText extends XmlComponent {
|
||||||
|
constructor(readonly text: string) {
|
||||||
|
super("m:t");
|
||||||
|
|
||||||
|
this.root.push(text);
|
||||||
|
}
|
||||||
|
}
|
38
src/file/paragraph/math/math.spec.ts
Normal file
38
src/file/paragraph/math/math.spec.ts
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
import { expect } from "chai";
|
||||||
|
|
||||||
|
import { Formatter } from "export/formatter";
|
||||||
|
|
||||||
|
import { Math } from "./math";
|
||||||
|
import { MathRun } from "./math-run";
|
||||||
|
|
||||||
|
describe("Math", () => {
|
||||||
|
describe("#constructor()", () => {
|
||||||
|
it("should create a Math with correct root key", () => {
|
||||||
|
const math = new Math({
|
||||||
|
children: [],
|
||||||
|
});
|
||||||
|
const tree = new Formatter().format(math);
|
||||||
|
expect(tree).to.deep.equal({
|
||||||
|
"m:oMath": {},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should be able to add children", () => {
|
||||||
|
const math = new Math({
|
||||||
|
children: [new MathRun("2+2")],
|
||||||
|
});
|
||||||
|
const tree = new Formatter().format(math);
|
||||||
|
expect(tree).to.deep.equal({
|
||||||
|
"m:oMath": [
|
||||||
|
{
|
||||||
|
"m:r": [
|
||||||
|
{
|
||||||
|
"m:t": ["2+2"],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
19
src/file/paragraph/math/math.ts
Normal file
19
src/file/paragraph/math/math.ts
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
// 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";
|
||||||
|
|
||||||
|
export interface IMathOptions {
|
||||||
|
readonly children: Array<MathRun | MathFraction>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class Math extends XmlComponent {
|
||||||
|
constructor(readonly options: IMathOptions) {
|
||||||
|
super("m:oMath");
|
||||||
|
|
||||||
|
for (const child of options.children) {
|
||||||
|
this.root.push(child);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -15,6 +15,7 @@ import { HeadingLevel, Style } from "./formatting/style";
|
|||||||
import { CenterTabStop, LeaderType, LeftTabStop, MaxRightTabStop, RightTabStop } from "./formatting/tab-stop";
|
import { CenterTabStop, LeaderType, LeftTabStop, MaxRightTabStop, RightTabStop } from "./formatting/tab-stop";
|
||||||
import { NumberProperties } from "./formatting/unordered-list";
|
import { NumberProperties } from "./formatting/unordered-list";
|
||||||
import { Bookmark, Hyperlink, OutlineLevel } from "./links";
|
import { Bookmark, Hyperlink, OutlineLevel } from "./links";
|
||||||
|
import { Math } from "./math";
|
||||||
import { ParagraphProperties } from "./properties";
|
import { ParagraphProperties } from "./properties";
|
||||||
import { PictureRun, Run, SequentialIdentifier, TextRun } from "./run";
|
import { PictureRun, Run, SequentialIdentifier, TextRun } from "./run";
|
||||||
|
|
||||||
@ -54,7 +55,7 @@ export interface IParagraphOptions {
|
|||||||
readonly level: number;
|
readonly level: number;
|
||||||
readonly custom?: boolean;
|
readonly custom?: boolean;
|
||||||
};
|
};
|
||||||
readonly children?: Array<TextRun | PictureRun | Hyperlink>;
|
readonly children?: Array<TextRun | PictureRun | Hyperlink | Math>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Paragraph extends XmlComponent {
|
export class Paragraph extends XmlComponent {
|
||||||
|
Reference in New Issue
Block a user