sheet-view-xform.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. const colCache = require('../../../utils/col-cache');
  2. const BaseXform = require('../base-xform');
  3. const VIEW_STATES = {
  4. frozen: 'frozen',
  5. frozenSplit: 'frozen',
  6. split: 'split',
  7. };
  8. class SheetViewXform extends BaseXform {
  9. get tag() {
  10. return 'sheetView';
  11. }
  12. prepare(model) {
  13. switch (model.state) {
  14. case 'frozen':
  15. case 'split':
  16. break;
  17. default:
  18. model.state = 'normal';
  19. break;
  20. }
  21. }
  22. render(xmlStream, model) {
  23. xmlStream.openNode('sheetView', {
  24. workbookViewId: model.workbookViewId || 0,
  25. });
  26. const add = function(name, value, included) {
  27. if (included) {
  28. xmlStream.addAttribute(name, value);
  29. }
  30. };
  31. add('rightToLeft', '1', model.rightToLeft === true);
  32. add('tabSelected', '1', model.tabSelected);
  33. add('showRuler', '0', model.showRuler === false);
  34. add('showRowColHeaders', '0', model.showRowColHeaders === false);
  35. add('showGridLines', '0', model.showGridLines === false);
  36. add('zoomScale', model.zoomScale, model.zoomScale);
  37. add('zoomScaleNormal', model.zoomScaleNormal, model.zoomScaleNormal);
  38. add('view', model.style, model.style);
  39. let topLeftCell;
  40. let xSplit;
  41. let ySplit;
  42. let activePane;
  43. switch (model.state) {
  44. case 'frozen':
  45. xSplit = model.xSplit || 0;
  46. ySplit = model.ySplit || 0;
  47. topLeftCell = model.topLeftCell || colCache.getAddress(ySplit + 1, xSplit + 1).address;
  48. activePane =
  49. (model.xSplit && model.ySplit && 'bottomRight') ||
  50. (model.xSplit && 'topRight') ||
  51. 'bottomLeft';
  52. xmlStream.leafNode('pane', {
  53. xSplit: model.xSplit || undefined,
  54. ySplit: model.ySplit || undefined,
  55. topLeftCell,
  56. activePane,
  57. state: 'frozen',
  58. });
  59. xmlStream.leafNode('selection', {
  60. pane: activePane,
  61. activeCell: model.activeCell,
  62. sqref: model.activeCell,
  63. });
  64. break;
  65. case 'split':
  66. if (model.activePane === 'topLeft') {
  67. model.activePane = undefined;
  68. }
  69. xmlStream.leafNode('pane', {
  70. xSplit: model.xSplit || undefined,
  71. ySplit: model.ySplit || undefined,
  72. topLeftCell: model.topLeftCell,
  73. activePane: model.activePane,
  74. });
  75. xmlStream.leafNode('selection', {
  76. pane: model.activePane,
  77. activeCell: model.activeCell,
  78. sqref: model.activeCell,
  79. });
  80. break;
  81. case 'normal':
  82. if (model.activeCell) {
  83. xmlStream.leafNode('selection', {
  84. activeCell: model.activeCell,
  85. sqref: model.activeCell,
  86. });
  87. }
  88. break;
  89. default:
  90. break;
  91. }
  92. xmlStream.closeNode();
  93. }
  94. parseOpen(node) {
  95. switch (node.name) {
  96. case 'sheetView':
  97. this.sheetView = {
  98. workbookViewId: parseInt(node.attributes.workbookViewId, 10),
  99. rightToLeft: node.attributes.rightToLeft === '1',
  100. tabSelected: node.attributes.tabSelected === '1',
  101. showRuler: !(node.attributes.showRuler === '0'),
  102. showRowColHeaders: !(node.attributes.showRowColHeaders === '0'),
  103. showGridLines: !(node.attributes.showGridLines === '0'),
  104. zoomScale: parseInt(node.attributes.zoomScale || '100', 10),
  105. zoomScaleNormal: parseInt(node.attributes.zoomScaleNormal || '100', 10),
  106. style: node.attributes.view,
  107. };
  108. this.pane = undefined;
  109. this.selections = {};
  110. return true;
  111. case 'pane':
  112. this.pane = {
  113. xSplit: parseInt(node.attributes.xSplit || '0', 10),
  114. ySplit: parseInt(node.attributes.ySplit || '0', 10),
  115. topLeftCell: node.attributes.topLeftCell,
  116. activePane: node.attributes.activePane || 'topLeft',
  117. state: node.attributes.state,
  118. };
  119. return true;
  120. case 'selection': {
  121. const name = node.attributes.pane || 'topLeft';
  122. this.selections[name] = {
  123. pane: name,
  124. activeCell: node.attributes.activeCell,
  125. };
  126. return true;
  127. }
  128. default:
  129. return false;
  130. }
  131. }
  132. parseText() {}
  133. parseClose(name) {
  134. let model;
  135. let selection;
  136. switch (name) {
  137. case 'sheetView':
  138. if (this.sheetView && this.pane) {
  139. model = this.model = {
  140. workbookViewId: this.sheetView.workbookViewId,
  141. rightToLeft: this.sheetView.rightToLeft,
  142. state: VIEW_STATES[this.pane.state] || 'split', // split is default
  143. xSplit: this.pane.xSplit,
  144. ySplit: this.pane.ySplit,
  145. topLeftCell: this.pane.topLeftCell,
  146. showRuler: this.sheetView.showRuler,
  147. showRowColHeaders: this.sheetView.showRowColHeaders,
  148. showGridLines: this.sheetView.showGridLines,
  149. zoomScale: this.sheetView.zoomScale,
  150. zoomScaleNormal: this.sheetView.zoomScaleNormal,
  151. };
  152. if (this.model.state === 'split') {
  153. model.activePane = this.pane.activePane;
  154. }
  155. selection = this.selections[this.pane.activePane];
  156. if (selection && selection.activeCell) {
  157. model.activeCell = selection.activeCell;
  158. }
  159. if (this.sheetView.style) {
  160. model.style = this.sheetView.style;
  161. }
  162. } else {
  163. model = this.model = {
  164. workbookViewId: this.sheetView.workbookViewId,
  165. rightToLeft: this.sheetView.rightToLeft,
  166. state: 'normal',
  167. showRuler: this.sheetView.showRuler,
  168. showRowColHeaders: this.sheetView.showRowColHeaders,
  169. showGridLines: this.sheetView.showGridLines,
  170. zoomScale: this.sheetView.zoomScale,
  171. zoomScaleNormal: this.sheetView.zoomScaleNormal,
  172. };
  173. selection = this.selections.topLeft;
  174. if (selection && selection.activeCell) {
  175. model.activeCell = selection.activeCell;
  176. }
  177. if (this.sheetView.style) {
  178. model.style = this.sheetView.style;
  179. }
  180. }
  181. return false;
  182. default:
  183. return true;
  184. }
  185. }
  186. reconcile() {}
  187. }
  188. module.exports = SheetViewXform;