MarkdownTableInterceptor.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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;
  30. // get strings from BOL(beginning of line) to current position
  31. const strFromBol = mtu.getStrFromBol(editor);
  32. if (mtu.isEndOfLine(editor) && mtu.linePartOfTableRE.test(strFromBol)) {
  33. // get lines all of table from current position to beginning of table
  34. const strFromBot = mtu.getStrFromBot(editor);
  35. let table = mtu.parseFromTableStringToMarkdownTable(strFromBot);
  36. mtu.addRowToMarkdownTable(table);
  37. const strToEot = mtu.getStrToEot(editor);
  38. const tableBottom = mtu.parseFromTableStringToMarkdownTable(strToEot);
  39. if (tableBottom.table.length > 0) {
  40. table = mtu.mergeMarkdownTable([table, tableBottom]);
  41. }
  42. mtu.replaceMarkdownTableWithReformed(editor, table);
  43. // report to manager that handling was done
  44. context.handlers.push(this.className);
  45. }
  46. // resolve
  47. return Promise.resolve(context);
  48. }
  49. }