Pārlūkot izejas kodu

- reform entire table even if the enter key is hit on the middle row.
- insert row when the enter key is hit at middle row, or last row.
- [BUG] empty row will removed when table is reformed.

Ryu Sato 8 gadi atpakaļ
vecāks
revīzija
a28cedd3c3

+ 19 - 3
resource/js/components/PageEditor/MarkdownTableHelper.js

@@ -62,12 +62,28 @@ export default class MarkdownTableUtil extends BasicInterceptor {
 
 
       const table = mtu.parseFromTableStringToJSON(editor, mtu.getBot(editor), editor.getCursor());
       const table = mtu.parseFromTableStringToJSON(editor, mtu.getBot(editor), editor.getCursor());
       console.log('table: ' + JSON.stringify(table));
       console.log('table: ' + JSON.stringify(table));
-      const strTableLinesFormated = table;
+
+      let newRow = [];
+      table.table[0].forEach(() => { newRow.push(' ') });
+      console.log('empty: ' + JSON.stringify(newRow));
+      table.table.push(newRow);
+      console.log('table: ' + JSON.stringify(table));
+
+      const curPos = editor.getCursor();
+      const nextLineHead = { line: curPos.line + 1, ch: 0 };
+      const tableBottom = mtu.parseFromTableStringToJSON(editor, nextLineHead, mtu.getEot(editor));
+      console.log('tableBottom: ' + JSON.stringify(tableBottom));
+      if (tableBottom.table.length > 0) {
+        table.table = table.table.concat(tableBottom.table);
+      }
+      console.log('table: ' + JSON.stringify(table));
+
+      const strTableLinesFormated = markdownTable(table.table, { align: table.align });
       console.log('strTableLinesFormated: ' + strTableLinesFormated);
       console.log('strTableLinesFormated: ' + strTableLinesFormated);
 
 
       // replace the lines to strFormatedTableLines
       // replace the lines to strFormatedTableLines
-      editor.getDoc().replaceRange(strTableLinesFormated, mtu.getBot(editor), editor.getCursor());
-      codemirror.commands.newlineAndIndent(editor);
+      editor.getDoc().replaceRange(strTableLinesFormated, mtu.getBot(editor), mtu.getEot(editor));
+      editor.getDoc().setCursor(curPos.line + 1, 2);
 
 
       // report to manager that handling was done
       // report to manager that handling was done
       context.handlers.push(this.className);
       context.handlers.push(this.className);

+ 6 - 6
resource/js/util/interceptor/MarkdownTableUtil.js

@@ -1,7 +1,5 @@
 import * as codemirror from 'codemirror';
 import * as codemirror from 'codemirror';
 
 
-import markdownTable from 'markdown-table';
-
 /**
 /**
  * Utility for markdown table
  * Utility for markdown table
  */
  */
@@ -11,6 +9,7 @@ class MarkdownTableUtil {
     // https://github.com/markdown-it/markdown-it/blob/d29f421927e93e88daf75f22089a3e732e195bd2/lib/rules_block/table.js#L83
     // https://github.com/markdown-it/markdown-it/blob/d29f421927e93e88daf75f22089a3e732e195bd2/lib/rules_block/table.js#L83
     // https://regex101.com/r/7BN2fR/7
     // https://regex101.com/r/7BN2fR/7
     this.tableAlignmentLineRE = /^[-:|][-:|\s]*$/;
     this.tableAlignmentLineRE = /^[-:|][-:|\s]*$/;
+    this.tableAlignmentLineNegRE = /^[^-:]*$/;  // it is need to check to ignore empty row which is matched above RE
     this.linePartOfTableRE = /^\|[^\r\n]*|[^\r\n]*\|$|([^\|\r\n]+\|[^\|\r\n]*)+/; // own idea
     this.linePartOfTableRE = /^\|[^\r\n]*|[^\r\n]*\|$|([^\|\r\n]+\|[^\|\r\n]*)+/; // own idea
 
 
     this.getBot = this.getBot.bind(this);
     this.getBot = this.getBot.bind(this);
@@ -92,7 +91,7 @@ class MarkdownTableUtil {
   }
   }
 
 
   /**
   /**
-   * returns object whose described by 'markdown-table' format
+   * returns JSON whose described by 'markdown-table' format
    *   ref. https://github.com/wooorm/markdown-table
    *   ref. https://github.com/wooorm/markdown-table
    * @param {string} lines all of table
    * @param {string} lines all of table
    */
    */
@@ -104,7 +103,7 @@ class MarkdownTableUtil {
       const line = editor.getDoc().getLine(pos.line);
       const line = editor.getDoc().getLine(pos.line);
       console.log("line#" + pos.line + ": " + line);
       console.log("line#" + pos.line + ": " + line);
 
 
-      if (this.tableAlignmentLineRE.test(line)) {
+      if (this.tableAlignmentLineRE.test(line) && !this.tableAlignmentLineNegRE.test(line)) {
         // parse line which described alignment
         // parse line which described alignment
         const alignRuleRE = [
         const alignRuleRE = [
           { align: 'c', regex: /^:-+:$/ },
           { align: 'c', regex: /^:-+:$/ },
@@ -118,19 +117,20 @@ class MarkdownTableUtil {
           const rule = alignRuleRE.find(rule => col.match(rule.regex));
           const rule = alignRuleRE.find(rule => col.match(rule.regex));
           return (rule != undefined) ? rule.align : '';
           return (rule != undefined) ? rule.align : '';
         });
         });
+        console.log('align: ' + aligns);
       } else {
       } else {
         // parse line whether header or body
         // parse line whether header or body
         let lineText = "";
         let lineText = "";
         lineText = line.replace(/\s*\|\s*/g, '|');
         lineText = line.replace(/\s*\|\s*/g, '|');
         lineText = lineText.replace(/^\||\|$/g, ''); // strip off pipe charactor which is placed head of line and last of line.
         lineText = lineText.replace(/^\||\|$/g, ''); // strip off pipe charactor which is placed head of line and last of line.
         const row = lineText.split(/\|/);
         const row = lineText.split(/\|/);
-        console.log('row: ' + row);
+        console.log('row: ' + JSON.stringify(row));
         contents.push(row);
         contents.push(row);
       }
       }
     }
     }
     console.log('contents: ' + JSON.stringify(contents));
     console.log('contents: ' + JSON.stringify(contents));
     console.log('aligns: ' + JSON.stringify(aligns));
     console.log('aligns: ' + JSON.stringify(aligns));
-    return markdownTable(contents, { align: aligns } );
+    return { table: contents, align: aligns };
   }
   }
 }
 }