sheet-rels-writer.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. "use strict";
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. 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); } }
  4. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  5. /* eslint-disable max-classes-per-file */
  6. var utils = require('../../utils/utils');
  7. var RelType = require('../../xlsx/rel-type');
  8. var HyperlinksProxy = /*#__PURE__*/function () {
  9. function HyperlinksProxy(sheetRelsWriter) {
  10. _classCallCheck(this, HyperlinksProxy);
  11. this.writer = sheetRelsWriter;
  12. }
  13. _createClass(HyperlinksProxy, [{
  14. key: "push",
  15. value: function push(hyperlink) {
  16. this.writer.addHyperlink(hyperlink);
  17. }
  18. }]);
  19. return HyperlinksProxy;
  20. }();
  21. var SheetRelsWriter = /*#__PURE__*/function () {
  22. function SheetRelsWriter(options) {
  23. _classCallCheck(this, SheetRelsWriter);
  24. // in a workbook, each sheet will have a number
  25. this.id = options.id; // count of all relationships
  26. this.count = 0; // keep record of all hyperlinks
  27. this._hyperlinks = [];
  28. this._workbook = options.workbook;
  29. }
  30. _createClass(SheetRelsWriter, [{
  31. key: "each",
  32. value: function each(fn) {
  33. return this._hyperlinks.forEach(fn);
  34. }
  35. }, {
  36. key: "addHyperlink",
  37. value: function addHyperlink(hyperlink) {
  38. // Write to stream
  39. var relationship = {
  40. Target: hyperlink.target,
  41. Type: RelType.Hyperlink,
  42. TargetMode: 'External'
  43. };
  44. var rId = this._writeRelationship(relationship); // store sheet stuff for later
  45. this._hyperlinks.push({
  46. rId: rId,
  47. address: hyperlink.address
  48. });
  49. }
  50. }, {
  51. key: "addMedia",
  52. value: function addMedia(media) {
  53. return this._writeRelationship(media);
  54. }
  55. }, {
  56. key: "addRelationship",
  57. value: function addRelationship(rel) {
  58. return this._writeRelationship(rel);
  59. }
  60. }, {
  61. key: "commit",
  62. value: function commit() {
  63. if (this.count) {
  64. // write xml utro
  65. this._writeClose(); // and close stream
  66. this.stream.end();
  67. }
  68. } // ================================================================================
  69. }, {
  70. key: "_writeOpen",
  71. value: function _writeOpen() {
  72. this.stream.write("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n <Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">");
  73. }
  74. }, {
  75. key: "_writeRelationship",
  76. value: function _writeRelationship(relationship) {
  77. if (!this.count) {
  78. this._writeOpen();
  79. }
  80. var rId = "rId".concat(++this.count);
  81. if (relationship.TargetMode) {
  82. this.stream.write("<Relationship Id=\"".concat(rId, "\"") + " Type=\"".concat(relationship.Type, "\"") + " Target=\"".concat(utils.xmlEncode(relationship.Target), "\"") + " TargetMode=\"".concat(relationship.TargetMode, "\"") + '/>');
  83. } else {
  84. this.stream.write("<Relationship Id=\"".concat(rId, "\" Type=\"").concat(relationship.Type, "\" Target=\"").concat(relationship.Target, "\"/>"));
  85. }
  86. return rId;
  87. }
  88. }, {
  89. key: "_writeClose",
  90. value: function _writeClose() {
  91. this.stream.write('</Relationships>');
  92. }
  93. }, {
  94. key: "stream",
  95. get: function get() {
  96. if (!this._stream) {
  97. // eslint-disable-next-line no-underscore-dangle
  98. this._stream = this._workbook._openStream("/xl/worksheets/_rels/sheet".concat(this.id, ".xml.rels"));
  99. }
  100. return this._stream;
  101. }
  102. }, {
  103. key: "length",
  104. get: function get() {
  105. return this._hyperlinks.length;
  106. }
  107. }, {
  108. key: "hyperlinksProxy",
  109. get: function get() {
  110. return this._hyperlinksProxy || (this._hyperlinksProxy = new HyperlinksProxy(this));
  111. }
  112. }]);
  113. return SheetRelsWriter;
  114. }();
  115. module.exports = SheetRelsWriter;
  116. //# sourceMappingURL=sheet-rels-writer.js.map