:feat: refine paragraph/run properties options

This commit is contained in:
wangfengming
2020-07-11 17:01:32 +08:00
parent 40dc90e585
commit 437e83ab78
20 changed files with 360 additions and 683 deletions

View File

@ -45,7 +45,7 @@ export class Body extends XmlComponent {
private createSectionParagraph(section: SectionProperties): Paragraph { private createSectionParagraph(section: SectionProperties): Paragraph {
const paragraph = new Paragraph({}); const paragraph = new Paragraph({});
const properties = new ParagraphProperties({}); const properties = new ParagraphProperties({});
properties.addChildElement(section); properties.push(section);
paragraph.addChildElement(properties); paragraph.addChildElement(properties);
return paragraph; return paragraph;
} }

View File

@ -292,6 +292,7 @@ describe("AbstractNumbering", () => {
style: { style: {
run: { run: {
size: 24, size: 24,
sizeComplexScript: false,
}, },
}, },
}, },
@ -487,6 +488,7 @@ describe("AbstractNumbering", () => {
style: { style: {
run: { run: {
bold: true, bold: true,
boldComplexScript: false,
}, },
}, },
}, },
@ -506,6 +508,7 @@ describe("AbstractNumbering", () => {
style: { style: {
run: { run: {
italics: true, italics: true,
italicsComplexScript: false,
}, },
}, },
}, },
@ -525,6 +528,7 @@ describe("AbstractNumbering", () => {
style: { style: {
run: { run: {
highlight: "005599", highlight: "005599",
highlightComplexScript: false,
}, },
}, },
}, },
@ -548,6 +552,7 @@ describe("AbstractNumbering", () => {
fill: "00FFFF", fill: "00FFFF",
color: "FF0000", color: "FF0000",
}, },
shadingComplexScript: false,
}, },
}, },
}, },

View File

@ -1,19 +1,7 @@
import { Attributes, XmlAttributeComponent, XmlComponent } from "file/xml-components"; import { Attributes, XmlAttributeComponent, XmlComponent } from "file/xml-components";
import { import { AlignmentType } from "../paragraph/formatting";
Alignment, import { IParagraphStylePropertiesOptions, ParagraphProperties } from "../paragraph/properties";
AlignmentType, import { IRunStylePropertiesOptions, RunProperties } from "../paragraph/run/properties";
Indent,
KeepLines,
KeepNext,
Spacing,
TabStop,
TabStopType,
ThematicBreak,
} from "../paragraph/formatting";
import { ParagraphProperties } from "../paragraph/properties";
import * as formatting from "../paragraph/run/formatting";
import { RunProperties } from "../paragraph/run/properties";
import { IParagraphStyleOptions2, IRunStyleOptions } from "../styles/style-options";
interface ILevelAttributesProperties { interface ILevelAttributesProperties {
readonly ilvl?: number; readonly ilvl?: number;
@ -85,8 +73,8 @@ export interface ILevelsOptions {
readonly start?: number; readonly start?: number;
readonly suffix?: LevelSuffix; readonly suffix?: LevelSuffix;
readonly style?: { readonly style?: {
readonly run?: IRunStyleOptions; readonly run?: IRunStylePropertiesOptions;
readonly paragraph?: IParagraphStyleOptions2; readonly paragraph?: IParagraphStylePropertiesOptions;
}; };
} }
@ -125,8 +113,8 @@ export class LevelBase extends XmlComponent {
this.root.push(new LevelText(text)); this.root.push(new LevelText(text));
} }
this.paragraphProperties = new ParagraphProperties({}); this.paragraphProperties = new ParagraphProperties(style && style.paragraph);
this.runProperties = new RunProperties(); this.runProperties = new RunProperties(style && style.run);
this.root.push(this.paragraphProperties); this.root.push(this.paragraphProperties);
this.root.push(this.runProperties); this.root.push(this.runProperties);
@ -134,104 +122,6 @@ export class LevelBase extends XmlComponent {
if (suffix) { if (suffix) {
this.root.push(new Suffix(suffix)); this.root.push(new Suffix(suffix));
} }
if (style) {
if (style.run) {
if (style.run.size) {
this.runProperties.push(new formatting.Size(style.run.size));
}
if (style.run.bold) {
this.runProperties.push(new formatting.Bold());
}
if (style.run.italics) {
this.runProperties.push(new formatting.Italics());
}
if (style.run.smallCaps) {
this.runProperties.push(new formatting.SmallCaps());
}
if (style.run.allCaps) {
this.runProperties.push(new formatting.Caps());
}
if (style.run.strike) {
this.runProperties.push(new formatting.Strike());
}
if (style.run.doubleStrike) {
this.runProperties.push(new formatting.DoubleStrike());
}
if (style.run.subScript) {
this.runProperties.push(new formatting.SubScript());
}
if (style.run.superScript) {
this.runProperties.push(new formatting.SuperScript());
}
if (style.run.underline) {
this.runProperties.push(new formatting.Underline(style.run.underline.type, style.run.underline.color));
}
if (style.run.emphasisMark) {
this.runProperties.push(new formatting.EmphasisMark(style.run.emphasisMark.type));
}
if (style.run.color) {
this.runProperties.push(new formatting.Color(style.run.color));
}
if (style.run.font) {
this.runProperties.push(new formatting.RunFonts(style.run.font));
}
if (style.run.highlight) {
this.runProperties.push(new formatting.Highlight(style.run.highlight));
}
if (style.run.shadow) {
this.runProperties.push(new formatting.Shading(style.run.shadow.type, style.run.shadow.fill, style.run.shadow.color));
}
}
if (style.paragraph) {
if (style.paragraph.alignment) {
this.paragraphProperties.push(new Alignment(style.paragraph.alignment));
}
if (style.paragraph.thematicBreak) {
this.paragraphProperties.push(new ThematicBreak());
}
if (style.paragraph.rightTabStop) {
this.paragraphProperties.push(new TabStop(TabStopType.RIGHT, style.paragraph.rightTabStop));
}
if (style.paragraph.leftTabStop) {
this.paragraphProperties.push(new TabStop(TabStopType.LEFT, style.paragraph.leftTabStop));
}
if (style.paragraph.indent) {
this.paragraphProperties.push(new Indent(style.paragraph.indent));
}
if (style.paragraph.spacing) {
this.paragraphProperties.push(new Spacing(style.paragraph.spacing));
}
if (style.paragraph.keepNext) {
this.paragraphProperties.push(new KeepNext());
}
if (style.paragraph.keepLines) {
this.paragraphProperties.push(new KeepLines());
}
}
}
} }
} }

View File

@ -3,48 +3,13 @@ import { FootnoteReferenceRun } from "file/footnotes/footnote/run/reference-run"
import { IXmlableObject, XmlComponent } from "file/xml-components"; import { IXmlableObject, XmlComponent } from "file/xml-components";
import { File } from "../file"; import { File } from "../file";
import { Alignment, AlignmentType } from "./formatting/alignment"; import { PageBreak } from "./formatting/page-break";
import { Bidirectional } from "./formatting/bidirectional"; import { Bookmark, HyperlinkRef } from "./links";
import { IBorderOptions, ThematicBreak } from "./formatting/border"; import { IParagraphPropertiesOptions, ParagraphProperties } from "./properties";
import { IIndentAttributesProperties, Indent } from "./formatting/indent";
import { KeepLines, KeepNext } from "./formatting/keep";
import { PageBreak, 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";
import { Bookmark, HyperlinkRef, OutlineLevel } from "./links";
import { ParagraphProperties } from "./properties";
import { PictureRun, Run, SequentialIdentifier, SymbolRun, TextRun } from "./run"; import { PictureRun, Run, SequentialIdentifier, SymbolRun, TextRun } from "./run";
export interface IParagraphOptions { export interface IParagraphOptions extends IParagraphPropertiesOptions {
readonly text?: string; readonly text?: string;
readonly border?: IBorderOptions;
readonly spacing?: ISpacingProperties;
readonly outlineLevel?: number;
readonly alignment?: AlignmentType;
readonly heading?: HeadingLevel;
readonly bidirectional?: boolean;
readonly thematicBreak?: boolean;
readonly pageBreakBefore?: boolean;
readonly contextualSpacing?: boolean;
readonly indent?: IIndentAttributesProperties;
readonly keepLines?: boolean;
readonly keepNext?: boolean;
readonly tabStops?: Array<{
readonly position: number | TabStopPosition;
readonly type: TabStopType;
readonly leader?: LeaderType;
}>;
readonly style?: string;
readonly bullet?: {
readonly level: number;
};
readonly numbering?: {
readonly reference: string;
readonly level: number;
readonly custom?: boolean;
};
readonly children?: Array< readonly children?: Array<
TextRun | PictureRun | SymbolRun | Bookmark | PageBreak | SequentialIdentifier | FootnoteReferenceRun | HyperlinkRef TextRun | PictureRun | SymbolRun | Bookmark | PageBreak | SequentialIdentifier | FootnoteReferenceRun | HyperlinkRef
>; >;
@ -70,9 +35,7 @@ export class Paragraph extends XmlComponent {
return; return;
} }
this.properties = new ParagraphProperties({ this.properties = new ParagraphProperties(options);
border: options.border,
});
this.root.push(this.properties); this.root.push(this.properties);
@ -80,72 +43,6 @@ export class Paragraph extends XmlComponent {
this.root.push(new TextRun(options.text)); this.root.push(new TextRun(options.text));
} }
if (options.spacing) {
this.properties.push(new Spacing(options.spacing));
}
if (options.outlineLevel !== undefined) {
this.properties.push(new OutlineLevel(options.outlineLevel));
}
if (options.alignment) {
this.properties.push(new Alignment(options.alignment));
}
if (options.heading) {
this.properties.push(new Style(options.heading));
}
if (options.bidirectional) {
this.properties.push(new Bidirectional());
}
if (options.thematicBreak) {
this.properties.push(new ThematicBreak());
}
if (options.pageBreakBefore) {
this.properties.push(new PageBreakBefore());
}
if (options.contextualSpacing) {
this.properties.push(new ContextualSpacing(options.contextualSpacing));
}
if (options.indent) {
this.properties.push(new Indent(options.indent));
}
if (options.keepLines) {
this.properties.push(new KeepLines());
}
if (options.keepNext) {
this.properties.push(new KeepNext());
}
if (options.tabStops) {
for (const tabStop of options.tabStops) {
this.properties.push(new TabStop(tabStop.type, tabStop.position, tabStop.leader));
}
}
if (options.style) {
this.properties.push(new Style(options.style));
}
if (options.bullet) {
this.properties.push(new Style("ListParagraph"));
this.properties.push(new NumberProperties(1, options.bullet.level));
}
if (options.numbering) {
if (!options.numbering.custom) {
this.properties.push(new Style("ListParagraph"));
}
this.properties.push(new NumberProperties(options.numbering.reference, options.numbering.level));
}
if (options.children) { if (options.children) {
for (const child of options.children) { for (const child of options.children) {
if (child instanceof Bookmark) { if (child instanceof Bookmark) {

View File

@ -1,19 +1,136 @@
// http://officeopenxml.com/WPparagraphProperties.php // http://officeopenxml.com/WPparagraphProperties.php
import { IgnoreIfEmptyXmlComponent, XmlComponent } from "file/xml-components"; import { IgnoreIfEmptyXmlComponent, XmlComponent } from "file/xml-components";
import { Alignment, AlignmentType } from "./formatting/alignment";
import { Bidirectional } from "./formatting/bidirectional";
import { Border, IBorderOptions, 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";
import { OutlineLevel } from "./links";
import { Border, IBorderOptions } from "./formatting/border"; 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;
}
interface IParagraphPropertiesOptions { export interface IParagraphPropertiesOptions extends IParagraphStylePropertiesOptions {
readonly border?: IBorderOptions; readonly border?: IBorderOptions;
readonly heading?: HeadingLevel;
readonly bidirectional?: boolean;
readonly pageBreakBefore?: boolean;
readonly tabStops?: Array<{
readonly position: number | TabStopPosition;
readonly type: TabStopType;
readonly leader?: LeaderType;
}>;
readonly style?: string;
readonly bullet?: {
readonly level: number;
};
readonly numbering?: {
readonly reference: string;
readonly level: number;
readonly custom?: boolean;
};
} }
export class ParagraphProperties extends IgnoreIfEmptyXmlComponent { export class ParagraphProperties extends IgnoreIfEmptyXmlComponent {
constructor(options: IParagraphPropertiesOptions) { constructor(options?: IParagraphPropertiesOptions) {
super("w:pPr"); super("w:pPr");
if (!options) {
return;
}
if (options.border) { if (options.border) {
this.push(new Border(options.border)); this.push(new Border(options.border));
} }
if (options.spacing) {
this.push(new Spacing(options.spacing));
}
if (options.outlineLevel !== undefined) {
this.push(new OutlineLevel(options.outlineLevel));
}
if (options.alignment) {
this.push(new Alignment(options.alignment));
}
if (options.heading) {
this.push(new Style(options.heading));
}
if (options.bidirectional) {
this.push(new Bidirectional());
}
if (options.thematicBreak) {
this.push(new ThematicBreak());
}
if (options.pageBreakBefore) {
this.push(new PageBreakBefore());
}
if (options.contextualSpacing) {
this.push(new ContextualSpacing(options.contextualSpacing));
}
if (options.indent) {
this.push(new Indent(options.indent));
}
if (options.keepLines) {
this.push(new KeepLines());
}
if (options.keepNext) {
this.push(new KeepNext());
}
if (options.tabStops) {
for (const tabStop of options.tabStops) {
this.push(new TabStop(tabStop.type, tabStop.position, tabStop.leader));
}
}
if (options.style) {
this.push(new Style(options.style));
}
if (options.bullet) {
this.push(new Style("ListParagraph"));
this.push(new NumberProperties(1, options.bullet.level));
}
if (options.numbering) {
if (!options.numbering.custom) {
this.push(new Style("ListParagraph"));
}
this.push(new NumberProperties(options.numbering.reference, options.numbering.level));
}
if (options.rightTabStop) {
this.push(new TabStop(TabStopType.RIGHT, options.rightTabStop));
}
if (options.leftTabStop) {
this.push(new TabStop(TabStopType.LEFT, options.leftTabStop));
}
} }
public push(item: XmlComponent): void { public push(item: XmlComponent): void {

View File

@ -1,13 +0,0 @@
import { XmlComponent } from "file/xml-components";
export class SmallCaps extends XmlComponent {
constructor() {
super("w:smallCaps");
}
}
export class Caps extends XmlComponent {
constructor() {
super("w:caps");
}
}

View File

@ -115,17 +115,6 @@ export class Imprint extends XmlComponent {
} }
} }
/* export class Shadow extends XmlComponent {
constructor() {
super("w:shadow");
this.root.push(
new Attributes({
val: true,
}),
);
}
} */
export class SmallCaps extends XmlComponent { export class SmallCaps extends XmlComponent {
constructor() { constructor() {
super("w:smallCaps"); super("w:smallCaps");

View File

@ -1,4 +1,5 @@
export * from "./run"; export * from "./run";
export * from "./properties";
export * from "./text-run"; export * from "./text-run";
export * from "./symbol-run"; export * from "./symbol-run";
export * from "./picture-run"; export * from "./picture-run";

View File

@ -1,8 +1,183 @@
import { ShadingType } from "file/table";
import { IgnoreIfEmptyXmlComponent, XmlComponent } from "file/xml-components"; import { IgnoreIfEmptyXmlComponent, XmlComponent } from "file/xml-components";
import { EmphasisMark, EmphasisMarkType } from "./emphasis-mark";
import {
Bold,
BoldComplexScript,
Caps,
CharacterSpacing,
Color,
DoubleStrike,
Highlight,
HighlightComplexScript,
Italics,
ItalicsComplexScript,
RightToLeft,
Shading,
ShadowComplexScript,
Size,
SizeComplexScript,
SmallCaps,
Strike,
} from "./formatting";
import { IFontAttributesProperties, RunFonts } from "./run-fonts";
import { SubScript, SuperScript } from "./script";
import { Style } from "./style";
import { Underline, UnderlineType } from "./underline";
interface IFontOptions {
readonly name: string;
readonly hint?: string;
}
export interface IRunStylePropertiesOptions {
readonly bold?: boolean;
readonly boldComplexScript?: boolean;
readonly italics?: boolean;
readonly italicsComplexScript?: boolean;
readonly underline?: {
readonly color?: string;
readonly type?: UnderlineType;
};
readonly emphasisMark?: {
readonly type?: EmphasisMarkType;
};
readonly color?: string;
readonly size?: number;
readonly sizeComplexScript?: boolean | number;
readonly rightToLeft?: boolean;
readonly smallCaps?: boolean;
readonly allCaps?: boolean;
readonly strike?: boolean;
readonly doubleStrike?: boolean;
readonly subScript?: boolean;
readonly superScript?: boolean;
readonly font?: string | IFontOptions | IFontAttributesProperties;
readonly highlight?: string;
readonly highlightComplexScript?: boolean | string;
readonly characterSpacing?: number;
readonly shading?: {
readonly type: ShadingType;
readonly fill: string;
readonly color: string;
};
readonly shadingComplexScript?: boolean | IRunStylePropertiesOptions["shading"];
readonly shadow?: IRunStylePropertiesOptions["shading"];
}
export interface IRunPropertiesOptions extends IRunStylePropertiesOptions {
readonly style?: string;
}
export class RunProperties extends IgnoreIfEmptyXmlComponent { export class RunProperties extends IgnoreIfEmptyXmlComponent {
constructor() { constructor(options?: IRunPropertiesOptions) {
super("w:rPr"); super("w:rPr");
if (!options) {
return;
}
if (options.bold) {
this.push(new Bold());
}
if ((options.boldComplexScript === undefined && options.bold) || options.boldComplexScript) {
this.push(new BoldComplexScript());
}
if (options.italics) {
this.push(new Italics());
}
if ((options.italicsComplexScript === undefined && options.italics) || options.italicsComplexScript) {
this.push(new ItalicsComplexScript());
}
if (options.underline) {
this.push(new Underline(options.underline.type, options.underline.color));
}
if (options.emphasisMark) {
this.push(new EmphasisMark(options.emphasisMark.type));
}
if (options.color) {
this.push(new Color(options.color));
}
if (options.size) {
this.push(new Size(options.size));
}
const szCs =
options.sizeComplexScript === undefined || options.sizeComplexScript === true ? options.size : options.sizeComplexScript;
if (szCs) {
this.push(new SizeComplexScript(szCs));
}
if (options.rightToLeft) {
this.push(new RightToLeft());
}
if (options.smallCaps) {
this.push(new SmallCaps());
}
if (options.allCaps) {
this.push(new Caps());
}
if (options.strike) {
this.push(new Strike());
}
if (options.doubleStrike) {
this.push(new DoubleStrike());
}
if (options.subScript) {
this.push(new SubScript());
}
if (options.superScript) {
this.push(new SuperScript());
}
if (options.style) {
this.push(new Style(options.style));
}
if (options.font) {
if (typeof options.font === "string") {
this.push(new RunFonts(options.font));
} else if ("name" in options.font) {
this.push(new RunFonts(options.font.name, options.font.hint));
} else {
this.push(new RunFonts(options.font));
}
}
if (options.highlight) {
this.push(new Highlight(options.highlight));
}
const highlightCs =
options.highlightComplexScript === undefined || options.highlightComplexScript === true
? options.highlight
: options.highlightComplexScript;
if (highlightCs) {
this.push(new HighlightComplexScript(highlightCs));
}
if (options.characterSpacing) {
this.push(new CharacterSpacing(options.characterSpacing));
}
const shading = options.shading || options.shadow;
if (shading) {
this.push(new Shading(shading.type, shading.fill, shading.color));
}
const shdCs =
options.shadingComplexScript === undefined || options.shadingComplexScript === true ? shading : options.shadingComplexScript;
if (shdCs) {
this.push(new ShadowComplexScript(shdCs.type, shdCs.fill, shdCs.color));
}
} }
public push(item: XmlComponent): void { public push(item: XmlComponent): void {

View File

@ -116,7 +116,7 @@ describe("Run", () => {
}); });
const tree = new Formatter().format(run); const tree = new Formatter().format(run);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:smallCaps": {} }] }], "w:r": [{ "w:rPr": [{ "w:smallCaps": { _attr: { "w:val": true } } }] }],
}); });
}); });
}); });
@ -128,7 +128,7 @@ describe("Run", () => {
}); });
const tree = new Formatter().format(run); const tree = new Formatter().format(run);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:caps": {} }] }], "w:r": [{ "w:rPr": [{ "w:caps": { _attr: { "w:val": true } } }] }],
}); });
}); });
}); });

View File

@ -1,69 +1,15 @@
// http://officeopenxml.com/WPtext.php // http://officeopenxml.com/WPtext.php
import { ShadingType } from "file/table";
import { XmlComponent } from "file/xml-components"; import { XmlComponent } from "file/xml-components";
import { FootnoteReferenceRun } from "file/footnotes/footnote/run/reference-run"; import { FootnoteReferenceRun } from "file/footnotes/footnote/run/reference-run";
import { FieldInstruction } from "file/table-of-contents/field-instruction"; import { FieldInstruction } from "file/table-of-contents/field-instruction";
import { Break } from "./break"; import { Break } from "./break";
import { Caps, SmallCaps } from "./caps";
import { EmphasisMark, EmphasisMarkType } from "./emphasis-mark";
import { Begin, End, Separate } from "./field"; import { Begin, End, Separate } from "./field";
import {
Bold,
BoldComplexScript,
Color,
DoubleStrike,
Highlight,
HighlightComplexScript,
Italics,
ItalicsComplexScript,
RightToLeft,
Shading,
ShadowComplexScript,
Size,
SizeComplexScript,
Strike,
} from "./formatting";
import { NumberOfPages, NumberOfPagesSection, Page } from "./page-number"; import { NumberOfPages, NumberOfPagesSection, Page } from "./page-number";
import { RunProperties } from "./properties"; import { IRunPropertiesOptions, RunProperties } from "./properties";
import { Text } from "./run-components/text"; import { Text } from "./run-components/text";
import { IFontAttributesProperties, RunFonts } from "./run-fonts";
import { SubScript, SuperScript } from "./script";
import { Style } from "./style";
import { Underline, UnderlineType } from "./underline";
interface IFontOptions { export interface IRunOptions extends IRunPropertiesOptions {
readonly name: string;
readonly hint?: string;
}
export interface IRunOptions {
readonly bold?: true;
readonly italics?: true;
readonly underline?: {
readonly color?: string;
readonly type?: UnderlineType;
};
readonly emphasisMark?: {
readonly type?: EmphasisMarkType;
};
readonly color?: string;
readonly size?: number;
readonly rightToLeft?: boolean;
readonly smallCaps?: boolean;
readonly allCaps?: boolean;
readonly strike?: boolean;
readonly doubleStrike?: boolean;
readonly subScript?: boolean;
readonly superScript?: boolean;
readonly style?: string;
readonly font?: IFontOptions | IFontAttributesProperties;
readonly highlight?: string;
readonly shading?: {
readonly type: ShadingType;
readonly fill: string;
readonly color: string;
};
readonly children?: Array<Begin | FieldInstruction | Separate | End | PageNumber | FootnoteReferenceRun | string>; readonly children?: Array<Begin | FieldInstruction | Separate | End | PageNumber | FootnoteReferenceRun | string>;
readonly text?: string; readonly text?: string;
} }
@ -79,86 +25,9 @@ export class Run extends XmlComponent {
constructor(options: IRunOptions) { constructor(options: IRunOptions) {
super("w:r"); super("w:r");
this.properties = new RunProperties(); this.properties = new RunProperties(options);
this.root.push(this.properties); this.root.push(this.properties);
if (options.bold) {
this.properties.push(new Bold());
this.properties.push(new BoldComplexScript());
}
if (options.italics) {
this.properties.push(new Italics());
this.properties.push(new ItalicsComplexScript());
}
if (options.underline) {
this.properties.push(new Underline(options.underline.type, options.underline.color));
}
if (options.emphasisMark) {
this.properties.push(new EmphasisMark(options.emphasisMark.type));
}
if (options.color) {
this.properties.push(new Color(options.color));
}
if (options.size) {
this.properties.push(new Size(options.size));
this.properties.push(new SizeComplexScript(options.size));
}
if (options.rightToLeft) {
this.properties.push(new RightToLeft());
}
if (options.smallCaps) {
this.properties.push(new SmallCaps());
}
if (options.allCaps) {
this.properties.push(new Caps());
}
if (options.strike) {
this.properties.push(new Strike());
}
if (options.doubleStrike) {
this.properties.push(new DoubleStrike());
}
if (options.subScript) {
this.properties.push(new SubScript());
}
if (options.superScript) {
this.properties.push(new SuperScript());
}
if (options.style) {
this.properties.push(new Style(options.style));
}
if (options.font) {
if ("name" in options.font) {
this.properties.push(new RunFonts(options.font.name, options.font.hint));
} else {
this.properties.push(new RunFonts(options.font));
}
}
if (options.highlight) {
this.properties.push(new Highlight(options.highlight));
this.properties.push(new HighlightComplexScript(options.highlight));
}
if (options.shading) {
this.properties.push(new Shading(options.shading.type, options.shading.fill, options.shading.color));
this.properties.push(new ShadowComplexScript(options.shading.type, options.shading.fill, options.shading.color));
}
if (options.children) { if (options.children) {
for (const child of options.children) { for (const child of options.children) {
if (typeof child === "string") { if (typeof child === "string") {

View File

@ -1,15 +1,24 @@
import { IParagraphStylePropertiesOptions } from "file/paragraph/properties";
import { IRunStylePropertiesOptions } from "file/paragraph/run/properties";
import { XmlComponent } from "file/xml-components"; import { XmlComponent } from "file/xml-components";
import { ParagraphPropertiesDefaults } from "./paragraph-properties"; import { ParagraphPropertiesDefaults } from "./paragraph-properties";
import { RunPropertiesDefaults } from "./run-properties"; import { RunPropertiesDefaults } from "./run-properties";
export interface IDocumentDefaultsOptions {
readonly paragraph?: IParagraphStylePropertiesOptions;
readonly run?: IRunStylePropertiesOptions;
}
export class DocumentDefaults extends XmlComponent { export class DocumentDefaults extends XmlComponent {
private readonly runPropertiesDefaults: RunPropertiesDefaults; private readonly runPropertiesDefaults: RunPropertiesDefaults;
private readonly paragraphPropertiesDefaults: ParagraphPropertiesDefaults; private readonly paragraphPropertiesDefaults: ParagraphPropertiesDefaults;
constructor() { constructor(options?: IDocumentDefaultsOptions) {
super("w:docDefaults"); super("w:docDefaults");
this.runPropertiesDefaults = new RunPropertiesDefaults();
this.paragraphPropertiesDefaults = new ParagraphPropertiesDefaults(); this.runPropertiesDefaults = new RunPropertiesDefaults(options && options.run);
this.paragraphPropertiesDefaults = new ParagraphPropertiesDefaults(options && options.paragraph);
this.root.push(this.runPropertiesDefaults); this.root.push(this.runPropertiesDefaults);
this.root.push(this.paragraphPropertiesDefaults); this.root.push(this.paragraphPropertiesDefaults);
} }

View File

@ -1,9 +1,9 @@
import { ParagraphProperties } from "file/paragraph/properties"; import { IParagraphStylePropertiesOptions, ParagraphProperties } from "file/paragraph/properties";
import { XmlComponent } from "file/xml-components"; import { XmlComponent } from "file/xml-components";
export class ParagraphPropertiesDefaults extends XmlComponent { export class ParagraphPropertiesDefaults extends XmlComponent {
constructor() { constructor(options?: IParagraphStylePropertiesOptions) {
super("w:pPrDefault"); super("w:pPrDefault");
this.root.push(new ParagraphProperties({})); this.root.push(new ParagraphProperties(options));
} }
} }

View File

@ -1,14 +1,14 @@
import { Size, SizeComplexScript } from "file/paragraph/run/formatting"; import { Size, SizeComplexScript } from "file/paragraph/run/formatting";
import { RunProperties } from "file/paragraph/run/properties"; import { IRunStylePropertiesOptions, RunProperties } from "file/paragraph/run/properties";
import { IFontAttributesProperties, RunFonts } from "file/paragraph/run/run-fonts"; import { IFontAttributesProperties, RunFonts } from "file/paragraph/run/run-fonts";
import { XmlComponent } from "file/xml-components"; import { XmlComponent } from "file/xml-components";
export class RunPropertiesDefaults extends XmlComponent { export class RunPropertiesDefaults extends XmlComponent {
private readonly properties: RunProperties; private readonly properties: RunProperties;
constructor() { constructor(options?: IRunStylePropertiesOptions) {
super("w:rPrDefault"); super("w:rPrDefault");
this.properties = new RunProperties(); this.properties = new RunProperties(options);
this.root.push(this.properties); this.root.push(this.properties);
} }

View File

@ -1,4 +1,4 @@
export * from "./styles"; export * from "./styles";
export * from "./style/character-style"; export * from "./style/character-style";
export * from "./style/paragraph-style"; export * from "./style/paragraph-style";
export * from "./style-options"; export * from "./defaults";

View File

@ -1,56 +0,0 @@
import {
AlignmentType,
EmphasisMarkType,
IFontAttributesProperties,
IIndentAttributesProperties,
ISpacingProperties,
UnderlineType,
} from "../paragraph";
import { ShadingType } from "../table";
export interface IRunStyleOptions {
readonly size?: number;
readonly bold?: boolean;
readonly italics?: boolean;
readonly smallCaps?: boolean;
readonly allCaps?: boolean;
readonly strike?: boolean;
readonly doubleStrike?: boolean;
readonly subScript?: boolean;
readonly superScript?: boolean;
readonly underline?: {
readonly type?: UnderlineType;
readonly color?: string;
};
readonly emphasisMark?: {
readonly type?: EmphasisMarkType;
};
readonly color?: string;
readonly font?: string | IFontAttributesProperties;
readonly characterSpacing?: number;
readonly highlight?: string;
readonly shadow?: {
readonly type: ShadingType;
readonly fill: string;
readonly color: string;
};
}
export interface IParagraphStyleOptions2 {
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;
}
// Needed because of: https://github.com/s-panferov/awesome-typescript-loader/issues/432
/**
* @ignore
*/
export const WORKAROUND4 = "";

View File

@ -582,6 +582,7 @@ describe("CharacterStyle", () => {
id: "myStyleId", id: "myStyleId",
run: { run: {
bold: true, bold: true,
boldComplexScript: false,
}, },
}); });
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
@ -610,6 +611,7 @@ describe("CharacterStyle", () => {
id: "myStyleId", id: "myStyleId",
run: { run: {
italics: true, italics: true,
italicsComplexScript: false,
}, },
}); });
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
@ -678,6 +680,7 @@ describe("CharacterStyle", () => {
id: "myStyleId", id: "myStyleId",
run: { run: {
highlight: "005599", highlight: "005599",
highlightComplexScript: false,
}, },
}); });
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
@ -710,6 +713,7 @@ describe("CharacterStyle", () => {
fill: "00FFFF", fill: "00FFFF",
color: "FF0000", color: "FF0000",
}, },
shadingComplexScript: false,
}, },
}); });
const tree = new Formatter().format(style); const tree = new Formatter().format(style);

View File

@ -1,8 +1,4 @@
import { EmphasisMarkType } from "file/paragraph/run/emphasis-mark"; import { IRunStylePropertiesOptions, RunProperties } from "file/paragraph/run/properties";
import * as formatting from "file/paragraph/run/formatting";
import { RunProperties } from "file/paragraph/run/properties";
import { IFontAttributesProperties } from "file/paragraph/run/run-fonts";
import { UnderlineType } from "file/paragraph/run/underline";
import { BasedOn, Link, SemiHidden, UiPriority, UnhideWhenUsed } from "./components"; import { BasedOn, Link, SemiHidden, UiPriority, UnhideWhenUsed } from "./components";
import { Style } from "./style"; import { Style } from "./style";
@ -11,33 +7,7 @@ export interface IBaseCharacterStyleOptions {
readonly basedOn?: string; readonly basedOn?: string;
readonly link?: string; readonly link?: string;
readonly semiHidden?: boolean; readonly semiHidden?: boolean;
readonly run?: { readonly run?: IRunStylePropertiesOptions;
readonly size?: number;
readonly bold?: boolean;
readonly italics?: boolean;
readonly smallCaps?: boolean;
readonly allCaps?: boolean;
readonly strike?: boolean;
readonly doubleStrike?: boolean;
readonly subScript?: boolean;
readonly superScript?: boolean;
readonly underline?: {
readonly type?: UnderlineType;
readonly color?: string;
};
readonly emphasisMark?: {
readonly type?: EmphasisMarkType;
};
readonly color?: string;
readonly font?: string | IFontAttributesProperties;
readonly characterSpacing?: number;
readonly highlight?: string;
readonly shadow?: {
readonly type: string;
readonly fill: string;
readonly color: string;
};
};
} }
export interface ICharacterStyleOptions extends IBaseCharacterStyleOptions { export interface ICharacterStyleOptions extends IBaseCharacterStyleOptions {
@ -50,7 +20,9 @@ export class CharacterStyle extends Style {
constructor(options: ICharacterStyleOptions) { constructor(options: ICharacterStyleOptions) {
super({ type: "character", styleId: options.id }, options.name); super({ type: "character", styleId: options.id }, options.name);
this.runProperties = new RunProperties();
this.runProperties = new RunProperties(options.run);
this.root.push(this.runProperties); this.root.push(this.runProperties);
this.root.push(new UiPriority(99)); this.root.push(new UiPriority(99));
this.root.push(new UnhideWhenUsed()); this.root.push(new UnhideWhenUsed());
@ -66,72 +38,5 @@ export class CharacterStyle extends Style {
if (options.semiHidden) { if (options.semiHidden) {
this.root.push(new SemiHidden()); this.root.push(new SemiHidden());
} }
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));
}
if (options.run.emphasisMark) {
this.runProperties.push(new formatting.EmphasisMark(options.run.emphasisMark.type));
}
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));
}
}
} }
} }

View File

@ -548,6 +548,7 @@ describe("ParagraphStyle", () => {
id: "myStyleId", id: "myStyleId",
run: { run: {
bold: true, bold: true,
boldComplexScript: false,
}, },
}); });
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
@ -566,6 +567,7 @@ describe("ParagraphStyle", () => {
id: "myStyleId", id: "myStyleId",
run: { run: {
italics: true, italics: true,
italicsComplexScript: false,
}, },
}); });
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
@ -584,6 +586,7 @@ describe("ParagraphStyle", () => {
id: "myStyleId", id: "myStyleId",
run: { run: {
highlight: "005599", highlight: "005599",
highlightComplexScript: false,
}, },
}); });
const tree = new Formatter().format(style); const tree = new Formatter().format(style);
@ -606,6 +609,7 @@ describe("ParagraphStyle", () => {
fill: "00FFFF", fill: "00FFFF",
color: "FF0000", color: "FF0000",
}, },
shadingComplexScript: false,
}, },
}); });
const tree = new Formatter().format(style); const tree = new Formatter().format(style);

View File

@ -1,19 +1,6 @@
import { import { IParagraphStylePropertiesOptions, IRunStylePropertiesOptions, ParagraphProperties } from "file/paragraph";
Alignment,
ContextualSpacing,
Indent,
KeepLines,
KeepNext,
OutlineLevel,
ParagraphProperties,
Spacing,
ThematicBreak,
} from "file/paragraph";
import { TabStop, TabStopType } from "file/paragraph/formatting";
import * as formatting from "file/paragraph/run/formatting";
import { RunProperties } from "file/paragraph/run/properties"; import { RunProperties } from "file/paragraph/run/properties";
import { IParagraphStyleOptions2, IRunStyleOptions } from "../style-options";
import { BasedOn, Link, Next, QuickFormat, SemiHidden, UiPriority, UnhideWhenUsed } from "./components"; import { BasedOn, Link, Next, QuickFormat, SemiHidden, UiPriority, UnhideWhenUsed } from "./components";
import { Style } from "./style"; import { Style } from "./style";
@ -25,22 +12,25 @@ export interface IBaseParagraphStyleOptions {
readonly semiHidden?: boolean; readonly semiHidden?: boolean;
readonly uiPriority?: number; readonly uiPriority?: number;
readonly unhideWhenUsed?: boolean; readonly unhideWhenUsed?: boolean;
readonly run?: IRunStyleOptions; readonly paragraph?: IParagraphStylePropertiesOptions;
readonly paragraph?: IParagraphStyleOptions2; readonly run?: IRunStylePropertiesOptions;
} }
export interface IParagraphStyleOptions extends IBaseParagraphStyleOptions { export interface IParagraphStyleOptions extends IBaseParagraphStyleOptions {
readonly id: string; readonly id: string;
readonly name?: string; readonly name?: string;
} }
export class ParagraphStyle extends Style { export class ParagraphStyle extends Style {
private readonly paragraphProperties: ParagraphProperties; private readonly paragraphProperties: ParagraphProperties;
private readonly runProperties: RunProperties; private readonly runProperties: RunProperties;
constructor(options: IParagraphStyleOptions) { constructor(options: IParagraphStyleOptions) {
super({ type: "paragraph", styleId: options.id }, options.name); super({ type: "paragraph", styleId: options.id }, options.name);
this.paragraphProperties = new ParagraphProperties({});
this.runProperties = new RunProperties(); this.paragraphProperties = new ParagraphProperties(options.paragraph);
this.runProperties = new RunProperties(options.run);
this.root.push(this.paragraphProperties); this.root.push(this.paragraphProperties);
this.root.push(this.runProperties); this.root.push(this.runProperties);
@ -71,114 +61,5 @@ export class ParagraphStyle extends Style {
if (options.unhideWhenUsed) { if (options.unhideWhenUsed) {
this.root.push(new 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));
}
if (options.run.emphasisMark) {
this.runProperties.push(new formatting.EmphasisMark(options.run.emphasisMark.type));
}
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));
}
if (options.paragraph.rightTabStop) {
this.paragraphProperties.push(new TabStop(TabStopType.RIGHT, options.paragraph.rightTabStop));
}
if (options.paragraph.leftTabStop) {
this.paragraphProperties.push(new TabStop(TabStopType.LEFT, options.paragraph.leftTabStop));
}
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));
}
}
} }
} }