Skip to content
Snippets Groups Projects
Commit fa2119a4 authored by Rodrigo Nascimento's avatar Rodrigo Nascimento
Browse files

Improve popup completion

parent a34da702
No related branches found
No related tags found
No related merge requests found
getCursorPosition = (input) ->
if not input? then return
if input.selectionStart?
return input.selectionStart
else if document.selection?
input.focus()
sel = document.selection.createRange()
selLen = document.selection.createRange().text.length
sel.moveStart('character', - input.value.length)
return sel.text.length - selLen
setCursorPosition = (input, caretPos) ->
if not input? then return
if input.selectionStart?
input.focus()
return input.setSelectionRange(caretPos, caretPos)
else if document.selection?
range = input.createTextRange()
range.move('character', caretPos)
range.select()
val = (v, d) -> val = (v, d) ->
return if v? then v else d return if v? then v else d
...@@ -52,7 +73,18 @@ Template.messagePopup.onCreated -> ...@@ -52,7 +73,18 @@ Template.messagePopup.onCreated ->
if event.which is 13 if event.which is 13
template.open.set false template.open.set false
template.input.value = template.input.value.replace template.selectorRegex, template.prefix + template.value.curValue + template.suffix
value = template.input.value
caret = getCursorPosition(template.input)
firstPartValue = value.substr 0, caret
lastPartValue = value.substr caret
firstPartValue = firstPartValue.replace(template.selectorRegex, template.prefix + template.value.curValue + template.suffix)
template.input.value = firstPartValue + lastPartValue
setCursorPosition template.input, firstPartValue.length
event.preventDefault() event.preventDefault()
event.stopPropagation() event.stopPropagation()
...@@ -63,8 +95,11 @@ Template.messagePopup.onCreated -> ...@@ -63,8 +95,11 @@ Template.messagePopup.onCreated ->
event.stopPropagation() event.stopPropagation()
return return
if template.matchSelectorRegex.test template.input.value value = template.input.value
template.textFilter.set(template.input.value.match(template.selectorRegex)[1]) value = value.substr 0, getCursorPosition(template.input)
if template.matchSelectorRegex.test value
template.textFilter.set(value.match(template.selectorRegex)[1])
template.open.set true template.open.set true
else else
template.open.set false template.open.set false
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment