Files
docx-js/src/file/paragraph/run/run.ts

72 lines
2.7 KiB
TypeScript
Raw Normal View History

2017-09-19 15:42:40 +01:00
// http://officeopenxml.com/WPtext.php
2019-08-06 21:37:33 +01:00
import { XmlComponent } from "file/xml-components";
2019-11-21 01:02:46 +00:00
import { FootnoteReferenceRun } from "file/footnotes/footnote/run/reference-run";
import { FieldInstruction } from "file/table-of-contents/field-instruction";
2017-09-19 15:42:40 +01:00
import { Break } from "./break";
import { Begin, End, Separate } from "./field";
2019-08-09 11:56:22 +03:00
import { NumberOfPages, NumberOfPagesSection, Page } from "./page-number";
import { IRunPropertiesOptions, RunProperties } from "./properties";
2019-11-21 01:02:46 +00:00
import { Text } from "./run-components/text";
2017-09-19 15:42:40 +01:00
export interface IRunOptions extends IRunPropertiesOptions {
2019-11-21 01:02:46 +00:00
readonly children?: Array<Begin | FieldInstruction | Separate | End | PageNumber | FootnoteReferenceRun | string>;
readonly text?: string;
}
export enum PageNumber {
CURRENT = "CURRENT",
TOTAL_PAGES = "TOTAL_PAGES",
TOTAL_PAGES_IN_SECTION = "TOTAL_PAGES_IN_SECTION",
2019-06-17 01:51:57 +01:00
}
2017-09-19 15:42:40 +01:00
export class Run extends XmlComponent {
protected readonly properties: RunProperties;
2017-09-19 15:42:40 +01:00
2019-06-17 01:51:57 +01:00
constructor(options: IRunOptions) {
2017-09-19 15:42:40 +01:00
super("w:r");
this.properties = new RunProperties(options);
2017-09-19 15:42:40 +01:00
this.root.push(this.properties);
if (options.children) {
for (const child of options.children) {
2019-11-21 01:02:46 +00:00
if (typeof child === "string") {
switch (child) {
case PageNumber.CURRENT:
this.root.push(new Begin());
this.root.push(new Page());
this.root.push(new Separate());
this.root.push(new End());
break;
case PageNumber.TOTAL_PAGES:
this.root.push(new Begin());
this.root.push(new NumberOfPages());
this.root.push(new Separate());
this.root.push(new End());
break;
case PageNumber.TOTAL_PAGES_IN_SECTION:
this.root.push(new Begin());
this.root.push(new NumberOfPagesSection());
this.root.push(new Separate());
this.root.push(new End());
break;
default:
this.root.push(new Text(child));
break;
}
continue;
}
this.root.push(child);
}
2019-11-21 01:02:46 +00:00
} else if (options.text) {
this.root.push(new Text(options.text));
}
2018-07-24 18:52:45 +03:00
}
2017-09-19 15:42:40 +01:00
public break(): Run {
this.root.splice(1, 0, new Break());
return this;
}
}