docxml
Version:
TypeScript (component) library for building and parsing a DOCX file
68 lines (67 loc) • 2.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Comment = void 0;
const Component_js_1 = require("../classes/Component.js");
const DocumentXml_js_1 = require("../files/DocumentXml.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");
/**
* The start of a range associated with a comment.
*/
class Comment extends Component_js_1.Component {
/**
* Creates an XML DOM node for this component instance.
*/
toNode(ancestry) {
const doc = ancestry.find((ancestor) => ancestor instanceof DocumentXml_js_1.DocumentXml);
if (!doc || !doc.comments.has(this.props.id)) {
throw new Error(`Comment "${this.props.id}" does not exist`);
}
return (0, dom_js_1.create)(`
element ${namespaces_js_1.QNS.w}r {
element ${namespaces_js_1.QNS.w}rPr {
element ${namespaces_js_1.QNS.w}rStyle {
attribute ${namespaces_js_1.QNS.w}val { "CommentReference" }
}
},
element ${namespaces_js_1.QNS.w}commentReference {
attribute ${namespaces_js_1.QNS.w}id { $id }
}
}
`, {
id: this.props.id,
});
}
/**
* Asserts whether or not a given XML node correlates with this component.
*/
static matchesNode(node) {
return node.nodeName === 'w:commentRangeStart';
}
/**
* Instantiate this component from the XML in an existing DOCX file.
*/
static fromNode(node) {
return new Comment((0, xquery_js_1.evaluateXPathToMap)(`
map {
"id": ./@${namespaces_js_1.QNS.w}id/number()
}
`, node));
}
}
exports.Comment = Comment;
Object.defineProperty(Comment, "children", {
enumerable: true,
configurable: true,
writable: true,
value: []
});
Object.defineProperty(Comment, "mixed", {
enumerable: true,
configurable: true,
writable: true,
value: false
});
(0, components_js_1.registerComponent)(Comment);