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

191 lines
6.4 KiB
TypeScript
Raw Normal View History

2017-09-21 14:56:46 +01:00
// http://officeopenxml.com/WPparagraphProperties.php
// https://c-rex.net/projects/samples/ooxml/e1/Part4/OOXML_P4_DOCX_suppressLineNumbers_topic_ID0ECJAO.html
import { IContext, IgnoreIfEmptyXmlComponent, IXmlableObject, OnOffElement, XmlComponent } from "@file/xml-components";
2021-03-12 03:58:05 +00:00
import { DocumentWrapper } from "../document-wrapper";
import { IShadingAttributesProperties, Shading } from "../shading";
import { Alignment, AlignmentType } from "./formatting/alignment";
import { Border, IBordersOptions, ThematicBreak } from "./formatting/border";
import { PageBreakBefore } from "./formatting/break";
2021-07-08 17:46:13 +10:00
import { IIndentAttributesProperties, Indent } from "./formatting/indent";
import { ISpacingProperties, Spacing } from "./formatting/spacing";
import { HeadingLevel, Style } from "./formatting/style";
2022-10-16 00:21:34 +01:00
import { TabStop, TabStopDefinition, 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";
import { OutlineLevel } from "./links";
2016-03-30 00:28:05 +01:00
export interface ILevelParagraphStylePropertiesOptions {
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;
}
export interface IParagraphStylePropertiesOptions extends ILevelParagraphStylePropertiesOptions {
readonly numbering?: {
readonly reference: string;
readonly level: number;
readonly instance?: number;
readonly custom?: boolean;
};
}
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;
2022-10-16 00:21:34 +01:00
readonly tabStops?: readonly TabStopDefinition[];
readonly style?: string;
readonly bullet?: {
readonly level: number;
};
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;
readonly suppressLineNumbers?: boolean;
2019-06-12 01:03:36 +01:00
}
2019-06-12 01:03:36 +01:00
export class ParagraphProperties extends IgnoreIfEmptyXmlComponent {
// eslint-disable-next-line functional/prefer-readonly-type
2021-03-12 03:58:05 +00:00
private readonly numberingReferences: { readonly reference: string; readonly instance: number }[] = [];
2022-08-31 07:52:27 +01:00
public 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 !== undefined) {
this.push(new OnOffElement("w:keepNext", options.keepNext));
}
if (options.keepLines !== undefined) {
this.push(new OnOffElement("w:keepLines", options.keepLines));
}
if (options.pageBreakBefore) {
this.push(new PageBreakBefore());
}
if (options.frame) {
this.push(new FrameProperties(options.frame));
}
if (options.widowControl !== undefined) {
this.push(new OnOffElement("w: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));
}
/**
* FIX: Multitab support for Libre Writer
* Ensure there is only one w:tabs tag with multiple w:tab
*/
2022-10-16 00:21:34 +01:00
const tabDefinitions: readonly TabStopDefinition[] = [
...(options.rightTabStop ? [{ type: TabStopType.RIGHT, position: options.rightTabStop }] : []),
...(options.tabStops ? options.tabStops : []),
...(options.leftTabStop ? [{ type: TabStopType.LEFT, position: options.leftTabStop }] : []),
];
if (tabDefinitions.length > 0) {
this.push(new TabStop(tabDefinitions));
}
/**
2022-10-16 00:20:16 +01:00
* FIX - END
*/
2020-10-28 01:05:31 +00:00
if (options.bidirectional !== undefined) {
this.push(new OnOffElement("w:bidi", options.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 !== undefined) {
this.push(new OnOffElement("w: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
}
if (options.suppressLineNumbers !== undefined) {
this.push(new OnOffElement("w:suppressLineNumbers", options.suppressLineNumbers));
}
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
}