MarkdownDrawioUtil.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. this.doc(editor).lineAt(bod).number++;
  98. // skip block end sesion("```")
  99. this.doc(editor).lineAt(eod).number--;
  100. return editor.state.sliceDoc(bod, eod);
  101. }
  102. return null;
  103. }
  104. replaceFocusedDrawioWithEditor(editor, drawioData) {
  105. const drawioBlock = ['``` drawio', drawioData.toString(), '```'].join('\n');
  106. let beginPos;
  107. let endPos;
  108. if (this.isInDrawioBlock(editor)) {
  109. beginPos = this.getBod(editor);
  110. endPos = this.getEod(editor);
  111. }
  112. else {
  113. beginPos = this.doc(editor).lineAt(this.curPos(editor)).from;
  114. endPos = this.doc(editor).lineAt(this.curPos(editor)).to;
  115. }
  116. editor.dispatch({
  117. changes: {
  118. from: beginPos,
  119. to: endPos,
  120. insert: drawioBlock,
  121. },
  122. });
  123. }
  124. /**
  125. * return markdown where the drawioData specified by line number params is replaced to the drawioData specified by drawioData param
  126. * @param {string} drawioData
  127. * @param {string} markdown
  128. * @param beginLineNumber
  129. * @param endLineNumber
  130. */
  131. replaceDrawioInMarkdown(drawioData, markdown, beginLineNumber, endLineNumber) {
  132. const splitMarkdown = markdown.split(/\r\n|\r|\n/);
  133. const markdownBeforeDrawio = splitMarkdown.slice(0, beginLineNumber - 1);
  134. const markdownAfterDrawio = splitMarkdown.slice(endLineNumber);
  135. let newMarkdown = '';
  136. if (markdownBeforeDrawio.length > 0) {
  137. newMarkdown += `${markdownBeforeDrawio.join('\n')}\n`;
  138. }
  139. newMarkdown += '``` drawio\n';
  140. newMarkdown += drawioData;
  141. newMarkdown += '\n```';
  142. if (markdownAfterDrawio.length > 0) {
  143. newMarkdown += `\n${markdownAfterDrawio.join('\n')}`;
  144. }
  145. return newMarkdown;
  146. }
  147. /**
  148. * return an array of the starting line numbers of the drawio sections found in markdown
  149. */
  150. findAllDrawioSection(editor) {
  151. const lineNumbers = [];
  152. // refs: https://github.com/codemirror/CodeMirror/blob/5.64.0/addon/fold/foldcode.js#L106-L111
  153. for (let i = 1, e = this.doc(editor).lines; i <= e; i++) {
  154. const line = this.doc(editor).line(i + 1).text;
  155. const match = this.lineBeginPartOfDrawioRE.exec(line);
  156. if (match) {
  157. lineNumbers.push(i);
  158. }
  159. }
  160. return lineNumbers;
  161. }
  162. }
  163. // singleton pattern
  164. const instance = new MarkdownDrawioUtil();
  165. Object.freeze(instance);
  166. export default instance;