vml-notes-xform.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. const XmlStream = require('../../../utils/xml-stream');
  2. const BaseXform = require('../base-xform');
  3. const VmlShapeXform = require('./vml-shape-xform');
  4. // This class is (currently) single purposed to insert the triangle
  5. // drawing icons on commented cells
  6. class VmlNotesXform extends BaseXform {
  7. constructor() {
  8. super();
  9. this.map = {
  10. 'v:shape': new VmlShapeXform(),
  11. };
  12. }
  13. get tag() {
  14. return 'xml';
  15. }
  16. render(xmlStream, model) {
  17. xmlStream.openXml(XmlStream.StdDocAttributes);
  18. xmlStream.openNode(this.tag, VmlNotesXform.DRAWING_ATTRIBUTES);
  19. xmlStream.openNode('o:shapelayout', {'v:ext': 'edit'});
  20. xmlStream.leafNode('o:idmap', {'v:ext': 'edit', data: 1});
  21. xmlStream.closeNode();
  22. xmlStream.openNode('v:shapetype', {
  23. id: '_x0000_t202',
  24. coordsize: '21600,21600',
  25. 'o:spt': 202,
  26. path: 'm,l,21600r21600,l21600,xe',
  27. });
  28. xmlStream.leafNode('v:stroke', {joinstyle: 'miter'});
  29. xmlStream.leafNode('v:path', {gradientshapeok: 't', 'o:connecttype': 'rect'});
  30. xmlStream.closeNode();
  31. model.comments.forEach((item, index) => {
  32. this.map['v:shape'].render(xmlStream, item, index);
  33. });
  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 this.tag:
  43. this.reset();
  44. this.model = {
  45. comments: [],
  46. };
  47. break;
  48. default:
  49. this.parser = this.map[node.name];
  50. if (this.parser) {
  51. this.parser.parseOpen(node);
  52. }
  53. break;
  54. }
  55. return true;
  56. }
  57. parseText(text) {
  58. if (this.parser) {
  59. this.parser.parseText(text);
  60. }
  61. }
  62. parseClose(name) {
  63. if (this.parser) {
  64. if (!this.parser.parseClose(name)) {
  65. this.model.comments.push(this.parser.model);
  66. this.parser = undefined;
  67. }
  68. return true;
  69. }
  70. switch (name) {
  71. case this.tag:
  72. return false;
  73. default:
  74. // could be some unrecognised tags
  75. return true;
  76. }
  77. }
  78. reconcile(model, options) {
  79. model.anchors.forEach(anchor => {
  80. if (anchor.br) {
  81. this.map['xdr:twoCellAnchor'].reconcile(anchor, options);
  82. } else {
  83. this.map['xdr:oneCellAnchor'].reconcile(anchor, options);
  84. }
  85. });
  86. }
  87. }
  88. VmlNotesXform.DRAWING_ATTRIBUTES = {
  89. 'xmlns:v': 'urn:schemas-microsoft-com:vml',
  90. 'xmlns:o': 'urn:schemas-microsoft-com:office:office',
  91. 'xmlns:x': 'urn:schemas-microsoft-com:office:excel',
  92. };
  93. module.exports = VmlNotesXform;