vml-client-data-xform.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. const BaseXform = require('../base-xform');
  2. const VmlAnchorXform = require('./vml-anchor-xform');
  3. const VmlProtectionXform = require('./style/vml-protection-xform');
  4. const VmlPositionXform = require('./style/vml-position-xform');
  5. const POSITION_TYPE = ['twoCells', 'oneCells', 'absolute'];
  6. class VmlClientDataXform extends BaseXform {
  7. constructor() {
  8. super();
  9. this.map = {
  10. 'x:Anchor': new VmlAnchorXform(),
  11. 'x:Locked': new VmlProtectionXform({tag: 'x:Locked'}),
  12. 'x:LockText': new VmlProtectionXform({tag: 'x:LockText'}),
  13. 'x:SizeWithCells': new VmlPositionXform({tag: 'x:SizeWithCells'}),
  14. 'x:MoveWithCells': new VmlPositionXform({tag: 'x:MoveWithCells'}),
  15. };
  16. }
  17. get tag() {
  18. return 'x:ClientData';
  19. }
  20. render(xmlStream, model) {
  21. const {protection, editAs} = model.note;
  22. xmlStream.openNode(this.tag, {ObjectType: 'Note'});
  23. this.map['x:MoveWithCells'].render(xmlStream, editAs, POSITION_TYPE);
  24. this.map['x:SizeWithCells'].render(xmlStream, editAs, POSITION_TYPE);
  25. this.map['x:Anchor'].render(xmlStream, model);
  26. this.map['x:Locked'].render(xmlStream, protection.locked);
  27. xmlStream.leafNode('x:AutoFill', null, 'False');
  28. this.map['x:LockText'].render(xmlStream, protection.lockText);
  29. xmlStream.leafNode('x:Row', null, model.refAddress.row - 1);
  30. xmlStream.leafNode('x:Column', null, model.refAddress.col - 1);
  31. xmlStream.closeNode();
  32. }
  33. parseOpen(node) {
  34. switch (node.name) {
  35. case this.tag:
  36. this.reset();
  37. this.model = {
  38. anchor: [],
  39. protection: {},
  40. editAs: '',
  41. };
  42. break;
  43. default:
  44. this.parser = this.map[node.name];
  45. if (this.parser) {
  46. this.parser.parseOpen(node);
  47. }
  48. break;
  49. }
  50. return true;
  51. }
  52. parseText(text) {
  53. if (this.parser) {
  54. this.parser.parseText(text);
  55. }
  56. }
  57. parseClose(name) {
  58. if (this.parser) {
  59. if (!this.parser.parseClose(name)) {
  60. this.parser = undefined;
  61. }
  62. return true;
  63. }
  64. switch (name) {
  65. case this.tag:
  66. this.normalizeModel();
  67. return false;
  68. default:
  69. return true;
  70. }
  71. }
  72. normalizeModel() {
  73. const position = Object.assign(
  74. {},
  75. this.map['x:MoveWithCells'].model,
  76. this.map['x:SizeWithCells'].model
  77. );
  78. const len = Object.keys(position).length;
  79. this.model.editAs = POSITION_TYPE[len];
  80. this.model.anchor = this.map['x:Anchor'].text;
  81. this.model.protection.locked = this.map['x:Locked'].text;
  82. this.model.protection.lockText = this.map['x:LockText'].text;
  83. }
  84. }
  85. module.exports = VmlClientDataXform;