Skip to content

Commit

Permalink
Add defaultTabStop setting (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
StarLederer authored Dec 4, 2023
1 parent 4481d0a commit 7c88319
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/files/SettingsXml.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expect, it, run } from 'https://deno.land/x/tincan@1.0.1/mod.ts';

import { RelationshipType } from '../enums.ts';
import { pt } from '../utilities/length.ts';
import { SettingsXml } from './SettingsXml.ts';

describe('SettingsXml', () => {
Expand All @@ -27,6 +28,12 @@ describe('SettingsXml', () => {
expect(meta).toBeTruthy();
expect(settings.relationships.getTarget(meta?.id as string)).toBe('foobar');
});
it('defaultTabStop', () => {
const settings = new SettingsXml('test');
expect(settings.get('defaultTabStop')).toBe(null);
settings.set('defaultTabStop', pt(50));
expect(settings.get('defaultTabStop')).toEqual(pt(50));
});
});

run();
42 changes: 38 additions & 4 deletions src/files/SettingsXml.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import * as path from 'https://deno.land/std@0.187.0/path/mod.ts';

import { ContentTypesXml } from '../../mod.ts';
import { ContentTypesXml, Length } from '../../mod.ts';
import { Archive } from '../classes/Archive.ts';
import { XmlFileWithContentTypes } from '../classes/XmlFile.ts';
import { FileMime, RelationshipType } from '../enums.ts';
import { create } from '../utilities/dom.ts';
import { twip } from '../utilities/length.ts';
import { ALL_NAMESPACE_DECLARATIONS, QNS } from '../utilities/namespaces.ts';
import { evaluateXPathToMap } from '../utilities/xquery.ts';
import { evaluateXPathToMap, evaluateXPathToNumber } from '../utilities/xquery.ts';
import { File, RelationshipsXml } from './RelationshipsXml.ts';

export type SettingsI = {
Expand All @@ -20,20 +21,29 @@ export type SettingsI = {
evenAndOddHeaders: boolean;

attachedTemplate: string | null;

defaultTabStop: Length | null;
};

const DEFAULT_SETTINGS: SettingsI = {
isTrackChangesEnabled: false,
evenAndOddHeaders: false,
attachedTemplate: null,
defaultTabStop: null,
};

enum SettingType {
Length,
OnOff,
Relationship,
}

type SettingMeta =
| {
docxmlName: keyof SettingsI;
ooxmlLocalName: string;
ooxmlType: SettingType.Length;
}
| {
docxmlName: keyof SettingsI;
ooxmlLocalName: string;
Expand Down Expand Up @@ -62,6 +72,11 @@ const settingsMeta: Array<SettingMeta> = [
ooxmlType: SettingType.Relationship,
ooxmlRelationshipType: RelationshipType.attachedTemplate,
},
{
docxmlName: 'defaultTabStop',
ooxmlLocalName: 'defaultTabStop',
ooxmlType: SettingType.Length,
},
];

export class SettingsXml extends XmlFileWithContentTypes {
Expand Down Expand Up @@ -139,7 +154,15 @@ export class SettingsXml extends XmlFileWithContentTypes {
} else (),
if ($attachedTemplate) then element ${QNS.w}attachedTemplate {
attribute ${QNS.r}id { $attachedTemplate }
} else ()
} else (),
${
this.#props.defaultTabStop
? `
element ${QNS.w}defaultTabStop {
attribute ${QNS.w}val { map:get($defaultTabStop, 'twip') }
}`
: '()'
}
}
</w:settings>`,
this.#props,
Expand Down Expand Up @@ -181,13 +204,24 @@ export class SettingsXml extends XmlFileWithContentTypes {
// );
}

const xml = await archive.readXml(location);

const settings = evaluateXPathToMap<SettingsI>(
`/${QNS.w}settings/map {
"isTrackChangesEnabled": docxml:ct-on-off(./${QNS.w}trackChanges),
"evenAndOddHeaders": docxml:ct-on-off(./${QNS.w}evenAndOddHeaders)
}`,
await archive.readXml(location),
xml,
);

const defaultTabStopTwips = evaluateXPathToNumber(
`number(/*/${QNS.w}defaultTabStop/@${QNS.w}val)`,
xml,
);
if (defaultTabStopTwips !== null) {
settings.defaultTabStop = twip(defaultTabStopTwips);
}

return new SettingsXml(
location,
relationships || new RelationshipsXml(relationshipsLocation),
Expand Down

0 comments on commit 7c88319

Please sign in to comment.