Commit 1e1a1dfd authored by RawKStar77's avatar RawKStar77

Add checks to see if markup has already been inserted

parent 4dd51e34
...@@ -38,11 +38,24 @@ markdown = ['$filter', '$timeout', ($filter, $timeout) -> ...@@ -38,11 +38,24 @@ markdown = ['$filter', '$timeout', ($filter, $timeout) ->
if text.selection == "" if text.selection == ""
newtext = text.before + markup + innertext + markup + text.after newtext = text.before + markup + innertext + markup + text.after
input[0].value = newtext input[0].value = newtext
input[0].selectionStart = (text.before + markup).length
input[0].selectionEnd = (text.before + innertext + markup).length
else else
newtext = text.before + markup + text.selection + markup + text.after # Check to see if markup has already been applied before to the selection.
input[0].value = newtext slice1 = text.before.slice((text.before.length - markup.length))
input[0].selectionStart = (text.before + markup).length slice2 = text.after.slice(0, markup.length)
input[0].selectionEnd = (text.before + innertext + markup).length if slice1 == markup and slice2 == markup
# Remove markup
newtext = text.before.slice(0, (text.before.length - markup.length)) + text.selection + text.after.slice(markup.length)
input[0].value = newtext
input[0].selectionStart = text.before.length - markup.length
input[0].selectionEnd = (text.before + text.selection).length - markup.length
else
# Apply markup
newtext = text.before + markup + text.selection + markup + text.after
input[0].value = newtext
input[0].selectionStart = (text.before + markup).length
input[0].selectionEnd = (text.before + text.selection + markup).length
scope.insertItalic = -> scope.insertItalic = ->
# Shares the same logic as insertBold() but with different markup. # Shares the same logic as insertBold() but with different markup.
...@@ -60,6 +73,9 @@ markdown = ['$filter', '$timeout', ($filter, $timeout) -> ...@@ -60,6 +73,9 @@ markdown = ['$filter', '$timeout', ($filter, $timeout) ->
input[0].selectionStart = text.before.length + 1 input[0].selectionStart = text.before.length + 1
input[0].selectionEnd = text.before.length + 10 input[0].selectionEnd = text.before.length + 10
else else
# Check to see if markup has already been applied to avoid double presses.
if text.selection == "Link Text" or text.selection == "https://example.com"
return
newtext = text.before + '[' + text.selection + '](https://example.com)' + text.after newtext = text.before + '[' + text.selection + '](https://example.com)' + text.after
input[0].value = newtext input[0].value = newtext
input[0].selectionStart = (text.before + text.selection).length + 3 input[0].selectionStart = (text.before + text.selection).length + 3
...@@ -73,6 +89,9 @@ markdown = ['$filter', '$timeout', ($filter, $timeout) -> ...@@ -73,6 +89,9 @@ markdown = ['$filter', '$timeout', ($filter, $timeout) ->
input[0].selectionStart = text.before.length + 21 input[0].selectionStart = text.before.length + 21
input[0].selectionEnd = text.before.length + 42 input[0].selectionEnd = text.before.length + 42
else else
# Check to see if markup has already been applied to avoid double presses.
if text.selection == "https://yourimage.jpg"
return
newtext = text.before + '![' + text.selection + '](https://yourimage.jpg)' + text.after newtext = text.before + '![' + text.selection + '](https://yourimage.jpg)' + text.after
input[0].value = newtext input[0].value = newtext
input[0].selectionStart = (text.before + text.selection).length + 4 input[0].selectionStart = (text.before + text.selection).length + 4
...@@ -80,7 +99,6 @@ markdown = ['$filter', '$timeout', ($filter, $timeout) -> ...@@ -80,7 +99,6 @@ markdown = ['$filter', '$timeout', ($filter, $timeout) ->
scope.insertList = (markup = "* ") -> scope.insertList = (markup = "* ") ->
text = scope.returnSelection() text = scope.returnSelection()
# If the character preceeding the curser is a newline just insert a "* ".
if text.selection != "" if text.selection != ""
newstring = "" newstring = ""
index = text.before.length index = text.before.length
...@@ -109,8 +127,7 @@ markdown = ['$filter', '$timeout', ($filter, $timeout) -> ...@@ -109,8 +127,7 @@ markdown = ['$filter', '$timeout', ($filter, $timeout) ->
index += 1 index += 1
if not newlinedetected if not newlinedetected
# Edge case: The selection does not include any new lines and does not start at 0. # Edge case: The selection does not include any new lines and does not start at 0.
# We need to find the newline before the currently selected text and add markup there. # We need to find the newline before the currently selected text and add markup there.
# console.log input[0].value.substring(0, (text.before + text.selection).length)
i = 0 i = 0
indexoflastnewline = undefined indexoflastnewline = undefined
newstring = "" newstring = ""
...@@ -140,19 +157,37 @@ markdown = ['$filter', '$timeout', ($filter, $timeout) -> ...@@ -140,19 +157,37 @@ markdown = ['$filter', '$timeout', ($filter, $timeout) ->
input[0].selectionEnd = (text.before + markup).length input[0].selectionEnd = (text.before + markup).length
else else
# No selection, cursor is not on new line. Go to the previous newline and insert markup there. # No selection, cursor is not on new line. Go to the previous newline and insert markup there.
# # Check to see if markup has already been inserted.
if text.before.slice(text.before.length - markup.length) == markup
newtext = text.before.substring(0, (index)) + "\n" + text.before.substring(index + 1 + markup.length) + text.after
i = 0 i = 0
for char in text.before for char in text.before
if char == "\n" and i != 0 if char == "\n" and i != 0
index = i index = i
i += 1 i += 1
if !index if !index # If the line of text happens to fall on the first line and index is not set.
# If the line of text happens to fall on the first line and index is not set. # Check to see if markup has already been inserted and undo it.
newtext = markup + text.before.substring(0) + text.after if text.before.slice(0, markup.length) == markup
newtext = text.before.substring(markup.length) + text.after
input[0].value = newtext
input[0].selectionStart = text.before.length - markup.length
input[0].selectionEnd = text.before.length - markup.length
else
newtext = markup + text.before.substring(0) + text.after
input[0].value = newtext
input[0].selectionStart = (text.before + markup).length
input[0].selectionEnd = (text.before + markup).length
# Check to see if markup has already been inserted and undo it.
else if text.before.slice((index + 1), (index + 1 + markup.length)) == markup
newtext = text.before.substring(0, (index)) + "\n" + text.before.substring(index + 1 + markup.length) + text.after
input[0].value = newtext
input[0].selectionStart = text.before.length - markup.length
input[0].selectionEnd = text.before.length - markup.length
else else
newtext = text.before.substring(0, (index)) + "\n" + markup + text.before.substring(index + 1) + text.after newtext = text.before.substring(0, (index)) + "\n" + markup + text.before.substring(index + 1) + text.after
input[0].value = newtext input[0].value = newtext
input[0].selectionStart = (text.before + markup).length input[0].selectionStart = (text.before + markup).length
input[0].selectionEnd = (text.before + markup).length input[0].selectionEnd = (text.before + markup).length
scope.insertNumList = -> scope.insertNumList = ->
# Shares the same logic as insertList but with different markup. # Shares the same logic as insertList but with different markup.
......
...@@ -63,6 +63,12 @@ ...@@ -63,6 +63,12 @@
width: 100%; width: 100%;
margin-bottom: -1px; margin-bottom: -1px;
padding: .7em .7em .7em .5em; padding: .7em .7em .7em .5em;
user-select: none;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
&.disable { &.disable {
i { i {
......
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