UNPKG

docxtemplater-link-module-free

Version:
91 lines (77 loc) 3.28 kB
'use strict'; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var LinkManager = require('./linkManager'); var wrapper = require('docxtemplater/js/module-wrapper'); var moduleName = 'linkmodule'; var LinkModule = function () { function LinkModule() { _classCallCheck(this, LinkModule); this.name = 'LinkModule'; this.prefix = '$'; } _createClass(LinkModule, [{ key: 'optionsTransformer', value: function optionsTransformer(options, docxtemplater) { var relsFiles = docxtemplater.zip.file(/\.xml\.rels/).concat(docxtemplater.zip.file(/\[Content_Types\].xml/)).map(function (file) { return file.name; }); this.fileTypeConfig = docxtemplater.fileTypeConfig; this.fileType = docxtemplater.fileType; this.zip = docxtemplater.zip; options.xmlFileNames = options.xmlFileNames.concat(relsFiles); return options; } }, { key: 'set', value: function set(obj) { if (obj.zip) { this.zip = obj.zip; } if (obj.compiled) { this.compiled = obj.compiled; } if (obj.xmlDocuments) { this.xmlDocuments = obj.xmlDocuments; } if (obj.data != null) { this.data = obj.data; } } }, { key: 'matchers', value: function matchers() { return [[this.prefix, moduleName]]; } }, { key: 'getRenderedMap', value: function getRenderedMap(mapper) { var _this = this; return Object.keys(this.compiled).reduce(function (mapper, from) { mapper[from] = { from: from, data: _this.data }; return mapper; }, mapper); } }, { key: 'render', value: function render(part, _ref) { var scopeManager = _ref.scopeManager, filePath = _ref.filePath; if (part.module !== moduleName) { return null; } var linkManager = new LinkManager(this.zip, filePath, this.xmlDocuments, this.fileType); var tagValue = scopeManager.getValue(part.value, { part: part }); var rId = linkManager.addLinkRels(tagValue); if (!tagValue) { return { value: this.fileTypeConfig.tagTextXml }; } var value = '</w:t></w:r><w:hyperlink r:id="' + rId + '" w:history="1"><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/><w:r w:rsidR="00052F25" w:rsidRPr="00052F25"><w:rPr><w:rStyle w:val="Hyperlink"/></w:rPr><w:t>' + tagValue + '</w:t></w:r></w:hyperlink><w:r><w:t xml:space="preserve">'; return { value: value }; } }]); return LinkModule; }(); module.exports = function () { return wrapper(new LinkModule()); };