MarkdownDrawioUtil.js 4.3 KB

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