2016-05-26 22:24:08 -04:00
|
|
|
((w) ->
|
2016-05-26 10:47:36 -04:00
|
|
|
w.gl ?= {}
|
|
|
|
w.gl.text ?= {}
|
|
|
|
|
2016-05-26 22:10:01 -04:00
|
|
|
gl.text.randomString = -> Math.random().toString(36).substring(7)
|
|
|
|
|
2016-05-26 10:47:36 -04:00
|
|
|
gl.text.replaceRange = (s, start, end, substitute) ->
|
|
|
|
s.substring(0, start) + substitute + s.substring(end);
|
|
|
|
|
2016-06-06 03:25:43 -04:00
|
|
|
gl.text.selectedText = (text, textarea) ->
|
|
|
|
text.substring(textarea.selectionStart, textarea.selectionEnd)
|
2016-05-26 10:47:36 -04:00
|
|
|
|
2016-06-06 03:25:43 -04:00
|
|
|
gl.text.insertText = (textArea, text, tag, selected, wrap) ->
|
2016-06-06 06:59:59 -04:00
|
|
|
selectedSplit = selected.split('\n')
|
2016-06-06 03:25:43 -04:00
|
|
|
startChar = if not wrap and textArea.selectionStart > 0 then '\n' else ''
|
2016-06-06 06:59:59 -04:00
|
|
|
|
|
|
|
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 ' '}"
|
2016-06-06 03:25:43 -04:00
|
|
|
|
|
|
|
if document.queryCommandSupported('insertText')
|
|
|
|
document.execCommand 'insertText', false, insertText
|
2016-05-26 22:32:12 -04:00
|
|
|
else
|
2016-06-06 03:25:43 -04:00
|
|
|
try
|
|
|
|
document.execCommand("ms-beginUndoUnit")
|
2016-05-26 22:32:12 -04:00
|
|
|
|
2016-06-06 03:25:43 -04:00
|
|
|
textArea.value = @replaceRange(
|
|
|
|
text,
|
|
|
|
textArea.selectionStart,
|
|
|
|
textArea.selectionEnd,
|
|
|
|
insertText)
|
|
|
|
try
|
|
|
|
document.execCommand("ms-endUndoUnit")
|
2016-05-26 22:10:01 -04:00
|
|
|
|
2016-06-06 03:25:43 -04:00
|
|
|
@moveCursor(textArea, tag, wrap)
|
2016-05-26 22:10:01 -04:00
|
|
|
|
2016-06-06 03:25:43 -04:00
|
|
|
gl.text.moveCursor = (textArea, tag, wrapped) ->
|
|
|
|
return unless textArea.setSelectionRange
|
2016-05-26 22:10:01 -04:00
|
|
|
|
2016-06-06 03:25:43 -04:00
|
|
|
if textArea.selectionStart is textArea.selectionEnd
|
|
|
|
if wrapped
|
|
|
|
pos = textArea.selectionStart - tag.length
|
|
|
|
else
|
|
|
|
pos = textArea.selectionStart
|
|
|
|
|
|
|
|
textArea.setSelectionRange pos, pos
|
2016-05-26 10:47:36 -04:00
|
|
|
|
2016-06-06 03:25:43 -04:00
|
|
|
gl.text.updateText = (textArea, tag, wrap) ->
|
|
|
|
$textArea = $(textArea)
|
|
|
|
oldVal = $textArea.val()
|
|
|
|
textArea = $textArea.get(0)
|
|
|
|
text = $textArea.val()
|
|
|
|
selected = @selectedText(text, textArea)
|
|
|
|
$textArea.focus()
|
2016-05-26 22:10:01 -04:00
|
|
|
|
2016-06-06 03:25:43 -04:00
|
|
|
@insertText(textArea, text, tag, selected, wrap)
|
2016-05-26 10:47:36 -04:00
|
|
|
|
2016-06-06 06:59:59 -04:00
|
|
|
gl.text.init = (form) ->
|
2016-05-26 10:47:36 -04:00
|
|
|
self = @
|
2016-06-06 06:59:59 -04:00
|
|
|
$('.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()
|
2016-05-26 10:47:36 -04:00
|
|
|
|
2016-06-06 03:25:43 -04:00
|
|
|
) window
|