alignment.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. import { align, getAlignmentClasses, checkSelectionConsistency, markLabelAsSelected } from './../utils';
  2. import { KEY as SEPARATOR } from './separator';
  3. export var KEY = 'alignment';
  4. export default function alignmentItem() {
  5. return {
  6. key: KEY,
  7. name: 'Alignment',
  8. disabled: function disabled() {
  9. return !(this.getSelectedRange() && !this.selection.selectedHeader.corner);
  10. },
  11. submenu: {
  12. items: [{
  13. key: KEY + ':left',
  14. name: function name() {
  15. var _this = this;
  16. var label = 'Left';
  17. var hasClass = checkSelectionConsistency(this.getSelectedRange(), function (row, col) {
  18. var className = _this.getCellMeta(row, col).className;
  19. if (className && className.indexOf('htLeft') !== -1) {
  20. return true;
  21. }
  22. });
  23. if (hasClass) {
  24. label = markLabelAsSelected(label);
  25. }
  26. return label;
  27. },
  28. callback: function callback() {
  29. var _this2 = this;
  30. var range = this.getSelectedRange();
  31. var stateBefore = getAlignmentClasses(range, function (row, col) {
  32. return _this2.getCellMeta(row, col).className;
  33. });
  34. var type = 'horizontal';
  35. var alignment = 'htLeft';
  36. this.runHooks('beforeCellAlignment', stateBefore, range, type, alignment);
  37. align(range, type, alignment, function (row, col) {
  38. return _this2.getCellMeta(row, col);
  39. }, function (row, col, key, value) {
  40. return _this2.setCellMeta(row, col, key, value);
  41. });
  42. this.render();
  43. },
  44. disabled: false
  45. }, {
  46. key: KEY + ':center',
  47. name: function name() {
  48. var _this3 = this;
  49. var label = 'Center';
  50. var hasClass = checkSelectionConsistency(this.getSelectedRange(), function (row, col) {
  51. var className = _this3.getCellMeta(row, col).className;
  52. if (className && className.indexOf('htCenter') !== -1) {
  53. return true;
  54. }
  55. });
  56. if (hasClass) {
  57. label = markLabelAsSelected(label);
  58. }
  59. return label;
  60. },
  61. callback: function callback() {
  62. var _this4 = this;
  63. var range = this.getSelectedRange();
  64. var stateBefore = getAlignmentClasses(range, function (row, col) {
  65. return _this4.getCellMeta(row, col).className;
  66. });
  67. var type = 'horizontal';
  68. var alignment = 'htCenter';
  69. this.runHooks('beforeCellAlignment', stateBefore, range, type, alignment);
  70. align(range, type, alignment, function (row, col) {
  71. return _this4.getCellMeta(row, col);
  72. }, function (row, col, key, value) {
  73. return _this4.setCellMeta(row, col, key, value);
  74. });
  75. this.render();
  76. },
  77. disabled: false
  78. }, {
  79. key: KEY + ':right',
  80. name: function name() {
  81. var _this5 = this;
  82. var label = 'Right';
  83. var hasClass = checkSelectionConsistency(this.getSelectedRange(), function (row, col) {
  84. var className = _this5.getCellMeta(row, col).className;
  85. if (className && className.indexOf('htRight') !== -1) {
  86. return true;
  87. }
  88. });
  89. if (hasClass) {
  90. label = markLabelAsSelected(label);
  91. }
  92. return label;
  93. },
  94. callback: function callback() {
  95. var _this6 = this;
  96. var range = this.getSelectedRange();
  97. var stateBefore = getAlignmentClasses(range, function (row, col) {
  98. return _this6.getCellMeta(row, col).className;
  99. });
  100. var type = 'horizontal';
  101. var alignment = 'htRight';
  102. this.runHooks('beforeCellAlignment', stateBefore, range, type, alignment);
  103. align(range, type, alignment, function (row, col) {
  104. return _this6.getCellMeta(row, col);
  105. }, function (row, col, key, value) {
  106. return _this6.setCellMeta(row, col, key, value);
  107. });
  108. this.render();
  109. },
  110. disabled: false
  111. }, {
  112. key: KEY + ':justify',
  113. name: function name() {
  114. var _this7 = this;
  115. var label = 'Justify';
  116. var hasClass = checkSelectionConsistency(this.getSelectedRange(), function (row, col) {
  117. var className = _this7.getCellMeta(row, col).className;
  118. if (className && className.indexOf('htJustify') !== -1) {
  119. return true;
  120. }
  121. });
  122. if (hasClass) {
  123. label = markLabelAsSelected(label);
  124. }
  125. return label;
  126. },
  127. callback: function callback() {
  128. var _this8 = this;
  129. var range = this.getSelectedRange();
  130. var stateBefore = getAlignmentClasses(range, function (row, col) {
  131. return _this8.getCellMeta(row, col).className;
  132. });
  133. var type = 'horizontal';
  134. var alignment = 'htJustify';
  135. this.runHooks('beforeCellAlignment', stateBefore, range, type, alignment);
  136. align(range, type, alignment, function (row, col) {
  137. return _this8.getCellMeta(row, col);
  138. }, function (row, col, key, value) {
  139. return _this8.setCellMeta(row, col, key, value);
  140. });
  141. this.render();
  142. },
  143. disabled: false
  144. }, {
  145. name: SEPARATOR
  146. }, {
  147. key: KEY + ':top',
  148. name: function name() {
  149. var _this9 = this;
  150. var label = 'Top';
  151. var hasClass = checkSelectionConsistency(this.getSelectedRange(), function (row, col) {
  152. var className = _this9.getCellMeta(row, col).className;
  153. if (className && className.indexOf('htTop') !== -1) {
  154. return true;
  155. }
  156. });
  157. if (hasClass) {
  158. label = markLabelAsSelected(label);
  159. }
  160. return label;
  161. },
  162. callback: function callback() {
  163. var _this10 = this;
  164. var range = this.getSelectedRange();
  165. var stateBefore = getAlignmentClasses(range, function (row, col) {
  166. return _this10.getCellMeta(row, col).className;
  167. });
  168. var type = 'vertical';
  169. var alignment = 'htTop';
  170. this.runHooks('beforeCellAlignment', stateBefore, range, type, alignment);
  171. align(range, type, alignment, function (row, col) {
  172. return _this10.getCellMeta(row, col);
  173. }, function (row, col, key, value) {
  174. return _this10.setCellMeta(row, col, key, value);
  175. });
  176. this.render();
  177. },
  178. disabled: false
  179. }, {
  180. key: KEY + ':middle',
  181. name: function name() {
  182. var _this11 = this;
  183. var label = 'Middle';
  184. var hasClass = checkSelectionConsistency(this.getSelectedRange(), function (row, col) {
  185. var className = _this11.getCellMeta(row, col).className;
  186. if (className && className.indexOf('htMiddle') !== -1) {
  187. return true;
  188. }
  189. });
  190. if (hasClass) {
  191. label = markLabelAsSelected(label);
  192. }
  193. return label;
  194. },
  195. callback: function callback() {
  196. var _this12 = this;
  197. var range = this.getSelectedRange();
  198. var stateBefore = getAlignmentClasses(range, function (row, col) {
  199. return _this12.getCellMeta(row, col).className;
  200. });
  201. var type = 'vertical';
  202. var alignment = 'htMiddle';
  203. this.runHooks('beforeCellAlignment', stateBefore, range, type, alignment);
  204. align(range, type, alignment, function (row, col) {
  205. return _this12.getCellMeta(row, col);
  206. }, function (row, col, key, value) {
  207. return _this12.setCellMeta(row, col, key, value);
  208. });
  209. this.render();
  210. },
  211. disabled: false
  212. }, {
  213. key: KEY + ':bottom',
  214. name: function name() {
  215. var _this13 = this;
  216. var label = 'Bottom';
  217. var hasClass = checkSelectionConsistency(this.getSelectedRange(), function (row, col) {
  218. var className = _this13.getCellMeta(row, col).className;
  219. if (className && className.indexOf('htBottom') !== -1) {
  220. return true;
  221. }
  222. });
  223. if (hasClass) {
  224. label = markLabelAsSelected(label);
  225. }
  226. return label;
  227. },
  228. callback: function callback() {
  229. var _this14 = this;
  230. var range = this.getSelectedRange();
  231. var stateBefore = getAlignmentClasses(range, function (row, col) {
  232. return _this14.getCellMeta(row, col).className;
  233. });
  234. var type = 'vertical';
  235. var alignment = 'htBottom';
  236. this.runHooks('beforeCellAlignment', stateBefore, range, type, alignment);
  237. align(range, type, alignment, function (row, col) {
  238. return _this14.getCellMeta(row, col);
  239. }, function (row, col, key, value) {
  240. return _this14.setCellMeta(row, col, key, value);
  241. });
  242. this.render();
  243. },
  244. disabled: false
  245. }]
  246. }
  247. };
  248. }