docxml
Version:
TypeScript (component) library for building and parsing a DOCX file
124 lines (123 loc) • 4.84 kB
JavaScript
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _Paragraph_sectionProperties;
// 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.
//
// Add items to this list that would otherwise only be depended on as a type definition.
import './BookmarkRangeEnd.js';
import './BookmarkRangeStart.js';
import './Comment.js';
import './CommentRangeEnd.js';
import './CommentRangeStart.js';
import './Field.js';
import './Hyperlink.js';
import './Text.js';
import './TextAddition.js';
import './TextDeletion.js';
import { Component } from '../classes/Component.js';
import { paragraphPropertiesFromNode, paragraphPropertiesToNode, } from '../properties/paragraph-properties.js';
import { createChildComponentsFromNodes, registerComponent } from '../utilities/components.js';
import { create } from '../utilities/dom.js';
import { QNS } from '../utilities/namespaces.js';
import { evaluateXPathToMap } from '../utilities/xquery.js';
/**
* A component that represents a paragraph in your DOCX document, which is one of the most
* widely used components and the most likely to contain a style rule or other.
*
* A paragraph is a block-level element and contains text and inlines, see also {@link Text}.
*/
export class Paragraph extends Component {
constructor() {
super(...arguments);
_Paragraph_sectionProperties.set(this, null);
}
/**
* Set properties to the section that this paragraph is supposed to represent. Not intended to be
* called manually. Only here because OOXML somehow decided that a section is defined in the last
* paragraph of it, rather than as an element of its own.
*/
setSectionProperties(properties) {
__classPrivateFieldSet(this, _Paragraph_sectionProperties, properties || null, "f");
}
/**
* Creates an XML DOM node for this component instance.
*/
async toNode(ancestry) {
return create(`
element ${QNS.w}p {
$pPr,
$children
}
`, {
pPr: paragraphPropertiesToNode(this.props, __classPrivateFieldGet(this, _Paragraph_sectionProperties, "f")),
children: await this.childrenToNode(ancestry),
});
}
/**
* Asserts whether or not a given XML node correlates with this component.
*/
static matchesNode(node) {
return node.nodeName === 'w:p';
}
/**
* Instantiate this component from the XML in an existing DOCX file.
*/
static fromNode(node, context) {
const { children, ppr, ...props } = evaluateXPathToMap(`
map {
"ppr": ./${QNS.w}pPr,
"style": ./${QNS.w}pPr/${QNS.w}pStyle/@${QNS.w}val/string(),
"children": array{ ./(
${QNS.w}r |
${QNS.w}hyperlink |
${QNS.w}fldSimple |
${QNS.w}del |
${QNS.w}ins |
${QNS.w}commentRangeStart |
${QNS.w}commentRangeEnd |
${QNS.w}bookmarkStart |
${QNS.w}bookmarkEnd
) }
}
`, node);
return new Paragraph({
...paragraphPropertiesFromNode(ppr),
...props,
}, ...createChildComponentsFromNodes(this.children, children, context));
}
}
_Paragraph_sectionProperties = new WeakMap();
Object.defineProperty(Paragraph, "children", {
enumerable: true,
configurable: true,
writable: true,
value: [
'BookmarkRangeEnd',
'BookmarkRangeStart',
'Comment',
'CommentRangeEnd',
'CommentRangeStart',
'Hyperlink',
'Text',
'TextAddition',
'TextDeletion',
'Field',
]
});
Object.defineProperty(Paragraph, "mixed", {
enumerable: true,
configurable: true,
writable: true,
value: false
});
registerComponent(Paragraph);