Просмотр исходного кода

Revert "135845 get and edit drawio"

This reverts commit d8cf5c5126cb88ec091a9bea9239a56d0ae14155.
soumaeda 2 лет назад
Родитель
Сommit
0d510dd8d4
1 измененных файлов с 32 добавлено и 67 удалено
  1. 32 67
      apps/app/src/components/PageEditor/MarkdownDrawioUtil.js

+ 32 - 67
apps/app/src/components/PageEditor/MarkdownDrawioUtil.js

@@ -6,38 +6,6 @@ class MarkdownDrawioUtil {
   constructor() {
     this.lineBeginPartOfDrawioRE = /^```(\s.*)drawio$/;
     this.lineEndPartOfDrawioRE = /^```$/;
-    this.curPos = this.curPos.bind(this);
-    this.doc = this.doc.bind(this);
-  }
-
-  // get cursor position(number)
-  curPos(editor) {
-    return editor.state.selection.main.head;
-  }
-
-  // get doc(Text)
-  doc(editor) {
-    return editor.state.doc;
-  }
-
-  // get first line number(numeber)
-  firstLineNum() {
-    return 1;
-  }
-
-  // get last line number(numeber)
-  lastLineNum(editor) {
-    return this.doc(editor).lines;
-  }
-
-  // get cursor line(line)
-  getCursorLine(editor) {
-    return this.doc(editor).lineAt(this.curPos(editor));
-  }
-
-  // get line(line)
-  getLine(editor, lineNum) {
-    return this.doc(editor).line(lineNum);
   }
 
   /**
@@ -45,15 +13,17 @@ class MarkdownDrawioUtil {
    * (If the BOD is not found after the cursor or the EOD is found before the BOD, return null)
    */
   getBod(editor) {
-    if (this.lineBeginPartOfDrawioRE.test(this.getCursorLine(editor).text)) {
-      // get the beginning of the line where the cursor is located
-      return this.getCursorLine(editor).from;
+    const curPos = editor.getCursor();
+    const firstLine = editor.getDoc().firstLine();
+
+    if (this.lineBeginPartOfDrawioRE.test(editor.getDoc().getLine(curPos.line))) {
+      return { line: curPos.line, ch: 0 };
     }
 
-    let line = this.getCursorLine(editor).number - 1;
+    let line = curPos.line - 1;
     let isFound = false;
-    for (; line >= this.firstLineNum(); line--) {
-      const strLine = this.getLine(editor, line).text;
+    for (; line >= firstLine; line--) {
+      const strLine = editor.getDoc().getLine(line);
       if (this.lineBeginPartOfDrawioRE.test(strLine)) {
         isFound = true;
         break;
@@ -69,9 +39,8 @@ class MarkdownDrawioUtil {
       return null;
     }
 
-    const firstLineNum = this.firstLineNum();
-    const botLine = Math.max(firstLineNum, line);
-    return this.getLine(editor, botLine).from;
+    const bodLine = Math.max(firstLine, line);
+    return { line: bodLine, ch: 0 };
   }
 
   /**
@@ -79,15 +48,17 @@ class MarkdownDrawioUtil {
    * (If the EOD is not found after the cursor or the BOD is found before the EOD, return null)
    */
   getEod(editor) {
-    if (this.lineEndPartOfDrawioRE.test(this.getCursorLine(editor).text)) {
-      // get the end of the line where the cursor is located
-      return this.getCursorLine(editor).to;
+    const curPos = editor.getCursor();
+    const lastLine = editor.getDoc().lastLine();
+
+    if (this.lineEndPartOfDrawioRE.test(editor.getDoc().getLine(curPos.line))) {
+      return { line: curPos.line, ch: editor.getDoc().getLine(curPos.line).length };
     }
 
-    let line = this.getCursorLine(editor).number + 1;
+    let line = curPos.line + 1;
     let isFound = false;
-    for (; line <= this.lastLineNum(editor); line++) {
-      const strLine = this.getLine(editor, line).text;
+    for (; line <= lastLine; line++) {
+      const strLine = editor.getDoc().getLine(line);
       if (this.lineEndPartOfDrawioRE.test(strLine)) {
         isFound = true;
         break;
@@ -103,9 +74,9 @@ class MarkdownDrawioUtil {
       return null;
     }
 
-    const lastLineNum = this.lastLineNum(editor);
-    const eodLine = Math.min(line, lastLineNum);
-    return this.getLine(editor, eodLine).to;
+    const eodLine = Math.min(line, lastLine);
+    const lineLength = editor.getDoc().getLine(eodLine).length;
+    return { line: eodLine, ch: lineLength };
   }
 
   /**
@@ -130,18 +101,18 @@ class MarkdownDrawioUtil {
       const eod = this.getEod(editor);
 
       // skip block begin sesion("``` drawio")
-      const bodLineNum = this.doc(editor).lineAt(bod).number + 1;
-      const bodLine = this.getLine(editor, bodLineNum).from;
+      bod.line++;
       // skip block end sesion("```")
-      const eodLineNum = this.doc(editor).lineAt(eod).number - 1;
-      const eodLine = this.getLine(editor, eodLineNum).to;
+      eod.line--;
+      eod.ch = editor.getDoc().getLine(eod.line).length;
 
-      return editor.state.sliceDoc(bodLine, eodLine);
+      return editor.getDoc().getRange(bod, eod);
     }
     return null;
   }
 
   replaceFocusedDrawioWithEditor(editor, drawioData) {
+    const curPos = editor.getCursor();
     const drawioBlock = ['``` drawio', drawioData.toString(), '```'].join('\n');
     let beginPos;
     let endPos;
@@ -151,17 +122,11 @@ class MarkdownDrawioUtil {
       endPos = this.getEod(editor);
     }
     else {
-      beginPos = this.curPos(editor);
-      endPos = this.curPos(editor);
+      beginPos = { line: curPos.line, ch: curPos.ch };
+      endPos = { line: curPos.line, ch: curPos.ch };
     }
 
-    editor.dispatch({
-      changes: {
-        from: beginPos,
-        to: endPos,
-        insert: drawioBlock,
-      },
-    });
+    editor.getDoc().replaceRange(drawioBlock, beginPos, endPos);
   }
 
   /**
@@ -196,9 +161,9 @@ class MarkdownDrawioUtil {
   findAllDrawioSection(editor) {
     const lineNumbers = [];
     // refs: https://github.com/codemirror/CodeMirror/blob/5.64.0/addon/fold/foldcode.js#L106-L111
-    for (let i = this.firstLineNum(), e = this.lastLineNum(editor); i <= e; i++) {
-      const lineText = this.getLine(editor, i).text;
-      const match = this.lineBeginPartOfDrawioRE.exec(lineText);
+    for (let i = editor.firstLine(), e = editor.lastLine(); i <= e; i++) {
+      const line = editor.getLine(i);
+      const match = this.lineBeginPartOfDrawioRE.exec(line);
       if (match) {
         lineNumbers.push(i);
       }