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

WIP: refactor - MarkdownTableInterceptor

Yuki Takei 7 лет назад
Родитель
Сommit
d2634dcb97

+ 1 - 5
resource/js/components/PageEditor/CodeMirrorEditor.js

@@ -82,8 +82,7 @@ export default class CodeMirrorEditor extends AbstractEditor {
     this.interceptorManager = new InterceptorManager();
     this.interceptorManager = new InterceptorManager();
     this.interceptorManager.addInterceptors([
     this.interceptorManager.addInterceptors([
       new MarkdownListInterceptor(),
       new MarkdownListInterceptor(),
-      // TODO refactor
-      // new MarkdownTableInterceptor(),
+      new MarkdownTableInterceptor(),
     ]);
     ]);
 
 
     this.loadedThemeSet = new Set(['eclipse', 'elegant']);   // themes imported in _vendor.scss
     this.loadedThemeSet = new Set(['eclipse', 'elegant']);   // themes imported in _vendor.scss
@@ -306,9 +305,6 @@ export default class CodeMirrorEditor extends AbstractEditor {
    * handle ENTER key
    * handle ENTER key
    */
    */
   handleEnterKey() {
   handleEnterKey() {
-    // TODO refactor
-    // input both of AbstractEditor and CodeMirror
-
     var context = {
     var context = {
       handlers: [],  // list of handlers which process enter key
       handlers: [],  // list of handlers which process enter key
       editor: this,
       editor: this,

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

@@ -28,7 +28,7 @@ export default class MarkdownListInterceptor extends BasicInterceptor {
    */
    */
   process(contextName, ...args) {
   process(contextName, ...args) {
     const context = Object.assign(args[0]);   // clone
     const context = Object.assign(args[0]);   // clone
-    const editor = context.editor;
+    const editor = context.editor;            // AbstractEditor instance
 
 
     // get strings from current position to EOL(end of line) before break the line
     // get strings from current position to EOL(end of line) before break the line
     const strToEol = editor.getStrToEol();
     const strToEol = editor.getStrToEol();

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

@@ -32,25 +32,34 @@ export default class MarkdownTableInterceptor extends BasicInterceptor {
    */
    */
   process(contextName, ...args) {
   process(contextName, ...args) {
     const context = Object.assign(args[0]);   // clone
     const context = Object.assign(args[0]);   // clone
-    const editor = context.editor;            // codemirror
+    const editor = context.editor;            // AbstractEditor instance
+
+    if (editor != null) {
+      if (editor.constructor.name !== 'CodeMirrorEditor') {
+        // resolve
+        return Promise.resolve(context);
+      }
+    }
+
+    const cm = editor.getCodeMirror();
 
 
     // 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 = editor.getStrFromBol();
 
 
-    if (mtu.isEndOfLine(editor) && mtu.linePartOfTableRE.test(strFromBol)) {
+    if (mtu.isEndOfLine(cm) && 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 strFromBot = mtu.getStrFromBot(editor);
+      const strFromBot = mtu.getStrFromBot(cm);
       let table = mtu.parseFromTableStringToMarkdownTable(strFromBot);
       let table = mtu.parseFromTableStringToMarkdownTable(strFromBot);
 
 
       mtu.addRowToMarkdownTable(table);
       mtu.addRowToMarkdownTable(table);
 
 
-      const strToEot = mtu.getStrToEot(editor);
+      const strToEot = mtu.getStrToEot(cm);
       const tableBottom = mtu.parseFromTableStringToMarkdownTable(strToEot);
       const tableBottom = mtu.parseFromTableStringToMarkdownTable(strToEot);
       if (tableBottom.table.length > 0) {
       if (tableBottom.table.length > 0) {
         table = mtu.mergeMarkdownTable([table, tableBottom]);
         table = mtu.mergeMarkdownTable([table, tableBottom]);
       }
       }
 
 
-      mtu.replaceMarkdownTableWithReformed(editor, table);
+      mtu.replaceMarkdownTableWithReformed(cm, table);
 
 
       // report to manager that handling was done
       // report to manager that handling was done
       context.handlers.push(this.className);
       context.handlers.push(this.className);

+ 0 - 9
resource/js/components/PageEditor/MarkdownTableUtil.js

@@ -18,7 +18,6 @@ class MarkdownTableUtil {
     this.getBol = this.getBol.bind(this);
     this.getBol = this.getBol.bind(this);
     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.parseFromTableStringToMarkdownTable = this.parseFromTableStringToMarkdownTable.bind(this);
     this.parseFromTableStringToMarkdownTable = this.parseFromTableStringToMarkdownTable.bind(this);
     this.replaceMarkdownTableWithReformed = this.replaceMarkdownTableWithReformed.bind(this);
     this.replaceMarkdownTableWithReformed = this.replaceMarkdownTableWithReformed.bind(this);
@@ -85,14 +84,6 @@ class MarkdownTableUtil {
     return editor.getDoc().getRange(curPos, this.getEot(editor));
     return editor.getDoc().getRange(curPos, this.getEot(editor));
   }
   }
 
 
-  /**
-   * return strings from BOL(beginning of line) to current position
-   */
-  getStrFromBol(editor) {
-    const curPos = editor.getCursor();
-    return editor.getDoc().getRange(this.getBol(editor), curPos);
-  }
-
   /**
   /**
    * returns markdown table 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