FIX: multiple tabStop support for LibreWriter

This commit is contained in:
Ronit Ramdam BK
2022-10-14 16:38:02 +05:45
parent 059455929b
commit 629c586014
3 changed files with 37 additions and 15 deletions

View File

@ -8,7 +8,7 @@ describe("LeftTabStop", () => {
let tabStop: TabStop; let tabStop: TabStop;
beforeEach(() => { beforeEach(() => {
tabStop = new TabStop(TabStopType.LEFT, 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(TabStopType.RIGHT, 100, LeaderType.DOT); tabStop = new TabStop({ type: TabStopType.RIGHT, position: 100, leader: LeaderType.DOT});
}); });
describe("#constructor()", () => { describe("#constructor()", () => {

View File

@ -1,10 +1,22 @@
// http://officeopenxml.com/WPtab.php // http://officeopenxml.com/WPtab.php
import { XmlAttributeComponent, XmlComponent } from "@file/xml-components"; import { XmlAttributeComponent, XmlComponent } from "@file/xml-components";
export interface TabStopDefinition {
type: TabStopType,
position: number | TabStopPosition,
leader?: LeaderType
}
export class TabStop extends XmlComponent { export class TabStop extends XmlComponent {
public constructor(type: TabStopType, position: number, leader?: LeaderType) { public constructor(tabDefs: (TabStopDefinition[] | TabStopDefinition)) {
super("w:tabs"); super("w:tabs");
this.root.push(new TabStopItem(type, position, leader)); if (Array.isArray(tabDefs)) {
tabDefs.forEach((function(tabDef) {
this.root.push(new TabStopItem(tabDef));
}).bind(this));
} else {
this.root.push(new TabStopItem(tabDefs));
}
} }
} }
@ -41,14 +53,14 @@ export class TabAttributes extends XmlAttributeComponent<{
} }
export class TabStopItem extends XmlComponent { export class TabStopItem extends XmlComponent {
public constructor(value: TabStopType, position: string | number, leader?: LeaderType) { public constructor(tabDef: TabStopDefinition) {
super("w:tab"); super("w:tab");
this.root.push( this.root.push(
new TabAttributes({ new TabAttributes({
val: value, val: tabDef.type,
pos: position, pos: tabDef.position,
leader, 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, TabStopPosition, TabStopType } from "./formatting/tab-stop"; import { LeaderType, TabStop, TabStopDefinition, TabStopPosition, 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";
@ -132,20 +132,30 @@ export class ParagraphProperties extends IgnoreIfEmptyXmlComponent {
this.push(new Shading(options.shading)); this.push(new Shading(options.shading));
} }
/**
* FIX: Multitab support for Libre Writer
* Ensure there is only one w:tabs tag with multiple w:tab
*/
let tabDefs: TabStopDefinition[] = [];
if (options.rightTabStop) { if (options.rightTabStop) {
this.push(new TabStop(TabStopType.RIGHT, options.rightTabStop)); tabDefs.push({ type: TabStopType.RIGHT, position: options.rightTabStop});
} }
if (options.tabStops) { if (options.tabStops) {
for (const tabStop of options.tabStops) { tabDefs = tabDefs.concat(options.tabStops);
this.push(new TabStop(tabStop.type, tabStop.position, tabStop.leader));
}
} }
if (options.leftTabStop) { if (options.leftTabStop) {
this.push(new TabStop(TabStopType.LEFT, options.leftTabStop)); tabDefs.push({ type: TabStopType.LEFT, position: options.leftTabStop });
} }
if (tabDefs.length) {
this.push(new TabStop(tabDefs));
}
/**
* FIX - END
*/
if (options.bidirectional !== undefined) { if (options.bidirectional !== undefined) {
this.push(new OnOffElement("w:bidi", options.bidirectional)); this.push(new OnOffElement("w:bidi", options.bidirectional));
} }