Ryu Sato 8 lat temu
rodzic
commit
592553cee2

+ 0 - 94
resource/js/components/PageEditor/MarkdownListInterceptor.js

@@ -7,8 +7,6 @@ export default class MarkdownListInterceptor extends BasicInterceptor {
 
 
   constructor() {
   constructor() {
     super();
     super();
-
-    this.pasteText = this.pasteText.bind(this);
   }
   }
 
 
   /**
   /**
@@ -49,96 +47,4 @@ export default class MarkdownListInterceptor extends BasicInterceptor {
     // resolve
     // resolve
     return Promise.resolve(context);
     return Promise.resolve(context);
   }
   }
-
-  /**
-   * paste text
-   * @param {any} editor An editor instance of CodeMirror
-   * @param {any} event
-   * @param {string} text
-   */
-  pasteText(editor, event, text) {
-    // get strings from BOL(beginning of line) to current position
-    const strFromBol = mlu.getStrFromBol(editor);
-
-    const matched = strFromBol.match(mlu.indentAndMarkRE);
-    // when match indentAndMarkOnlyRE
-    // (this means the current position is the beginning of the list item)
-    if (mlu.indentAndMarkOnlyRE.test(strFromBol)) {
-      const adjusted = this.adjustPastedData(strFromBol, text);
-
-      // replace
-      if (adjusted != null) {
-        event.preventDefault();
-        editor.getDoc().replaceRange(adjusted, mlu.getBol(editor), editor.getCursor());
-      }
-    }
-  }
-
-  /**
-   * return adjusted pasted data by indentAndMark
-   *
-   * @param {string} indentAndMark
-   * @param {string} text
-   * @returns adjusted pasted data
-   *      returns null when adjustment is not necessary
-   */
-  adjustPastedData(indentAndMark, text) {
-    let adjusted = null;
-
-    // list data (starts with indent and mark)
-    if (text.match(mlu.indentAndMarkRE)) {
-      const indent = indentAndMark.match(mlu.indentAndMarkRE)[1];
-
-      // splice to an array of line
-      const lines = text.match(/[^\r\n]+/g);
-      // indent
-      const replacedLines = lines.map((line) => {
-        return indent + line;
-      })
-
-      adjusted = replacedLines.join('\n');
-    }
-    // listful data
-    else if (this.isListfulData(text)) {
-      // do nothing (return null)
-    }
-    // not listful data
-    else {
-      // append `indentAndMark` at the beginning of all lines (except the first line)
-      const replacedText = text.replace(/(\r\n|\r|\n)/g, "$1" + indentAndMark);
-      // append `indentAndMark` to the first line
-      adjusted = indentAndMark + replacedText;
-    }
-
-    return adjusted;
-  }
-
-  /**
-   * evaluate whether `text` is list like data or not
-   * @param {string} text
-   */
-  isListfulData(text) {
-    // return false if includes at least one blank line
-    // see https://stackoverflow.com/a/16369725
-    if (text.match(/^\s*[\r\n]/m) != null) {
-      return false;
-    }
-
-    const lines = text.match(/[^\r\n]+/g);
-    // count lines that starts with indent and mark
-    let isListful = false;
-    let count = 0;
-    lines.forEach((line) => {
-      if (line.match(mlu.indentAndMarkRE)) {
-        count++;
-      }
-      // ensure to be true if it is 50% or more
-      if (count >= lines.length / 2) {
-        isListful = true;
-        return;
-      }
-    });
-
-    return isListful;
-  }
 }
 }

+ 6 - 12
resource/js/components/PageEditor/MarkdownTableInterceptor.js

@@ -1,5 +1,4 @@
 import { BasicInterceptor } from 'crowi-pluginkit';
 import { BasicInterceptor } from 'crowi-pluginkit';
-import markdownTable from 'markdown-table';
 
 
 import mtu from './MarkdownTableUtil';
 import mtu from './MarkdownTableUtil';
 
 
@@ -35,31 +34,26 @@ export default class MarkdownTableInterceptor extends BasicInterceptor {
     const context = Object.assign(args[0]);   // clone
     const context = Object.assign(args[0]);   // clone
     const editor = context.editor;
     const editor = context.editor;
 
 
-    const curPos = editor.getCursor();
-    const isEndOfLine = (curPos.ch == editor.getDoc().getLine(curPos.line).length);
-
     // get strings from BOL(beginning of line) to current position
     // get strings from BOL(beginning of line) to current position
     const strFromBol = mtu.getStrFromBol(editor);
     const strFromBol = mtu.getStrFromBol(editor);
 
 
-    if (isEndOfLine && mtu.linePartOfTableRE.test(strFromBol)) {
+    if (mtu.isEndOfLine(editor) && mtu.linePartOfTableRE.test(strFromBol)) {
       // get lines all of table from current position to beginning of table
       // get lines all of table from current position to beginning of table
       const strTableLines = mtu.getStrFromBot(editor);
       const strTableLines = mtu.getStrFromBot(editor);
 
 
-      const table = mtu.parseFromTableStringToJSON(editor, mtu.getBot(editor), editor.getCursor());
+      let table = mtu.parseFromTableStringToMarkdownTable(editor, mtu.getBot(editor), editor.getCursor());
 
 
-      let newRow = [];
-      table.table[0].forEach(() => { newRow.push('') });
-      table.table.push(newRow);
+      mtu.addRowToMarkdownTable(table);
 
 
       const curPos = editor.getCursor();
       const curPos = editor.getCursor();
       const nextLineHead = { line: curPos.line + 1, ch: 0 };
       const nextLineHead = { line: curPos.line + 1, ch: 0 };
-      const tableBottom = mtu.parseFromTableStringToJSON(editor, nextLineHead, mtu.getEot(editor));
+      const tableBottom = mtu.parseFromTableStringToMarkdownTable(editor, nextLineHead, mtu.getEot(editor));
       if (tableBottom.table.length > 0) {
       if (tableBottom.table.length > 0) {
-        table.table = table.table.concat(tableBottom.table);
+        table = mtu.mergeMarkdownTable([table, tableBottom]);
       }
       }
 
 
       // replace the lines to strTableLinesFormated
       // replace the lines to strTableLinesFormated
-      const strTableLinesFormated = markdownTable(table.table, { align: table.align });
+      const strTableLinesFormated = table.toString();
       editor.getDoc().replaceRange(strTableLinesFormated, mtu.getBot(editor), mtu.getEot(editor));
       editor.getDoc().replaceRange(strTableLinesFormated, mtu.getBot(editor), mtu.getEot(editor));
       editor.getDoc().setCursor(curPos.line + 1, 2);
       editor.getDoc().setCursor(curPos.line + 1, 2);
 
 

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

@@ -1,3 +1,5 @@
+import markdown_table from 'markdown-table';
+
 /**
 /**
  * Utility for markdown table
  * Utility for markdown table
  */
  */
@@ -16,7 +18,8 @@ class MarkdownTableUtil {
     this.getStrFromBot = this.getStrFromBot.bind(this);
     this.getStrFromBot = this.getStrFromBot.bind(this);
     this.getStrToEot = this.getStrToEot.bind(this);
     this.getStrToEot = this.getStrToEot.bind(this);
     this.getStrFromBol = this.getStrFromBol.bind(this);
     this.getStrFromBol = this.getStrFromBol.bind(this);
-    this.parseFromTableStringToJSON = this.parseFromTableStringToJSON.bind(this);
+
+    this.parseFromTableStringToMarkdownTable = this.parseFromTableStringToMarkdownTable.bind(this);
   }
   }
 
 
   /**
   /**
@@ -89,11 +92,11 @@ class MarkdownTableUtil {
   }
   }
 
 
   /**
   /**
-   * returns JSON whose described by 'markdown-table' format
+   * returns markdown table 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
    */
    */
-  parseFromTableStringToJSON(editor, posBeg, posEnd) {
+  parseFromTableStringToMarkdownTable(editor, posBeg, posEnd) {
     let contents = [];
     let contents = [];
     let aligns = [];
     let aligns = [];
     for (let pos = posBeg; pos.line <= posEnd.line; pos.line++) {
     for (let pos = posBeg; pos.line <= posEnd.line; pos.line++) {
@@ -122,7 +125,64 @@ class MarkdownTableUtil {
         contents.push(row);
         contents.push(row);
       }
       }
     }
     }
-    return { table: contents, align: aligns };
+    return (new MarkdownTable(contents, { align: aligns }));
+  }
+
+  /**
+   * return boolean value whether the current position of cursor is end of line
+   */
+  isEndOfLine(editor) {
+    const curPos = editor.getCursor();
+    return (curPos.ch == editor.getDoc().getLine(curPos.line).length);
+  }
+
+  /**
+   * add a row at the end
+   * (This function overwrite directory markdown table specified as argument.)
+   * @param {MarkdownTable} markdown table
+   */
+  addRowToMarkdownTable(mdtable) {
+    const numCol = mdtable.table.length > 0 ? mdtable.table[0].length : 1;
+    let newRow = [];
+    (new Array(numCol)).forEach(() => newRow.push('')); // create cols
+    mdtable.table.push(newRow);
+  }
+
+  /**
+   * returns markdown table that is merged all of markdown table in array
+   * (The merged markdown table options are used for the first markdown table.)
+   * @param {Array} array of markdown table
+   */
+  mergeMarkdownTable(mdtable_list) {
+    if (mdtable_list == undefined
+        || !(mdtable_list instanceof Array)) {
+      return undefined;
+    }
+
+    let newTable = [];
+    const options = mdtable_list[0].options; // use option of first markdown-table
+    mdtable_list.forEach((mdtable) => {
+      newTable = newTable.concat(mdtable.table)
+    });
+    return (new MarkdownTable(newTable, options));
+  }
+}
+
+/**
+ * markdown table class for markdown-table module
+ *   ref. https://github.com/wooorm/markdown-table
+ */
+class MarkdownTable {
+
+  constructor(table, options) {
+    this.table = table || [];
+    this.options = options || {};
+
+    this.toString = this.toString.bind(this);
+  }
+
+  toString() {
+    return markdown_table(this.table, this.options);
   }
   }
 }
 }
 
 

+ 2 - 2
resource/js/components/PageEditor/PasteHelper.js

@@ -1,6 +1,6 @@
 import accepts from 'attr-accept'
 import accepts from 'attr-accept'
 
 
-import markdownListInterceptor from './MarkdownListInterceptor';
+import markdownListUtil from './MarkdownListUtil';
 
 
 class PasteHelper {
 class PasteHelper {
 
 
@@ -21,7 +21,7 @@ class PasteHelper {
       return;
       return;
     }
     }
 
 
-    markdownListInterceptor.pasteText(editor, event, text);
+    markdownListUtil.pasteText(editor, event, text);
   }
   }
 
 
   // Firefox versions prior to 53 return a bogus MIME type for every file drag, so dragovers with
   // Firefox versions prior to 53 return a bogus MIME type for every file drag, so dragovers with