Close spell check menu on scroll so it doesn't appear in the wrong position

This commit is contained in:
James Allen 2014-11-28 13:27:25 +00:00
parent 793763f206
commit dbd7b95823
2 changed files with 6 additions and 82 deletions

View file

@ -18,12 +18,18 @@ define [
onChange = (e) =>
@runCheckOnChange(e)
onScroll = () =>
@closeContextMenu()
@editor.on "changeSession", (e) =>
@runSpellCheckSoon(200)
e.oldSession?.getDocument().off "change", onChange
e.session.getDocument().on "change", onChange
e.oldSession?.off "changeScrollTop", onScroll
e.session.on "changeScrollTop", onScroll
@$scope.spellingMenu = {left: '0px', top: '0px'}
@editor.on "nativecontextmenu", (e) =>

View file

@ -1,82 +0,0 @@
define [
"libs/backbone"
"libs/mustache"
], () ->
SUGGESTIONS_TO_SHOW = 5
SpellingMenuView = Backbone.View.extend
templates:
menu: $("#spellingMenuTemplate").html()
entry: $("#spellingMenuEntryTemplate").html()
events:
"click a#learnWord": ->
@trigger "click:learn", @_currentHighlight
@hide()
initialize: (options) ->
@ide = options.ide
@ide.editor.getContainerElement().append @render().el
@ide.editor.on "click", () => @hide()
@ide.editor.on "scroll", () => @hide()
@ide.editor.on "update:doc", () => @hide()
@ide.editor.on "change:doc", () => @hide()
render: () ->
@setElement($(@templates.menu))
@$el.css "z-index" : 10000
@$(".dropdown-toggle").dropdown()
@hide()
return @
showForHighlight: (highlight) ->
if @_currentHighlight? and highlight != @_currentHighlight
@_close()
if !@_currentHighlight?
@_currentHighlight = highlight
@_setSuggestions(highlight)
position = @ide.editor.textToEditorCoordinates(
highlight.row
highlight.column + highlight.word.length
)
@_position(position.x, position.y)
@_show()
hideIfAppropriate: (cursorPosition) ->
if @_currentHighlight?
if !@_cursorCloseToHighlight(cursorPosition, @_currentHighlight) and !@_isOpen()
@hide()
hide: () ->
delete @_currentHighlight
@_close()
@$el.hide()
_setSuggestions: (highlight) ->
@$(".spelling-suggestion").remove()
divider = @$(".divider")
for suggestion in highlight.suggestions.slice(0, SUGGESTIONS_TO_SHOW)
do (suggestion) =>
entry = $(Mustache.to_html(@templates.entry, word: suggestion))
divider.before(entry)
entry.on "click", () =>
@trigger "click:suggestion", suggestion, highlight
_show: () -> @$el.show()
_isOpen: () ->
@$(".dropdown-menu").is(":visible")
_close: () ->
if @_isOpen()
@$el.dropdown("toggle")
_cursorCloseToHighlight: (position, highlight) ->
position.row == highlight.row and
position.column >= highlight.column and
position.column <= highlight.column + highlight.word.length + 1
_position: (x,y) -> @$el.css left: x, top: y