MarkdownDrawioUtil.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /**
  2. * Utility for markdown drawio
  3. */
  4. class MarkdownDrawioUtil {
  5. constructor() {
  6. this.lineBeginPartOfDrawioRE = /^:::(\s.*)drawio$/;
  7. this.lineEndPartOfDrawioRE = /^:::$/;
  8. }
  9. /**
  10. * return the postion of the BOD(beginning of drawio)
  11. * (If the BOD is not found after the cursor or the EOD is found before the BOD, return null)
  12. */
  13. getBod(editor) {
  14. const curPos = editor.getCursor();
  15. const firstLine = editor.getDoc().firstLine();
  16. if (this.lineBeginPartOfDrawioRE.test(editor.getDoc().getLine(curPos.line))) {
  17. return { line: curPos.line, ch: 0 };
  18. }
  19. let line = curPos.line - 1;
  20. let isFound = false;
  21. for (; line >= firstLine; line--) {
  22. const strLine = editor.getDoc().getLine(line);
  23. if (this.lineBeginPartOfDrawioRE.test(strLine)) {
  24. isFound = true;
  25. break;
  26. }
  27. if (this.lineEndPartOfDrawioRE.test(strLine)) {
  28. isFound = false;
  29. break;
  30. }
  31. }
  32. if (!isFound) {
  33. return null;
  34. }
  35. const bodLine = Math.max(firstLine, line);
  36. return { line: bodLine, ch: 0 };
  37. }
  38. /**
  39. * return the postion of the EOD(end of drawio)
  40. * (If the EOD is not found after the cursor or the BOD is found before the EOD, return null)
  41. */
  42. getEod(editor) {
  43. const curPos = editor.getCursor();
  44. const lastLine = editor.getDoc().lastLine();
  45. if (this.lineEndPartOfDrawioRE.test(editor.getDoc().getLine(curPos.line))) {
  46. return { line: curPos.line, ch: editor.getDoc().getLine(curPos.line).length };
  47. }
  48. let line = curPos.line + 1;
  49. let isFound = false;
  50. for (; line <= lastLine; line++) {
  51. const strLine = editor.getDoc().getLine(line);
  52. if (this.lineEndPartOfDrawioRE.test(strLine)) {
  53. isFound = true;
  54. break;
  55. }
  56. if (this.lineBeginPartOfDrawioRE.test(strLine)) {
  57. isFound = false;
  58. break;
  59. }
  60. }
  61. if (!isFound) {
  62. return null;
  63. }
  64. const eodLine = Math.min(line, lastLine);
  65. const lineLength = editor.getDoc().getLine(eodLine).length;
  66. return { line: eodLine, ch: lineLength };
  67. }
  68. /**
  69. * return boolean value whether the cursor position is in a drawio
  70. */
  71. isInDrawioBlock(editor) {
  72. const bod = this.getBod(editor);
  73. const eod = this.getEod(editor);
  74. if (bod === null || eod === null) {
  75. return false;
  76. }
  77. return JSON.stringify(bod) !== JSON.stringify(eod);
  78. }
  79. /**
  80. * return drawioData instance where the cursor is
  81. * (If the cursor is not in a drawio block, return null)
  82. */
  83. getMarkdownDrawioMxfile(editor) {
  84. if (this.isInDrawioBlock(editor)) {
  85. const bod = this.getBod(editor);
  86. const eod = this.getEod(editor);
  87. // skip block begin sesion("::: drawio")
  88. bod.line++;
  89. // skip block end sesion(":::")
  90. eod.line--;
  91. eod.ch = editor.getDoc().getLine(eod.line).length;
  92. return editor.getDoc().getRange(bod, eod);
  93. }
  94. return null;
  95. }
  96. replaceFocusedDrawioWithEditor(editor, drawioData) {
  97. const curPos = editor.getCursor();
  98. const drawioBlock = ['::: drawio', drawioData.toString(), ':::'].join('\n');
  99. let beginPos;
  100. let endPos;
  101. if (this.isInDrawioBlock(editor)) {
  102. beginPos = this.getBod(editor);
  103. endPos = this.getEod(editor);
  104. }
  105. else {
  106. beginPos = { line: curPos.line, ch: curPos.ch };
  107. endPos = { line: curPos.line, ch: curPos.ch };
  108. }
  109. editor.getDoc().replaceRange(drawioBlock, beginPos, endPos);
  110. }
  111. /**
  112. * return markdown where the drawioData specified by line number params is replaced to the drawioData specified by drawioData param
  113. * @param {string} drawioData
  114. * @param {string} markdown
  115. * @param beginLineNumber
  116. * @param endLineNumber
  117. */
  118. replaceDrawioInMarkdown(drawioData, markdown, beginLineNumber, endLineNumber) {
  119. const splitMarkdown = markdown.split(/\r\n|\r|\n/);
  120. const markdownBeforeDrawio = splitMarkdown.slice(0, beginLineNumber - 1);
  121. const markdownAfterDrawio = splitMarkdown.slice(endLineNumber);
  122. let newMarkdown = '';
  123. if (markdownBeforeDrawio.length > 0) {
  124. newMarkdown += `${markdownBeforeDrawio.join('\n')}\n`;
  125. }
  126. newMarkdown += '::: drawio\n';
  127. newMarkdown += drawioData;
  128. newMarkdown += '\n:::';
  129. if (markdownAfterDrawio.length > 0) {
  130. newMarkdown += `\n${markdownAfterDrawio.join('\n')}`;
  131. }
  132. return newMarkdown;
  133. }
  134. }
  135. // singleton pattern
  136. const instance = new MarkdownDrawioUtil();
  137. Object.freeze(instance);
  138. export default instance;