GridEditorUtil.js 4.4 KB

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