2
0

MarkdownTableInterceptor.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { BasicInterceptor } from 'growi-commons';
  2. import mtu from './MarkdownTableUtil';
  3. import MarkdownTable from '../../models/MarkdownTable';
  4. /**
  5. * Interceptor for markdown table
  6. */
  7. export default class MarkdownTableInterceptor extends BasicInterceptor {
  8. /**
  9. * @inheritdoc
  10. */
  11. isInterceptWhen(contextName) {
  12. return (
  13. contextName === 'preHandleEnter'
  14. );
  15. }
  16. /**
  17. * return boolean value whether processable parallel
  18. */
  19. isProcessableParallel() {
  20. return false;
  21. }
  22. /**
  23. * @inheritdoc
  24. */
  25. process(contextName, ...args) {
  26. const context = Object.assign(args[0]); // clone
  27. const editor = context.editor; // AbstractEditor instance
  28. // do nothing if editor is not a CodeMirrorEditor
  29. if (editor == null || editor.getCodeMirror() == null) {
  30. return Promise.resolve(context);
  31. }
  32. const cm = editor.getCodeMirror();
  33. // get strings from BOL(beginning of line) to current position
  34. const strFromBol = editor.getStrFromBol();
  35. if (mtu.isEndOfLine(cm) && mtu.linePartOfTableRE.test(strFromBol)) {
  36. // get lines all of table from current position to beginning of table
  37. const strFromBot = mtu.getStrFromBot(cm);
  38. let table = MarkdownTable.fromMarkdownString(strFromBot);
  39. mtu.addRowToMarkdownTable(table);
  40. const strToEot = mtu.getStrToEot(cm);
  41. const tableBottom = MarkdownTable.fromMarkdownString(strToEot);
  42. if (tableBottom.table.length > 0) {
  43. table = mtu.mergeMarkdownTable([table, tableBottom]);
  44. }
  45. mtu.replaceMarkdownTableWithReformed(cm, table);
  46. // report to manager that handling was done
  47. context.handlers.push(this.className);
  48. }
  49. // resolve
  50. return Promise.resolve(context);
  51. }
  52. }