aboutsummaryrefslogtreecommitdiffstats
path: root/content_scripts/mode_insert.coffee
diff options
context:
space:
mode:
authorStephen Blott2015-01-16 07:31:24 +0000
committerStephen Blott2015-01-16 09:47:26 +0000
commitbbc7257842293fbd58dd2f84a58c86691ceae3e1 (patch)
tree50398e9df540c19926b2f88f46f0c7d55e1218a8 /content_scripts/mode_insert.coffee
parent091cd99b6fcbb17f30e552b0c0f6461c4c1529cb (diff)
downloadvimium-bbc7257842293fbd58dd2f84a58c86691ceae3e1.tar.bz2
Modes; tweaks.
Diffstat (limited to 'content_scripts/mode_insert.coffee')
-rw-r--r--content_scripts/mode_insert.coffee53
1 files changed, 30 insertions, 23 deletions
diff --git a/content_scripts/mode_insert.coffee b/content_scripts/mode_insert.coffee
index 9be520c7..204c629d 100644
--- a/content_scripts/mode_insert.coffee
+++ b/content_scripts/mode_insert.coffee
@@ -1,7 +1,6 @@
class InsertMode extends Mode
- # There is one permanently-installed instance of InsertMode. This allows PostFindMode to query the active
- # element.
+ # There is one permanently-installed instance of InsertMode.
@permanentInstance: null
constructor: (options = {}) ->
@@ -15,25 +14,34 @@ class InsertMode extends Mode
keyup: (event) => @handleKeyEvent event
super extend defaults, options
- @insertModeLock = if options.targetElement? then options.targetElement else null
+
+ @insertModeLock =
+ if document.activeElement and DomUtils.isEditable document.activeElement
+ # We have already focused an input element, so use it.
+ document.activeElement
+ else
+ null
@push
"blur": (event) => @alwaysContinueBubbling =>
- if DomUtils.isFocusable event.target
- @exit event, event.target
- Mode.updateBadge()
+ target = event.target
+ # We can't rely on focus and blur events arriving in the expected order. When the active element
+ # changes, we might get "blur" before "focus". The approach we take is to track the active element in
+ # @insertModeLock, and exit only when the that element blurs.
+ @exit event, target if target == @insertModeLock and DomUtils.isFocusable target
"focus": (event) => @alwaysContinueBubbling =>
- @insertModeLock = event.target if DomUtils.isFocusable event.target
-
- # We may already have focused an input element, so check.
- @insertModeLock = document.activeElement if document.activeElement and DomUtils.isEditable document.activeElement
+ if @insertModeLock != event.target and DomUtils.isFocusable event.target
+ @insertModeLock = event.target
+ Mode.updateBadge()
isActive: ->
- return true if @insertModeLock != null or @global
+ return true if @insertModeLock or @global
# Some sites (e.g. inbox.google.com) change the contentEditable property on the fly (see #1245); and
# unfortunately, the focus event fires *before* the change. Therefore, we need to re-check whether the
# active element is contentEditable.
- @insertModeLock = document.activeElement if document.activeElement?.isContentEditable
+ if document.activeElement?.isContentEditable and @insertModeLock != document.activeElement
+ @insertModeLock = document.activeElement
+ Mode.updateBadge()
@insertModeLock != null
handleKeydownEvent: (event) ->
@@ -41,7 +49,6 @@ class InsertMode extends Mode
return @stopBubblingAndTrue unless KeyboardUtils.isEscape event
DomUtils.suppressKeyupAfterEscape handlerStack
@exit event, event.srcElement
- Mode.updateBadge()
@suppressEvent
# Handles keypress and keyup events.
@@ -49,17 +56,17 @@ class InsertMode extends Mode
if @isActive() and event != InsertMode.suppressedEvent then @stopBubblingAndTrue else @continueBubbling
exit: (_, target) ->
- if target and (target == @insertModeLock or @global) and DomUtils.isFocusable target
- # Remove focus so the user can't just get himself back into insert mode by typing in the same input
- # box.
- # NOTE(smblott, 2014/12/22) Including embeds for .blur() etc. here is experimental. It appears to be
- # the right thing to do for most common use cases. However, it could also cripple flash-based sites and
- # games. See discussion in #1211 and #1194.
- target.blur()
- if target == undefined or target == @insertModeLock or @global
+ if (target and target == @insertModeLock) or @global or target == undefined
@insertModeLock = null
- # Now really exit, unless this is the permanently-installed instance.
- super() unless @ == InsertMode.permanentInstance
+ if target and DomUtils.isFocusable target
+ # Remove the focus, so the user can't just get himself back into insert mode by typing in the same input
+ # box.
+ # NOTE(smblott, 2014/12/22) Including embeds for .blur() etc. here is experimental. It appears to be
+ # the right thing to do for most common use cases. However, it could also cripple flash-based sites and
+ # games. See discussion in #1211 and #1194.
+ target.blur()
+ # Really exit, but only if this isn't the permanently-installed instance.
+ if @ == InsertMode.permanentInstance then Mode.updateBadge() else super()
chooseBadge: (badge) ->
return if badge == InsertMode.suppressedEvent