Refactor code to simplify interface

This commit is contained in:
Dolan Miu
2022-10-16 00:21:34 +01:00
parent 70a3b25d54
commit 2c4b55c692
3 changed files with 26 additions and 58 deletions

View File

@ -8,7 +8,7 @@ describe("LeftTabStop", () => {
let tabStop: TabStop; let tabStop: TabStop;
beforeEach(() => { beforeEach(() => {
tabStop = new TabStop({ type: TabStopType.LEFT, position: 100 }); tabStop = new TabStop([{ type: TabStopType.LEFT, position: 100 }]);
}); });
describe("#constructor()", () => { describe("#constructor()", () => {
@ -32,7 +32,7 @@ describe("RightTabStop", () => {
let tabStop: TabStop; let tabStop: TabStop;
beforeEach(() => { beforeEach(() => {
tabStop = new TabStop({ type: TabStopType.RIGHT, position: 100, leader: LeaderType.DOT }); tabStop = new TabStop([{ type: TabStopType.RIGHT, position: 100, leader: LeaderType.DOT }]);
}); });
describe("#constructor()", () => { describe("#constructor()", () => {

View File

@ -2,26 +2,17 @@
import { XmlAttributeComponent, XmlComponent } from "@file/xml-components"; import { XmlAttributeComponent, XmlComponent } from "@file/xml-components";
export interface TabStopDefinition { export interface TabStopDefinition {
type: TabStopType; readonly type: TabStopType;
position: number | TabStopPosition; readonly position: number | TabStopPosition;
leader?: LeaderType; readonly leader?: LeaderType;
} }
export class TabStop extends XmlComponent { export class TabStop extends XmlComponent {
public constructor(tabDefs: TabStopDefinition[] | TabStopDefinition | TabStopType, position?: number, leader?: LeaderType) { public constructor(tabDefinitions: readonly TabStopDefinition[]) {
super("w:tabs"); super("w:tabs");
if (typeof tabDefs === "string") {
this.root.push(new TabStopItem(tabDefs, position, leader)); for (const tabDefinition of tabDefinitions) {
} else { this.root.push(new TabStopItem(tabDefinition));
if (Array.isArray(tabDefs)) {
tabDefs.forEach(
function (tabDef) {
this.root.push(new TabStopItem(tabDef));
}.bind(this),
);
} else {
this.root.push(new TabStopItem(tabDefs));
}
} }
} }
} }
@ -59,26 +50,14 @@ export class TabAttributes extends XmlAttributeComponent<{
} }
export class TabStopItem extends XmlComponent { export class TabStopItem extends XmlComponent {
public constructor(tabDef: TabStopDefinition | TabStopType, position?: number, leader?: LeaderType) { public constructor({ type, position, leader }: TabStopDefinition) {
super("w:tab"); super("w:tab");
if (typeof tabDef === "string") {
if (typeof position === "number")
this.root.push( this.root.push(
new TabAttributes({ new TabAttributes({
val: tabDef, val: type,
pos: position, pos: position,
leader, leader: leader,
}),
);
else throw Error("Undefined position: " + position);
} else {
this.root.push(
new TabAttributes({
val: tabDef.type,
pos: tabDef.position,
leader: tabDef.leader,
}), }),
); );
} }
} }
}

View File

@ -9,7 +9,7 @@ import { PageBreakBefore } from "./formatting/break";
import { IIndentAttributesProperties, Indent } from "./formatting/indent"; import { IIndentAttributesProperties, Indent } from "./formatting/indent";
import { ISpacingProperties, Spacing } from "./formatting/spacing"; import { ISpacingProperties, Spacing } from "./formatting/spacing";
import { HeadingLevel, Style } from "./formatting/style"; import { HeadingLevel, Style } from "./formatting/style";
import { LeaderType, TabStop, TabStopDefinition, TabStopPosition, TabStopType } from "./formatting/tab-stop"; import { TabStop, TabStopDefinition, TabStopType } from "./formatting/tab-stop";
import { NumberProperties } from "./formatting/unordered-list"; import { NumberProperties } from "./formatting/unordered-list";
import { FrameProperties, IFrameOptions } from "./frame/frame-properties"; import { FrameProperties, IFrameOptions } from "./frame/frame-properties";
import { OutlineLevel } from "./links"; import { OutlineLevel } from "./links";
@ -41,11 +41,7 @@ export interface IParagraphPropertiesOptions extends IParagraphStylePropertiesOp
readonly heading?: HeadingLevel; readonly heading?: HeadingLevel;
readonly bidirectional?: boolean; readonly bidirectional?: boolean;
readonly pageBreakBefore?: boolean; readonly pageBreakBefore?: boolean;
readonly tabStops?: readonly { readonly tabStops?: readonly TabStopDefinition[];
readonly position: number | TabStopPosition;
readonly type: TabStopType;
readonly leader?: LeaderType;
}[];
readonly style?: string; readonly style?: string;
readonly bullet?: { readonly bullet?: {
readonly level: number; readonly level: number;
@ -136,21 +132,14 @@ export class ParagraphProperties extends IgnoreIfEmptyXmlComponent {
* FIX: Multitab support for Libre Writer * FIX: Multitab support for Libre Writer
* Ensure there is only one w:tabs tag with multiple w:tab * Ensure there is only one w:tabs tag with multiple w:tab
*/ */
let tabDefs: TabStopDefinition[] = []; const tabDefinitions: readonly TabStopDefinition[] = [
if (options.rightTabStop) { ...(options.rightTabStop ? [{ type: TabStopType.RIGHT, position: options.rightTabStop }] : []),
tabDefs.push({ type: TabStopType.RIGHT, position: options.rightTabStop }); ...(options.tabStops ? options.tabStops : []),
} ...(options.leftTabStop ? [{ type: TabStopType.LEFT, position: options.leftTabStop }] : []),
];
if (options.tabStops) { if (tabDefinitions.length > 0) {
tabDefs = tabDefs.concat(options.tabStops); this.push(new TabStop(tabDefinitions));
}
if (options.leftTabStop) {
tabDefs.push({ type: TabStopType.LEFT, position: options.leftTabStop });
}
if (tabDefs.length) {
this.push(new TabStop(tabDefs));
} }
/** /**
* FIX - END * FIX - END