Merge pull request #413 from jamesmontalvo3/master
Add SymbolRun to allow adding symbols inline
This commit is contained in:
@ -15,7 +15,7 @@ import { CenterTabStop, LeaderType, LeftTabStop, MaxRightTabStop, RightTabStop }
|
||||
import { NumberProperties } from "./formatting/unordered-list";
|
||||
import { Bookmark, Hyperlink, OutlineLevel } from "./links";
|
||||
import { ParagraphProperties } from "./properties";
|
||||
import { PictureRun, Run, SequentialIdentifier, TextRun } from "./run";
|
||||
import { PictureRun, Run, SequentialIdentifier, SymbolRun, TextRun } from "./run";
|
||||
|
||||
interface ITabStopOptions {
|
||||
readonly position: number;
|
||||
@ -53,7 +53,7 @@ export interface IParagraphOptions {
|
||||
readonly level: number;
|
||||
readonly custom?: boolean;
|
||||
};
|
||||
readonly children?: Array<TextRun | PictureRun | Hyperlink>;
|
||||
readonly children?: Array<TextRun | PictureRun | Hyperlink | SymbolRun>;
|
||||
}
|
||||
|
||||
export class Paragraph extends XmlComponent {
|
||||
|
@ -1,5 +1,6 @@
|
||||
export * from "./run";
|
||||
export * from "./text-run";
|
||||
export * from "./symbol-run";
|
||||
export * from "./picture-run";
|
||||
export * from "./run-fonts";
|
||||
export * from "./sequential-identifier";
|
||||
|
28
src/file/paragraph/run/run-components/symbol.spec.ts
Normal file
28
src/file/paragraph/run/run-components/symbol.spec.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { Formatter } from "export/formatter";
|
||||
|
||||
import { Symbol } from "./symbol";
|
||||
|
||||
describe("Symbol", () => {
|
||||
describe("#constructor", () => {
|
||||
// Note: if no character is given, the output is a MS Windows logo
|
||||
it("creates an empty symbol run if no character is given", () => {
|
||||
const s = new Symbol();
|
||||
const f = new Formatter().format(s);
|
||||
expect(f).to.deep.equal({ "w:sym": { _attr: { "w:char": "", "w:font": "Wingdings" } } });
|
||||
});
|
||||
|
||||
it("creates the provided symbol with default font", () => {
|
||||
const s = new Symbol("F071");
|
||||
const f = new Formatter().format(s);
|
||||
expect(f).to.deep.equal({ "w:sym": { _attr: { "w:char": "F071", "w:font": "Wingdings" } } });
|
||||
});
|
||||
|
||||
it("creates the provided symbol with the provided font", () => {
|
||||
const s = new Symbol("F071", "Arial");
|
||||
const f = new Formatter().format(s);
|
||||
expect(f).to.deep.equal({ "w:sym": { _attr: { "w:char": "F071", "w:font": "Arial" } } });
|
||||
});
|
||||
});
|
||||
});
|
20
src/file/paragraph/run/run-components/symbol.ts
Normal file
20
src/file/paragraph/run/run-components/symbol.ts
Normal file
@ -0,0 +1,20 @@
|
||||
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
|
||||
|
||||
interface ISymbolAttributesProperties {
|
||||
readonly char: string;
|
||||
readonly symbolfont?: string;
|
||||
}
|
||||
|
||||
class SymbolAttributes extends XmlAttributeComponent<ISymbolAttributesProperties> {
|
||||
protected readonly xmlKeys = {
|
||||
char: "w:char",
|
||||
symbolfont: "w:font",
|
||||
};
|
||||
}
|
||||
|
||||
export class Symbol extends XmlComponent {
|
||||
constructor(char: string = "", symbolfont: string = "Wingdings") {
|
||||
super("w:sym");
|
||||
this.root.push(new SymbolAttributes({ char: char, symbolfont: symbolfont }));
|
||||
}
|
||||
}
|
76
src/file/paragraph/run/symbol-run.spec.ts
Normal file
76
src/file/paragraph/run/symbol-run.spec.ts
Normal file
@ -0,0 +1,76 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { Formatter } from "export/formatter";
|
||||
|
||||
import { UnderlineType } from "./underline";
|
||||
|
||||
import { SymbolRun } from "./symbol-run";
|
||||
|
||||
describe("SymbolRun", () => {
|
||||
let run: SymbolRun;
|
||||
|
||||
describe("#constructor()", () => {
|
||||
it("should create symbol run from text input", () => {
|
||||
run = new SymbolRun("F071");
|
||||
const f = new Formatter().format(run);
|
||||
expect(f).to.deep.equal({
|
||||
"w:r": [{ "w:sym": { _attr: { "w:char": "F071", "w:font": "Wingdings" } } }],
|
||||
});
|
||||
});
|
||||
|
||||
it("should create symbol run from object input with just 'char' specified", () => {
|
||||
run = new SymbolRun({ char: "F071" });
|
||||
const f = new Formatter().format(run);
|
||||
expect(f).to.deep.equal({
|
||||
"w:r": [{ "w:sym": { _attr: { "w:char": "F071", "w:font": "Wingdings" } } }],
|
||||
});
|
||||
});
|
||||
|
||||
it("should create symbol run from object input with just 'char' specified", () => {
|
||||
run = new SymbolRun({ char: "F071", symbolfont: "Arial" });
|
||||
const f = new Formatter().format(run);
|
||||
expect(f).to.deep.equal({
|
||||
"w:r": [{ "w:sym": { _attr: { "w:char": "F071", "w:font": "Arial" } } }],
|
||||
});
|
||||
});
|
||||
|
||||
it("should add other standard run properties", () => {
|
||||
run = new SymbolRun({
|
||||
char: "F071",
|
||||
symbolfont: "Arial",
|
||||
italics: true,
|
||||
bold: true,
|
||||
underline: {
|
||||
color: "red",
|
||||
type: UnderlineType.DOUBLE,
|
||||
},
|
||||
color: "green",
|
||||
size: 40,
|
||||
highlight: "yellow",
|
||||
});
|
||||
|
||||
const f = new Formatter().format(run);
|
||||
expect(f).to.deep.equal({
|
||||
"w:r": [
|
||||
{
|
||||
"w:rPr": [
|
||||
{ "w:b": { _attr: { "w:val": true } } },
|
||||
{ "w:bCs": { _attr: { "w:val": true } } },
|
||||
{ "w:i": { _attr: { "w:val": true } } },
|
||||
{ "w:iCs": { _attr: { "w:val": true } } },
|
||||
{ "w:u": { _attr: { "w:val": "double", "w:color": "red" } } },
|
||||
{ "w:color": { _attr: { "w:val": "green" } } },
|
||||
{ "w:sz": { _attr: { "w:val": 40 } } },
|
||||
{ "w:szCs": { _attr: { "w:val": 40 } } },
|
||||
{ "w:highlight": { _attr: { "w:val": "yellow" } } },
|
||||
{ "w:highlightCs": { _attr: { "w:val": "yellow" } } },
|
||||
],
|
||||
},
|
||||
{
|
||||
"w:sym": { _attr: { "w:char": "F071", "w:font": "Arial" } },
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
20
src/file/paragraph/run/symbol-run.ts
Normal file
20
src/file/paragraph/run/symbol-run.ts
Normal file
@ -0,0 +1,20 @@
|
||||
import { IRunOptions, Run } from "./run";
|
||||
import { Symbol } from "./run-components/symbol";
|
||||
|
||||
export interface ISymbolRunOptions extends IRunOptions {
|
||||
readonly char: string;
|
||||
readonly symbolfont?: string;
|
||||
}
|
||||
|
||||
export class SymbolRun extends Run {
|
||||
constructor(options: ISymbolRunOptions | string) {
|
||||
if (typeof options === "string") {
|
||||
super({});
|
||||
this.root.push(new Symbol(options));
|
||||
return;
|
||||
}
|
||||
|
||||
super(options);
|
||||
this.root.push(new Symbol(options.char, options.symbolfont));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user