MarkdownTableInterceptor.js 1.7 KB

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