GridEditorUtil.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**
  2. * Utility for grid editor
  3. */
  4. class GridEditorUtil {
  5. constructor() {
  6. // TODO url
  7. this.lineBeginPartOfGridRE = /(<[^/].*>)/;
  8. this.lineEndPartOfGridRE = /(<\/.*>)/;
  9. this.linePartOfGridRE = /(<[^/].*>)[\s\S]*<\/.*>$/;
  10. this.replaceGridWithHtmlWithEditor = this.replaceGridWithHtmlWithEditor.bind(this);
  11. }
  12. /**
  13. * return boolean value whether the cursor position is in a row
  14. */
  15. isInRow(editor) {
  16. const curPos = editor.getCursor();
  17. // return this.linePartOfTableRE.test(editor.getDoc().getLine(curPos.line));
  18. return this.linePartOfRow.test(editor.getDoc().getLine(curPos.line));
  19. }
  20. /**
  21. * return the postion of the BOD(beginning of grid)
  22. */
  23. getBog(editor) {
  24. const curPos = editor.getCursor();
  25. const firstLine = editor.getDoc().firstLine();
  26. let line = curPos.line - 1;
  27. let isFound = false;
  28. for (; line >= firstLine; line--) {
  29. const strLine = editor.getDoc().getLine(line);
  30. if (this.lineBeginPartOfGridRE.test(strLine)) {
  31. isFound = true;
  32. break;
  33. }
  34. if (this.lineBeginPartOfGridRE.test(strLine)) {
  35. isFound = false;
  36. break;
  37. }
  38. }
  39. if (!isFound) {
  40. return { line: curPos.line, ch: curPos.ch };
  41. }
  42. const bodLine = Math.max(firstLine, line);
  43. return { line: bodLine, ch: 0 };
  44. }
  45. /**
  46. * return the postion of the EOD(end of grid)
  47. */
  48. getEog(editor) {
  49. const curPos = editor.getCursor();
  50. const lastLine = editor.getDoc().lastLine();
  51. let line = curPos.line;
  52. let isFound = false;
  53. for (; line <= lastLine; line++) {
  54. const strLine = editor.getDoc().getLine(line);
  55. if (this.lineEndPartOfGridRE.test(strLine)) {
  56. isFound = true;
  57. break;
  58. }
  59. if (this.lineEndPartOfGridRE.test(strLine)) {
  60. isFound = false;
  61. break;
  62. }
  63. }
  64. if (!isFound) {
  65. return { line: curPos.line, ch: curPos.ch };
  66. }
  67. const eodLine = Math.min(line, lastLine);
  68. const lineLength = editor.getDoc().getLine(eodLine).length;
  69. return { line: eodLine, ch: lineLength };
  70. // return { line: lastLine, ch: curPos.ch };
  71. }
  72. replaceGridWithHtmlWithEditor(editor, grid) {
  73. const curPos = editor.getCursor();
  74. editor.getDoc().setCursor(curPos.line + 1, 2);
  75. }
  76. }
  77. // singleton pattern
  78. const instance = new GridEditorUtil();
  79. Object.freeze(instance);
  80. export default instance;