GridEditorUtil.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * Utility for grid editor
  3. */
  4. class GridEditorUtil {
  5. constructor() {
  6. // https://regex101.com/r/7BN2fR/11
  7. this.lineBeginPartOfGridRE = /^:::(\s.*)editable-row$/;
  8. this.lineEndPartOfGridRE = /^:::$/;
  9. this.mappingAllGridDivisionPatterns = [
  10. {
  11. numberOfGridDivisions: 2,
  12. mapping: [[2, 10], [4, 8], [6, 6], [8, 4], [10, 2]],
  13. },
  14. {
  15. numberOfGridDivisions: 3,
  16. mapping: [[2, 5, 5], [5, 2, 5], [5, 5, 2], [4, 4, 4], [3, 3, 6], [3, 6, 3], [6, 3, 3]],
  17. },
  18. {
  19. numberOfGridDivisions: 4,
  20. mapping: [[2, 2, 4, 4], [4, 4, 2, 2], [2, 4, 2, 4], [4, 2, 4, 2], [3, 3, 3, 3], [2, 2, 2, 6], [6, 2, 2, 2]],
  21. },
  22. ];
  23. this.isInGridBlock = this.isInGridBlock.bind(this);
  24. this.replaceGridWithHtmlWithEditor = this.replaceGridWithHtmlWithEditor.bind(this);
  25. }
  26. /**
  27. * return boolean value whether the cursor position is in a grid block
  28. */
  29. isInGridBlock(editor) {
  30. const bog = this.getBog(editor);
  31. const eog = this.getEog(editor);
  32. if (bog === null || eog === null) {
  33. return false;
  34. }
  35. return (JSON.stringify(bog) !== JSON.stringify(eog));
  36. }
  37. /**
  38. * return grid html where the cursor is
  39. */
  40. getGridHtml(editor) {
  41. const curPos = editor.getCursor();
  42. if (this.isInGridBlock(editor)) {
  43. const bog = this.getBog(editor);
  44. const eog = this.getEog(editor);
  45. // skip block begin sesion("::: editable-row")
  46. bog.line++;
  47. // skip block end sesion(":::")
  48. eog.line--;
  49. eog.ch = editor.getDoc().getLine(eog.line).length;
  50. return editor.getDoc().getRange(bog, eog);
  51. }
  52. return editor.getDoc().getLine(curPos.line);
  53. }
  54. /**
  55. * return the postion of the BOD(beginning of grid)
  56. */
  57. getBog(editor) {
  58. const curPos = editor.getCursor();
  59. const firstLine = editor.getDoc().firstLine();
  60. if (this.lineBeginPartOfGridRE.test(editor.getDoc().getLine(curPos.line))) {
  61. return { line: curPos.line, ch: 0 };
  62. }
  63. let line = curPos.line - 1;
  64. let isFound = false;
  65. for (; line >= firstLine; line--) {
  66. const strLine = editor.getDoc().getLine(line);
  67. if (this.lineBeginPartOfGridRE.test(strLine)) {
  68. isFound = true;
  69. break;
  70. }
  71. if (this.lineEndPartOfGridRE.test(strLine)) {
  72. isFound = false;
  73. break;
  74. }
  75. }
  76. if (!isFound) {
  77. return null;
  78. }
  79. const bodLine = Math.max(firstLine, line);
  80. return { line: bodLine, ch: 0 };
  81. }
  82. /**
  83. * return the postion of the EOD(end of grid)
  84. */
  85. getEog(editor) {
  86. const curPos = editor.getCursor();
  87. const lastLine = editor.getDoc().lastLine();
  88. if (this.lineEndPartOfGridRE.test(editor.getDoc().getLine(curPos.line))) {
  89. return { line: curPos.line, ch: editor.getDoc().getLine(curPos.line).length };
  90. }
  91. let line = curPos.line + 1;
  92. let isFound = false;
  93. for (; line <= lastLine; line++) {
  94. const strLine = editor.getDoc().getLine(line);
  95. if (this.lineEndPartOfGridRE.test(strLine)) {
  96. isFound = true;
  97. break;
  98. }
  99. if (this.lineBeginPartOfGridRE.test(strLine)) {
  100. isFound = false;
  101. break;
  102. }
  103. }
  104. if (!isFound) {
  105. return null;
  106. }
  107. const eodLine = Math.min(line, lastLine);
  108. const lineLength = editor.getDoc().getLine(eodLine).length;
  109. return { line: eodLine, ch: lineLength };
  110. }
  111. replaceGridWithHtmlWithEditor(editor, grid) {
  112. const curPos = editor.getCursor();
  113. let bog = this.getBog(editor);
  114. let eog = this.getEog(editor);
  115. if (bog === null || eog === null) {
  116. bog = curPos;
  117. eog = curPos;
  118. }
  119. editor.getDoc().replaceRange(grid.toString(), bog, eog);
  120. editor.getDoc().setCursor(curPos.line + 1, 2);
  121. }
  122. convertRatiosAndSizeToHTML(ratioNumbers, responsiveSize) {
  123. const cols = ratioNumbers.map((ratioNumber, i) => {
  124. const spaceTab = ' ';
  125. const className = `col${responsiveSize !== 'xs' ? `-${responsiveSize}` : ''}-${ratioNumber} bsGrid${i + 1}`;
  126. return `${spaceTab}${spaceTab}<div class="${className}">Content</div>`;
  127. });
  128. return cols.join('\n');
  129. }
  130. }
  131. // singleton pattern
  132. const instance = new GridEditorUtil();
  133. Object.freeze(instance);
  134. export default instance;