FIX: don't steal focus when text in editor is replaced (#6712)

FIX: don’t steal focus when text in editor is replaced (#6712)

From 142361d6da0215b6a12f4b8051afc156ba88a0a0 Mon Sep 17 00:00:00 2001
From: Maja Komel <maja.komel@gmail.com>
Date: Mon, 3 Dec 2018 09:49:17 +0100
Subject: [PATCH] FIX: don't steal focus when text in editor is replaced
 (#6712)


diff --git a/app/assets/javascripts/discourse/components/d-editor.js.es6 b/app/assets/javascripts/discourse/components/d-editor.js.es6
index 1293d03..f48836b 100644
--- a/app/assets/javascripts/discourse/components/d-editor.js.es6
+++ b/app/assets/javascripts/discourse/components/d-editor.js.es6
@@ -676,8 +676,13 @@ export default Ember.Component.extend({
     // Replace value (side effect: cursor at the end).
     this.set("value", val.replace(oldVal, newVal));
 
-    // Restore cursor.
-    this._selectText(newSelection.start, newSelection.end - newSelection.start);
+    if ($("textarea.d-editor-input").is(":focus")) {
+      // Restore cursor.
+      this._selectText(
+        newSelection.start,
+        newSelection.end - newSelection.start
+      );
+    }
   },
 
   _addBlock(sel, text) {
diff --git a/test/javascripts/components/d-editor-test.js.es6 b/test/javascripts/components/d-editor-test.js.es6
index 19dedaf..a034c7a 100644
--- a/test/javascripts/components/d-editor-test.js.es6
+++ b/test/javascripts/components/d-editor-test.js.es6
@@ -804,6 +804,10 @@ composerTestCase("replace-text event for composer", async function(assert) {
       assert,
       textarea
     ) {
+      const focusEvent = $.Event("focus");
+      const $input = $('textarea.d-editor-input');
+      $input.trigger(focusEvent);
+
       this.set("value", BEFORE);
       await setSelection(textarea, CASE.before);

GitHub

1 Like

This commit has been mentioned on Discourse Meta. There might be relevant details there:

1 Like