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

impl TextAreaEditor.setCaretLine

Yuki Takei 7 лет назад
Родитель
Сommit
272c39ae19
2 измененных файлов с 12 добавлено и 2 удалено
  1. 10 0
      resource/js/components/PageEditor/TextAreaEditor.js
  2. 2 2
      resource/js/legacy/crowi.js

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

@@ -53,6 +53,9 @@ export default class TextAreaEditor extends AbstractEditor {
    * @inheritDoc
    */
   forceToFocus() {
+    setTimeout(() => {
+      this.textarea.focus();
+    }, 150);
   }
 
   /**
@@ -63,6 +66,13 @@ export default class TextAreaEditor extends AbstractEditor {
       return;
     }
 
+    const lines = this.textarea.value.split('\n').slice(0, line+1);
+    const pos = lines
+        .map(lineStr => lineStr.length + 1) // correct length+1 of each lines
+        .reduce((a, x) => a += x, 0)        // sum
+        - 1;                                // -1
+
+    this.textarea.setSelectionRange(pos, pos);
   }
 
   /**

+ 2 - 2
resource/js/legacy/crowi.js

@@ -83,8 +83,8 @@ Crowi.setCaretLineAndFocusToEditor = function() {
 
   const line = pageEditorDom.getAttribute('data-caret-line');
 
-  if (line != null) {
-    crowi.setCaretLine(line);
+  if (line != null && !isNaN(line)) {
+    crowi.setCaretLine(+line);
     // reset data-caret-line attribute
     pageEditorDom.removeAttribute('data-caret-line');
   }