HandsontableUtil.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /**
  2. * Utility for Handsontable (and cooperation with MarkdownTable)
  3. */
  4. export default class HandsontableUtil {
  5. static setClassNameToColumns(core, startCol, endCol, className) {
  6. for (let i = startCol; i <= endCol; i++) {
  7. for (let j = 0; j < core.countRows(); j++) {
  8. core.setCellMeta(j, i, 'className', className);
  9. }
  10. }
  11. core.render();
  12. }
  13. /**
  14. * return a function(handsontable event handler) to adjust the handsontable alignment to the markdown table
  15. */
  16. static createHandlerToSynchronizeHandontableAlignWith(markdownTableAlign) {
  17. const mapping = {
  18. 'r': 'htRight',
  19. 'c': 'htCenter',
  20. 'l': 'htLeft',
  21. '': ''
  22. };
  23. return function() {
  24. const align = markdownTableAlign;
  25. for (let i = 0; i < align.length; i++) {
  26. HandsontableUtil.setClassNameToColumns(this, i, i, mapping[align[i]]);
  27. }
  28. };
  29. }
  30. /**
  31. * return MarkdownTable alignment retrieved from Handsontable instance
  32. */
  33. static getMarkdownTableAlignmentFrom(handsontable) {
  34. const cellMetasAtFirstRow = handsontable.getCellMetaAtRow(0);
  35. const mapping = {
  36. 'htRight': 'r',
  37. 'htCenter': 'c',
  38. 'htLeft': 'l',
  39. '': ''
  40. };
  41. let align = [];
  42. for (let i = 0; i < cellMetasAtFirstRow.length; i++) {
  43. align.push(mapping[cellMetasAtFirstRow[i].className]);
  44. }
  45. return align;
  46. }
  47. }