UNPKG

docxml

Version:

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

113 lines (112 loc) 6.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.sectionPropertiesToNode = exports.sectionPropertiesFromNode = void 0; const dom_js_1 = require("../utilities/dom.js"); const namespaces_js_1 = require("../utilities/namespaces.js"); const xquery_js_1 = require("../utilities/xquery.js"); function sectionPropertiesFromNode(node) { if (!node) { return {}; } const data = (0, xquery_js_1.evaluateXPathToMap)(`map { "headers": map { "first": ./${namespaces_js_1.QNS.w}headerReference[@${namespaces_js_1.QNS.w}type = 'first']/@${namespaces_js_1.QNS.r}id/string(), "even": ./${namespaces_js_1.QNS.w}headerReference[@${namespaces_js_1.QNS.w}type = 'even']/@${namespaces_js_1.QNS.r}id/string(), "odd": ./${namespaces_js_1.QNS.w}headerReference[@${namespaces_js_1.QNS.w}type = 'default']/@${namespaces_js_1.QNS.r}id/string() }, "footers": map { "first": ./${namespaces_js_1.QNS.w}footerReference[@${namespaces_js_1.QNS.w}type = 'first']/@${namespaces_js_1.QNS.r}id/string(), "even": ./${namespaces_js_1.QNS.w}footerReference[@${namespaces_js_1.QNS.w}type = 'even']/@${namespaces_js_1.QNS.r}id/string(), "odd": ./${namespaces_js_1.QNS.w}footerReference[@${namespaces_js_1.QNS.w}type = 'default']/@${namespaces_js_1.QNS.r}id/string() }, "pageWidth": docxml:length(${namespaces_js_1.QNS.w}pgSz/@${namespaces_js_1.QNS.w}w, 'twip'), "pageHeight": docxml:length(${namespaces_js_1.QNS.w}pgSz/@${namespaces_js_1.QNS.w}h, 'twip'), "pageOrientation": ./${namespaces_js_1.QNS.w}pgSz/@${namespaces_js_1.QNS.w}orient/string(), "pageMargin": map { "top": docxml:length(./${namespaces_js_1.QNS.w}pgMar/@${namespaces_js_1.QNS.w}top, 'twip'), "right": docxml:length(./${namespaces_js_1.QNS.w}pgMar/@${namespaces_js_1.QNS.w}right, 'twip'), "bottom": docxml:length(./${namespaces_js_1.QNS.w}pgMar/@${namespaces_js_1.QNS.w}bottom, 'twip'), "left": docxml:length(./${namespaces_js_1.QNS.w}pgMar/@${namespaces_js_1.QNS.w}left, 'twip'), "header": docxml:length(./${namespaces_js_1.QNS.w}pgMar/@${namespaces_js_1.QNS.w}header, 'twip'), "footer": docxml:length(./${namespaces_js_1.QNS.w}pgMar/@${namespaces_js_1.QNS.w}footer, 'twip'), "gutter": docxml:length(./${namespaces_js_1.QNS.w}pgMar/@${namespaces_js_1.QNS.w}gutter, 'twip') }, "isTitlePage": exists(./${namespaces_js_1.QNS.w}titlePg) and (not(./${namespaces_js_1.QNS.w}titlePg/@${namespaces_js_1.QNS.w}val) or docxml:st-on-off(./${namespaces_js_1.QNS.w}titlePg/@${namespaces_js_1.QNS.w}val)) }`, node); return data; } exports.sectionPropertiesFromNode = sectionPropertiesFromNode; function sectionPropertiesToNode(data = {}) { return (0, dom_js_1.create)(`element ${namespaces_js_1.QNS.w}sectPr { if (exists($headers('first'))) then element ${namespaces_js_1.QNS.w}headerReference { attribute ${namespaces_js_1.QNS.r}id { $headers('first') }, attribute ${namespaces_js_1.QNS.w}type { 'first' } } else (), if (exists($headers('even'))) then element ${namespaces_js_1.QNS.w}headerReference { attribute ${namespaces_js_1.QNS.r}id { $headers('even') }, attribute ${namespaces_js_1.QNS.w}type { 'even' } } else (), if (exists($headers('odd'))) then element ${namespaces_js_1.QNS.w}headerReference { attribute ${namespaces_js_1.QNS.r}id { $headers('odd') }, attribute ${namespaces_js_1.QNS.w}type { 'default' } } else (), if (exists($footers('first'))) then element ${namespaces_js_1.QNS.w}footerReference { attribute ${namespaces_js_1.QNS.r}id { $footers('first') }, attribute ${namespaces_js_1.QNS.w}type { 'first' } } else (), if (exists($footers('even'))) then element ${namespaces_js_1.QNS.w}footerReference { attribute ${namespaces_js_1.QNS.r}id { $footers('even') }, attribute ${namespaces_js_1.QNS.w}type { 'even' } } else (), if (exists($footers('odd'))) then element ${namespaces_js_1.QNS.w}footerReference { attribute ${namespaces_js_1.QNS.r}id { $footers('odd') }, attribute ${namespaces_js_1.QNS.w}type { 'default' } } else (), if (exists($pageWidth) or exists($pageHeight) or $pageOrientation) then element ${namespaces_js_1.QNS.w}pgSz { if (exists($pageWidth)) then attribute ${namespaces_js_1.QNS.w}w { round($pageWidth('twip')) } else (), if (exists($pageHeight)) then attribute ${namespaces_js_1.QNS.w}h { round($pageHeight('twip')) } else (), if ($pageOrientation) then attribute ${namespaces_js_1.QNS.w}orient { $pageOrientation } else () } else (), if (exists($pageMargin)) then element ${namespaces_js_1.QNS.w}pgMar { if (exists($pageMargin('top'))) then attribute ${namespaces_js_1.QNS.w}top { round($pageMargin('top')('twip')) } else (), if (exists($pageMargin('right'))) then attribute ${namespaces_js_1.QNS.w}right { round($pageMargin('right')('twip')) } else (), if (exists($pageMargin('bottom'))) then attribute ${namespaces_js_1.QNS.w}bottom { round($pageMargin('bottom')('twip')) } else (), if (exists($pageMargin('left'))) then attribute ${namespaces_js_1.QNS.w}left { round($pageMargin('left')('twip')) } else (), if (exists($pageMargin('header'))) then attribute ${namespaces_js_1.QNS.w}header { round($pageMargin('header')('twip')) } else (), if (exists($pageMargin('footer'))) then attribute ${namespaces_js_1.QNS.w}footer { round($pageMargin('footer')('twip')) } else (), if (exists($pageMargin('gutter'))) then attribute ${namespaces_js_1.QNS.w}gutter { round($pageMargin('gutter')('twip')) } else () } else (), if(exists($isTitlePage)) then element ${namespaces_js_1.QNS.w}titlePg { attribute ${namespaces_js_1.QNS.w}val { "1" } } else () }`, { headers: typeof data.headers === 'string' ? { first: data.headers, even: data.headers, odd: data.headers } : data.headers || {}, footers: typeof data.footers === 'string' ? { first: data.footers, even: data.footers, odd: data.footers } : data.footers || {}, pageWidth: data.pageWidth || null, pageHeight: data.pageHeight || null, pageMargin: data.pageMargin || null, pageOrientation: data.pageOrientation || null, isTitlePage: data.isTitlePage || null, }); } exports.sectionPropertiesToNode = sectionPropertiesToNode;