FIX: Caret moves to a wrong position when uploading an image via toolbar (#15684)

When uploading an image, we change the uploading placeholder several times. Every time, we correct the position of the cursor after replacing. But we schedule repositioning of cursor to the afterRender queue in Ember Run Loop. As a result, sometimes we replace the placeholder several times but correct the cursor position only once at the end.

It just cannot work correctly with scheduling, we'll always be dealing with cumulative error. Removing scheduling fixes the problem.

Sadly, I cannot make the test work, I skipped it for now, going to give it another try later.
This commit is contained in:
Andrei Prigorshnev 2022-02-04 15:26:48 +01:00 committed by GitHub
parent d13117fa05
commit 778abb067f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 14 deletions

View File

@ -477,7 +477,7 @@ export default Component.extend(TextareaTextManipulation, {
key: "#",
afterComplete: (value) => {
this.set("value", value);
return this._focusTextArea();
schedule("afterRender", this, this._focusTextArea);
},
transformComplete: (obj) => {
return obj.text;
@ -504,7 +504,7 @@ export default Component.extend(TextareaTextManipulation, {
key: ":",
afterComplete: (text) => {
this.set("value", text);
this._focusTextArea();
schedule("afterRender", this, this._focusTextArea);
},
onKeyUp: (text, cp) => {

View File

@ -25,18 +25,16 @@ export default Mixin.create({
// ensures textarea scroll position is correct
_focusTextArea() {
schedule("afterRender", () => {
if (!this.element || this.isDestroying || this.isDestroyed) {
return;
}
if (!this.element || this.isDestroying || this.isDestroyed) {
return;
}
if (!this._textarea) {
return;
}
if (!this._textarea) {
return;
}
this._textarea.blur();
this._textarea.focus();
});
this._textarea.blur();
this._textarea.focus();
},
_insertBlock(text) {
@ -171,7 +169,7 @@ export default Mixin.create({
this._$textarea.prop("selectionStart", (pre + text).length + 2);
this._$textarea.prop("selectionEnd", (pre + text).length + 2);
this._focusTextArea();
schedule("afterRender", this, this._focusTextArea);
},
_addText(sel, text, options) {

View File

@ -10,7 +10,7 @@ import bootbox from "bootbox";
import { authorizedExtensions } from "discourse/lib/uploads";
import { click, fillIn, settled, visit } from "@ember/test-helpers";
import I18n from "I18n";
import { test } from "qunit";
import { skip, test } from "qunit";
import { Promise } from "rsvp";
function pretender(server, helper) {
@ -313,6 +313,30 @@ acceptance("Uppy Composer Attachment - Upload Placeholder", function (needs) {
appEvents.trigger("composer:add-files", image);
});
skip("should place cursor properly after inserting a placeholder", async function (assert) {
const appEvents = loggedInUser().appEvents;
const done = assert.async();
await visit("/");
await click("#create-topic");
await fillIn(".d-editor-input", "The image:\ntext after image");
const input = query(".d-editor-input");
input.selectionStart = 10;
input.selectionEnd = 10;
appEvents.on("composer:all-uploads-complete", () => {
// after uploading we have this in the textarea:
// "The image:\n![avatar.PNG|690x320](upload://yoj8pf9DdIeHRRULyw7i57GAYdz.jpeg)\ntext after image"
// cursor should be just before "text after image":
assert.equal(input.selectionStart, 76);
assert.equal(input.selectionEnd, 76);
done();
});
const image = createFile("avatar.png");
appEvents.trigger("composer:add-files", image);
});
test("should be able to paste a table with files and not upload the files", async function (assert) {
await visit("/");
await click("#create-topic");