MarkdownTableHelper.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import * as codemirror from 'codemirror';
  2. class MarkdownTableHelper {
  3. constructor() {
  4. // https://stackoverflow.com/questions/9837935/regex-for-markdown-table-syntax
  5. // https://regex101.com/r/7BN2fR/6
  6. this.tableTitleAndHeaderAndBodyRE = /\|(?:([^\r\n\|]*)\|)+\r?\n\|(?:(\:?-+\:?)\|)+\r?\n(\|(?:([^\r\n\|]*)\|)+\r?\n)+/;
  7. this.isMatchedContext = this.isMatchedContext.bind(this);
  8. this.handleNewLine = this.handleNewLine.bind(this);
  9. this.newlineAndIndentContinueMarkdownTable = this.newlineAndIndentContinueMarkdownTable.bind(this);
  10. this.pasteText = this.pasteText.bind(this);
  11. this.getBot = this.getBot.bind(this);
  12. this.getEot = this.getEot.bind(this);
  13. this.getBol = this.getBol.bind(this);
  14. this.getStrFromBot = this.getStrFromBot.bind(this);
  15. this.getStrFromBol = this.getStrFromBol.bind(this);
  16. }
  17. /**
  18. * return whether context is matched by table
  19. * @param {any} editor An editor instance of CodeMirror
  20. */
  21. isMatchedContext(editor) {
  22. console.log('MarkdownTableHelper.isMatchedContext');
  23. // get strings from BOL(beginning of line) to current position
  24. const strFromBot = this.getStrFromBot(editor);
  25. console.log('strFromBol: ' + strFromBot);
  26. console.log('will return ' + (this.tableTitleAndHeaderAndBodyRE.test(strFromBot) ? 'true' : 'false'));
  27. return this.tableTitleAndHeaderAndBodyRE.test(strFromBot);
  28. }
  29. /**
  30. * handle new line
  31. * @param {any} editor An editor instance of CodeMirror
  32. */
  33. handleNewLine(editor) {
  34. console.log('MarkdownTableHelper.handleNewLine');
  35. this.newlineAndIndentContinueMarkdownTable(editor);
  36. }
  37. /**
  38. * insert new line with auto shaping format of Markdown table
  39. * @param {any} editor An editor instance of CodeMirror
  40. */
  41. newlineAndIndentContinueMarkdownTable(editor) {
  42. console.log('MarkdownTableHelper.newlineAndIndentContinueMarkdownTable');
  43. if (!this.isMatchedContext(editor)) {
  44. return;
  45. }
  46. // get lines all of table from current position to beginning of table
  47. const strTableLines = this.getStrFromBot(editor);
  48. // [TODO] Format table lines
  49. strTableLinesFormated = strTableLines;
  50. // replace the lines to strFormatedTableLines
  51. editor.getDoc().replaceRange(strTableLinesFormated, this.getBot(editor), this.getEot(editor));
  52. codemirror.commands.newline(editor);
  53. }
  54. /**
  55. * paste text
  56. * @param {any} editor An editor instance of CodeMirror
  57. * @param {any} event
  58. * @param {string} text
  59. */
  60. pasteText(editor, event, text) {
  61. // [TODO] replace to formated table markdown
  62. }
  63. /**
  64. * return the postion of the BOT(beginning of table)
  65. */
  66. getBot(editor) {
  67. // [TODO] return the postion of the BOT(beginning of table)
  68. const curPos = editor.getCursor();
  69. return { line: curPos.line, ch: 0 };
  70. }
  71. /**
  72. * return the postion of the EOT(end of table)
  73. */
  74. getEot(editor) {
  75. // [TODO] return the postion of the EOT(end of table)
  76. const curPos = editor.getCursor();
  77. const lineLength = editor.getDoc().getLine(curPos.line).length;
  78. return { line: curPos.line, ch: lineLength };
  79. }
  80. /**
  81. * return the postion of the BOL(beginning of line)
  82. */
  83. getBol(editor) {
  84. const curPos = editor.getCursor();
  85. return { line: curPos.line, ch: 0 };
  86. }
  87. /**
  88. * return strings from current position to BOL(beginning of table)
  89. */
  90. getStrFromBot(editor) {
  91. const curPos = editor.getCursor();
  92. return editor.getDoc().getRange(this.getBot(editor), curPos);
  93. }
  94. /**
  95. * return strings from BOL(beginning of line) to current position
  96. */
  97. getStrFromBol(editor) {
  98. const curPos = editor.getCursor();
  99. return editor.getDoc().getRange(this.getBol(editor), curPos);
  100. }
  101. }
  102. // singleton pattern
  103. const instance = new MarkdownTableHelper();
  104. Object.freeze(instance);
  105. export default instance;