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

modified violation of responsibility of intercept classes.

Ryu Sato 8 лет назад
Родитель
Сommit
4f6604b6ac

+ 1 - 4
resource/js/components/PageEditor/MarkdownListInterceptor.js

@@ -35,10 +35,7 @@ export default class MarkdownListInterceptor extends BasicInterceptor {
     // get strings from current position to EOL(end of line) before break the line
     const strToEol = mlu.getStrToEol(editor);
     if (mlu.indentAndMarkRE.test(strToEol)) {
-      codemirror.commands.newlineAndIndent(editor);
-
-      // replace the line with strToEol (abort auto indent)
-      editor.getDoc().replaceRange(strToEol, mlu.getBol(editor), mlu.getEol(editor));
+      mlu.newlineWithoutIndent(editor, strToEol);
 
       // report to manager that handling was done
       context.handlers.push(this.className);

+ 11 - 0
resource/js/components/PageEditor/MarkdownListUtil.js

@@ -15,6 +15,7 @@ class MarkdownListUtil {
     this.getEol = this.getEol.bind(this);
     this.getStrFromBol = this.getStrFromBol.bind(this);
     this.getStrToEol = this.getStrToEol.bind(this);
+    this.newlineWithoutIndent = this.newlineWithoutIndent.bind(this);
   }
 
   /**
@@ -141,6 +142,16 @@ class MarkdownListUtil {
     const curPos = editor.getCursor();
     return editor.getDoc().getRange(curPos, this.getEol(editor));
   }
+
+  /**
+   * insert newline without indent
+   */
+  newlineWithoutIndent(editor, strToEol) {
+    codemirror.commands.newlineAndIndent(editor);
+
+    // replace the line with strToEol (abort auto indent)
+    editor.getDoc().replaceRange(strToEol, this.getBol(editor), this.getEol(editor));
+  }
 }
 
 // singleton pattern

+ 5 - 10
resource/js/components/PageEditor/MarkdownTableInterceptor.js

@@ -39,23 +39,18 @@ export default class MarkdownTableInterceptor extends BasicInterceptor {
 
     if (mtu.isEndOfLine(editor) && mtu.linePartOfTableRE.test(strFromBol)) {
       // get lines all of table from current position to beginning of table
-      const strTableLines = mtu.getStrFromBot(editor);
-
-      let table = mtu.parseFromTableStringToMarkdownTable(editor, mtu.getBot(editor), editor.getCursor());
+      const strFromBot = mtu.getStrFromBot(editor);
+      let table = mtu.parseFromTableStringToMarkdownTable(strFromBot);
 
       mtu.addRowToMarkdownTable(table);
 
-      const curPos = editor.getCursor();
-      const nextLineHead = { line: curPos.line + 1, ch: 0 };
-      const tableBottom = mtu.parseFromTableStringToMarkdownTable(editor, nextLineHead, mtu.getEot(editor));
+      const strToEot = mtu.getStrToEot(editor);
+      const tableBottom = mtu.parseFromTableStringToMarkdownTable(strToEot);
       if (tableBottom.table.length > 0) {
         table = mtu.mergeMarkdownTable([table, tableBottom]);
       }
 
-      // replace the lines to strTableLinesFormated
-      const strTableLinesFormated = table.toString();
-      editor.getDoc().replaceRange(strTableLinesFormated, mtu.getBot(editor), mtu.getEot(editor));
-      editor.getDoc().setCursor(curPos.line + 1, 2);
+      mtu.replaceMarkdownTableWithReformed(editor, table);
 
       // report to manager that handling was done
       context.handlers.push(this.className);

+ 21 - 4
resource/js/components/PageEditor/MarkdownTableUtil.js

@@ -20,6 +20,7 @@ class MarkdownTableUtil {
     this.getStrFromBol = this.getStrFromBol.bind(this);
 
     this.parseFromTableStringToMarkdownTable = this.parseFromTableStringToMarkdownTable.bind(this);
+    this.replaceMarkdownTableWithReformed = this.replaceMarkdownTableWithReformed.bind(this);
   }
 
   /**
@@ -96,11 +97,12 @@ class MarkdownTableUtil {
    *   ref. https://github.com/wooorm/markdown-table
    * @param {string} lines all of table
    */
-  parseFromTableStringToMarkdownTable(editor, posBeg, posEnd) {
+  parseFromTableStringToMarkdownTable(strMDTable) {
+    const arrMDTableLines = strMDTable.split(/[\r\n]+/);
     let contents = [];
     let aligns = [];
-    for (let pos = posBeg; pos.line <= posEnd.line; pos.line++) {
-      const line = editor.getDoc().getLine(pos.line);
+    for (let n = 0; n < arrMDTableLines.length; n++) {
+      const line = arrMDTableLines[n];
 
       if (this.tableAlignmentLineRE.test(line) && !this.tableAlignmentLineNegRE.test(line)) {
         // parse line which described alignment
@@ -116,7 +118,7 @@ class MarkdownTableUtil {
           const rule = alignRuleRE.find(rule => col.match(rule.regex));
           return (rule != undefined) ? rule.align : '';
         });
-      } else {
+      } else if (this.linePartOfTableRE.test(line)) {
         // parse line whether header or body
         let lineText = "";
         lineText = line.replace(/\s*\|\s*/g, '|');
@@ -166,6 +168,21 @@ class MarkdownTableUtil {
     });
     return (new MarkdownTable(newTable, options));
   }
+
+  /**
+   * replace markdown table which is reformed by markdown-table
+   * @param {MarkdownTable} markdown table
+   */
+  replaceMarkdownTableWithReformed(editor, table) {
+    const curPos = editor.getCursor();
+
+    // replace the lines to strTableLinesFormated
+    const strTableLinesFormated = table.toString();
+    editor.getDoc().replaceRange(strTableLinesFormated, this.getBot(editor), this.getEot(editor));
+
+    // set cursor to first column
+    editor.getDoc().setCursor(curPos.line + 1, 2);
+  }
 }
 
 /**