comment-xform.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. const RichTextXform = require('../strings/rich-text-xform');
  2. const utils = require('../../../utils/utils');
  3. const BaseXform = require('../base-xform');
  4. /**
  5. <comment ref="B1" authorId="0">
  6. <text>
  7. <r>
  8. <rPr>
  9. <b/>
  10. <sz val="9"/>
  11. <rFont val="宋体"/>
  12. <charset val="134"/>
  13. </rPr>
  14. <t>51422:</t>
  15. </r>
  16. <r>
  17. <rPr>
  18. <sz val="9"/>
  19. <rFont val="宋体"/>
  20. <charset val="134"/>
  21. </rPr>
  22. <t xml:space="preserve">&#10;test</t>
  23. </r>
  24. </text>
  25. </comment>
  26. */
  27. const CommentXform = (module.exports = function(model) {
  28. this.model = model;
  29. });
  30. utils.inherits(CommentXform, BaseXform, {
  31. get tag() {
  32. return 'r';
  33. },
  34. get richTextXform() {
  35. if (!this._richTextXform) {
  36. this._richTextXform = new RichTextXform();
  37. }
  38. return this._richTextXform;
  39. },
  40. render(xmlStream, model) {
  41. model = model || this.model;
  42. xmlStream.openNode('comment', {
  43. ref: model.ref,
  44. authorId: 0,
  45. });
  46. xmlStream.openNode('text');
  47. if (model && model.note && model.note.texts) {
  48. model.note.texts.forEach(text => {
  49. this.richTextXform.render(xmlStream, text);
  50. });
  51. }
  52. xmlStream.closeNode();
  53. xmlStream.closeNode();
  54. },
  55. parseOpen(node) {
  56. if (this.parser) {
  57. this.parser.parseOpen(node);
  58. return true;
  59. }
  60. switch (node.name) {
  61. case 'comment':
  62. this.model = {
  63. type: 'note',
  64. note: {
  65. texts: [],
  66. },
  67. ...node.attributes,
  68. };
  69. return true;
  70. case 'r':
  71. this.parser = this.richTextXform;
  72. this.parser.parseOpen(node);
  73. return true;
  74. default:
  75. return false;
  76. }
  77. },
  78. parseText(text) {
  79. if (this.parser) {
  80. this.parser.parseText(text);
  81. }
  82. },
  83. parseClose(name) {
  84. switch (name) {
  85. case 'comment':
  86. return false;
  87. case 'r':
  88. this.model.note.texts.push(this.parser.model);
  89. this.parser = undefined;
  90. return true;
  91. default:
  92. if (this.parser) {
  93. this.parser.parseClose(name);
  94. }
  95. return true;
  96. }
  97. },
  98. });