docxml
Version:
TypeScript (component) library for building and parsing a DOCX file
103 lines (102 loc) • 4.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Section = exports.sectionChildComponentNames = void 0;
// Import without assignment ensures Deno does not tree-shake this component. To avoid circular
// definitions, components register themselves in a side-effect of their module.
require("./Paragraph.js");
require("./Table.js");
const Component_js_1 = require("../classes/Component.js");
const section_properties_js_1 = require("../properties/section-properties.js");
const components_js_1 = require("../utilities/components.js");
const namespaces_js_1 = require("../utilities/namespaces.js");
const xquery_js_1 = require("../utilities/xquery.js");
const Paragraph_js_1 = require("./Paragraph.js");
exports.sectionChildComponentNames = [
'Table',
'Paragraph',
'BookmarkRangeStart',
'BookmarkRangeEnd',
];
/**
* A component that represents a DOCX section, which could have its own page sizing options and so
* on.
*
* In normal OOXML this information belongs at either the end of the document, or inside the
* formatting options of the last paragraph belonging to that section. This component will smooth
* that over in such a way that you can simply put `<Paragraph>` (etc.) inside `<Section>`.
*/
class Section extends Component_js_1.Component {
/**
* Creates an XML DOM node for this component instance.
*/
async toNode(ancestry) {
const parent = ancestry[0];
if (!parent) {
throw new Error(`Cannot serialize a section without parent context.`);
}
const siblings = (0, Component_js_1.isComponentDefinition)(parent)
? parent.children
: await parent.children;
const isLastSection = siblings[siblings.length - 1] === this;
if (isLastSection) {
return [...(await this.childrenToNode(ancestry)), (0, section_properties_js_1.sectionPropertiesToNode)(this.props)];
}
const lastChild = this.children[this.children.length - 1];
if (lastChild instanceof Paragraph_js_1.Paragraph) {
lastChild.setSectionProperties(this.props);
}
else {
const paragraph = new Paragraph_js_1.Paragraph({});
paragraph.setSectionProperties(this.props);
this.children.push(paragraph);
}
const nodes = await this.childrenToNode(ancestry);
return nodes;
}
/**
* Asserts whether or not a given XML node correlates with this component.
*/
static matchesNode(node) {
return node.nodeName === 'w:sectPr';
}
/**
* Instantiate this component from the XML in an existing DOCX file.
*/
static fromNode(node, context) {
const { children } = (0, xquery_js_1.evaluateXPathToMap)(`
map {
"children": array{
if (parent::${namespaces_js_1.QNS.w}body) then (
./preceding-sibling::${namespaces_js_1.QNS.w}*[
not(./${namespaces_js_1.QNS.w}pPr/${namespaces_js_1.QNS.w}sectPr) and
not(following-sibling::${namespaces_js_1.QNS.w}p[./${namespaces_js_1.QNS.w}pPr/${namespaces_js_1.QNS.w}sectPr])
]
) else (
let $nth := count(../../preceding-sibling::${namespaces_js_1.QNS.w}p[./${namespaces_js_1.QNS.w}pPr/${namespaces_js_1.QNS.w}sectPr])
return (
../../preceding-sibling::${namespaces_js_1.QNS.w}*[
count(preceding-sibling::${namespaces_js_1.QNS.w}p[./${namespaces_js_1.QNS.w}pPr/${namespaces_js_1.QNS.w}sectPr]) = $nth
],
../..
)
)
}
}
`, node);
return new Section((0, section_properties_js_1.sectionPropertiesFromNode)(node), ...(0, components_js_1.createChildComponentsFromNodes)(this.children, children, context));
}
}
exports.Section = Section;
Object.defineProperty(Section, "children", {
enumerable: true,
configurable: true,
writable: true,
value: exports.sectionChildComponentNames
});
Object.defineProperty(Section, "mixed", {
enumerable: true,
configurable: true,
writable: true,
value: false
});
(0, components_js_1.registerComponent)(Section);