Commit 4140c462 authored by Phil Hughes's avatar Phil Hughes

Made markdown buttons work on all markdown textareas

Selecting multiple rows & a list correctly creates the selected text into a list
parent d5b331b7
...@@ -34,6 +34,8 @@ class @GLForm ...@@ -34,6 +34,8 @@ class @GLForm
# form and textarea event listeners # form and textarea event listeners
@addEventListeners() @addEventListeners()
gl.text.init(@form)
# hide discard button # hide discard button
@form.find('.js-note-discard').hide() @form.find('.js-note-discard').hide()
...@@ -42,6 +44,7 @@ class @GLForm ...@@ -42,6 +44,7 @@ class @GLForm
clearEventListeners: -> clearEventListeners: ->
@textarea.off 'focus' @textarea.off 'focus'
@textarea.off 'blur' @textarea.off 'blur'
gl.text.removeListeners(@form)
addEventListeners: -> addEventListeners: ->
@textarea.on 'focus', -> @textarea.on 'focus', ->
......
...@@ -11,8 +11,18 @@ ...@@ -11,8 +11,18 @@
text.substring(textarea.selectionStart, textarea.selectionEnd) text.substring(textarea.selectionStart, textarea.selectionEnd)
gl.text.insertText = (textArea, text, tag, selected, wrap) -> gl.text.insertText = (textArea, text, tag, selected, wrap) ->
selectedSplit = selected.split('\n')
startChar = if not wrap and textArea.selectionStart > 0 then '\n' else '' 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') if document.queryCommandSupported('insertText')
document.execCommand 'insertText', false, insertText document.execCommand 'insertText', false, insertText
...@@ -51,17 +61,19 @@ ...@@ -51,17 +61,19 @@
@insertText(textArea, text, tag, selected, wrap) @insertText(textArea, text, tag, selected, wrap)
gl.text.addListeners = -> gl.text.init = (form) ->
self = @ self = @
$('.js-md').on 'click', -> $('.js-md', form)
$this = $(@) .off 'click'
self.updateText( .on 'click', ->
$this.closest('.md-area').find('textarea'), $this = $(@)
$this.data('md-tag'), self.updateText(
not $this.data('md-prepend') $this.closest('.md-area').find('textarea'),
) $this.data('md-tag'),
not $this.data('md-prepend')
gl.text.removeListeners = -> )
$('.js-md').off()
gl.text.removeListeners = (form) ->
$('.js-md', form).off()
) window ) window
...@@ -5,9 +5,3 @@ ...@@ -5,9 +5,3 @@
#notes #notes
= render 'projects/notes/notes_with_form' = render 'projects/notes/notes_with_form'
:javascript
$(function(){
gl.text.removeListeners();
gl.text.addListeners();
})
\ No newline at end of file
...@@ -6,9 +6,3 @@ ...@@ -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"} = 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" #notes= render "projects/notes/notes_with_form"
:javascript
$(function(){
gl.text.removeListeners();
gl.text.addListeners();
})
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment