MarkdownTableHelper.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import * as codemirror from 'codemirror';
  2. class MarkdownTableHelper {
  3. constructor() {
  4. // https://github.com/markdown-it/markdown-it/blob/d29f421927e93e88daf75f22089a3e732e195bd2/lib/rules_block/table.js#L83
  5. // https://regex101.com/r/7BN2fR/7
  6. this.tableAlignmentLineRE = /^[-:|][-:|\s]*$/;
  7. this.linePartOfTableRE = /^\|[^\r\n]*|[^\r\n]*\|$|([^\|\r\n]+\|[^\|\r\n]*)+/; // own idea
  8. this.isMatchedContext = this.isMatchedContext.bind(this);
  9. this.handleNewLine = this.handleNewLine.bind(this);
  10. this.newlineAndIndentContinueMarkdownTable = this.newlineAndIndentContinueMarkdownTable.bind(this);
  11. this.pasteText = this.pasteText.bind(this);
  12. this.getBot = this.getBot.bind(this);
  13. this.getEot = this.getEot.bind(this);
  14. this.getBol = this.getBol.bind(this);
  15. this.getStrFromBot = this.getStrFromBot.bind(this);
  16. this.getStrToEot = this.getStrToEot.bind(this);
  17. this.getStrFromBol = this.getStrFromBol.bind(this);
  18. }
  19. /**
  20. * return whether context is matched by table
  21. * @param {any} editor An editor instance of CodeMirror
  22. */
  23. isMatchedContext(editor) {
  24. console.log('MarkdownTableHelper.isMatchedContext');
  25. // get strings from BOL(beginning of line) to current position
  26. const strFromBol = this.getStrFromBol(editor);
  27. console.log('strFromBol: ' + strFromBol);
  28. console.log('will return ' + (this.linePartOfTableRE.test(strFromBol) ? 'true' : 'false'));
  29. return this.linePartOfTableRE.test(strFromBol);
  30. }
  31. /**
  32. * handle new line
  33. * @param {any} editor An editor instance of CodeMirror
  34. */
  35. handleNewLine(editor) {
  36. console.log('MarkdownTableHelper.handleNewLine');
  37. this.newlineAndIndentContinueMarkdownTable(editor);
  38. }
  39. /**
  40. * insert new line with auto shaping format of Markdown table
  41. * @param {any} editor An editor instance of CodeMirror
  42. */
  43. newlineAndIndentContinueMarkdownTable(editor) {
  44. console.log('MarkdownTableHelper.newlineAndIndentContinueMarkdownTable');
  45. if (!this.isMatchedContext(editor)) return;
  46. // get lines all of table from current position to beginning of table
  47. const strTableLines = this.getStrFromBot(editor) + this.getStrToEot(editor);
  48. console.log('strTableLines: ' + strTableLines);
  49. // [TODO] Format table lines
  50. const strTableLinesFormated = strTableLines;
  51. // replace the lines to strFormatedTableLines
  52. editor.getDoc().replaceRange(strTableLinesFormated, this.getBot(editor), this.getEot(editor));
  53. codemirror.commands.newlineAndIndent(editor);
  54. }
  55. /**
  56. * paste text
  57. * @param {any} editor An editor instance of CodeMirror
  58. * @param {any} event
  59. * @param {string} text
  60. */
  61. pasteText(editor, event, text) {
  62. // [TODO] replace to formated table markdown
  63. }
  64. /**
  65. * return the postion of the BOT(beginning of table)
  66. * (It is assumed that current line is a part of table)
  67. */
  68. getBot(editor) {
  69. const firstLine = editor.getDoc().firstLine();
  70. const curPos = editor.getCursor();
  71. let begLine = curPos.line - 1;
  72. for (; begLine >= firstLine; begLine--) {
  73. const strLine = editor.getDoc().getLine(begLine);
  74. if (!this.linePartOfTableRE.test(strLine)) {
  75. break;
  76. }
  77. }
  78. return { line: begLine, ch: 0 };
  79. }
  80. /**
  81. * return the postion of the EOT(end of table)
  82. * (It is assumed that current line is a part of table)
  83. */
  84. getEot(editor) {
  85. const lastLine = editor.getDoc().lastLine();
  86. const curPos = editor.getCursor();
  87. let endLine = curPos.line + 1;
  88. for (; endLine <= lastLine; endLine++) {
  89. const strLine = editor.getDoc().getLine(endLine);
  90. if (!this.linePartOfTableRE.test(strLine)) {
  91. break;
  92. }
  93. }
  94. const lineLength = editor.getDoc().getLine(Math.min(endLine, lastLine)).length;
  95. return { line: endLine, ch: lineLength };
  96. }
  97. /**
  98. * return the postion of the BOL(beginning of line)
  99. */
  100. getBol(editor) {
  101. const curPos = editor.getCursor();
  102. return { line: curPos.line, ch: 0 };
  103. }
  104. /**
  105. * return strings from BOT(beginning of table) to current position
  106. */
  107. getStrFromBot(editor) {
  108. const curPos = editor.getCursor();
  109. return editor.getDoc().getRange(this.getBot(editor), curPos);
  110. }
  111. /**
  112. * return strings from current position to EOT(end of table)
  113. */
  114. getStrToEot(editor) {
  115. const curPos = editor.getCursor();
  116. return editor.getDoc().getRange(curPos, this.getEot(editor));
  117. }
  118. /**
  119. * return strings from BOL(beginning of line) to current position
  120. */
  121. getStrFromBol(editor) {
  122. const curPos = editor.getCursor();
  123. return editor.getDoc().getRange(this.getBol(editor), curPos);
  124. }
  125. }
  126. // singleton pattern
  127. const instance = new MarkdownTableHelper();
  128. Object.freeze(instance);
  129. export default instance;