MarkdownDrawioUtil.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 cursor is not in a drawio block, return its position)
  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 { line: curPos.line, ch: curPos.ch };
  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 cursor is not in a drawio block, return its position)
  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 { line: curPos.line, ch: curPos.ch };
  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. return (JSON.stringify(bod) !== JSON.stringify(eod));
  75. }
  76. /**
  77. * return drawioData instance where the cursor is
  78. * (If the cursor is not in a drawio block, return current line)
  79. */
  80. getMarkdownDrawioMxfile(editor) {
  81. const curPos = editor.getCursor();
  82. if (this.isInDrawioBlock(editor)) {
  83. const bod = this.getBod(editor);
  84. const eod = this.getEod(editor);
  85. // skip block begin sesion("::: drawio")
  86. bod.line++;
  87. // skip block end sesion(":::")
  88. eod.line--;
  89. eod.ch = editor.getDoc().getLine(eod.line).length;
  90. return editor.getDoc().getRange(bod, eod);
  91. }
  92. return editor.getDoc().getLine(curPos.line);
  93. }
  94. replaceFocusedDrawioWithEditor(editor, drawioData) {
  95. const curPos = editor.getCursor();
  96. const drawioBlock = ['::: drawio', drawioData.toString(), ':::'].join('\n');
  97. let beginPos;
  98. let endPos;
  99. if (this.isInDrawioBlock(editor)) {
  100. beginPos = this.getBod(editor);
  101. endPos = this.getEod(editor);
  102. }
  103. else {
  104. beginPos = { line: curPos.line, ch: curPos.ch };
  105. endPos = { line: curPos.line, ch: curPos.ch };
  106. }
  107. editor.getDoc().replaceRange(drawioBlock, beginPos, endPos);
  108. }
  109. /**
  110. * return markdown where the drawioData specified by line number params is replaced to the drawioData specified by drawioData param
  111. * @param {string} drawioData
  112. * @param {string} markdown
  113. * @param beginLineNumber
  114. * @param endLineNumber
  115. */
  116. replaceDrawioInMarkdown(drawioData, markdown, beginLineNumber, endLineNumber) {
  117. const splitMarkdown = markdown.split(/\r\n|\r|\n/);
  118. const markdownBeforeDrawio = splitMarkdown.slice(0, beginLineNumber);
  119. const markdownAfterDrawio = splitMarkdown.slice(endLineNumber);
  120. let newMarkdown = '';
  121. if (markdownBeforeDrawio.length > 0) {
  122. newMarkdown += `${markdownBeforeDrawio.join('\n')}\n`;
  123. newMarkdown += '::: drawio\n';
  124. }
  125. newMarkdown += drawioData;
  126. if (markdownAfterDrawio.length > 0) {
  127. newMarkdown += '\n:::';
  128. newMarkdown += `\n${markdownAfterDrawio.join('\n')}`;
  129. }
  130. return newMarkdown;
  131. }
  132. }
  133. // singleton pattern
  134. const instance = new MarkdownDrawioUtil();
  135. Object.freeze(instance);
  136. export default instance;