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

185 lines
5.9 KiB
TypeScript
Raw Normal View History

2017-09-21 14:56:46 +01:00
// http://officeopenxml.com/WPparagraphProperties.php
2021-03-12 03:58:05 +00:00
import { IContext, IgnoreIfEmptyXmlComponent, IXmlableObject, XmlComponent } from "file/xml-components";
import { DocumentWrapper } from "../document-wrapper";
import { IShadingAttributesProperties, Shading } from "../shading";
import { Alignment, AlignmentType } from "./formatting/alignment";
import { Bidirectional } from "./formatting/bidirectional";
import { Border, IBordersOptions, ThematicBreak } from "./formatting/border";
import { IIndentAttributesProperties, Indent } from "./formatting/indent";
import { KeepLines, KeepNext } from "./formatting/keep";
import { PageBreakBefore } from "./formatting/page-break";
import { ContextualSpacing, ISpacingProperties, Spacing } from "./formatting/spacing";
import { HeadingLevel, Style } from "./formatting/style";
import { LeaderType, TabStop, TabStopPosition, TabStopType } from "./formatting/tab-stop";
import { NumberProperties } from "./formatting/unordered-list";
2021-03-13 04:07:44 +00:00
import { WidowControl } from "./formatting/widow-control";
2021-03-14 17:00:42 +00:00
import { FrameProperties, IFrameOptions } from "./frame/frame-properties";
import { OutlineLevel } from "./links";
2016-03-30 00:28:05 +01: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
export interface IParagraphPropertiesOptions extends IParagraphStylePropertiesOptions {
readonly border?: IBordersOptions;
readonly heading?: HeadingLevel;
readonly bidirectional?: boolean;
readonly pageBreakBefore?: boolean;
2020-08-01 17:58:16 +01:00
readonly tabStops?: {
readonly position: number | TabStopPosition;
readonly type: TabStopType;
readonly leader?: LeaderType;
2020-08-01 17:58:16 +01: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;
readonly custom?: boolean;
};
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
}
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 }[] = [];
constructor(options?: IParagraphPropertiesOptions) {
super("w:pPr");
if (!options) {
return this;
}
if (options.heading) {
this.push(new Style(options.heading));
}
if (options.bullet) {
this.push(new Style("ListParagraph"));
}
if (options.numbering) {
if (!options.style && !options.heading) {
if (!options.numbering.custom) {
this.push(new Style("ListParagraph"));
}
}
}
if (options.style) {
this.push(new Style(options.style));
}
if (options.keepNext) {
this.push(new KeepNext());
}
if (options.keepLines) {
this.push(new KeepLines());
}
if (options.pageBreakBefore) {
this.push(new PageBreakBefore());
}
if (options.frame) {
this.push(new FrameProperties(options.frame));
}
if (options.widowControl) {
this.push(new WidowControl(options.widowControl));
}
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));
}
if (options.border) {
this.push(new Border(options.border));
}
if (options.thematicBreak) {
this.push(new ThematicBreak());
}
if (options.shading) {
this.push(new Shading(options.shading));
}
if (options.rightTabStop) {
this.push(new TabStop(TabStopType.RIGHT, options.rightTabStop));
}
if (options.tabStops) {
for (const tabStop of options.tabStops) {
this.push(new TabStop(tabStop.type, tabStop.position, tabStop.leader));
}
}
if (options.leftTabStop) {
this.push(new TabStop(TabStopType.LEFT, options.leftTabStop));
}
2020-10-28 01:05:31 +00:00
if (options.bidirectional) {
this.push(new Bidirectional());
2020-10-28 01:05:31 +00:00
}
2021-03-13 04:07:44 +00: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
if (options.indent) {
this.push(new Indent(options.indent));
}
if (options.contextualSpacing) {
this.push(new ContextualSpacing(options.contextualSpacing));
}
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
}