UNPKG

docxml

Version:

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

88 lines (87 loc) 3.76 kB
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; import './Text.js'; import { Component } from '../classes/Component.js'; import { RelationshipType } from '../enums.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 hyperlink to another part of the same document. */ export class Hyperlink extends Component { constructor() { super(...arguments); _Hyperlink_relationshipId.set(this, null); } ensureRelationship(relationships) { if (!this.props.url) { return; } __classPrivateFieldSet(this, _Hyperlink_relationshipId, relationships.add(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 create(` element ${QNS.w}hyperlink { if(exists($relationshipId)) then attribute ${QNS.r}id { $relationshipId } else (), if(exists($anchor)) then attribute ${QNS.w}anchor { $anchor } else (), if(exists($tooltip)) then attribute ${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 } = evaluateXPathToMap(`map { "anchor": ./@${QNS.w}anchor/string(), "tooltip": ./@${QNS.w}tooltip/string(), "children": array{ ./( ${QNS.w}r | ${QNS.w}fldSimple ) } }`, node); return new Hyperlink(props, ...createChildComponentsFromNodes(this.children, children, context)); } } _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 }); registerComponent(Hyperlink);