# Cursor is overriden when upload finishes

**URL:** https://meta.discourse.org/t/cursor-is-overriden-when-upload-finishes/54892
**Category:** Feature
**Created:** [28 Dezembro , 2016 17:55 UTC](https://meta.discourse.org/t/cursor-is-overriden-when-upload-finishes/54892 "2016-12-28T17:55:27Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![claas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/claas/32/71725_2.png) [@claas](https://meta.discourse.org/u/claas)
#### Post date: [28 Dezembro , 2016 17:55 UTC](https://meta.discourse.org/t/cursor-is-overriden-when-upload-finishes/54892/1 "2016-12-28T17:55:27Z")

</div>

1. I’m writing a post and upload a photo (which is large or which takes long due to slow internet connection)…  
 ![](https://global.discourse-cdn.com/meta/original/3X/5/b/5b4433bfc4abcd780cae4e79564bacb0a3dfa87e.png)
2. While it uploads, I continue writing…  
 ![](https://global.discourse-cdn.com/meta/original/3X/2/a/2a8e3ba1ead55639ca24cc409ea365e81db6453e.png)
3. Once the download finishes, my cursor lands behind the image!  
 ![](https://global.discourse-cdn.com/meta/original/3X/9/8/9801a6ad6aa9f66ceffb61e17085c8dd18dd0ffd.png)

Expectation: The cursor should be preserved.

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [28 Dezembro , 2016 18:00 UTC](https://meta.discourse.org/t/cursor-is-overriden-when-upload-finishes/54892/2 "2016-12-28T18:00:28Z")

</div>

Isn’t this expected since the text has to be replaced when the upload is complete? That’s not a bug.

---

<div class="post-metadata">

### Author: ![claas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/claas/32/71725_2.png) [@claas](https://meta.discourse.org/u/claas)
#### Post date: [28 Dezembro , 2016 18:05 UTC](https://meta.discourse.org/t/cursor-is-overriden-when-upload-finishes/54892/3 "2016-12-28T18:05:31Z")

</div>

> [@codinghorror](#):
>
> Isn’t this expected since the text has to be replaced when the upload is complete? That’s not a bug.

It’s certainly a side effect of replacing the text, but it’s also not so nice for the user. I think we could save the cursor position before we replace the text and restore it thereafter.

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [28 Dezembro , 2016 18:16 UTC](https://meta.discourse.org/t/cursor-is-overriden-when-upload-finishes/54892/4 "2016-12-28T18:16:42Z")

</div>

Still if you are actively typing when that happens weird stuff is gonna happen.

---

<div class="post-metadata">

### Author: ![claas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/claas/32/71725_2.png) [@claas](https://meta.discourse.org/u/claas)
#### Post date: [28 Dezembro , 2016 18:34 UTC](https://meta.discourse.org/t/cursor-is-overriden-when-upload-finishes/54892/5 "2016-12-28T18:34:41Z")

</div>

> [@codinghorror](#):
>
> Still if you are actively typing when that happens weird stuff is gonna happen.

Just tried it out a couple of times and all I could reproduce is that, if I type very fast, one character is omitted. Is this what you refer to as “weird stuff”?

I would say though that these are two phenomena that add up and getting rid of each of them is desirable, or isn’t it?

---

<div class="post-metadata">

### Author: ![claas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/claas/32/71725_2.png) [@claas](https://meta.discourse.org/u/claas)
#### Post date: [28 Dezembro , 2016 20:28 UTC](https://meta.discourse.org/t/cursor-is-overriden-when-upload-finishes/54892/6 "2016-12-28T20:28:57Z")

</div>

So the upload placeholder replacement is triggered in `composer-editor.js.es6` [here](https://github.com/discourse/discourse/blob/52cd9972bb832a32a51c014ad6096a1bd01ecdcd/app/assets/javascripts/discourse/components/composer-editor.js.es6#L211) (on cancel) and [here](https://github.com/discourse/discourse/blob/52cd9972bb832a32a51c014ad6096a1bd01ecdcd/app/assets/javascripts/discourse/components/composer-editor.js.es6#L265) (on success) and handled by `d-editor.js.es6` [here](https://github.com/discourse/discourse/blob/7515f4eec20abb5c869434c2ccb2541127628969/app/assets/javascripts/discourse/components/d-editor.js.es6#L249) or here respectively:

> <https://github.com/discourse/discourse/blob/7515f4eec20abb5c869434c2ccb2541127628969/app/assets/javascripts/discourse/components/d-editor.js.es6#L526-L533>

This would be the place where we _could_ preserve the cursor position, given that we replace the text in a _synchronous_ fashion (\*):

- If the cursor position is before the needle, preserve it.
- If the cursor position is in the needle, set it to the end of the replacement.
- If the cursor position is behind the needle, add `(replacement.length - needle.length)` to the cursor.

(\*) = …but `_setSelection` (which is used by `_replaceText` to _explicitly_ set the cursor behind the image) works [in an asynchronous fashion](https://github.com/discourse/discourse/blob/7515f4eec20abb5c869434c2ccb2541127628969/app/assets/javascripts/discourse/components/d-editor.js.es6#L417-L429).

---

<div class="post-metadata">

### Author: ![claas](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/claas/32/71725_2.png) [@claas](https://meta.discourse.org/u/claas)
#### Post date: [28 Dezembro , 2016 21:28 UTC](https://meta.discourse.org/t/cursor-is-overriden-when-upload-finishes/54892/7 "2016-12-28T21:28:08Z")

</div>

Just in case you change your mind and give it a try (works for me):

[https://github.com/discourse/discourse/pull/4630](https://github.com/discourse/discourse/pull/4630)

PS: Last PR for some time, as vacation ends for me today.

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [28 Dezembro , 2016 22:26 UTC](https://meta.discourse.org/t/cursor-is-overriden-when-upload-finishes/54892/8 "2016-12-28T22:26:34Z")

</div>

Sure if @zogstrip likes it we can pull it in.

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [28 Dezembro , 2016 22:27 UTC](https://meta.discourse.org/t/cursor-is-overriden-when-upload-finishes/54892/9 "2016-12-28T22:27:10Z")

</div>

❤ it. Just need to fix the tests and I’ll merge it 😉
