Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2388 - Add default tab stop #2470

Merged
merged 4 commits into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions demo/75-tab-stops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const receiptTabStops = [
const twoTabStops = [{ type: TabStopType.RIGHT, position: TabStopPosition.MAX }];

const doc = new Document({
defaultTabStop: 0,
sections: [
{
properties: {},
Expand Down
1 change: 1 addition & 0 deletions src/file/core-properties/properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface IPropertiesOptions {
readonly compatibility?: ICompatibilityOptions;
readonly customProperties?: readonly ICustomPropertyOptions[];
readonly evenAndOddHeaderAndFooters?: boolean;
readonly defaultTabStop?: number;
}

// <xs:element name="coreProperties" type="CT_CoreProperties"/>
Expand Down
1 change: 1 addition & 0 deletions src/file/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export class File {
evenAndOddHeaders: options.evenAndOddHeaderAndFooters ? true : false,
trackRevisions: options.features?.trackRevisions,
updateFields: options.features?.updateFields,
defaultTabStop: options.defaultTabStop,
});

this.media = new Media();
Expand Down
4 changes: 2 additions & 2 deletions src/file/paragraph/properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,9 @@ export class ParagraphProperties extends IgnoreIfEmptyXmlComponent {
* Ensure there is only one w:tabs tag with multiple w:tab
*/
const tabDefinitions: readonly TabStopDefinition[] = [
...(options.rightTabStop ? [{ type: TabStopType.RIGHT, position: options.rightTabStop }] : []),
...(options.rightTabStop !== undefined ? [{ type: TabStopType.RIGHT, position: options.rightTabStop }] : []),
...(options.tabStops ? options.tabStops : []),
...(options.leftTabStop ? [{ type: TabStopType.LEFT, position: options.leftTabStop }] : []),
...(options.leftTabStop !== undefined ? [{ type: TabStopType.LEFT, position: options.leftTabStop }] : []),
];

if (tabDefinitions.length > 0) {
Expand Down
17 changes: 17 additions & 0 deletions src/file/settings/settings.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,23 @@ describe("Settings", () => {
});
});

it("should add defaultTabStop setting with version", () => {
const settings = new Settings({
defaultTabStop: 100,
});

const tree = new Formatter().format(settings);
expect(Object.keys(tree)).has.length(1);
expect(tree["w:settings"]).to.be.an("array");
expect(tree["w:settings"]).to.deep.include({
"w:defaultTabStop": {
_attr: {
"w:val": 100,
},
},
});
});

// TODO: Remove when deprecating compatibilityModeVersion
it("should add compatibility setting with legacy version", () => {
const settings = new Settings({
Expand Down
8 changes: 7 additions & 1 deletion src/file/settings/settings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OnOffElement, XmlAttributeComponent, XmlComponent } from "@file/xml-components";
import { NumberValueElement, OnOffElement, XmlAttributeComponent, XmlComponent } from "@file/xml-components";

import { Compatibility, ICompatibilityOptions } from "./compatibility";

Expand Down Expand Up @@ -152,6 +152,7 @@ export interface ISettingsOptions {
readonly trackRevisions?: boolean;
readonly updateFields?: boolean;
readonly compatibility?: ICompatibilityOptions;
readonly defaultTabStop?: number;
}

export class Settings extends XmlComponent {
Expand Down Expand Up @@ -198,6 +199,11 @@ export class Settings extends XmlComponent {
this.root.push(new OnOffElement("w:updateFields", options.updateFields));
}

// https://c-rex.net/samples/ooxml/e1/Part4/OOXML_P4_DOCX_defaultTabStop_topic_ID0EIXSX.html
if (options.defaultTabStop !== undefined) {
this.root.push(new NumberValueElement("w:defaultTabStop", options.defaultTabStop));
}

this.root.push(
new Compatibility({
...(options.compatibility ?? {}),
Expand Down