docxml
Version:
TypeScript (component) library for building and parsing a DOCX file
128 lines (127 loc) • 5.32 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;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Paragraph = 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.
//
// Add items to this list that would otherwise only be depended on as a type definition.
require("./BookmarkRangeEnd.js");
require("./BookmarkRangeStart.js");
require("./Comment.js");
require("./CommentRangeEnd.js");
require("./CommentRangeStart.js");
require("./Field.js");
require("./Hyperlink.js");
require("./Text.js");
require("./TextAddition.js");
require("./TextDeletion.js");
const Component_js_1 = require("../classes/Component.js");
const paragraph_properties_js_1 = require("../properties/paragraph-properties.js");
const components_js_1 = require("../utilities/components.js");
const dom_js_1 = require("../utilities/dom.js");
const namespaces_js_1 = require("../utilities/namespaces.js");
const xquery_js_1 = require("../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}.
*/
class Paragraph extends Component_js_1.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 (0, dom_js_1.create)(`
element ${namespaces_js_1.QNS.w}p {
$pPr,
$children
}
`, {
pPr: (0, paragraph_properties_js_1.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 } = (0, xquery_js_1.evaluateXPathToMap)(`
map {
"ppr": ./${namespaces_js_1.QNS.w}pPr,
"style": ./${namespaces_js_1.QNS.w}pPr/${namespaces_js_1.QNS.w}pStyle/@${namespaces_js_1.QNS.w}val/string(),
"children": array{ ./(
${namespaces_js_1.QNS.w}r |
${namespaces_js_1.QNS.w}hyperlink |
${namespaces_js_1.QNS.w}fldSimple |
${namespaces_js_1.QNS.w}del |
${namespaces_js_1.QNS.w}ins |
${namespaces_js_1.QNS.w}commentRangeStart |
${namespaces_js_1.QNS.w}commentRangeEnd |
${namespaces_js_1.QNS.w}bookmarkStart |
${namespaces_js_1.QNS.w}bookmarkEnd
) }
}
`, node);
return new Paragraph({
...(0, paragraph_properties_js_1.paragraphPropertiesFromNode)(ppr),
...props,
}, ...(0, components_js_1.createChildComponentsFromNodes)(this.children, children, context));
}
}
exports.Paragraph = Paragraph;
_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
});
(0, components_js_1.registerComponent)(Paragraph);