Commit cb44d394 authored by RawKStar77's avatar RawKStar77

Rename variables

Went with Aron's suggestion of suffixing raw nodes with 'El,' and got rid of the
variable 'ourIframeSelection' as it has nothing to do with iframes and the code
could be written without the need for that variable.

input -> inputEl
textarea -> input
parent 410e24a3
...@@ -11,21 +11,20 @@ markdown = ['$filter', '$timeout', '$window', ($filter, $timeout, $window) -> ...@@ -11,21 +11,20 @@ markdown = ['$filter', '$timeout', '$window', ($filter, $timeout, $window) ->
link: (scope, elem, attr, ctrl) -> link: (scope, elem, attr, ctrl) ->
return unless ctrl? return unless ctrl?
input = elem.find('textarea') inputEl = elem.find('.js-markdown-input')
output = elem.find('div')[1] input = elem.find('.js-markdown-input')[0]
textarea = input[0] output = elem.find('.js-markdown-preview')[0]
userSelection = -> userSelection = ->
ourIframeSelection = $window.getSelection().toString() if input.selectionStart != undefined
if textarea.selectionStart != undefined startPos = input.selectionStart
startPos = textarea.selectionStart endPos = input.selectionEnd
endPos = textarea.selectionEnd if $window.getSelection().toString()
if ourIframeSelection selectedText = $window.getSelection().toString()
selectedText = ourIframeSelection
else else
selectedText = textarea.value.substring(startPos, endPos) selectedText = input.value.substring(startPos, endPos)
textBefore = textarea.value.substring(0, (startPos)) textBefore = input.value.substring(0, (startPos))
textAfter = textarea.value.substring(endPos) textAfter = input.value.substring(endPos)
selection = { selection = {
before: textBefore before: textBefore
after: textAfter after: textAfter
...@@ -36,13 +35,13 @@ markdown = ['$filter', '$timeout', '$window', ($filter, $timeout, $window) -> ...@@ -36,13 +35,13 @@ markdown = ['$filter', '$timeout', '$window', ($filter, $timeout, $window) ->
return selection return selection
insertMarkup = (value, selectionStart, selectionEnd) -> insertMarkup = (value, selectionStart, selectionEnd) ->
# New value is set for the textarea # New value is set for the input
textarea.value = value input.value = value
# A new selection is set, or the cursor is positioned inside the textarea. # A new selection is set, or the cursor is positioned inside the input.
textarea.selectionStart = selectionStart input.selectionStart = selectionStart
textarea.selectionEnd = selectionEnd input.selectionEnd = selectionEnd
# Focus the textarea # Focus the input
textarea.focus() input.focus()
applyInlineMarkup = (markup, innertext)-> applyInlineMarkup = (markup, innertext)->
text = userSelection() text = userSelection()
...@@ -115,7 +114,7 @@ markdown = ['$filter', '$timeout', '$window', ($filter, $timeout, $window) -> ...@@ -115,7 +114,7 @@ markdown = ['$filter', '$timeout', '$window', ($filter, $timeout, $window) ->
newstring = "" newstring = ""
index = text.before.length index = text.before.length
if index == 0 if index == 0
# The selection takes place at the very start of the textarea # The selection takes place at the very start of the input
for char in text.selection for char in text.selection
if char == "\n" if char == "\n"
newstring = newstring + "\n" + markup newstring = newstring + "\n" + markup
...@@ -126,7 +125,7 @@ markdown = ['$filter', '$timeout', '$window', ($filter, $timeout, $window) -> ...@@ -126,7 +125,7 @@ markdown = ['$filter', '$timeout', '$window', ($filter, $timeout, $window) ->
index += 1 index += 1
else else
newlinedetected = false newlinedetected = false
if textarea.value.substring(index - 1).charAt(0) == "\n" if input.value.substring(index - 1).charAt(0) == "\n"
# Look to see if the selection falls at the beginning of a new line. # Look to see if the selection falls at the beginning of a new line.
newstring = newstring + markup newstring = newstring + markup
newlinedetected = true newlinedetected = true
...@@ -158,13 +157,13 @@ markdown = ['$filter', '$timeout', '$window', ($filter, $timeout, $window) -> ...@@ -158,13 +157,13 @@ markdown = ['$filter', '$timeout', '$window', ($filter, $timeout, $window) ->
end = (text.before + text.selection + markup).length end = (text.before + text.selection + markup).length
insertMarkup(value, start, end) insertMarkup(value, start, end)
return return
# Sets textarea value and selection for cases where there are new lines in the selection # Sets input value and selection for cases where there are new lines in the selection
# or the selection is at the start # or the selection is at the start
value = text.before + newstring + text.after value = text.before + newstring + text.after
start = (text.before + newstring).length start = (text.before + newstring).length
end = (text.before + newstring).length end = (text.before + newstring).length
insertMarkup(value, start, end) insertMarkup(value, start, end)
else if textarea.value.substring((text.start - 1 ), text.start) == "\n" else if input.value.substring((text.start - 1 ), text.start) == "\n"
# Edge case, no selection, the cursor is on a new line. # Edge case, no selection, the cursor is on a new line.
value = text.before + markup + text.selection + text.after value = text.before + markup + text.selection + text.after
start = (text.before + markup).length start = (text.before + markup).length
...@@ -234,20 +233,20 @@ markdown = ['$filter', '$timeout', '$window', ($filter, $timeout, $window) -> ...@@ -234,20 +233,20 @@ markdown = ['$filter', '$timeout', '$window', ($filter, $timeout, $window) ->
if !scope.readonly if !scope.readonly
scope.preview = !scope.preview scope.preview = !scope.preview
if scope.preview if scope.preview
output.style.height = textarea.style.height output.style.height = input.style.height
ctrl.$render() ctrl.$render()
else else
textarea.style.height = output.style.height input.style.height = output.style.height
$timeout -> input.focus() $timeout -> inputEl.focus()
# Re-render the markdown when the view needs updating. # Re-render the markdown when the view needs updating.
ctrl.$render = -> ctrl.$render = ->
input.val (ctrl.$viewValue or '') inputEl.val (ctrl.$viewValue or '')
scope.rendered = ($filter 'converter') (ctrl.$viewValue or '') scope.rendered = ($filter 'converter') (ctrl.$viewValue or '')
# React to the changes to the text area # React to the changes to the input
input.bind 'blur change keyup', -> inputEl.bind 'blur change keyup', ->
ctrl.$setViewValue input.val() ctrl.$setViewValue inputEl.val()
scope.$digest() scope.$digest()
# Reset height of output div incase it has been changed. # Reset height of output div incase it has been changed.
...@@ -257,7 +256,7 @@ markdown = ['$filter', '$timeout', '$window', ($filter, $timeout, $window) -> ...@@ -257,7 +256,7 @@ markdown = ['$filter', '$timeout', '$window', ($filter, $timeout, $window) ->
scope.preview = false scope.preview = false
output.style.height = "" output.style.height = ""
ctrl.$render() ctrl.$render()
unless readonly then $timeout -> input.focus() unless readonly then $timeout -> inputEl.focus()
require: '?ngModel' require: '?ngModel'
restrict: 'A' restrict: 'A'
......
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