# Cursor is overriden when upload finishes

**URL:** <https://meta.discourse.org/t/cursor-is-overriden-when-upload-finishes/54892>\
**Category:** Feature\
**Created:** [12월 28, 2016, 5:55오후 UTC](https://meta.discourse.org/t/cursor-is-overriden-when-upload-finishes/54892 "2016-12-28T17:55:27Z")\
**Posts on this page:** 1\
**Showing post:** 6

<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:** [12월 28, 2016, 8: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).

---

_[View the full topic](https://meta.discourse.org/t/cursor-is-overriden-when-upload-finishes/54892)._
