Added support for w:fldSimple, including convenience class for simple MailMerge fields
This commit is contained in:
@ -10,7 +10,7 @@ import { PageBreak } from "./formatting/page-break";
|
||||
import { Bookmark, ConcreteHyperlink, ExternalHyperlink, InternalHyperlink } from "./links";
|
||||
import { Math } from "./math";
|
||||
import { IParagraphPropertiesOptions, ParagraphProperties } from "./properties";
|
||||
import { ImageRun, Run, SequentialIdentifier, SymbolRun, TextRun } from "./run";
|
||||
import { ImageRun, Run, SequentialIdentifier, SimpleField, SimpleMailMergeField, SymbolRun, TextRun } from "./run";
|
||||
|
||||
export type ParagraphChild =
|
||||
| TextRun
|
||||
@ -24,7 +24,9 @@ export type ParagraphChild =
|
||||
| ExternalHyperlink
|
||||
| InsertedTextRun
|
||||
| DeletedTextRun
|
||||
| Math;
|
||||
| Math
|
||||
| SimpleField
|
||||
| SimpleMailMergeField;
|
||||
|
||||
export interface IParagraphOptions extends IParagraphPropertiesOptions {
|
||||
readonly text?: string;
|
||||
|
@ -8,3 +8,4 @@ export * from "./sequential-identifier";
|
||||
export * from "./underline";
|
||||
export * from "./emphasis-mark";
|
||||
export * from "./tab";
|
||||
export * from "./simple-field";
|
||||
|
38
src/file/paragraph/run/simple-field.spec.ts
Normal file
38
src/file/paragraph/run/simple-field.spec.ts
Normal file
@ -0,0 +1,38 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { Formatter } from "export/formatter";
|
||||
|
||||
import { SimpleField, SimpleMailMergeField } from "./simple-field";
|
||||
|
||||
describe("SimpleField", () => {
|
||||
describe("#constructor()", () => {
|
||||
it("uses the instruction given", () => {
|
||||
const tree = new Formatter().format(new SimpleField("FILENAME"));
|
||||
expect(tree).to.deep.equal({ "w:fldSimple": { _attr: { "w:instr": "FILENAME" } } });
|
||||
});
|
||||
|
||||
it("accepts a cached value", () => {
|
||||
const tree = new Formatter().format(new SimpleField("FILENAME", "ExampleDoc.docx"));
|
||||
expect(tree).to.deep.equal({
|
||||
"w:fldSimple": [
|
||||
{ _attr: { "w:instr": "FILENAME" } },
|
||||
{ "w:r": [{ "w:t": [{ _attr: { "xml:space": "preserve" } }, "ExampleDoc.docx"] }] },
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("SimpleMailMergeField", () => {
|
||||
describe("#constructor()", () => {
|
||||
it("creates a simple field", () => {
|
||||
const tree = new Formatter().format(new SimpleMailMergeField("Name"));
|
||||
expect(tree).to.deep.equal({
|
||||
"w:fldSimple": [
|
||||
{ _attr: { "w:instr": " MERGEFIELD Name " } },
|
||||
{ "w:r": [{ "w:t": [{ _attr: { "xml:space": "preserve" } }, "«Name»"] }] },
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
23
src/file/paragraph/run/simple-field.ts
Normal file
23
src/file/paragraph/run/simple-field.ts
Normal file
@ -0,0 +1,23 @@
|
||||
// http://www.datypic.com/sc/ooxml/e-w_fldSimple-1.html
|
||||
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
|
||||
import { TextRun } from "./text-run";
|
||||
|
||||
class FldSimpleAttrs extends XmlAttributeComponent<{ readonly instr: string }> {
|
||||
protected readonly xmlKeys = { instr: "w:instr" };
|
||||
}
|
||||
|
||||
export class SimpleField extends XmlComponent {
|
||||
constructor(instruction: string, cachedValue?: string) {
|
||||
super("w:fldSimple");
|
||||
this.root.push(new FldSimpleAttrs({ instr: instruction }));
|
||||
if (cachedValue !== undefined) {
|
||||
this.root.push(new TextRun(cachedValue));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class SimpleMailMergeField extends SimpleField {
|
||||
constructor(fieldName: string) {
|
||||
super(` MERGEFIELD ${fieldName} `, `«${fieldName}»`);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user