Commit 7eb036d5 authored by RawKStar77's avatar RawKStar77

Clean up code. Implemenet @tilgovi's suggestions.

Add a third argument to the applyInlineMarkup() to handle inline math markup which uses both \\( and \\).

Delete the now unnecessary inlineMath() function, applyInlineMarkup() is now used instead.

Simplify scope.insertMath().
parent b68567b0
...@@ -53,31 +53,35 @@ markdown = ['$filter', '$sanitize', '$sce', '$timeout', ($filter, $sanitize, $sc ...@@ -53,31 +53,35 @@ markdown = ['$filter', '$sanitize', '$sce', '$timeout', ($filter, $sanitize, $sc
# Focus the input # Focus the input
input.focus() input.focus()
applyInlineMarkup = (markup, innertext)-> applyInlineMarkup = (markupL, innertext, markupR) ->
markupR or= markupL
text = userSelection() text = userSelection()
if text.selection == "" if text.selection == ""
newtext = text.before + markup + innertext + markup + text.after newtext = text.before + markupL + innertext + markupR + text.after
start = (text.before + markup).length start = (text.before + markupL).length
end = (text.before + innertext + markup).length end = (text.before + innertext + markupR).length
insertMarkup(newtext, start, end) insertMarkup(newtext, start, end)
else else
# Check to see if markup has already been applied before to the selection. # Check to see if markup has already been applied before to the selection.
slice1 = text.before.slice(text.before.length - markup.length) slice1 = text.before.slice(text.before.length - markupL.length)
slice2 = text.after.slice(0, markup.length) slice2 = text.after.slice(0, markupR.length)
if slice1 == markup and slice2 == markup # Edge case: scope.insertMath() is called for inline and block markup, to avoid a case
# in which pressing the math button to remove block math markup adds inline math inside of
# block math $$\(LaTex or MathML\)$$ we check for $$ here.
if (slice1 == markupL and slice2 == markupR) or (slice1 == '$$' and slice2 == '$$')
# Remove markup # Remove markup
newtext = ( newtext = (
text.before.slice(0, (text.before.length - markup.length)) + text.before.slice(0, (text.before.length - markupL.length)) +
text.selection + text.after.slice(markup.length) text.selection + text.after.slice(markupR.length)
) )
start = text.before.length - markup.length start = text.before.length - markupL.length
end = (text.before + text.selection).length - markup.length end = (text.before + text.selection).length - markupR.length
insertMarkup(newtext, start, end) insertMarkup(newtext, start, end)
else else
# Apply markup # Apply markup
newtext = text.before + markup + text.selection + markup + text.after newtext = text.before + markupL + text.selection + markupR + text.after
start = (text.before + markup).length start = (text.before + markupL).length
end = (text.before + text.selection + markup).length end = (text.before + text.selection + markupR).length
insertMarkup(newtext, start, end) insertMarkup(newtext, start, end)
scope.insertBold = -> scope.insertBold = ->
...@@ -86,43 +90,13 @@ markdown = ['$filter', '$sanitize', '$sce', '$timeout', ($filter, $sanitize, $sc ...@@ -86,43 +90,13 @@ markdown = ['$filter', '$sanitize', '$sce', '$timeout', ($filter, $sanitize, $sc
scope.insertItalic = -> scope.insertItalic = ->
applyInlineMarkup("*", "Italic") applyInlineMarkup("*", "Italic")
inlineMath = (text) ->
slice1 = text.before.slice(text.before.length - 2)
slice2 = text.after.slice(0, 2)
if slice1 == "\\(" or slice1 == "$$"
if slice2 == "\\)" or slice2 == "$$"
# Remove markup
newtext = (
text.before.slice(0, (text.before.length - 2)) +
text.selection + text.after.slice(2)
)
start = text.before.length - 2
end = (text.before + text.selection).length - 2
insertMarkup(newtext, start, end)
return
newtext = text.before + "\\(" + "LaTex or MathML" + "\\)" + text.after
start = text.before.length + 2
end = (text.before + "LaTex or MathML").length + 2
insertMarkup(newtext, start, end)
scope.insertMath = -> scope.insertMath = ->
text = userSelection() text = userSelection()
index = text.before.length index = text.before.length
if index == 0 if index == 0 or input.value[index - 1] == '\n'
# The selection takes place at the very start of the input applyInlineMarkup('$$', 'LaTeX or MathML')
applyInlineMarkup("$$", "LaTex or MathML")
else if text.selection != ""
if input.value.substring(index - 1).charAt(0) == "\n"
# Look to see if the selection falls at the beginning of a new line.
applyInlineMarkup("$$", "LaTex or MathML")
else else
inlineMath(text) applyInlineMarkup('\\(', 'LaTeX or MathML', '\\)')
else if input.value.substring((text.start - 1 ), text.start) == "\n"
# Edge case, no selection, the cursor is on a new line.
applyInlineMarkup("$$", "LaTex or MathML")
else
# No selection, cursor is not on new line.
inlineMath(text)
scope.insertLink = -> scope.insertLink = ->
text = userSelection() text = userSelection()
...@@ -332,19 +306,17 @@ markdown = ['$filter', '$sanitize', '$sce', '$timeout', ($filter, $sanitize, $sc ...@@ -332,19 +306,17 @@ markdown = ['$filter', '$sanitize', '$sce', '$timeout', ($filter, $sanitize, $sc
match = undefined match = undefined
indexes = [] indexes = []
while match = re.exec(textToCheck) while match = re.exec(textToCheck)
indexes.push [ indexes.push match.index
match.index
]
for index in indexes for index in indexes
if startMath == null if startMath == null
startMath = index[0] + 2 startMath = index + 2
else else
endMath = index[0] endMath = index
if startMath != null and endMath != null if startMath != null and endMath != null
math = katex.renderToString(textToCheck.substring(startMath, endMath)) math = katex.renderToString(textToCheck.substring(startMath, endMath))
textToCheck = ( textToCheck = (
textToCheck.substring(0, (startMath - 2)) + math + textToCheck.substring(0, (startMath - 2)) + math +
textToCheck.substring((endMath + 2)) textToCheck.substring(endMath + 2)
) )
startMath = null startMath = null
endMath = null endMath = null
......
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