fix #2385 - refactor wysiwyg list insert
parent
cc9c4bd042
commit
6bc2078284
|
@ -24,6 +24,33 @@ bindOnce = @.taiga.bindOnce
|
||||||
|
|
||||||
module = angular.module("taigaCommon")
|
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
|
## WYSIWYG markitup editor directive
|
||||||
|
@ -64,35 +91,52 @@ tgMarkitupDirective = ($rootscope, $rs, $tr, $selectedText, $template) ->
|
||||||
markdown.off(".preview")
|
markdown.off(".preview")
|
||||||
closePreviewMode()
|
closePreviewMode()
|
||||||
|
|
||||||
markdownCaretPositon = false
|
setCaretPosition = (textarea, caretPosition) ->
|
||||||
|
if textarea.createTextRange
|
||||||
setCaretPosition = (elm, caretPos) ->
|
range = textarea.createTextRange()
|
||||||
if elm.createTextRange
|
range.move("character", caretPosition)
|
||||||
range = elm.createTextRange()
|
|
||||||
range.move("character", caretPos)
|
|
||||||
range.select()
|
range.select()
|
||||||
|
|
||||||
else if elm.selectionStart
|
else if textarea.selectionStart
|
||||||
elm.focus()
|
textarea.focus()
|
||||||
elm.setSelectionRange(caretPos, caretPos)
|
textarea.setSelectionRange(caretPosition, caretPosition)
|
||||||
|
|
||||||
removeEmptyLine = (textarea, line, currentCaretPosition) ->
|
# 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")
|
lines = textarea.value.split("\n")
|
||||||
removedLineLength = lines[line].length
|
|
||||||
|
|
||||||
lines[line] = ""
|
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")
|
textarea.value = lines.join("\n")
|
||||||
|
|
||||||
#return the new position
|
#return the new position
|
||||||
return currentCaretPosition - removedLineLength + 1
|
if replace
|
||||||
|
return cursorPosition - lines[nline].length + replace.length - 1
|
||||||
|
else
|
||||||
|
return cursorPosition
|
||||||
|
|
||||||
markdownSettings =
|
markdownSettings =
|
||||||
nameSpace: "markdown"
|
nameSpace: "markdown"
|
||||||
onShiftEnter: {keepDefault:false, openWith:"\n\n"}
|
onShiftEnter: {keepDefault:false, openWith:"\n\n"}
|
||||||
onEnter:
|
onEnter:
|
||||||
keepDefault: false
|
keepDefault: false,
|
||||||
replaceWith: (data) =>
|
replaceWith: () -> "\n"
|
||||||
|
afterInsert: (data) ->
|
||||||
lines = data.textarea.value.split("\n")
|
lines = data.textarea.value.split("\n")
|
||||||
cursorLine = data.textarea.value[0..(data.caretPosition - 1)].split("\n").length
|
cursorLine = data.textarea.value[0..(data.caretPosition - 1)].split("\n").length
|
||||||
newLineContent = data.textarea.value[data.caretPosition..].split("\n")[0]
|
newLineContent = data.textarea.value[data.caretPosition..].split("\n")[0]
|
||||||
|
@ -105,12 +149,9 @@ tgMarkitupDirective = ($rootscope, $rs, $tr, $selectedText, $template) ->
|
||||||
emptyListItem = lastLine.match /^(\s*)\-\s$/
|
emptyListItem = lastLine.match /^(\s*)\-\s$/
|
||||||
|
|
||||||
if emptyListItem
|
if emptyListItem
|
||||||
markdownCaretPositon = removeEmptyLine(data.textarea, lines.length - 1, data.caretPosition)
|
markdownCaretPositon = addLine(data.textarea, cursorLine - 1)
|
||||||
else
|
else
|
||||||
breakLineAtBeginning = newLineContent.match /^(\s*)\-\s/
|
markdownCaretPositon = addLine(data.textarea, cursorLine, "#{match[1]}")
|
||||||
|
|
||||||
if !breakLineAtBeginning
|
|
||||||
return "\n#{match[1]}" if match
|
|
||||||
|
|
||||||
# unordered list *
|
# unordered list *
|
||||||
match = lastLine.match /^(\s*\* ).*/
|
match = lastLine.match /^(\s*\* ).*/
|
||||||
|
@ -119,12 +160,9 @@ tgMarkitupDirective = ($rootscope, $rs, $tr, $selectedText, $template) ->
|
||||||
emptyListItem = lastLine.match /^(\s*\* )$/
|
emptyListItem = lastLine.match /^(\s*\* )$/
|
||||||
|
|
||||||
if emptyListItem
|
if emptyListItem
|
||||||
markdownCaretPositon = removeEmptyLine(data.textarea, lines.length - 1, data.caretPosition)
|
markdownCaretPositon = addLine(data.textarea, cursorLine - 1)
|
||||||
else
|
else
|
||||||
breakLineAtBeginning = newLineContent.match /^(\s*)\*\s/
|
markdownCaretPositon = addLine(data.textarea, cursorLine, "#{match[1]}")
|
||||||
|
|
||||||
if !breakLineAtBeginning
|
|
||||||
return "\n#{match[1]}" if match
|
|
||||||
|
|
||||||
# ordered list
|
# ordered list
|
||||||
match = lastLine.match /^(\s*)(\d+)\.\s/
|
match = lastLine.match /^(\s*)(\d+)\.\s/
|
||||||
|
@ -133,29 +171,12 @@ tgMarkitupDirective = ($rootscope, $rs, $tr, $selectedText, $template) ->
|
||||||
emptyListItem = lastLine.match /^(\s*)(\d+)\.\s$/
|
emptyListItem = lastLine.match /^(\s*)(\d+)\.\s$/
|
||||||
|
|
||||||
if emptyListItem
|
if emptyListItem
|
||||||
markdownCaretPositon = removeEmptyLine(data.textarea, lines.length - 1, data.caretPosition)
|
markdownCaretPositon = addLine(data.textarea, cursorLine - 1)
|
||||||
else
|
else
|
||||||
breakLineAtBeginning = newLineContent.match /^(\s*)(\d+)\.\s/
|
markdownCaretPositon = addLine(data.textarea, cursorLine, "#{match[1] + (parseInt(match[2], 10) + 1)}. ")
|
||||||
|
|
||||||
if !breakLineAtBeginning
|
|
||||||
return "\n#{match[1] + (parseInt(match[2], 10) + 1)}. "
|
|
||||||
|
|
||||||
return "\n"
|
setCaretPosition(data.textarea, markdownCaretPositon) if markdownCaretPositon
|
||||||
|
|
||||||
afterInsert: (data) ->
|
|
||||||
# Calculate the scroll position
|
|
||||||
|
|
||||||
if markdownCaretPositon
|
|
||||||
setCaretPosition(data.textarea, markdownCaretPositon)
|
|
||||||
caretPosition = markdownCaretPositon
|
|
||||||
markdownCaretPositon = false
|
|
||||||
else
|
|
||||||
caretPosition = data.caretPosition
|
|
||||||
|
|
||||||
totalLines = data.textarea.value.split("\n").length
|
|
||||||
line = data.textarea.value[0..(caretPosition - 1)].split("\n").length
|
|
||||||
scrollRelation = line / totalLines
|
|
||||||
$el.scrollTop((scrollRelation * $el[0].scrollHeight) - ($el.height() / 2))
|
|
||||||
|
|
||||||
markupSet: [
|
markupSet: [
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue