: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

@ -1,15 +1,24 @@
import { IParagraphStylePropertiesOptions } from "file/paragraph/properties";
import { IRunStylePropertiesOptions } from "file/paragraph/run/properties";
import { XmlComponent } from "file/xml-components";
import { ParagraphPropertiesDefaults } from "./paragraph-properties";
import { RunPropertiesDefaults } from "./run-properties";
export interface IDocumentDefaultsOptions {
readonly paragraph?: IParagraphStylePropertiesOptions;
readonly run?: IRunStylePropertiesOptions;
}
export class DocumentDefaults extends XmlComponent {
private readonly runPropertiesDefaults: RunPropertiesDefaults;
private readonly paragraphPropertiesDefaults: ParagraphPropertiesDefaults;
constructor() {
constructor(options?: IDocumentDefaultsOptions) {
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.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";
export class ParagraphPropertiesDefaults extends XmlComponent {
constructor() {
constructor(options?: IParagraphStylePropertiesOptions) {
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 { RunProperties } from "file/paragraph/run/properties";
import { IRunStylePropertiesOptions, RunProperties } from "file/paragraph/run/properties";
import { IFontAttributesProperties, RunFonts } from "file/paragraph/run/run-fonts";
import { XmlComponent } from "file/xml-components";
export class RunPropertiesDefaults extends XmlComponent {
private readonly properties: RunProperties;
constructor() {
constructor(options?: IRunStylePropertiesOptions) {
super("w:rPrDefault");
this.properties = new RunProperties();
this.properties = new RunProperties(options);
this.root.push(this.properties);
}

View File

@ -1,4 +1,4 @@
export * from "./styles";
export * from "./style/character-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",
run: {
bold: true,
boldComplexScript: false,
},
});
const tree = new Formatter().format(style);
@ -610,6 +611,7 @@ describe("CharacterStyle", () => {
id: "myStyleId",
run: {
italics: true,
italicsComplexScript: false,
},
});
const tree = new Formatter().format(style);
@ -678,6 +680,7 @@ describe("CharacterStyle", () => {
id: "myStyleId",
run: {
highlight: "005599",
highlightComplexScript: false,
},
});
const tree = new Formatter().format(style);
@ -710,6 +713,7 @@ describe("CharacterStyle", () => {
fill: "00FFFF",
color: "FF0000",
},
shadingComplexScript: false,
},
});
const tree = new Formatter().format(style);

View File

@ -1,8 +1,4 @@
import { EmphasisMarkType } from "file/paragraph/run/emphasis-mark";
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 { IRunStylePropertiesOptions, RunProperties } from "file/paragraph/run/properties";
import { BasedOn, Link, SemiHidden, UiPriority, UnhideWhenUsed } from "./components";
import { Style } from "./style";
@ -11,33 +7,7 @@ export interface IBaseCharacterStyleOptions {
readonly basedOn?: string;
readonly link?: string;
readonly semiHidden?: boolean;
readonly run?: {
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;
};
};
readonly run?: IRunStylePropertiesOptions;
}
export interface ICharacterStyleOptions extends IBaseCharacterStyleOptions {
@ -50,7 +20,9 @@ export class CharacterStyle extends Style {
constructor(options: ICharacterStyleOptions) {
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(new UiPriority(99));
this.root.push(new UnhideWhenUsed());
@ -66,72 +38,5 @@ export class CharacterStyle extends Style {
if (options.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",
run: {
bold: true,
boldComplexScript: false,
},
});
const tree = new Formatter().format(style);
@ -566,6 +567,7 @@ describe("ParagraphStyle", () => {
id: "myStyleId",
run: {
italics: true,
italicsComplexScript: false,
},
});
const tree = new Formatter().format(style);
@ -584,6 +586,7 @@ describe("ParagraphStyle", () => {
id: "myStyleId",
run: {
highlight: "005599",
highlightComplexScript: false,
},
});
const tree = new Formatter().format(style);
@ -606,6 +609,7 @@ describe("ParagraphStyle", () => {
fill: "00FFFF",
color: "FF0000",
},
shadingComplexScript: false,
},
});
const tree = new Formatter().format(style);

View File

@ -1,19 +1,6 @@
import {
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 { IParagraphStylePropertiesOptions, IRunStylePropertiesOptions, ParagraphProperties } from "file/paragraph";
import { RunProperties } from "file/paragraph/run/properties";
import { IParagraphStyleOptions2, IRunStyleOptions } from "../style-options";
import { BasedOn, Link, Next, QuickFormat, SemiHidden, UiPriority, UnhideWhenUsed } from "./components";
import { Style } from "./style";
@ -25,22 +12,25 @@ export interface IBaseParagraphStyleOptions {
readonly semiHidden?: boolean;
readonly uiPriority?: number;
readonly unhideWhenUsed?: boolean;
readonly run?: IRunStyleOptions;
readonly paragraph?: IParagraphStyleOptions2;
readonly paragraph?: IParagraphStylePropertiesOptions;
readonly run?: IRunStylePropertiesOptions;
}
export interface IParagraphStyleOptions extends IBaseParagraphStyleOptions {
readonly id: string;
readonly name?: string;
}
export class ParagraphStyle extends Style {
private readonly paragraphProperties: ParagraphProperties;
private readonly runProperties: RunProperties;
constructor(options: IParagraphStyleOptions) {
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.runProperties);
@ -71,114 +61,5 @@ export class ParagraphStyle extends Style {
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));
}
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));
}
}
}
}