MarkdownListHelper.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import { BasicInterceptor } from 'crowi-pluginkit';
  2. import * as codemirror from 'codemirror';
  3. import mlu from '../../util/interceptor/MarkdownListUtil';
  4. export default class MarkdownListHelper extends BasicInterceptor {
  5. constructor() {
  6. super();
  7. // https://github.com/codemirror/CodeMirror/blob/c7853a989c77bb9f520c9c530cbe1497856e96fc/addon/edit/continuelist.js#L14
  8. // https://regex101.com/r/7BN2fR/5
  9. this.indentAndMarkRE = /^(\s*)(>[> ]*|[*+-] \[[x ]\]\s|[*+-]\s|(\d+)([.)]))(\s*)/;
  10. this.indentAndMarkOnlyRE = /^(\s*)(>[> ]*|[*+-] \[[x ]\]|[*+-]|(\d+)[.)])(\s*)$/;
  11. this.pasteText = this.pasteText.bind(this);
  12. }
  13. /**
  14. * @inheritdoc
  15. */
  16. isInterceptWhen(contextName) {
  17. return (
  18. contextName === 'preHandleEnter'
  19. );
  20. }
  21. /**
  22. * return boolean value whether processable parallel
  23. */
  24. isProcessableParallel() {
  25. return false;
  26. }
  27. /**
  28. * @inheritdoc
  29. */
  30. process(contextName, ...args) {
  31. console.log(performance.now() + ': AbortContinueMarkdownListInterceptor.process is started');
  32. const orgContext = args[0];
  33. const editor = orgContext.editor;
  34. console.log('AbortContinueMarkdownListInterceptor.process');
  35. // get strings from current position to EOL(end of line) before break the line
  36. const strToEol = mlu.getStrToEol(editor);
  37. if (this.indentAndMarkRE.test(strToEol)) {
  38. const context = Object.assign(args[0]); // clone
  39. console.log('AbortContinueMarkdownListInterceptor.newlineAndIndentContinueMarkdownList: abort auto indent');
  40. codemirror.commands.newlineAndIndent(editor);
  41. // replace the line with strToEol (abort auto indent)
  42. editor.getDoc().replaceRange(strToEol, mlu.getBol(editor), mlu.getEol(editor));
  43. // report to manager that handling was done
  44. context.handlers.push(this.className);
  45. }
  46. console.log(performance.now() + ': AbortContinueMarkdownListInterceptor.process is finished');
  47. // resolve
  48. return Promise.resolve(orgContext);
  49. }
  50. /**
  51. * paste text
  52. * @param {any} editor An editor instance of CodeMirror
  53. * @param {any} event
  54. * @param {string} text
  55. */
  56. pasteText(editor, event, text) {
  57. // get strings from BOL(beginning of line) to current position
  58. const strFromBol = mlu.getStrFromBol(editor);
  59. const matched = strFromBol.match(this.indentAndMarkRE);
  60. // when match indentAndMarkOnlyRE
  61. // (this means the current position is the beginning of the list item)
  62. if (this.indentAndMarkOnlyRE.test(strFromBol)) {
  63. const adjusted = this.adjustPastedData(strFromBol, text);
  64. // replace
  65. if (adjusted != null) {
  66. event.preventDefault();
  67. editor.getDoc().replaceRange(adjusted, mlu.getBol(editor), editor.getCursor());
  68. }
  69. }
  70. }
  71. /**
  72. * return adjusted pasted data by indentAndMark
  73. *
  74. * @param {string} indentAndMark
  75. * @param {string} text
  76. * @returns adjusted pasted data
  77. * returns null when adjustment is not necessary
  78. */
  79. adjustPastedData(indentAndMark, text) {
  80. let adjusted = null;
  81. // list data (starts with indent and mark)
  82. if (text.match(this.indentAndMarkRE)) {
  83. const indent = indentAndMark.match(this.indentAndMarkRE)[1];
  84. // splice to an array of line
  85. const lines = text.match(/[^\r\n]+/g);
  86. // indent
  87. const replacedLines = lines.map((line) => {
  88. return indent + line;
  89. })
  90. adjusted = replacedLines.join('\n');
  91. }
  92. // listful data
  93. else if (this.isListfulData(text)) {
  94. // do nothing (return null)
  95. }
  96. // not listful data
  97. else {
  98. // append `indentAndMark` at the beginning of all lines (except the first line)
  99. const replacedText = text.replace(/(\r\n|\r|\n)/g, "$1" + indentAndMark);
  100. // append `indentAndMark` to the first line
  101. adjusted = indentAndMark + replacedText;
  102. }
  103. return adjusted;
  104. }
  105. /**
  106. * evaluate whether `text` is list like data or not
  107. * @param {string} text
  108. */
  109. isListfulData(text) {
  110. // return false if includes at least one blank line
  111. // see https://stackoverflow.com/a/16369725
  112. if (text.match(/^\s*[\r\n]/m) != null) {
  113. return false;
  114. }
  115. const lines = text.match(/[^\r\n]+/g);
  116. // count lines that starts with indent and mark
  117. let isListful = false;
  118. let count = 0;
  119. lines.forEach((line) => {
  120. if (line.match(this.indentAndMarkRE)) {
  121. count++;
  122. }
  123. // ensure to be true if it is 50% or more
  124. if (count >= lines.length / 2) {
  125. isListful = true;
  126. return;
  127. }
  128. });
  129. return isListful;
  130. }
  131. }