Skip to content

A platform for community discussion. Free, open, simple.

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

Maja Komel142361d6da02
diff --git a/app/assets/javascripts/discourse/components/d-editor.js.es6 b/app/assets/javascripts/discourse/components/d-editor.js.es6
index 1293d032d42..f48836be14b 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 19dedaf2bcb..a034c7aa449 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);