diff options
| author | Stephen Blott | 2015-01-14 07:57:09 +0000 |
|---|---|---|
| committer | Stephen Blott | 2015-01-14 09:46:00 +0000 |
| commit | 9b0a48955c61c262cc4428b2360938d4b54d2d41 (patch) | |
| tree | 3abda5d154e96ad019eb9aa67ff59a362775fa23 /content_scripts/mode_find.coffee | |
| parent | 7e1c3475ed9241a5e1fbf78b8134e6ed669ea906 (diff) | |
| download | vimium-9b0a48955c61c262cc4428b2360938d4b54d2d41.tar.bz2 | |
Modes; substantial reworking of insert mode (and friends).
Diffstat (limited to 'content_scripts/mode_find.coffee')
| -rw-r--r-- | content_scripts/mode_find.coffee | 58 |
1 files changed, 37 insertions, 21 deletions
diff --git a/content_scripts/mode_find.coffee b/content_scripts/mode_find.coffee index bf6e7f5b..08bc9e5d 100644 --- a/content_scripts/mode_find.coffee +++ b/content_scripts/mode_find.coffee @@ -2,33 +2,33 @@ # When we use find mode, the selection/focus can end up in a focusable/editable element. In this situation, # special considerations apply. We implement three special cases: -# 1. Prevent keyboard events from dropping us unintentionally into insert mode. This is achieved by... -# 2. Prevent all printable keypress events on the active element from propagating. This is achieved by setting the -# suppressPrintableEvents option. There's some controversy as to whether this is the right thing to do. -# See discussion in #1415. This implements Option 2 from there. +# 1. Prevent keyboard events from dropping us unintentionally into insert mode. +# 2. Prevent all printable keypress events on the active element from propagating beyond normal mode. See +# #1415. This implements Option 2 from there. # 3. If the very-next keystroke is Escape, then drop immediately into insert mode. # class PostFindMode extends Mode constructor: (findModeAnchorNode) -> element = document.activeElement - initialSelection = window.getSelection().toString() super name: "post-find" + badge: "N" # Pretend to be normal mode (because we don't want the insert-mode badge). # Be a singleton. That way, we don't have to keep track of any currently-active instance. Any active # instance is automatically deactivated when a new instance is activated. singleton: PostFindMode exitOnBlur: element - suppressPrintableEvents: element - # If the selection changes (e.g. via paste, or the arrow keys), then the user is interacting with the - # element, so get out of the way and activate insert mode. This implements 5c (without the input - # listener) as discussed in #1415. - keyup: => + exitOnClick: true + keydown: (event) -> InsertMode.suppressEvent event + keypress: (event) -> InsertMode.suppressEvent event + keyup: (event) => @alwaysContinueBubbling => - if window.getSelection().toString() != initialSelection + if document.getSelection().type != "Range" + # If the selection is no longer a range, then the user is interacting with the element, so get out + # of the way and stop suppressing insert mode. See discussion of Option 5c from #1415. @exit() - new InsertMode - targetElement: element + else + InsertMode.suppressEvent event return @exit() unless element and findModeAnchorNode @@ -36,21 +36,37 @@ class PostFindMode extends Mode # cannot. canTakeInput = DomUtils.isSelectable(element) and DomUtils.isDOMDescendant findModeAnchorNode, element canTakeInput ||= element.isContentEditable - canTakeInput ||= findModeAnchorNode.parentElement?.isContentEditable + canTakeInput ||= findModeAnchorNode.parentElement?.isContentEditable # FIXME(smblott) This is too specific. return @exit() unless canTakeInput + # If the very-next keydown is Esc, drop immediately into insert mode. self = @ @push _name: "mode-#{@id}/handle-escape" keydown: (event) -> - if element == document.activeElement and KeyboardUtils.isEscape event - self.exit() - new InsertMode - targetElement: element + if document.activeElement == element and KeyboardUtils.isEscape event DomUtils.suppressKeyupAfterEscape handlerStack - return false - @remove() - true + self.exit() + false # Suppress event. + else + @remove() + true # Continue bubbling. + + # Prevent printable keyboard events from propagating to to the page; see Option 2 from #1415. + do => + handler = (event) => + if event.srcElement == element and KeyboardUtils.isPrintable event + @suppressEvent + else + @continueBubbling + + # Note. We use unshift here, instead of push; therefore we see events *after* normal mode, and so only + # unmapped keys. + @unshift + _name: "mode-#{@id}/suppressPrintableEvents" + keydown: handler + keypress: handler + keyup: handler root = exports ? window root.PostFindMode = PostFindMode |
