open-agreements
Version:
Open-source legal template filling CLI and library
68 lines • 3.19 kB
JavaScript
/**
* word/settings.xml emitter.
*
* Only emitted when the document actually needs a setting: `w:evenAndOddHeaders`
* for any section declaring an even-page header or footer, or
* `w:clrSchemeMapping` when theme-relative authoring or a custom theme is used.
*
* @conformance ECMA-376 edition 5, Part 1 § 17.10.1
*/
import { createWmlElement } from '../../primitives/dom-helpers.js';
import { OOXML, W } from '../../primitives/namespaces.js';
import { parseXml, serializeXml, XML_DECL } from '../../primitives/xml.js';
const SETTINGS_CONTENT_TYPE = 'application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml';
const SETTINGS_REL_TYPE = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings';
export function emitSettingsPartIfNeeded(spec, ctx) {
const needsEvenOdd = spec.sections.some((s) => s.headers?.even || s.footers?.even);
const needsColorSchemeMapping = spec.theme !== undefined || usesThemeRelativeAuthoring(spec);
if (!needsEvenOdd && !needsColorSchemeMapping)
return;
ctx.registerPart('word/settings.xml', SETTINGS_CONTENT_TYPE, SETTINGS_REL_TYPE);
const doc = parseXml(`<w:settings xmlns:w="${OOXML.W_NS}"/>`);
if (needsEvenOdd) {
doc.documentElement.appendChild(createWmlElement(doc, W.evenAndOddHeaders));
}
if (needsColorSchemeMapping) {
doc.documentElement.appendChild(createWmlElement(doc, W.clrSchemeMapping, {
'w:bg1': 'light1',
'w:t1': 'dark1',
'w:bg2': 'light2',
'w:t2': 'dark2',
'w:accent1': 'accent1',
'w:accent2': 'accent2',
'w:accent3': 'accent3',
'w:accent4': 'accent4',
'w:accent5': 'accent5',
'w:accent6': 'accent6',
'w:hyperlink': 'hyperlink',
'w:followedHyperlink': 'followedHyperlink',
}));
}
ctx.setFileContent('word/settings.xml', XML_DECL + serializeXml(doc));
}
function usesThemeRelativeAuthoring(spec) {
if (spec.styles?.some((style) => style.run?.themeColor !== undefined))
return true;
if (spec.numbering?.some((numbering) => numbering.levels.some((level) => level.runProps?.themeColor !== undefined)))
return true;
return spec.sections.some((section) => {
const blocks = [
...section.blocks,
...Object.values(section.headers ?? {}).flatMap((story) => story?.blocks ?? []),
...Object.values(section.footers ?? {}).flatMap((story) => story?.blocks ?? []),
];
return blocks.some(blockUsesThemeRelativeAuthoring);
});
}
function blockUsesThemeRelativeAuthoring(block) {
if (block.kind === 'paragraph')
return block.runs.some(inlineUsesThemeRelativeAuthoring);
return block.rows.some((row) => row.cells.some(cellUsesThemeRelativeAuthoring));
}
function inlineUsesThemeRelativeAuthoring(inline) {
return (inline.kind === 'text' || inline.kind === 'field') && inline.themeColor !== undefined;
}
function cellUsesThemeRelativeAuthoring(cell) {
return cell.themeFill !== undefined || cell.blocks.some(blockUsesThemeRelativeAuthoring);
}
//# sourceMappingURL=settings-part.js.map