sheet-comments-writer.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. var XmlStream = require('../../utils/xml-stream');
  6. var RelType = require('../../xlsx/rel-type');
  7. var colCache = require('../../utils/col-cache');
  8. var CommentXform = require('../../xlsx/xform/comment/comment-xform');
  9. var VmlShapeXform = require('../../xlsx/xform/comment/vml-shape-xform');
  10. var SheetCommentsWriter = /*#__PURE__*/function () {
  11. function SheetCommentsWriter(worksheet, sheetRelsWriter, options) {
  12. _classCallCheck(this, SheetCommentsWriter);
  13. // in a workbook, each sheet will have a number
  14. this.id = options.id;
  15. this.count = 0;
  16. this._worksheet = worksheet;
  17. this._workbook = options.workbook;
  18. this._sheetRelsWriter = sheetRelsWriter;
  19. }
  20. _createClass(SheetCommentsWriter, [{
  21. key: "_addRelationships",
  22. value: function _addRelationships() {
  23. var commentRel = {
  24. Type: RelType.Comments,
  25. Target: "../comments".concat(this.id, ".xml")
  26. };
  27. this._sheetRelsWriter.addRelationship(commentRel);
  28. var vmlDrawingRel = {
  29. Type: RelType.VmlDrawing,
  30. Target: "../drawings/vmlDrawing".concat(this.id, ".vml")
  31. };
  32. this.vmlRelId = this._sheetRelsWriter.addRelationship(vmlDrawingRel);
  33. }
  34. }, {
  35. key: "_addCommentRefs",
  36. value: function _addCommentRefs() {
  37. this._workbook.commentRefs.push({
  38. commentName: "comments".concat(this.id),
  39. vmlDrawing: "vmlDrawing".concat(this.id)
  40. });
  41. }
  42. }, {
  43. key: "_writeOpen",
  44. value: function _writeOpen() {
  45. this.commentsStream.write('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' + '<comments xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">' + '<authors><author>Author</author></authors>' + '<commentList>');
  46. this.vmlStream.write('<?xml version="1.0" encoding="UTF-8"?>' + '<xml xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:x="urn:schemas-microsoft-com:office:excel">' + '<o:shapelayout v:ext="edit">' + '<o:idmap v:ext="edit" data="1" />' + '</o:shapelayout>' + '<v:shapetype id="_x0000_t202" coordsize="21600,21600" o:spt="202" path="m,l,21600r21600,l21600,xe">' + '<v:stroke joinstyle="miter" />' + '<v:path gradientshapeok="t" o:connecttype="rect" />' + '</v:shapetype>');
  47. }
  48. }, {
  49. key: "_writeComment",
  50. value: function _writeComment(comment, index) {
  51. var commentXform = new CommentXform();
  52. var commentsXmlStream = new XmlStream();
  53. commentXform.render(commentsXmlStream, comment);
  54. this.commentsStream.write(commentsXmlStream.xml);
  55. var vmlShapeXform = new VmlShapeXform();
  56. var vmlXmlStream = new XmlStream();
  57. vmlShapeXform.render(vmlXmlStream, comment, index);
  58. this.vmlStream.write(vmlXmlStream.xml);
  59. }
  60. }, {
  61. key: "_writeClose",
  62. value: function _writeClose() {
  63. this.commentsStream.write('</commentList></comments>');
  64. this.vmlStream.write('</xml>');
  65. }
  66. }, {
  67. key: "addComments",
  68. value: function addComments(comments) {
  69. var _this = this;
  70. if (comments && comments.length) {
  71. if (!this.startedData) {
  72. this._worksheet.comments = [];
  73. this._writeOpen();
  74. this._addRelationships();
  75. this._addCommentRefs();
  76. this.startedData = true;
  77. }
  78. comments.forEach(function (item) {
  79. item.refAddress = colCache.decodeAddress(item.ref);
  80. });
  81. comments.forEach(function (comment) {
  82. _this._writeComment(comment, _this.count);
  83. _this.count += 1;
  84. });
  85. }
  86. }
  87. }, {
  88. key: "commit",
  89. value: function commit() {
  90. if (this.count) {
  91. this._writeClose();
  92. this.commentsStream.end();
  93. this.vmlStream.end();
  94. }
  95. }
  96. }, {
  97. key: "commentsStream",
  98. get: function get() {
  99. if (!this._commentsStream) {
  100. // eslint-disable-next-line no-underscore-dangle
  101. this._commentsStream = this._workbook._openStream("/xl/comments".concat(this.id, ".xml"));
  102. }
  103. return this._commentsStream;
  104. }
  105. }, {
  106. key: "vmlStream",
  107. get: function get() {
  108. if (!this._vmlStream) {
  109. // eslint-disable-next-line no-underscore-dangle
  110. this._vmlStream = this._workbook._openStream("xl/drawings/vmlDrawing".concat(this.id, ".vml"));
  111. }
  112. return this._vmlStream;
  113. }
  114. }]);
  115. return SheetCommentsWriter;
  116. }();
  117. module.exports = SheetCommentsWriter;
  118. //# sourceMappingURL=sheet-comments-writer.js.map