From fc334e72f40ab5394c70f5cd29829dc3c0b0d485 Mon Sep 17 00:00:00 2001
From: Phil Hughes <me@iamphill.com>
Date: Fri, 29 Jan 2016 10:28:05 +0000
Subject: [PATCH] Adds the markdown link at the cursor poisition

When writing a comment and then dropping an image it will correctly add the image markdown link at the cursor position

Fixes #12822
---
 app/assets/javascripts/dropzone_input.js.coffee | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/app/assets/javascripts/dropzone_input.js.coffee b/app/assets/javascripts/dropzone_input.js.coffee
index c714c0fa939..32a5594a5b1 100644
--- a/app/assets/javascripts/dropzone_input.js.coffee
+++ b/app/assets/javascripts/dropzone_input.js.coffee
@@ -65,8 +65,13 @@ class @DropzoneInput
         return
 
       success: (header, response) ->
+        link_markdown = response.link.markdown
         child = $(dropzone[0]).children("textarea")
-        $(child).val $(child).val() + response.link.markdown + "\n"
+        cursor_pos = child.prop "selectionStart"
+        value = $(child).val()
+        new_text = "#{value.substring(0,  cursor_pos)}#{link_markdown}#{value.substring(cursor_pos, value.length)}"
+
+        $(child).val new_text + "\n"
         return
 
       error: (temp, errorMessage) ->
-- 
GitLab