2017-09-21 14:56:46 +01:00
|
|
|
// http://officeopenxml.com/WPparagraphProperties.php
|
2021-05-24 11:28:10 +03:00
|
|
|
import { IContext, IgnoreIfEmptyXmlComponent, IXmlableObject, OnOffElement, XmlComponent } from "file/xml-components";
|
2021-03-12 03:58:05 +00:00
|
|
|
import { DocumentWrapper } from "../document-wrapper";
|
2021-05-23 04:25:07 +03:00
|
|
|
import { IShadingAttributesProperties, Shading } from "../shading";
|
2020-07-11 17:01:32 +08:00
|
|
|
import { Alignment, AlignmentType } from "./formatting/alignment";
|
2021-05-23 08:00:49 +03:00
|
|
|
import { Border, IBordersOptions, ThematicBreak } from "./formatting/border";
|
2020-07-11 17:01:32 +08:00
|
|
|
import { IIndentAttributesProperties, Indent } from "./formatting/indent";
|
|
|
|
import { PageBreakBefore } from "./formatting/page-break";
|
2021-05-24 11:28:10 +03:00
|
|
|
import { ISpacingProperties, Spacing } from "./formatting/spacing";
|
2020-07-11 17:01:32 +08:00
|
|
|
import { HeadingLevel, Style } from "./formatting/style";
|
|
|
|
import { LeaderType, TabStop, TabStopPosition, TabStopType } from "./formatting/tab-stop";
|
|
|
|
import { NumberProperties } from "./formatting/unordered-list";
|
2021-03-14 17:00:42 +00:00
|
|
|
import { FrameProperties, IFrameOptions } from "./frame/frame-properties";
|
2020-07-11 17:01:32 +08:00
|
|
|
import { OutlineLevel } from "./links";
|
2016-03-30 00:28:05 +01:00
|
|
|
|
2020-07-11 17:01:32 +08:00
|
|
|
export interface IParagraphStylePropertiesOptions {
|
|
|
|
readonly alignment?: AlignmentType;
|
|
|
|
readonly thematicBreak?: boolean;
|
|
|
|
readonly contextualSpacing?: boolean;
|
|
|
|
readonly rightTabStop?: number;
|
|
|
|
readonly leftTabStop?: number;
|
|
|
|
readonly indent?: IIndentAttributesProperties;
|
|
|
|
readonly spacing?: ISpacingProperties;
|
|
|
|
readonly keepNext?: boolean;
|
|
|
|
readonly keepLines?: boolean;
|
|
|
|
readonly outlineLevel?: number;
|
|
|
|
}
|
2019-06-12 01:03:36 +01:00
|
|
|
|
2020-07-11 17:01:32 +08:00
|
|
|
export interface IParagraphPropertiesOptions extends IParagraphStylePropertiesOptions {
|
2021-05-23 08:00:49 +03:00
|
|
|
readonly border?: IBordersOptions;
|
2020-07-11 17:01:32 +08:00
|
|
|
readonly heading?: HeadingLevel;
|
|
|
|
readonly bidirectional?: boolean;
|
|
|
|
readonly pageBreakBefore?: boolean;
|
2020-08-01 17:58:16 +01:00
|
|
|
readonly tabStops?: {
|
2020-07-11 17:01:32 +08:00
|
|
|
readonly position: number | TabStopPosition;
|
|
|
|
readonly type: TabStopType;
|
|
|
|
readonly leader?: LeaderType;
|
2020-08-01 17:58:16 +01:00
|
|
|
}[];
|
2020-07-11 17:01:32 +08:00
|
|
|
readonly style?: string;
|
|
|
|
readonly bullet?: {
|
|
|
|
readonly level: number;
|
|
|
|
};
|
|
|
|
readonly numbering?: {
|
|
|
|
readonly reference: string;
|
|
|
|
readonly level: number;
|
2021-03-12 03:58:05 +00:00
|
|
|
readonly instance?: number;
|
2020-07-11 17:01:32 +08:00
|
|
|
readonly custom?: boolean;
|
|
|
|
};
|
2021-05-23 04:25:07 +03:00
|
|
|
readonly shading?: IShadingAttributesProperties;
|
2021-03-13 04:07:44 +00:00
|
|
|
readonly widowControl?: boolean;
|
2021-03-14 17:00:42 +00:00
|
|
|
readonly frame?: IFrameOptions;
|
2019-06-12 01:03:36 +01:00
|
|
|
}
|
2018-08-19 19:37:36 -04:00
|
|
|
|
2019-06-12 01:03:36 +01:00
|
|
|
export class ParagraphProperties extends IgnoreIfEmptyXmlComponent {
|
2021-03-12 03:58:05 +00:00
|
|
|
private readonly numberingReferences: { readonly reference: string; readonly instance: number }[] = [];
|
|
|
|
|
2020-07-11 17:01:32 +08:00
|
|
|
constructor(options?: IParagraphPropertiesOptions) {
|
2016-05-09 03:44:16 +01:00
|
|
|
super("w:pPr");
|
2018-08-19 19:37:36 -04:00
|
|
|
|
2020-07-11 17:01:32 +08:00
|
|
|
if (!options) {
|
2021-05-20 01:06:07 +03:00
|
|
|
return this;
|
2020-07-11 17:01:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (options.heading) {
|
|
|
|
this.push(new Style(options.heading));
|
|
|
|
}
|
|
|
|
|
2021-05-20 01:06:07 +03:00
|
|
|
if (options.bullet) {
|
|
|
|
this.push(new Style("ListParagraph"));
|
2020-07-11 17:01:32 +08:00
|
|
|
}
|
|
|
|
|
2021-05-20 01:06:07 +03:00
|
|
|
if (options.numbering) {
|
|
|
|
if (!options.style && !options.heading) {
|
|
|
|
if (!options.numbering.custom) {
|
|
|
|
this.push(new Style("ListParagraph"));
|
|
|
|
}
|
|
|
|
}
|
2020-07-11 17:01:32 +08:00
|
|
|
}
|
|
|
|
|
2021-05-20 01:06:07 +03:00
|
|
|
if (options.style) {
|
|
|
|
this.push(new Style(options.style));
|
2020-07-11 17:01:32 +08:00
|
|
|
}
|
|
|
|
|
2021-05-24 11:28:10 +03:00
|
|
|
if (options.keepNext !== undefined) {
|
|
|
|
this.push(new OnOffElement("w:keepNext", options.keepNext));
|
2020-07-11 17:01:32 +08:00
|
|
|
}
|
|
|
|
|
2021-05-24 11:28:10 +03:00
|
|
|
if (options.keepLines !== undefined) {
|
|
|
|
this.push(new OnOffElement("w:keepLines", options.keepLines));
|
2020-07-11 17:01:32 +08:00
|
|
|
}
|
|
|
|
|
2021-05-20 01:06:07 +03:00
|
|
|
if (options.pageBreakBefore) {
|
|
|
|
this.push(new PageBreakBefore());
|
2020-07-11 17:01:32 +08:00
|
|
|
}
|
|
|
|
|
2021-05-20 01:06:07 +03:00
|
|
|
if (options.frame) {
|
|
|
|
this.push(new FrameProperties(options.frame));
|
2020-07-11 17:01:32 +08:00
|
|
|
}
|
|
|
|
|
2021-05-24 11:28:10 +03:00
|
|
|
if (options.widowControl !== undefined) {
|
|
|
|
this.push(new OnOffElement("w:widowControl", options.widowControl));
|
2020-07-11 17:01:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (options.bullet) {
|
|
|
|
this.push(new NumberProperties(1, options.bullet.level));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.numbering) {
|
2021-03-12 03:58:05 +00:00
|
|
|
this.numberingReferences.push({
|
|
|
|
reference: options.numbering.reference,
|
|
|
|
instance: options.numbering.instance ?? 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
this.push(new NumberProperties(`${options.numbering.reference}-${options.numbering.instance ?? 0}`, options.numbering.level));
|
2020-07-11 17:01:32 +08:00
|
|
|
}
|
|
|
|
|
2021-05-20 01:06:07 +03:00
|
|
|
if (options.border) {
|
|
|
|
this.push(new Border(options.border));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.thematicBreak) {
|
|
|
|
this.push(new ThematicBreak());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.shading) {
|
2021-05-23 04:25:07 +03:00
|
|
|
this.push(new Shading(options.shading));
|
2021-05-20 01:06:07 +03:00
|
|
|
}
|
|
|
|
|
2020-07-11 17:01:32 +08:00
|
|
|
if (options.rightTabStop) {
|
|
|
|
this.push(new TabStop(TabStopType.RIGHT, options.rightTabStop));
|
|
|
|
}
|
|
|
|
|
2021-05-20 01:06:07 +03:00
|
|
|
if (options.tabStops) {
|
|
|
|
for (const tabStop of options.tabStops) {
|
|
|
|
this.push(new TabStop(tabStop.type, tabStop.position, tabStop.leader));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-11 17:01:32 +08:00
|
|
|
if (options.leftTabStop) {
|
|
|
|
this.push(new TabStop(TabStopType.LEFT, options.leftTabStop));
|
|
|
|
}
|
2020-10-28 01:05:31 +00:00
|
|
|
|
2021-05-24 11:28:10 +03:00
|
|
|
if (options.bidirectional !== undefined) {
|
|
|
|
this.push(new OnOffElement("w:bidi", options.contextualSpacing));
|
2020-10-28 01:05:31 +00:00
|
|
|
}
|
2021-03-13 04:07:44 +00:00
|
|
|
|
2021-05-20 01:06:07 +03:00
|
|
|
if (options.spacing) {
|
|
|
|
this.push(new Spacing(options.spacing));
|
2021-03-13 04:07:44 +00:00
|
|
|
}
|
2021-03-14 17:00:42 +00:00
|
|
|
|
2021-05-20 01:06:07 +03:00
|
|
|
if (options.indent) {
|
|
|
|
this.push(new Indent(options.indent));
|
|
|
|
}
|
|
|
|
|
2021-05-24 11:28:10 +03:00
|
|
|
if (options.contextualSpacing !== undefined) {
|
|
|
|
this.push(new OnOffElement("w:contextualSpacing", options.contextualSpacing));
|
2021-05-20 01:06:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (options.alignment) {
|
|
|
|
this.push(new Alignment(options.alignment));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.outlineLevel !== undefined) {
|
|
|
|
this.push(new OutlineLevel(options.outlineLevel));
|
2021-03-14 17:00:42 +00:00
|
|
|
}
|
2016-03-30 00:28:05 +01:00
|
|
|
}
|
|
|
|
|
2017-03-08 21:36:09 +00:00
|
|
|
public push(item: XmlComponent): void {
|
2016-04-09 20:16:35 +01:00
|
|
|
this.root.push(item);
|
2016-03-30 00:28:05 +01:00
|
|
|
}
|
2021-03-12 03:58:05 +00:00
|
|
|
|
|
|
|
public prepForXml(context: IContext): IXmlableObject | undefined {
|
|
|
|
if (context.viewWrapper instanceof DocumentWrapper) {
|
|
|
|
for (const reference of this.numberingReferences) {
|
|
|
|
context.file.Numbering.createConcreteNumberingInstance(reference.reference, reference.instance);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return super.prepForXml(context);
|
|
|
|
}
|
2017-03-08 21:36:09 +00:00
|
|
|
}
|