comments-xform.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. const XmlStream = require('../../../utils/xml-stream');
  2. const utils = require('../../../utils/utils');
  3. const BaseXform = require('../base-xform');
  4. const CommentXform = require('./comment-xform');
  5. const CommentsXform = (module.exports = function() {
  6. this.map = {
  7. comment: new CommentXform(),
  8. };
  9. });
  10. utils.inherits(
  11. CommentsXform,
  12. BaseXform,
  13. {
  14. COMMENTS_ATTRIBUTES: {
  15. xmlns: 'http://schemas.openxmlformats.org/spreadsheetml/2006/main',
  16. },
  17. },
  18. {
  19. render(xmlStream, model) {
  20. model = model || this.model;
  21. xmlStream.openXml(XmlStream.StdDocAttributes);
  22. xmlStream.openNode('comments', CommentsXform.COMMENTS_ATTRIBUTES);
  23. // authors
  24. // TODO: support authors properly
  25. xmlStream.openNode('authors');
  26. xmlStream.leafNode('author', null, 'Author');
  27. xmlStream.closeNode();
  28. // comments
  29. xmlStream.openNode('commentList');
  30. model.comments.forEach(comment => {
  31. this.map.comment.render(xmlStream, comment);
  32. });
  33. xmlStream.closeNode();
  34. xmlStream.closeNode();
  35. },
  36. parseOpen(node) {
  37. if (this.parser) {
  38. this.parser.parseOpen(node);
  39. return true;
  40. }
  41. switch (node.name) {
  42. case 'commentList':
  43. this.model = {
  44. comments: [],
  45. };
  46. return true;
  47. case 'comment':
  48. this.parser = this.map.comment;
  49. this.parser.parseOpen(node);
  50. return true;
  51. default:
  52. return false;
  53. }
  54. },
  55. parseText(text) {
  56. if (this.parser) {
  57. this.parser.parseText(text);
  58. }
  59. },
  60. parseClose(name) {
  61. switch (name) {
  62. case 'commentList':
  63. return false;
  64. case 'comment':
  65. this.model.comments.push(this.parser.model);
  66. this.parser = undefined;
  67. return true;
  68. default:
  69. if (this.parser) {
  70. this.parser.parseClose(name);
  71. }
  72. return true;
  73. }
  74. },
  75. }
  76. );