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

WIP: refactor - bridge Editor and CodeMirrorEditor

Yuki Takei 7 лет назад
Родитель
Сommit
095da300ec
2 измененных файлов с 22 добавлено и 29 удалено
  1. 0 10
      resource/js/components/PageEditor/AbstractEditor.js
  2. 22 19
      resource/js/components/PageEditor/Editor.js

+ 0 - 10
resource/js/components/PageEditor/AbstractEditor.js

@@ -5,16 +5,6 @@ export default class AbstractEditor extends React.Component {
 
 
   constructor(props) {
   constructor(props) {
     super(props);
     super(props);
-
-    this.state = {
-      value: this.props.value,
-    };
-
-    this.forceToFocus = this.forceToFocus.bind(this);
-    this.setCaretLine = this.setCaretLine.bind(this);
-    this.setScrollTopByLine = this.setScrollTopByLine.bind(this);
-    this.insertText = this.insertText.bind(this);
-    this.dispatchSave = this.dispatchSave.bind(this);
   }
   }
 
 
   forceToFocus() {
   forceToFocus() {

+ 22 - 19
resource/js/components/PageEditor/Editor.js

@@ -19,6 +19,10 @@ export default class Editor extends AbstractEditor {
       isUploading: false,
       isUploading: false,
     };
     };
 
 
+    this.getEditorSubstance = this.getEditorSubstance.bind(this);
+
+    this.setCaretLine = this.setCaretLine.bind(this);
+
     this.dragEnterHandler = this.dragEnterHandler.bind(this);
     this.dragEnterHandler = this.dragEnterHandler.bind(this);
     this.dragLeaveHandler = this.dragLeaveHandler.bind(this);
     this.dragLeaveHandler = this.dragLeaveHandler.bind(this);
     this.dropHandler = this.dropHandler.bind(this);
     this.dropHandler = this.dropHandler.bind(this);
@@ -33,42 +37,38 @@ export default class Editor extends AbstractEditor {
     this.setCaretLine(0);
     this.setCaretLine(0);
   }
   }
 
 
+  getEditorSubstance() {
+    return this.props.isMobile
+      ? this.refs.simpleEditor
+      : this.refs.cmEditor;
+  }
+
+  /**
+   * @inheritDoc
+   */
   forceToFocus() {
   forceToFocus() {
-    if (this.props.isMobile) {
-    }
-    else {
-    }
+    this.getEditorSubstance().forceToFocus();
   }
   }
 
 
   /**
   /**
-   * set caret position of codemirror
-   * @param {string} number
+   * @inheritDoc
    */
    */
   setCaretLine(line) {
   setCaretLine(line) {
-    if (this.props.isMobile) {
-    }
-    else {
-    }
+    this.getEditorSubstance().setCaretLine(line);
   }
   }
 
 
   /**
   /**
    * @inheritDoc
    * @inheritDoc
    */
    */
   setScrollTopByLine(line) {
   setScrollTopByLine(line) {
-    if (this.props.isMobile) {
-    }
-    else {
-    }
+    this.getEditorSubstance().setScrollTopByLine(line);
   }
   }
 
 
   /**
   /**
    * @inheritDoc
    * @inheritDoc
    */
    */
   insertText(text) {
   insertText(text) {
-    if (this.props.isMobile) {
-    }
-    else {
-    }
+    this.getEditorSubstance().insertText(text);
   }
   }
 
 
   /**
   /**
@@ -227,6 +227,7 @@ export default class Editor extends AbstractEditor {
           {/* for PC */}
           {/* for PC */}
           { !isMobile &&
           { !isMobile &&
             <CodeMirrorEditor
             <CodeMirrorEditor
+              ref="cmEditor"
               onPasteFiles={this.pasteFilesHandler}
               onPasteFiles={this.pasteFilesHandler}
               onDragEnter={this.dragEnterHandler}
               onDragEnter={this.dragEnterHandler}
               {...this.props}
               {...this.props}
@@ -235,7 +236,9 @@ export default class Editor extends AbstractEditor {
 
 
           {/* for mobile */}
           {/* for mobile */}
           { isMobile &&
           { isMobile &&
-            <FormControl componentClass="textarea" className="textarea-for-mobile"
+            <FormControl
+              ref="simpleEditor"
+              componentClass="textarea" className="textarea-for-mobile"
               inputRef={ref => { this.mobileeditor = ref }}
               inputRef={ref => { this.mobileeditor = ref }}
               defaultValue={this.state.value}
               defaultValue={this.state.value}
               onChange={(e) => {
               onChange={(e) => {