UNPKG

docxml

Version:

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

84 lines (83 loc) 2.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Field = void 0; require("./Text.js"); const Component_js_1 = require("../classes/Component.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 (simple) instruction field. */ class Field extends Component_js_1.Component { /** * 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}fldSimple { attribute ${namespaces_js_1.QNS.w}instr { $instruction }, if ($isDirty) then attribute ${namespaces_js_1.QNS.w}dirty { "1" } else (), if ($isLocked) then attribute ${namespaces_js_1.QNS.w}fldLock { "1" } else (), $children } `, { instruction: this.props.instruction, isDirty: !!this.props.isDirty, isLocked: !!this.props.isLocked, children: await this.childrenToNode(ancestry), }); } /** * Asserts whether or not a given XML node correlates with this component. */ static matchesNode(node) { return node.nodeName === 'w:fldSimple'; } /** * Instantiate this component from the XML in an existing DOCX file. */ static fromNode(node, context) { const { children, ...props } = (0, xquery_js_1.evaluateXPathToMap)(`map { "instruction": ./@${namespaces_js_1.QNS.w}instruction/string(), "isDirty": docxml:st-on-off(@${namespaces_js_1.QNS.w}dirty), "isLocked": docxml:st-on-off(@${namespaces_js_1.QNS.w}fldLock), "children": array{ ./( ${namespaces_js_1.QNS.w}r | ${namespaces_js_1.QNS.w}hyperlink | ${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}bookmarkRangeStart | ${namespaces_js_1.QNS.w}bookmarkRangeEnd ) } }`, node); return new Field(props, ...(0, components_js_1.createChildComponentsFromNodes)(this.children, children, context)); } } exports.Field = Field; Object.defineProperty(Field, "children", { enumerable: true, configurable: true, writable: true, value: [ 'BookmarkRangeStart', 'BookmarkRangeEnd', 'CommentRangeStart', 'CommentRangeEnd', 'TextDeletion', 'TextAddition', 'Text', 'Hyperlink', ] }); Object.defineProperty(Field, "mixed", { enumerable: true, configurable: true, writable: true, value: false }); (0, components_js_1.registerComponent)(Field);