summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhil Hughes <me@iamphill.com>2016-06-06 11:59:59 +0100
committerPhil Hughes <me@iamphill.com>2016-06-17 11:52:22 +0100
commit4140c4622f07d8a1793a77ecdf810fbc628568c7 (patch)
treeb9f6fff914d24ba1b89d27272ac11e6e15c2494b
parentd5b331b76bb6838e121e54cdf50122023932576c (diff)
downloadgitlab-ce-4140c4622f07d8a1793a77ecdf810fbc628568c7.tar.gz
Made markdown buttons work on all markdown textareas
Selecting multiple rows & a list correctly creates the selected text into a list
-rw-r--r--app/assets/javascripts/gl_form.js.coffee3
-rw-r--r--app/assets/javascripts/lib/text_utility.js.coffee36
-rw-r--r--app/views/projects/issues/_discussion.html.haml6
-rw-r--r--app/views/projects/merge_requests/_discussion.html.haml6
4 files changed, 27 insertions, 24 deletions
diff --git a/app/assets/javascripts/gl_form.js.coffee b/app/assets/javascripts/gl_form.js.coffee
index d540cc4dc46..77512d187c9 100644
--- a/app/assets/javascripts/gl_form.js.coffee
+++ b/app/assets/javascripts/gl_form.js.coffee
@@ -34,6 +34,8 @@ class @GLForm
# form and textarea event listeners
@addEventListeners()
+ gl.text.init(@form)
+
# hide discard button
@form.find('.js-note-discard').hide()
@@ -42,6 +44,7 @@ class @GLForm
clearEventListeners: ->
@textarea.off 'focus'
@textarea.off 'blur'
+ gl.text.removeListeners(@form)
addEventListeners: ->
@textarea.on 'focus', ->
diff --git a/app/assets/javascripts/lib/text_utility.js.coffee b/app/assets/javascripts/lib/text_utility.js.coffee
index 52ef001894c..bb2772dfed2 100644
--- a/app/assets/javascripts/lib/text_utility.js.coffee
+++ b/app/assets/javascripts/lib/text_utility.js.coffee
@@ -11,8 +11,18 @@
text.substring(textarea.selectionStart, textarea.selectionEnd)
gl.text.insertText = (textArea, text, tag, selected, wrap) ->
+ selectedSplit = selected.split('\n')
startChar = if not wrap and textArea.selectionStart > 0 then '\n' else ''
- insertText = "#{startChar}#{tag}#{selected}#{if wrap then tag else ' '}"
+
+ if selectedSplit.length > 1 and not wrap
+ insertText = selectedSplit.map((val) ->
+ if val.indexOf(tag) is 0
+ "#{val.replace(tag, '')}"
+ else
+ "#{tag}#{val}"
+ ).join('\n')
+ else
+ insertText = "#{startChar}#{tag}#{selected}#{if wrap then tag else ' '}"
if document.queryCommandSupported('insertText')
document.execCommand 'insertText', false, insertText
@@ -51,17 +61,19 @@
@insertText(textArea, text, tag, selected, wrap)
- gl.text.addListeners = ->
+ gl.text.init = (form) ->
self = @
- $('.js-md').on 'click', ->
- $this = $(@)
- self.updateText(
- $this.closest('.md-area').find('textarea'),
- $this.data('md-tag'),
- not $this.data('md-prepend')
- )
-
- gl.text.removeListeners = ->
- $('.js-md').off()
+ $('.js-md', form)
+ .off 'click'
+ .on 'click', ->
+ $this = $(@)
+ self.updateText(
+ $this.closest('.md-area').find('textarea'),
+ $this.data('md-tag'),
+ not $this.data('md-prepend')
+ )
+
+ gl.text.removeListeners = (form) ->
+ $('.js-md', form).off()
) window
diff --git a/app/views/projects/issues/_discussion.html.haml b/app/views/projects/issues/_discussion.html.haml
index 8ed0e3d7e1d..b151393abab 100644
--- a/app/views/projects/issues/_discussion.html.haml
+++ b/app/views/projects/issues/_discussion.html.haml
@@ -5,9 +5,3 @@
#notes
= render 'projects/notes/notes_with_form'
-
-:javascript
- $(function(){
- gl.text.removeListeners();
- gl.text.addListeners();
- }) \ No newline at end of file
diff --git a/app/views/projects/merge_requests/_discussion.html.haml b/app/views/projects/merge_requests/_discussion.html.haml
index c8b6a25952b..393998f15b9 100644
--- a/app/views/projects/merge_requests/_discussion.html.haml
+++ b/app/views/projects/merge_requests/_discussion.html.haml
@@ -6,9 +6,3 @@
= link_to 'Reopen merge request', merge_request_path(@merge_request, merge_request: {state_event: :reopen }), method: :put, class: "btn btn-nr btn-comment btn-grouped btn-reopen reopen-mr-link js-note-target-reopen", title: "Reopen merge request", data: {original_text: "Reopen merge request", alternative_text: "Comment & reopen merge request"}
#notes= render "projects/notes/notes_with_form"
-
-:javascript
- $(function(){
- gl.text.removeListeners();
- gl.text.addListeners();
- }) \ No newline at end of file