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

impl getStrFromBolToSelectedUpperPos method

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

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

@@ -49,6 +49,13 @@ export default class AbstractEditor extends React.Component {
     throw new Error('this method should be impelemented in subclass');
     throw new Error('this method should be impelemented in subclass');
   }
   }
 
 
+  /**
+   * return strings from BOL(beginning of line) to current position
+   */
+  getStrFromBolToSelectedUpperPos() {
+    throw new Error('this method should be impelemented in subclass');
+  }
+
   /**
   /**
    * replace Beggining Of Line to current position with param 'text'
    * replace Beggining Of Line to current position with param 'text'
    * @param {string} text
    * @param {string} text

+ 35 - 0
resource/js/components/PageEditor/CodeMirrorEditor.js

@@ -176,6 +176,15 @@ export default class CodeMirrorEditor extends AbstractEditor {
     return editor.getDoc().getRange(curPos, this.getEol());
     return editor.getDoc().getRange(curPos, this.getEol());
   }
   }
 
 
+  /**
+   * @inheritDoc
+   */
+  getStrFromBolToSelectedUpperPos() {
+    const editor = this.getCodeMirror();
+    const pos = this.selectUpperPos(editor.getCursor('from'), editor.getCursor('to'));
+    return editor.getDoc().getRange(this.getBol(), pos);
+  }
+
   /**
   /**
    * @inheritDoc
    * @inheritDoc
    */
    */
@@ -211,6 +220,32 @@ export default class CodeMirrorEditor extends AbstractEditor {
     return { line: curPos.line, ch: lineLength };
     return { line: curPos.line, ch: lineLength };
   }
   }
 
 
+  /**
+   * select the upper position of pos1 and pos2
+   * @param {{line: number, ch: number}} pos1
+   * @param {{line: number, ch: number}} pos2
+   */
+  selectUpperPos(pos1, pos2) {
+    // if both is in same line
+    if (pos1.line === pos2.line) {
+      return (pos1.ch < pos2.ch) ? pos1 : pos2;
+    }
+    return (pos1.line < pos2.line) ? pos1 : pos2;
+  }
+
+  /**
+   * select the lower position of pos1 and pos2
+   * @param {{line: number, ch: number}} pos1
+   * @param {{line: number, ch: number}} pos2
+   */
+  selectLowerPos(pos1, pos2) {
+    // if both is in same line
+    if (pos1.line === pos2.line) {
+      return (pos1.ch < pos2.ch) ? pos2 : pos1;
+    }
+    return (pos1.line < pos2.line) ? pos2 : pos1;
+  }
+
   loadCss(source) {
   loadCss(source) {
     return new Promise((resolve) => {
     return new Promise((resolve) => {
       loadCssSync(source);
       loadCssSync(source);

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

@@ -109,6 +109,16 @@ export default class TextAreaEditor extends AbstractEditor {
     return this.textarea.value.substring(currentPos, this.getEolPos());
     return this.textarea.value.substring(currentPos, this.getEolPos());
   }
   }
 
 
+  /**
+   * @inheritDoc
+   */
+  getStrFromBolToSelectedUpperPos() {
+    const startPos = this.textarea.selectionStart;
+    const endPos = this.textarea.selectionEnd;
+    const upperPos = (startPos < endPos) ? startPos : endPos;
+    return this.textarea.value.substring(this.getBolPos(), upperPos);
+  }
+
   /**
   /**
    * @inheritDoc
    * @inheritDoc
    */
    */