MarkdownDrawioUtil.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /**
  2. * Utility for markdown drawio
  3. */
  4. class MarkdownDrawioUtil {
  5. constructor() {
  6. this.lineBeginPartOfDrawioRE = /^```(\s.*)drawio$/;
  7. this.lineEndPartOfDrawioRE = /^```$/;
  8. this.curPos = this.curPos.bind(this);
  9. this.doc = this.doc.bind(this);
  10. }
  11. // get cursor position from editor
  12. curPos(editor) {
  13. return editor.state.selection.main.head;
  14. }
  15. // get doc from editor
  16. doc(editor) {
  17. return editor.state.doc;
  18. }
  19. /**
  20. * return the postion of the BOD(beginning of drawio)
  21. * (If the BOD is not found after the cursor or the EOD is found before the BOD, return null)
  22. */
  23. getBod(editor) {
  24. const firstLine = 1;
  25. if (this.lineBeginPartOfDrawioRE.test(this.doc(editor).lineAt(this.curPos(editor))).text) {
  26. // get the beginning of the line where the cursor is located
  27. return this.doc(editor).lineAt(this.curPos(editor)).from;
  28. }
  29. let line = this.doc(editor).lineAt(this.curPos(editor)).number - 1;
  30. let isFound = false;
  31. for (; line >= firstLine; line--) {
  32. const strLine = this.doc(editor).line(line).text;
  33. if (this.lineBeginPartOfDrawioRE.test(strLine)) {
  34. isFound = true;
  35. break;
  36. }
  37. if (this.lineEndPartOfDrawioRE.test(strLine)) {
  38. isFound = false;
  39. break;
  40. }
  41. }
  42. if (!isFound) {
  43. return null;
  44. }
  45. const botLine = Math.max(firstLine, line);
  46. return this.doc(editor).line(botLine).from;
  47. }
  48. /**
  49. * return the postion of the EOD(end of drawio)
  50. * (If the EOD is not found after the cursor or the BOD is found before the EOD, return null)
  51. */
  52. getEod(editor) {
  53. const lastLine = this.doc(editor).lines;
  54. if (this.lineEndPartOfDrawioRE.test(this.doc(editor).lineAt(this.curPos(editor))).text) {
  55. // get the end of the line where the cursor is located
  56. return this.doc(editor).lineAt(this.curPos(editor)).to;
  57. }
  58. let line = this.doc(editor).lineAt(this.curPos(editor)).number + 1;
  59. let isFound = false;
  60. for (; line <= lastLine; line++) {
  61. const strLine = this.doc(editor).line(line).text;
  62. if (this.lineEndPartOfDrawioRE.test(strLine)) {
  63. isFound = true;
  64. break;
  65. }
  66. if (this.lineBeginPartOfDrawioRE.test(strLine)) {
  67. isFound = false;
  68. break;
  69. }
  70. }
  71. if (!isFound) {
  72. return null;
  73. }
  74. const eodLine = Math.min(line, lastLine);
  75. return this.doc(editor).line(eodLine).to;
  76. }
  77. /**
  78. * return boolean value whether the cursor position is in a drawio
  79. */
  80. isInDrawioBlock(editor) {
  81. const bod = this.getBod(editor);
  82. const eod = this.getEod(editor);
  83. if (bod === null || eod === null) {
  84. return false;
  85. }
  86. return JSON.stringify(bod) !== JSON.stringify(eod);
  87. }
  88. /**
  89. * return drawioData instance where the cursor is
  90. * (If the cursor is not in a drawio block, return null)
  91. */
  92. getMarkdownDrawioMxfile(editor) {
  93. if (this.isInDrawioBlock(editor)) {
  94. const bod = this.getBod(editor);
  95. const eod = this.getEod(editor);
  96. // skip block begin sesion("``` drawio")
  97. const bodLineNum = this.doc(editor).lineAt(bod).number + 1;
  98. const bodLine = this.doc(editor).line(bodLineNum).from;
  99. // skip block end sesion("```")
  100. const eodLineNum = this.doc(editor).lineAt(eod).number - 1;
  101. const eodLine = this.doc(editor).line(eodLineNum).to;
  102. return editor.state.sliceDoc(bodLine, eodLine);
  103. }
  104. return null;
  105. }
  106. replaceFocusedDrawioWithEditor(editor, drawioData) {
  107. const drawioBlock = ['``` drawio', drawioData.toString(), '```'].join('\n');
  108. let beginPos;
  109. let endPos;
  110. if (this.isInDrawioBlock(editor)) {
  111. beginPos = this.getBod(editor);
  112. endPos = this.getEod(editor);
  113. }
  114. else {
  115. beginPos = this.doc(editor).lineAt(this.curPos(editor)).from;
  116. endPos = this.doc(editor).lineAt(this.curPos(editor)).to;
  117. }
  118. editor.dispatch({
  119. changes: {
  120. from: beginPos,
  121. to: endPos,
  122. insert: drawioBlock,
  123. },
  124. });
  125. }
  126. /**
  127. * return markdown where the drawioData specified by line number params is replaced to the drawioData specified by drawioData param
  128. * @param {string} drawioData
  129. * @param {string} markdown
  130. * @param beginLineNumber
  131. * @param endLineNumber
  132. */
  133. replaceDrawioInMarkdown(drawioData, markdown, beginLineNumber, endLineNumber) {
  134. const splitMarkdown = markdown.split(/\r\n|\r|\n/);
  135. const markdownBeforeDrawio = splitMarkdown.slice(0, beginLineNumber - 1);
  136. const markdownAfterDrawio = splitMarkdown.slice(endLineNumber);
  137. let newMarkdown = '';
  138. if (markdownBeforeDrawio.length > 0) {
  139. newMarkdown += `${markdownBeforeDrawio.join('\n')}\n`;
  140. }
  141. newMarkdown += '``` drawio\n';
  142. newMarkdown += drawioData;
  143. newMarkdown += '\n```';
  144. if (markdownAfterDrawio.length > 0) {
  145. newMarkdown += `\n${markdownAfterDrawio.join('\n')}`;
  146. }
  147. return newMarkdown;
  148. }
  149. /**
  150. * return an array of the starting line numbers of the drawio sections found in markdown
  151. */
  152. findAllDrawioSection(editor) {
  153. const lineNumbers = [];
  154. // refs: https://github.com/codemirror/CodeMirror/blob/5.64.0/addon/fold/foldcode.js#L106-L111
  155. for (let i = 1, e = this.doc(editor).lines; i <= e; i++) {
  156. const line = this.doc(editor).line(i + 1).text;
  157. const match = this.lineBeginPartOfDrawioRE.exec(line);
  158. if (match) {
  159. lineNumbers.push(i);
  160. }
  161. }
  162. return lineNumbers;
  163. }
  164. }
  165. // singleton pattern
  166. const instance = new MarkdownDrawioUtil();
  167. Object.freeze(instance);
  168. export default instance;