فهرست منبع

135395 sammarize curPos definition

soumaeda 2 سال پیش
والد
کامیت
8bd1533410
1فایلهای تغییر یافته به همراه14 افزوده شده و 16 حذف شده
  1. 14 16
      apps/app/src/components/PageEditor/MarkdownTableUtil.js

+ 14 - 16
apps/app/src/components/PageEditor/MarkdownTableUtil.js

@@ -14,6 +14,7 @@ class MarkdownTableUtil {
     // https://regex101.com/r/1UuWBJ/3
     this.emptyLineOfTableRE = /^([^\r\n|]*)\|((\s*\|)+)$/;
 
+    this.curPos = this.curPos.bind(this);
     this.getBot = this.getBot.bind(this);
     this.getEot = this.getEot.bind(this);
     this.getStrFromBot = this.getStrFromBot.bind(this);
@@ -23,19 +24,22 @@ class MarkdownTableUtil {
     this.replaceMarkdownTableWithReformed = this.replaceFocusedMarkdownTableWithEditor; // alias
   }
 
+  curPos(editor) {
+    return editor.state.selection.main.head;
+  }
+
   /**
    * return the postion of the BOT(beginning of table)
    * (If the cursor is not in a table, return its position)
    */
   getBot(editor) {
-    const curPos = editor.state.selection.main.head;
     if (!this.isInTable(editor)) {
-      return curPos;
+      return this.curPos;
     }
 
     const doc = editor.state.doc;
     const firstLine = doc.line(1);
-    let line = doc.lineAt(curPos).number - 1;
+    let line = doc.lineAt(this.curPos).number - 1;
     for (; line >= firstLine.number; line--) {
       const strLine = doc.line(line);
       if (!this.linePartOfTableRE.test(strLine.text)) {
@@ -51,14 +55,13 @@ class MarkdownTableUtil {
    * (If the cursor is not in a table, return its position)
    */
   getEot(editor) {
-    const curPos = editor.state.selection.main.head;
     if (!this.isInTable(editor)) {
-      return curPos;
+      return this.curPos;
     }
 
     const doc = editor.state.doc;
     const lastLine = doc.line(doc.lines);
-    let line = doc.lineAt(curPos).number + 1;
+    let line = doc.lineAt(this.curPos).number + 1;
     for (; line <= lastLine.number; line++) {
       const strLine = doc.line(line);
       if (!this.linePartOfTableRE.test(strLine.text)) {
@@ -73,16 +76,14 @@ class MarkdownTableUtil {
    * return strings from BOT(beginning of table) to the cursor position
    */
   getStrFromBot(editor) {
-    const curPos = editor.state.selection.main.head;
-    return editor.state.sliceDoc(this.getBot(editor), curPos);
+    return editor.state.sliceDoc(this.getBot(editor), this.curPos);
   }
 
   /**
    * return strings from the cursor position to EOT(end of table)
    */
   getStrToEot(editor) {
-    const curPos = editor.state.selection.main.head;
-    return editor.state.sliceDoc(curPos, this.getEot(editor));
+    return editor.state.sliceDoc(this.curPos, this.getEot(editor));
   }
 
   /**
@@ -107,16 +108,14 @@ class MarkdownTableUtil {
    * return boolean value whether the cursor position is end of line
    */
   isEndOfLine(editor) {
-    const curPos = editor.state.selection.main.head;
-    return (curPos === editor.state.doc.line(curPos.line + 1).length);
+    return (this.curPos === editor.state.doc.line(this.curPos.line + 1).length);
   }
 
   /**
    * return boolean value whether the cursor position is in a table
    */
   isInTable(editor) {
-    const curPos = editor.state.selection.main.head;
-    const lineText = editor.state.doc.lineAt(curPos).text;
+    const lineText = editor.state.doc.lineAt(this.curPos).text;
     return this.linePartOfTableRE.test(lineText);
   }
 
@@ -158,7 +157,6 @@ class MarkdownTableUtil {
   replaceFocusedMarkdownTableWithEditor(editor, table) {
     const botPos = this.getBot(editor);
     const eotPos = this.getEot(editor);
-    const curPos = editor.state.selection.main.head;
 
     editor.dispatch({
       changes: {
@@ -168,7 +166,7 @@ class MarkdownTableUtil {
       },
     });
     editor.dispatch({
-      selection: { anchor: editor.state.doc.lineAt(curPos).to },
+      selection: { anchor: editor.state.doc.lineAt(this.curPos).to },
     });
   }