Files
docx-js/src/file/styles/style/paragraph-style.ts

185 lines
6.0 KiB
TypeScript
Raw Normal View History

2019-12-11 15:21:50 +13:00
import {
Alignment,
ContextualSpacing,
Indent,
KeepLines,
KeepNext,
OutlineLevel,
ParagraphProperties,
Spacing,
ThematicBreak,
} from "file/paragraph";
2019-11-01 01:57:01 +00:00
import { TabStop, TabStopType } from "file/paragraph/formatting";
import * as formatting from "file/paragraph/run/formatting";
import { RunProperties } from "file/paragraph/run/properties";
2019-10-04 01:20:41 +01:00
2019-11-01 01:57:01 +00:00
import { IParagraphStyleOptions2, IRunStyleOptions } from "../style-options";
2018-11-13 11:04:29 -02:00
import { BasedOn, Link, Next, QuickFormat, SemiHidden, UiPriority, UnhideWhenUsed } from "./components";
import { Style } from "./style";
2019-10-04 01:20:41 +01:00
export interface IBaseParagraphStyleOptions {
readonly basedOn?: string;
readonly next?: string;
readonly quickFormat?: boolean;
readonly link?: string;
readonly semiHidden?: boolean;
readonly uiPriority?: number;
readonly unhideWhenUsed?: boolean;
2019-11-01 01:57:01 +00:00
readonly run?: IRunStyleOptions;
readonly paragraph?: IParagraphStyleOptions2;
2019-10-04 01:20:41 +01:00
}
export interface IParagraphStyleOptions extends IBaseParagraphStyleOptions {
readonly id: string;
readonly name?: string;
}
export class ParagraphStyle extends Style {
private readonly paragraphProperties: ParagraphProperties;
private readonly runProperties: RunProperties;
2019-10-04 01:20:41 +01:00
constructor(options: IParagraphStyleOptions) {
super({ type: "paragraph", styleId: options.id }, options.name);
2019-06-12 01:03:36 +01:00
this.paragraphProperties = new ParagraphProperties({});
this.runProperties = new RunProperties();
this.root.push(this.paragraphProperties);
this.root.push(this.runProperties);
2019-10-04 01:20:41 +01:00
if (options.basedOn) {
this.root.push(new BasedOn(options.basedOn));
}
2019-10-04 01:20:41 +01:00
if (options.next) {
this.root.push(new Next(options.next));
}
2019-10-04 01:20:41 +01:00
if (options.quickFormat) {
this.root.push(new QuickFormat());
}
2019-10-04 01:20:41 +01:00
if (options.link) {
this.root.push(new Link(options.link));
}
2019-10-04 01:20:41 +01:00
if (options.semiHidden) {
this.root.push(new SemiHidden());
}
2019-10-04 01:20:41 +01:00
if (options.uiPriority) {
this.root.push(new UiPriority(options.uiPriority));
}
2019-10-04 01:20:41 +01:00
if (options.unhideWhenUsed) {
this.root.push(new UnhideWhenUsed());
}
if (options.run) {
if (options.run.size) {
this.runProperties.push(new formatting.Size(options.run.size));
this.runProperties.push(new formatting.SizeComplexScript(options.run.size));
}
if (options.run.bold) {
this.runProperties.push(new formatting.Bold());
}
if (options.run.italics) {
this.runProperties.push(new formatting.Italics());
}
if (options.run.smallCaps) {
this.runProperties.push(new formatting.SmallCaps());
}
if (options.run.allCaps) {
this.runProperties.push(new formatting.Caps());
}
if (options.run.strike) {
this.runProperties.push(new formatting.Strike());
}
if (options.run.doubleStrike) {
this.runProperties.push(new formatting.DoubleStrike());
}
if (options.run.subScript) {
this.runProperties.push(new formatting.SubScript());
}
if (options.run.superScript) {
this.runProperties.push(new formatting.SuperScript());
}
if (options.run.underline) {
this.runProperties.push(new formatting.Underline(options.run.underline.type, options.run.underline.color));
}
2020-05-22 12:22:45 +08:00
if (options.run.emphasisMark) {
this.runProperties.push(new formatting.EmphasisMark(options.run.emphasisMark.type));
}
2019-10-04 01:20:41 +01:00
if (options.run.color) {
this.runProperties.push(new formatting.Color(options.run.color));
}
if (options.run.font) {
this.runProperties.push(new formatting.RunFonts(options.run.font));
}
if (options.run.characterSpacing) {
this.runProperties.push(new formatting.CharacterSpacing(options.run.characterSpacing));
}
if (options.run.highlight) {
this.runProperties.push(new formatting.Highlight(options.run.highlight));
}
if (options.run.shadow) {
this.runProperties.push(new formatting.Shading(options.run.shadow.type, options.run.shadow.fill, options.run.shadow.color));
}
}
if (options.paragraph) {
if (options.paragraph.alignment) {
this.paragraphProperties.push(new Alignment(options.paragraph.alignment));
}
if (options.paragraph.thematicBreak) {
this.paragraphProperties.push(new ThematicBreak());
}
if (options.paragraph.contextualSpacing) {
this.paragraphProperties.push(new ContextualSpacing(options.paragraph.contextualSpacing));
}
2019-10-04 01:20:41 +01:00
if (options.paragraph.rightTabStop) {
2019-10-09 20:56:31 +01:00
this.paragraphProperties.push(new TabStop(TabStopType.RIGHT, options.paragraph.rightTabStop));
2019-10-04 01:20:41 +01:00
}
if (options.paragraph.leftTabStop) {
2019-10-09 20:56:31 +01:00
this.paragraphProperties.push(new TabStop(TabStopType.LEFT, options.paragraph.leftTabStop));
2019-10-04 01:20:41 +01:00
}
if (options.paragraph.indent) {
this.paragraphProperties.push(new Indent(options.paragraph.indent));
}
if (options.paragraph.spacing) {
this.paragraphProperties.push(new Spacing(options.paragraph.spacing));
}
if (options.paragraph.keepNext) {
this.paragraphProperties.push(new KeepNext());
}
if (options.paragraph.keepLines) {
this.paragraphProperties.push(new KeepLines());
}
if (options.paragraph.outlineLevel) {
this.paragraphProperties.push(new OutlineLevel(options.paragraph.outlineLevel));
}
}
}
}