UNPKG

docxml

Version:

TypeScript (component) library for building and parsing a DOCX file

92 lines (91 loc) 4.1 kB
"use strict"; 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 _Hyperlink_relationshipId; Object.defineProperty(exports, "__esModule", { value: true }); exports.Hyperlink = void 0; require("./Text.js"); const Component_js_1 = require("../classes/Component.js"); const enums_js_1 = require("../enums.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 hyperlink to another part of the same document. */ class Hyperlink extends Component_js_1.Component { constructor() { super(...arguments); _Hyperlink_relationshipId.set(this, null); } ensureRelationship(relationships) { if (!this.props.url) { return; } __classPrivateFieldSet(this, _Hyperlink_relationshipId, relationships.add(enums_js_1.RelationshipType.hyperlink, this.props.url), "f"); } /** * Creates an XML DOM node for this component instance. */ // eslint-disable-next-line @typescript-eslint/no-unused-vars async toNode(ancestry) { return (0, dom_js_1.create)(` element ${namespaces_js_1.QNS.w}hyperlink { if(exists($relationshipId)) then attribute ${namespaces_js_1.QNS.r}id { $relationshipId } else (), if(exists($anchor)) then attribute ${namespaces_js_1.QNS.w}anchor { $anchor } else (), if(exists($tooltip)) then attribute ${namespaces_js_1.QNS.w}tooltip { $tooltip } else (), $children } `, { relationshipId: __classPrivateFieldGet(this, _Hyperlink_relationshipId, "f"), anchor: this.props.bookmark?.name || this.props.anchor || null, tooltip: this.props.tooltip || null, children: await this.childrenToNode(ancestry), }); } /** * Asserts whether or not a given XML node correlates with this component. */ static matchesNode(node) { return node.nodeName === 'w:hyperlink'; } /** * Instantiate this component from the XML in an existing DOCX file. */ static fromNode(node, context) { const { children, ...props } = (0, xquery_js_1.evaluateXPathToMap)(`map { "anchor": ./@${namespaces_js_1.QNS.w}anchor/string(), "tooltip": ./@${namespaces_js_1.QNS.w}tooltip/string(), "children": array{ ./( ${namespaces_js_1.QNS.w}r | ${namespaces_js_1.QNS.w}fldSimple ) } }`, node); return new Hyperlink(props, ...(0, components_js_1.createChildComponentsFromNodes)(this.children, children, context)); } } exports.Hyperlink = Hyperlink; _Hyperlink_relationshipId = new WeakMap(); Object.defineProperty(Hyperlink, "children", { enumerable: true, configurable: true, writable: true, value: ['Text', 'Field'] }); Object.defineProperty(Hyperlink, "mixed", { enumerable: true, configurable: true, writable: true, value: false }); (0, components_js_1.registerComponent)(Hyperlink);