 
 module = angular.module("taigaCommon")
 
+# How to test lists (-, *, 1.)
+# test it with text after & before the list
+# + is the cursor position
+
+# CASE 1
+# - aa+
+# --> enter
+# - aa
+# - +
+
+# CASE 1
+# - +
+# --> enter
+
+# +
+
+# CASE 3
+# - bb+cc
+# --> enter
+# - bb
+# - cc
+
+# CASE 3
+# +- aa
+# --> enter
+
+# - aa
 
 #############################################################################
 ## WYSIWYG markitup editor directive

                        emptyListItem = lastLine.match /^(\s*)\-\s$/

                        if emptyListItem
                            markdownCaretPositon = removeEmptyLine(data.textarea, lines.length - 1, data.caretPosition)
                        else
                            breakLineAtBeginning = newLineContent.match /^(\s*)\-\s/

                            if !breakLineAtBeginning
                                return "\n#{match[1]}" if match

                    # unordered list *
                    match = lastLine.match /^(\s*\* ).*/

                    markdown.off(".preview")
                    closePreviewMode()

        setCaretPosition = (textarea, caretPosition) ->
            if textarea.createTextRange
                range = textarea.createTextRange()
                range.move("character", caretPosition)
                range.select()

            else if textarea.selectionStart
                textarea.focus()
                textarea.setSelectionRange(caretPosition, caretPosition)

            # Calculate the scroll position
            totalLines = textarea.value.split("\n").length
            line = textarea.value[0..(caretPosition - 1)].split("\n").length
            scrollRelation = line / totalLines
            $el.scrollTop((scrollRelation * $el[0].scrollHeight) - ($el.height() / 2))

        addLine = (textarea, nline, replace) ->
            lines = textarea.value.split("\n")

            if replace
                lines[nline] = replace + lines[nline]
            else
                lines[nline] = ""

            cursorPosition = 0

            for line, key in lines
                cursorPosition += line.length + 1 || 1

                break if key == nline

            textarea.value = lines.join("\n")

            #return the new position
            if replace
                return cursorPosition - lines[nline].length + replace.length - 1
            else
                return cursorPosition

        markdownSettings =
            nameSpace: "markdown"
            onShiftEnter: {keepDefault:false, openWith:"\n\n"}
            onEnter:
                keepDefault: false,
                replaceWith: () -> "\n"
                afterInsert: (data) ->
                    lines = data.textarea.value.split("\n")
                    cursorLine = data.textarea.value[0..(data.caretPosition - 1)].split("\n").length
                    newLineContent = data.textarea.value[data.caretPosition..].split("\n")[0]
