Procházet zdrojové kódy

move methods about markdown table operation into MarkdownTableUtil

utsushiiro před 7 roky
rodič
revize
7dd238177b

+ 2 - 22
src/client/js/components/PageEditor/Editor.js

@@ -13,7 +13,6 @@ import HandsontableModal from './HandsontableModal';
 import pasteHelper from './PasteHelper';
 
 import mtu from './MarkdownTableUtil';
-import MarkdownTable from '../../models/MarkdownTable';
 
 export default class Editor extends AbstractEditor {
 
@@ -36,7 +35,6 @@ export default class Editor extends AbstractEditor {
     this.getDropzoneAccept = this.getDropzoneAccept.bind(this);
     this.getDropzoneClassName = this.getDropzoneClassName.bind(this);
     this.renderDropzoneOverlay = this.renderDropzoneOverlay.bind(this);
-    this.replaceMarkDownTable = this.replaceMarkDownTable.bind(this);
   }
 
   getEditorSubstance() {
@@ -206,31 +204,13 @@ export default class Editor extends AbstractEditor {
       <div className="m-0 navbar navbar-default navbar-editor" style={{ minHeight: 'unset' }}>
         <ul className="pr-4 nav nav-navbar navbar-right">
           <li>
-            <Button bsSize="small" onClick={ () => this.refs.handsontableModal.show(this.getMarkDownTable()) }><i className="icon-grid"></i></Button>
+            <Button bsSize="small" onClick={ () => this.refs.handsontableModal.show(mtu.getMarkdownTable(this.getEditorSubstance().getCodeMirror())) }><i className="icon-grid"></i></Button>
           </li>
         </ul>
       </div>
     );
   }
 
-  /**
-   * TODO return null if the cursor not in a table
-   */
-  getMarkDownTable() {
-    const cm = this.getEditorSubstance().getCodeMirror();
-    return MarkdownTable.fromMarkdownString(mtu.getStrFromBotToEot(cm));
-  }
-
-  /**
-   * TODO move this func to a proper class
-   */
-  replaceMarkDownTable(markdownTable) {
-    const cm = this.getEditorSubstance().getCodeMirror();
-    const curPos = cm.getCursor();
-    cm.getDoc().replaceRange(markdownTable.toString(), mtu.getBot(cm), mtu.getEot(cm));
-    cm.getDoc().setCursor(curPos.line + 1, 2);
-  }
-
   render() {
     const flexContainer = {
       height: '100%',
@@ -293,7 +273,7 @@ export default class Editor extends AbstractEditor {
           </span>
         </button>
 
-        <HandsontableModal ref='handsontableModal' onSave={ this.replaceMarkDownTable }/>
+        <HandsontableModal ref='handsontableModal' onSave={ table => mtu.replaceMarkdownTable(this.getEditorSubstance().getCodeMirror(), table) }/>
 
       </div>
     );

+ 10 - 12
src/client/js/components/PageEditor/MarkdownTableUtil.js

@@ -18,7 +18,8 @@ class MarkdownTableUtil {
     this.getStrFromBot = this.getStrFromBot.bind(this);
     this.getStrToEot = this.getStrToEot.bind(this);
 
-    this.replaceMarkdownTableWithReformed = this.replaceMarkdownTableWithReformed.bind(this);
+    this.replaceMarkdownTable = this.replaceMarkdownTable.bind(this);
+    this.replaceMarkdownTableWithReformed = this.replaceMarkdownTable; // alias
   }
 
   /**
@@ -83,10 +84,11 @@ class MarkdownTableUtil {
   }
 
   /**
-   * return strings from BOT(beginning of table) to EOT(end of table)
+   * return MarkdownTable instance of the table where cursor exists
    */
-  getStrFromBotToEot(editor) {
-    return editor.getDoc().getRange(this.getBot(editor), this.getEot(editor));
+  getMarkdownTable(editor) {
+    const strFromBotToEot = editor.getDoc().getRange(this.getBot(editor), this.getEot(editor));
+    return MarkdownTable.fromMarkdownString(strFromBotToEot);
   }
 
   /**
@@ -129,17 +131,13 @@ class MarkdownTableUtil {
   }
 
   /**
-   * replace markdown table which is reformed by markdown-table
+   * replace markdown table
+   * (table is reformed by markdown-table)
    * @param {MarkdownTable} markdown table
    */
-  replaceMarkdownTableWithReformed(editor, table) {
+  replaceMarkdownTable(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().replaceRange(table.toString(), this.getBot(editor), this.getEot(editor));
     editor.getDoc().setCursor(curPos.line + 1, 2);
   }
 }