drawing-xform.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. const colCache = require('../../../utils/col-cache');
  2. const XmlStream = require('../../../utils/xml-stream');
  3. const BaseXform = require('../base-xform');
  4. const TwoCellAnchorXform = require('./two-cell-anchor-xform');
  5. const OneCellAnchorXform = require('./one-cell-anchor-xform');
  6. function getAnchorType(model) {
  7. const range = typeof model.range === 'string' ? colCache.decode(model.range) : model.range;
  8. return range.br ? 'xdr:twoCellAnchor' : 'xdr:oneCellAnchor';
  9. }
  10. class DrawingXform extends BaseXform {
  11. constructor() {
  12. super();
  13. this.map = {
  14. 'xdr:twoCellAnchor': new TwoCellAnchorXform(),
  15. 'xdr:oneCellAnchor': new OneCellAnchorXform(),
  16. };
  17. }
  18. prepare(model) {
  19. model.anchors.forEach((item, index) => {
  20. item.anchorType = getAnchorType(item);
  21. const anchor = this.map[item.anchorType];
  22. anchor.prepare(item, {index});
  23. });
  24. }
  25. get tag() {
  26. return 'xdr:wsDr';
  27. }
  28. render(xmlStream, model) {
  29. xmlStream.openXml(XmlStream.StdDocAttributes);
  30. xmlStream.openNode(this.tag, DrawingXform.DRAWING_ATTRIBUTES);
  31. model.anchors.forEach(item => {
  32. const anchor = this.map[item.anchorType];
  33. anchor.render(xmlStream, item);
  34. });
  35. xmlStream.closeNode();
  36. }
  37. parseOpen(node) {
  38. if (this.parser) {
  39. this.parser.parseOpen(node);
  40. return true;
  41. }
  42. switch (node.name) {
  43. case this.tag:
  44. this.reset();
  45. this.model = {
  46. anchors: [],
  47. };
  48. break;
  49. default:
  50. this.parser = this.map[node.name];
  51. if (this.parser) {
  52. this.parser.parseOpen(node);
  53. }
  54. break;
  55. }
  56. return true;
  57. }
  58. parseText(text) {
  59. if (this.parser) {
  60. this.parser.parseText(text);
  61. }
  62. }
  63. parseClose(name) {
  64. if (this.parser) {
  65. if (!this.parser.parseClose(name)) {
  66. this.model.anchors.push(this.parser.model);
  67. this.parser = undefined;
  68. }
  69. return true;
  70. }
  71. switch (name) {
  72. case this.tag:
  73. return false;
  74. default:
  75. // could be some unrecognised tags
  76. return true;
  77. }
  78. }
  79. reconcile(model, options) {
  80. model.anchors.forEach(anchor => {
  81. if (anchor.br) {
  82. this.map['xdr:twoCellAnchor'].reconcile(anchor, options);
  83. } else {
  84. this.map['xdr:oneCellAnchor'].reconcile(anchor, options);
  85. }
  86. });
  87. }
  88. }
  89. DrawingXform.DRAWING_ATTRIBUTES = {
  90. 'xmlns:xdr': 'http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing',
  91. 'xmlns:a': 'http://schemas.openxmlformats.org/drawingml/2006/main',
  92. };
  93. module.exports = DrawingXform;