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

integrate functions to class xxHelper for util, and xxInterceptor for interceptor.

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

+ 4 - 4
resource/js/components/PageEditor/Editor.js

@@ -38,8 +38,8 @@ import emojiAutoCompleteHelper from './EmojiAutoCompleteHelper';
 
 
 import InterceptorManager from '../../../../lib/util/interceptor-manager';
 import InterceptorManager from '../../../../lib/util/interceptor-manager';
 
 
-import AbortContinueMarkdownListInterceptor from '../../util/interceptor/AbortContinueMarkdownListInterceptor';
-import ReformMarkdownTableInterceptor from '../../util/interceptor/ReformMarkdownTableInterceptor';
+import MarkdownListHelper from './MarkdownListHelper';
+import MarkdownTableHelper from './MarkdownTableHelper';
 
 
 export default class Editor extends React.Component {
 export default class Editor extends React.Component {
 
 
@@ -51,8 +51,8 @@ export default class Editor extends React.Component {
 
 
     this.interceptorManager = new InterceptorManager();
     this.interceptorManager = new InterceptorManager();
     this.interceptorManager.addInterceptors([
     this.interceptorManager.addInterceptors([
-      new AbortContinueMarkdownListInterceptor(),
-      new ReformMarkdownTableInterceptor(),
+      new MarkdownListHelper(),
+      new MarkdownTableHelper(),
     ]);
     ]);
 
 
     this.state = {
     this.state = {

+ 44 - 55
resource/js/components/PageEditor/MarkdownListHelper.js

@@ -1,38 +1,65 @@
+import { BasicInterceptor } from 'crowi-pluginkit';
 import * as codemirror from 'codemirror';
 import * as codemirror from 'codemirror';
 
 
-class MarkdownListHelper {
+import mlu from '../../util/interceptor/MarkdownListUtil';
+
+export default class MarkdownListHelper extends BasicInterceptor {
 
 
   constructor() {
   constructor() {
+    super();
+
     // https://github.com/codemirror/CodeMirror/blob/c7853a989c77bb9f520c9c530cbe1497856e96fc/addon/edit/continuelist.js#L14
     // https://github.com/codemirror/CodeMirror/blob/c7853a989c77bb9f520c9c530cbe1497856e96fc/addon/edit/continuelist.js#L14
     // https://regex101.com/r/7BN2fR/5
     // https://regex101.com/r/7BN2fR/5
     this.indentAndMarkRE = /^(\s*)(>[> ]*|[*+-] \[[x ]\]\s|[*+-]\s|(\d+)([.)]))(\s*)/;
     this.indentAndMarkRE = /^(\s*)(>[> ]*|[*+-] \[[x ]\]\s|[*+-]\s|(\d+)([.)]))(\s*)/;
     this.indentAndMarkOnlyRE = /^(\s*)(>[> ]*|[*+-] \[[x ]\]|[*+-]|(\d+)[.)])(\s*)$/;
     this.indentAndMarkOnlyRE = /^(\s*)(>[> ]*|[*+-] \[[x ]\]|[*+-]|(\d+)[.)])(\s*)$/;
 
 
-    this.newlineAndIndentContinueMarkdownList = this.newlineAndIndentContinueMarkdownList.bind(this);
     this.pasteText = this.pasteText.bind(this);
     this.pasteText = this.pasteText.bind(this);
+  }
 
 
-    this.getBol = this.getBol.bind(this);
-    this.getEol = this.getEol.bind(this);
-    this.getStrFromBol = this.getStrFromBol.bind(this);
-    this.getStrToEol = this.getStrToEol.bind(this);
+  /**
+   * @inheritdoc
+   */
+  isInterceptWhen(contextName) {
+    return (
+      contextName === 'preHandleEnter'
+    );
   }
   }
 
 
   /**
   /**
-   * wrap codemirror.commands.newlineAndIndentContinueMarkdownList
-   * @param {any} editor An editor instance of CodeMirror
+   * return boolean value whether processable parallel
    */
    */
-  newlineAndIndentContinueMarkdownList(editor) {
-    // get strings from current position to EOL(end of line) before break the line
-    const strToEol = this.getStrToEol(editor);
+  isProcessableParallel() {
+    return false;
+  }
+
+  /**
+   * @inheritdoc
+   */
+  process(contextName, ...args) {
+    console.log(performance.now() + ': AbortContinueMarkdownListInterceptor.process is started');
+    const orgContext = args[0];
+    const editor = orgContext.editor;
+
+    console.log('AbortContinueMarkdownListInterceptor.process');
 
 
+    // get strings from current position to EOL(end of line) before break the line
+    const strToEol = mlu.getStrToEol(editor);
     if (this.indentAndMarkRE.test(strToEol)) {
     if (this.indentAndMarkRE.test(strToEol)) {
+      const context = Object.assign(args[0]);   // clone
+
+      console.log('AbortContinueMarkdownListInterceptor.newlineAndIndentContinueMarkdownList: abort auto indent');
       codemirror.commands.newlineAndIndent(editor);
       codemirror.commands.newlineAndIndent(editor);
       // replace the line with strToEol (abort auto indent)
       // replace the line with strToEol (abort auto indent)
-      editor.getDoc().replaceRange(strToEol, this.getBol(editor), this.getEol(editor));
-    }
-    else {
-      codemirror.commands.newlineAndIndentContinueMarkdownList(editor);
+      editor.getDoc().replaceRange(strToEol, mlu.getBol(editor), mlu.getEol(editor));
+
+      // report to manager that handling was done
+      context.handlers.push(this.className);
     }
     }
+
+    console.log(performance.now() + ': AbortContinueMarkdownListInterceptor.process is finished');
+
+    // resolve
+    return Promise.resolve(orgContext);
   }
   }
 
 
   /**
   /**
@@ -43,7 +70,7 @@ class MarkdownListHelper {
    */
    */
   pasteText(editor, event, text) {
   pasteText(editor, event, text) {
     // get strings from BOL(beginning of line) to current position
     // get strings from BOL(beginning of line) to current position
-    const strFromBol = this.getStrFromBol(editor);
+    const strFromBol = mlu.getStrFromBol(editor);
 
 
     const matched = strFromBol.match(this.indentAndMarkRE);
     const matched = strFromBol.match(this.indentAndMarkRE);
     // when match indentAndMarkOnlyRE
     // when match indentAndMarkOnlyRE
@@ -54,7 +81,7 @@ class MarkdownListHelper {
       // replace
       // replace
       if (adjusted != null) {
       if (adjusted != null) {
         event.preventDefault();
         event.preventDefault();
-        editor.getDoc().replaceRange(adjusted, this.getBol(editor), editor.getCursor());
+        editor.getDoc().replaceRange(adjusted, mlu.getBol(editor), editor.getCursor());
       }
       }
     }
     }
   }
   }
@@ -126,42 +153,4 @@ class MarkdownListHelper {
 
 
     return isListful;
     return isListful;
   }
   }
-
-  /**
-   * return the postion of the BOL(beginning of line)
-   */
-  getBol(editor) {
-    const curPos = editor.getCursor();
-    return { line: curPos.line, ch: 0 };
-  }
-
-  /**
-   * return the postion of the EOL(end of line)
-   */
-  getEol(editor) {
-    const curPos = editor.getCursor();
-    const lineLength = editor.getDoc().getLine(curPos.line).length;
-    return { line: curPos.line, ch: lineLength };
-  }
-
-  /**
-   * return strings from BOL(beginning of line) to current position
-   */
-  getStrFromBol(editor) {
-    const curPos = editor.getCursor();
-    return editor.getDoc().getRange(this.getBol(editor), curPos);
-  }
-
-  /**
-   * return strings from current position to EOL(end of line)
-   */
-  getStrToEol(editor) {
-    const curPos = editor.getCursor();
-    return editor.getDoc().getRange(curPos, this.getEol(editor));
-  }
 }
 }
-
-// singleton pattern
-const instance = new MarkdownListHelper();
-Object.freeze(instance);
-export default instance;

+ 78 - 0
resource/js/components/PageEditor/MarkdownTableHelper.js

@@ -0,0 +1,78 @@
+import { BasicInterceptor } from 'crowi-pluginkit';
+import * as codemirror from 'codemirror';
+import markdownTable from 'markdown-table';
+
+import mtu from '../../util/interceptor/MarkdownTableUtil';
+
+/**
+ * Utility for markdown table
+ */
+export default class MarkdownTableUtil extends BasicInterceptor {
+
+  constructor() {
+    super();
+
+    // https://github.com/markdown-it/markdown-it/blob/d29f421927e93e88daf75f22089a3e732e195bd2/lib/rules_block/table.js#L83
+    // https://regex101.com/r/7BN2fR/7
+    this.tableAlignmentLineRE = /^[-:|][-:|\s]*$/;
+    this.linePartOfTableRE = /^\|[^\r\n]*|[^\r\n]*\|$|([^\|\r\n]+\|[^\|\r\n]*)+/; // own idea
+  }
+
+  /**
+   * @inheritdoc
+   */
+  isInterceptWhen(contextName) {
+    return (
+      contextName === 'preHandleEnter'
+    );
+  }
+
+  /**
+   * return boolean value whether processable parallel
+   */
+  isProcessableParallel() {
+    return false;
+  }
+
+  /**
+   * @inheritdoc
+   */
+  process(contextName, ...args) {
+    console.log(performance.now() + ': ReformMarkdownTableInterceptor.process is started');
+
+    const orgContext = args[0];
+    const editor = orgContext.editor;
+
+    // get strings from BOL(beginning of line) to current position
+    const strFromBol = mtu.getStrFromBol(editor);
+
+    if (this.linePartOfTableRE.test(strFromBol)) {
+      const context = Object.assign(args[0]);   // clone
+      const editor = context.editor;
+
+      console.log('MarkdownTableHelper.process');
+
+      // get lines all of table from current position to beginning of table
+      const strTableLines = mtu.getStrFromBot(editor);
+      console.log('strTableLines: ' + strTableLines);
+
+      const table = mtu.parseFromTableStringToJSON(editor, mtu.getBot(editor), editor.getCursor());
+      console.log('table: ' + JSON.stringify(table));
+      const strTableLinesFormated = table;
+      console.log('strTableLinesFormated: ' + strTableLinesFormated);
+
+      // replace the lines to strFormatedTableLines
+      editor.getDoc().replaceRange(strTableLinesFormated, mtu.getBot(editor), editor.getCursor());
+      codemirror.commands.newlineAndIndent(editor);
+
+      // report to manager that handling was done
+      context.handlers.push(this.className);
+    }
+
+    console.log(performance.now() + ': ReformMarkdownTableInterceptor.process is finished');
+
+    // resolve
+    // return Promise.resolve(context);
+    return Promise.resolve(orgContext);
+  }
+}

+ 7 - 54
resource/js/util/interceptor/AbortContinueMarkdownListInterceptor.js → resource/js/util/interceptor/MarkdownListUtil.js

@@ -1,17 +1,11 @@
-import { BasicInterceptor } from 'crowi-pluginkit';
-
 import * as codemirror from 'codemirror';
 import * as codemirror from 'codemirror';
 
 
-import markdownTable from 'markdown-table';
-
 /**
 /**
- * The interceptor that reform markdown table
+ * Utility for markdown list
  */
  */
-export default class AbortContinueMarkdownListInterceptor extends BasicInterceptor {
+class MarkdownListUtil {
 
 
   constructor() {
   constructor() {
-    super();
-
     // https://github.com/codemirror/CodeMirror/blob/c7853a989c77bb9f520c9c530cbe1497856e96fc/addon/edit/continuelist.js#L14
     // https://github.com/codemirror/CodeMirror/blob/c7853a989c77bb9f520c9c530cbe1497856e96fc/addon/edit/continuelist.js#L14
     // https://regex101.com/r/7BN2fR/5
     // https://regex101.com/r/7BN2fR/5
     this.indentAndMarkRE = /^(\s*)(>[> ]*|[*+-] \[[x ]\]\s|[*+-]\s|(\d+)([.)]))(\s*)/;
     this.indentAndMarkRE = /^(\s*)(>[> ]*|[*+-] \[[x ]\]\s|[*+-]\s|(\d+)([.)]))(\s*)/;
@@ -25,52 +19,6 @@ export default class AbortContinueMarkdownListInterceptor extends BasicIntercept
     this.getStrToEol = this.getStrToEol.bind(this);
     this.getStrToEol = this.getStrToEol.bind(this);
   }
   }
 
 
-  /**
-   * @inheritdoc
-   */
-  isInterceptWhen(contextName) {
-    return (
-      contextName === 'preHandleEnter'
-    );
-  }
-
-  /**
-   * return boolean value whether processable parallel
-   */
-  isProcessableParallel() {
-    return false;
-  }
-
-  /**
-   * @inheritdoc
-   */
-  process(contextName, ...args) {
-    console.log(performance.now() + ': AbortContinueMarkdownListInterceptor.process is started');
-    const orgContext = args[0];
-    const editor = orgContext.editor;
-
-    console.log('AbortContinueMarkdownListInterceptor.process');
-
-    // get strings from current position to EOL(end of line) before break the line
-    const strToEol = this.getStrToEol(editor);
-    if (this.indentAndMarkRE.test(strToEol)) {
-      const context = Object.assign(args[0]);   // clone
-
-      console.log('AbortContinueMarkdownListInterceptor.newlineAndIndentContinueMarkdownList: abort auto indent');
-      codemirror.commands.newlineAndIndent(editor);
-      // replace the line with strToEol (abort auto indent)
-      editor.getDoc().replaceRange(strToEol, this.getBol(editor), this.getEol(editor));
-
-      // report to manager that handling was done
-      context.handlers.push(this.className);
-    }
-
-    console.log(performance.now() + ': AbortContinueMarkdownListInterceptor.process is finished');
-
-    // resolve
-    return Promise.resolve(orgContext);
-  }
-
   /**
   /**
    * paste text
    * paste text
    * @param {any} editor An editor instance of CodeMirror
    * @param {any} editor An editor instance of CodeMirror
@@ -196,3 +144,8 @@ export default class AbortContinueMarkdownListInterceptor extends BasicIntercept
     return editor.getDoc().getRange(curPos, this.getEol(editor));
     return editor.getDoc().getRange(curPos, this.getEol(editor));
   }
   }
 }
 }
+
+// singleton pattern
+const instance = new MarkdownListUtil();
+Object.freeze(instance);
+export default instance;

+ 7 - 64
resource/js/util/interceptor/ReformMarkdownTableInterceptor.js → resource/js/util/interceptor/MarkdownTableUtil.js

@@ -1,17 +1,13 @@
-import { BasicInterceptor } from 'crowi-pluginkit';
-
 import * as codemirror from 'codemirror';
 import * as codemirror from 'codemirror';
 
 
 import markdownTable from 'markdown-table';
 import markdownTable from 'markdown-table';
 
 
 /**
 /**
- * The interceptor that reform markdown table
+ * Utility for markdown table
  */
  */
-export default class ReformMarkdownTableInterceptor extends BasicInterceptor {
+class MarkdownTableUtil {
 
 
   constructor() {
   constructor() {
-    super();
-
     // 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]*$/;
@@ -26,64 +22,6 @@ export default class ReformMarkdownTableInterceptor extends BasicInterceptor {
     this.parseFromTableStringToJSON = this.parseFromTableStringToJSON.bind(this);
     this.parseFromTableStringToJSON = this.parseFromTableStringToJSON.bind(this);
   }
   }
 
 
-  /**
-   * @inheritdoc
-   */
-  isInterceptWhen(contextName) {
-    return (
-      contextName === 'preHandleEnter'
-    );
-  }
-
-  /**
-   * return boolean value whether processable parallel
-   */
-  isProcessableParallel() {
-    return false;
-  }
-
-  /**
-   * @inheritdoc
-   */
-  process(contextName, ...args) {
-    console.log(performance.now() + ': ReformMarkdownTableInterceptor.process is started');
-
-    const orgContext = args[0];
-    const editor = orgContext.editor;
-
-    // get strings from BOL(beginning of line) to current position
-    const strFromBol = this.getStrFromBol(editor);
-
-    if (this.linePartOfTableRE.test(strFromBol)) {
-      const context = Object.assign(args[0]);   // clone
-      const editor = context.editor;
-
-      console.log('MarkdownTableHelper.process');
-
-      // get lines all of table from current position to beginning of table
-      const strTableLines = this.getStrFromBot(editor);
-      console.log('strTableLines: ' + strTableLines);
-
-      const table = this.parseFromTableStringToJSON(editor, this.getBot(editor), editor.getCursor());
-      console.log('table: ' + JSON.stringify(table));
-      const strTableLinesFormated = table;
-      console.log('strTableLinesFormated: ' + strTableLinesFormated);
-
-      // replace the lines to strFormatedTableLines
-      editor.getDoc().replaceRange(strTableLinesFormated, this.getBot(editor), editor.getCursor());
-      codemirror.commands.newlineAndIndent(editor);
-
-      // report to manager that handling was done
-      context.handlers.push(this.className);
-    }
-
-    console.log(performance.now() + ': ReformMarkdownTableInterceptor.process is finished');
-
-    // resolve
-    // return Promise.resolve(context);
-    return Promise.resolve(orgContext);
-  }
-
   /**
   /**
    * return the postion of the BOT(beginning of table)
    * return the postion of the BOT(beginning of table)
    * (It is assumed that current line is a part of table)
    * (It is assumed that current line is a part of table)
@@ -195,3 +133,8 @@ export default class ReformMarkdownTableInterceptor extends BasicInterceptor {
     return markdownTable(contents, { align: aligns } );
     return markdownTable(contents, { align: aligns } );
   }
   }
 }
 }
+
+// singleton pattern
+const instance = new MarkdownTableUtil();
+Object.freeze(instance);
+export default instance;