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;
beforeEach(() => {
tabStop = new TabStop({ type: TabStopType.LEFT, position: 100 });
tabStop = new TabStop([{ type: TabStopType.LEFT, position: 100 }]);
});
describe("#constructor()", () => {
@ -32,7 +32,7 @@ describe("RightTabStop", () => {
let tabStop: TabStop;
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()", () => {

View File

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

View File

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